lib/forj-config.rb in forj-0.0.34 vs lib/forj-config.rb in forj-0.0.35
- old
+ new
@@ -19,12 +19,12 @@
# puts "test_library included"
require 'rubygems'
require 'yaml'
-require_relative 'log.rb'
-include Logging
+#require_relative 'log.rb'
+#include Logging
class ForjDefault
# @sDefaultsName='defaults.yaml'
# @yDefaults = defaults.yaml file data hash
@@ -36,10 +36,12 @@
# If config doesn't exist, it will be created, empty with 'defaults:' only
if not $LIB_PATH
raise 'Internal $LIB_PATH was not set.'
end
+
+ Logging.info ('Reading default configuration...')
@sDefaultsName=File.join($LIB_PATH,'defaults.yaml')
@yDefaults=YAML.load_file(@sDefaultsName)
end
@@ -48,14 +50,14 @@
class ForjConfig
# Internal variables:
# @sConfigName='config.yaml'
+ # @yRuntime = data in memory.
# @yLocal = config.yaml file data hash.
# @yConfig = defaults.yaml + local_config data hash
-
attr_reader :yLocal
attr_reader :yConfig
attr_reader :sConfigName
def initialize(sConfigName=nil)
@@ -67,18 +69,17 @@
raise 'Internal $FORJ_DATA_PATH was not set.'
end
sConfigDefaultName='config.yaml'
- # binding.pry
if sConfigName
if File.dirname(sConfigName) == '.'
sConfigName= File.join($FORJ_DATA_PATH,sConfigName)
end
sConfigName = File.expand_path(sConfigName)
if not File.exists?(sConfigName)
- Logging.error('Config file %s doesn\'t exists. Using default one.')
+ Logging.warning("Config file '%s' doesn't exists. Using default one." % [sConfigName] )
@sConfigName=File.join($FORJ_DATA_PATH,sConfigDefaultName)
else
@sConfigName=sConfigName
end
else
@@ -96,33 +97,35 @@
if not File.exists?(@sConfigName)
# Write the empty file
if not File.exists?($FORJ_DATA_PATH)
Dir.mkdir($FORJ_DATA_PATH)
end
- puts ('INFO: Creating your default configuration file ...')
+ Logging.info ('Creating your default configuration file ...')
self.SaveConfig()
end
end
BuildConfig()
+
+ @yRuntime={}
end
def SaveConfig()
begin
File.open(@sConfigName, 'w') do |out|
YAML.dump(@yLocal, out)
end
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
return false
end
- # puts ('INFO: Configuration file "%s" updated.' % @sConfigName)
+ Logging.info ('Configuration file "%s" updated.' % @sConfigName)
return true
end
- def ConfigSet(key, value, section = 'default')
+ def LocalSet(key, value, section = 'default')
if not key or not value
return false
end
if @yLocal[section] == nil
@yLocal[section]={}
@@ -134,11 +137,11 @@
end
BuildConfig()
return true
end
- def ConfigDel(key,section = 'default')
+ def LocalDel(key,section = 'default')
if not key
return false
end
if not @yLocal.has_key?(section)
return false
@@ -146,9 +149,38 @@
@yLocal[section].delete(key)
BuildConfig()
return true
end
+ def setDefault(key, value)
+ if not key
+ return false
+ end
+ if not @yRuntime[key]
+ @yRuntime[key] = value
+ end
+ return true
+ end
+
+ def set(key, value)
+ if not key
+ return false
+ end
+ if value
+ @yRuntime[key] = value
+ else
+ @yRuntime.delete(key)
+ end
+ return true
+ end
+
+ def get(key, default = @yConfig['default'][key])
+ if @yRuntime.has_key?(key)
+ @yRuntime[key]
+ else
+ default
+ end
+ end
def BuildConfig()
# This function implements the logic to get defaults, superseed by local config.
# Take care of ports array, which is simply merged.