lib/forj-config.rb in forj-0.0.40 vs lib/forj-config.rb in forj-0.0.41
- old
+ new
@@ -35,23 +35,26 @@
if not $LIB_PATH
Logging.fatal(1, 'Internal $LIB_PATH was not set.')
end
- Logging.info ('Reading default configuration...')
+ Logging.info('Reading default configuration...')
@sDefaultsName=File.join($LIB_PATH,'defaults.yaml')
@yDefaults=YAML.load_file(@sDefaultsName)
end
- def exist?(key, section = 'default')
+ def exist?(key, section = :default)
+ key = key.to_sym if key.class == String
(rhExist?(@yDefaults, section, key) == 2)
end
- def get(key, section = 'default')
- rhGet(@yDefaults, section, key)
+ def get(key, section = :default)
+ key = key.to_sym if key.class == String
+ return(rhGet(@yDefaults, section, key)) if key
+ rhGet(@yDefaults, section) if not key
end
def dump()
@yDefaults
end
@@ -71,29 +74,29 @@
# Load yaml documents (defaults + config)
# If config doesn't exist, it will be created, empty with 'defaults:' only
def default_dump(interms = nil)
# Build a config hash.
+
res = {}
- @oDefaults.dump['default'].each_key { |key|
+ @oDefaults.dump[:default].each_key { |key|
dump_key = exist?(key)
rhSet(res, get(key), dump_key, key)
}
- if rhExist?(@yLocal, 'default') == 1
- @yLocal['default'].each_key { |key|
+ if rhExist?(@yLocal, :default) == 1
+ @yLocal[:default].each_key { |key|
dump_key = exist?(key)
rhSet(res, get(key), dump_key, key) if rhExist?(res, dump_key, key) != 2
}
end
if interms
if interms.instance_of? Hash
@interms.each_key { | key|
dump_key = exist?(key)
rhSet(res, get(key), dump_key, key) if rhExist?(res, dump_key, key) != 2
}
- elsif interms.instance_of? Array # Array of hash
- iCount=0
+ elsif interms.instance_of? Array # Array of hash of hash
interms.each { | elem |
elem.each_key { | key|
dump_key = exist?(key)
rhSet(res, get(key), dump_key, key) if rhExist?(res, dump_key, key) != 2
}
@@ -102,11 +105,11 @@
end
@yRuntime.each_key { |key|
dump_key = exist?(key)
rhSet(res, get(key), dump_key, key) if rhExist?(res, dump_key, key) != 2
}
-
+
res
end
def initialize(sConfigName=nil)
@@ -133,12 +136,17 @@
@oDefaults = ForjDefault.new
if File.exists?(@sConfigName)
@yLocal = YAML.load_file(@sConfigName)
+ if rhKeyToSymbol?(@yLocal, 2)
+ @yLocal = rhKeyToSymbol(@yLocal, 2)
+ self.SaveConfig()
+ end
+
else
- @yLocal = { 'default' => nil }
+ @yLocal = { :default => nil }
# Write the empty file
Logging.info('Creating your default configuration file ...')
self.SaveConfig()
end
@@ -153,11 +161,11 @@
end
rescue => e
Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
return false
end
- Logging.info ('Configuration file "%s" updated.' % @sConfigName)
+ Logging.info('Configuration file "%s" updated.' % @sConfigName)
return true
end
def ExtraSave(sFile, section, name)
hVal = rhGet(@yObjConfig, section, name)
@@ -168,11 +176,11 @@
end
rescue => e
Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
return false
end
- Logging.info ('Configuration file "%s" updated.' % sFile)
+ Logging.info('Configuration file "%s" updated.' % sFile)
return true
end
end
def ExtraLoad(sFile, section, name)
@@ -184,61 +192,73 @@
end
def ExtraExist?(section, name, key = nil)
return nil if not section or not name
+ key = key.to_sym if key.class == String
+
return(rhExist?(@yObjConfig, section, name) == 2) if not key
return(rhExist?(@yObjConfig, section, name, key) == 3)
end
def ExtraGet(section, name, key = nil, default = nil)
return nil if not section or not name
+ key = key.to_sym if key.class == String
return default unless ExtraExist?(section, name, key)
return rhGet(@yObjConfig, section, name, key) if key
rhGet(@yObjConfig, section, name)
end
- def ExtraSet(section, name, key = nil, value)
- if key
+ def ExtraSet(section, name, key, value)
+ key = key.to_sym if key.class == String
+ if key
rhSet(@yObjConfig, value, section, name, key)
else
rhSet(@yObjConfig, value, section, name)
end
end
def set(key, value)
# Function to set a runtime key/value, but remove it if value is nil.
# To set in config.yaml, use LocalSet
# To set on extra data, like account information, use ExtraSet
- if not key
- return false
- end
+
+ key = key.to_sym if key.class == String
+ return false if key.class != Symbol
+
if value
rhSet(@yRuntime, value, key)
else
@yRuntime.delete(key)
end
true
end
def get(key, interms = nil, default = nil)
+
+ key = key.to_sym if key.class == String
+ return nil if key.class != Symbol
# If key is in runtime
return rhGet(@yRuntime, key) if rhExist?(@yRuntime, key) == 1
# Check data in intermediate hashes or array of hash. (like account data - key have to be identical)
if interms
if interms.instance_of? Hash
return rhGet(interms, key) if rhExist?(interms, key) == 1
- elsif interms.instance_of? Array # Array of hash
+ elsif interms.instance_of? Array # Array of hashes
iCount=0
+ oVal = nil
interms.each { | elem |
if elem.class == Hash
- oVal = nil
elem.each { | hashkey, value |
- if value.class == Hash and rhExist?(elem, hashkey, key) == 2
- oVal = rhGet?(elem, hashkey, key)
+ if value.class == Hash and rhExist?(elem, hashkey, key) == 2 # hash of hash
+ oVal = rhGet(elem, hashkey, key)
break
+ elsif value.class != Hash and rhExist?(elem, hashkey) == 1 # single hash: key = value.
+ oVal = rhGet(elem, hashkey)
+ break
+
end
}
break if oVal
end
iCount += 1
@@ -252,26 +272,32 @@
return @oDefaults.get(key) if @oDefaults.exist?(key)
# else default
default
end
- def getAppDefault(section, key)
+ def getAppDefault(section, key = nil)
+
+ key = key.to_sym if key.class == String
+
@oDefaults.get(key, section)
end
def exist?(key, interms = nil)
+ key = key.to_sym if key.class == String
+
# Check data in intermediate hashes or array of hash. (like account data - key have to be identical)
return "runtime" if rhExist?(@yRuntime, key) == 1
if interms
if interms.instance_of? Hash
return 'hash' if rhExist?(interms, key) == 1
elsif interms.instance_of? Array # Array of hash
iCount = 0
- Array.each { | elem |
+ interms.each { | elem |
if elem.class == Hash
elem.each { | hashkey, value |
return ("%s" % hashkey) if value.class == Hash and rhExist?(elem, hashkey, key) == 2
+ return ("hash[%s]" % iCount) if value.class != Hash and rhExist?(elem, hashkey) == 1
}
end
iCount += 1
}
end
@@ -284,16 +310,19 @@
def LocalDefaultExist?(key)
LocalExist?(key)
end
- def LocalExist?(key, section = 'default')
+ def LocalExist?(key, section = :default)
+
+ key = key.to_sym if key.class == String
return true if rhExist?(@yLocal, section, key) == 2
false
end
- def LocalSet(key, value, section = 'default')
+ def LocalSet(key, value, section = :default)
+ key = key.to_sym if key.class == String
if not key or not value
return false
end
if @yLocal[section] == nil
@yLocal[section]={}
@@ -304,16 +333,19 @@
@yLocal.merge!(section => {key => value})
end
return true
end
- def LocalGet(key, section = 'default', default = nil)
+ def LocalGet(key, section = :default, default = nil)
+ key = key.to_sym if key.class == String
+
return default if rhExist?(@yLocal, section, key) != 2
rhGet(@yLocal, section, key)
end
- def LocalDel(key, section = 'default')
+ def LocalDel(key, section = :default)
+ key = key.to_sym if key.class == String
if not key
return false
end
if not @yLocal.has_key?(section)
return false
@@ -340,11 +372,10 @@
return 0
end
return 0 if not yVal or not yVal[p[0]]
ret = rhExist?(yVal[p[0]], p.drop(1)) if yVal[p[0]]
return 1 + ret
- 0
end
def rhGet(yVal, *p)
if p.length() == 0 or not yVal
@@ -365,20 +396,63 @@
return yVal
end
p=p.flatten
if p.length() == 1
if yVal
- yVal[p[0]] = value
+ if value
+ yVal[p[0]] = value
+ else
+ yVal.delete(p[0])
+ end
return yVal
end
- ret = { p[0] => value }
+ if value
+ ret = { p[0] => value }
+ else
+ ret = {}
+ end
return ret
end
if yVal
yVal[p[0]] = {} if not yVal[p[0]] or yVal[p[0]].class != Hash
ret=rhSet(yVal[p[0]], value, p.drop(1))
return yVal
else
ret = rhSet(nil, value, p.drop(1))
return { p[0] => ret }
end
+end
+
+def rhKeyToSymbol(yVal, levels = 1)
+ return nil if not yVal
+ yRes = {}
+ yVal.each { | key, value |
+ if key.class == String
+ if levels <= 1
+ yRes[key.to_sym] = value
+ else
+ yRes[key.to_sym] = rhKeyToSymbol(value, levels - 1)
+ end
+ else
+ if levels <= 1
+ yRes[key] = value
+ else
+ yRes[key] = rhKeyToSymbol(value, levels - 1)
+ end
+ end
+ }
+ yRes
+end
+
+def rhKeyToSymbol?(yVal, levels = 1)
+ return false if not yVal
+ yVal.each { | key, value |
+ if key.class == String
+ return true
+ end
+ if levels >1
+ res = rhKeyToSymbol?(value, levels - 1)
+ return true if res
+ end
+ }
+ false
end