lib/qooxview/config_yaml.rb in qooxview-1.9.10 vs lib/qooxview/config_yaml.rb in qooxview-1.9.11
- old
+ new
@@ -1,11 +1,60 @@
-$config = {} if not defined? $config
-if defined?(CONFIG_FILE) and FileTest.exist?(CONFIG_FILE)
- File.open(CONFIG_FILE) { |f| $config = YAML::load(f).to_sym }
+$config ||= {}
+$name = $0.match(/.*?([^\/]*?)(.rb)*$/)[1]
+
+# Searches for name in every directory from 'dir' towards the root and
+# returns the found filename.
+# If nothing is found, it returns nil
+# If 'dir' is nil, the directory of the running script is taken
+def search_up(name, dir = nil)
+ dir ||= File.realdirpath(File.dirname($0))
+ dputs(3){"Directory is #{dir}, searching #{name}"}
+ return File.join(dir, name) if File.exists?(File.join(dir, name))
+ return nil if File.realdirpath(dir) == '/'
+ search_up(name, File.realdirpath(File.join(dir, '..')))
end
-$name = $0.match(/.*?([^\/]*).rb/)[1]
+def load_config_global(path=nil)
+ #dputs_func
+ if conf = search_up("#{$name.downcase}.conf", path)
+ dputs(3) { "Found configuration-file #{conf}" }
+ IO.readlines(conf).each { |l|
+ dputs(4) { "Reading line #{l}" }
+ next if l =~ /(^#|^\s*$)/
+ name, value =
+ case l
+ when /^\s*(.*?)="(.*?)".*$/
+ [$1, $2]
+ when /^\s*(.*?)=([^\s#]*)/
+ [$1, $2]
+ else
+ [nil, nil]
+ end
+ if name && value
+ dputs(3) { "Writing configuration _#{$1}_ = _#{$2}_" }
+ $config[$1.to_sym] = $2
+ end
+ }
+ end
+ unless defined?($data_dir)
+ $data_dir = $config[:DATA_DIR] || "/var/lib/#{$name.downcase}"
+ FileUtils.mkdir_p($data_dir)
+ end
+ unless defined?($config_file)
+ $config_file = File.join($data_dir, 'config.yaml')
+ end
+end
+
+def load_config_yaml(path = nil)
+ if defined?($config_file)
+ file = path ? File.join(path, $config_file) : $config_file
+ if FileTest.exist?(file)
+ File.open(file) { |f| $config.merge!(YAML::load(f).to_sym) }
+ end
+ end
+end
+
def get_config(default, *path)
get_config_rec(path, default)
end
def get_config_rec(path, default, config = $config)
@@ -37,6 +86,7 @@
config[path.last] = value
dputs(4) { "config is #{config.inspect} - $config is #{$config.inspect}" }
end
end
-defined?(CONFIG_FILE) and dputs(2) { "config is #{$config.inspect} - file is #{CONFIG_FILE}" }
+load_config_global
+load_config_yaml