class Configuration
Attributes
dicts[R]
fdelim[R]
fields[R]
placeholders[R]
template[R]
Public Class Methods
new(options)
click to toggle source
do initializations
# File lib/configuration.rb, line 39 def initialize(options) init_logger(STDOUT) @log.level = $log_level set(options) @log.debug('config-file is ' << @@config_file) end
Public Instance Methods
method_missing(msg, *args)
click to toggle source
return any value stored in @config
# File lib/configuration.rb, line 67 def method_missing(msg, *args) ms = msg.to_sym # Exception-handling is not a control-structure. # This is. if @config[ms] return @config[ms] else return nil end end
user_conf()
click to toggle source
# File lib/configuration.rb, line 46 def user_conf confdir = ENV['HOME'].dup << File::Separator << '.config' Dir.mkdir(confdir) if !Dir.exist?(confdir) confdir = confdir << File::Separator << APPNAME Dir.mkdir(confdir) if !Dir.exist?(confdir) config = confdir << File::Separator << 'config' if(!File.exist?(config ) ) begin File.open(config, 'w') {|co| co.write(File.read(@@config_file))} @log.info("Created user-version of the configuration-file in\n\t" << config) rescue Exception => ex @log.error('Cannot create the configuration: ' << ex.message) give_up end end return config end
Private Instance Methods
give_up()
click to toggle source
exit on error
# File lib/configuration.rb, line 172 def give_up @log.error("\t" << ("Aborting. Bye!")) exit false end
set(options)
click to toggle source
Configure with the command-line arguments.
# File lib/configuration.rb, line 81 def set(options) @log.debug('merging options ' << options.to_s) # User-provided configuration-file? if(options['config']) cf = options['config'] @log.debug('config should be ' << cf.to_s) msg = file_check(cf, :file, :readable) if(!msg) @@config_file = cf else msg = ("The file %s " << msg.split[1,100].join(' ')) %msg.split[0] @log.error(("ERROR! Unsuitable file") << ' ' << msg) give_up end else @@config_file = user_conf end @log.debug('config-file is ' << @@config_file) # read defaults from configuration-file co = OpenStruct.new(YAML::load_file(@@config_file)) # merge and overwrite with the command-line arguments @config = co.to_h.update(options.to_h) if(! @config[:source] ) msg = ('missing argument %s') %'source' @log.error msg @log.error(("Start this program with parameter -h or --help to see the usage-message.") ) give_up end # ----- define the template html ---- warn = false # set template if @config[:template] @template = @config[:template] else @log.warn 'Using default-template!' warn ||= true end # fields in the template file if @config[:placeholders] && @config[:template] @placeholders = @config[:placeholders] @log.debug('placeholders from config: ' << @placeholders.to_s) else @placeholders = Template.default(:placeholders) if @config[:placeholders] @log.warn 'Placeholders are defined, but no template-file is given.' else @log.warn 'Template is given, but placeholders are not defined.' end @log.warn 'Using default placeholders ' << @placeholders.to_a.collect{|p|p.join(': ')}.join(', ') warn = true end @fields = [@placeholders[:dict_list], @placeholders[:glossary]] # the field-delimiter if @config[:fdelim] && @config[:template] @fdelim = @config[:fdelim] else @fdelim = Template.default(:fdelim) if @config[:template] @log.warn 'Template is given, but field delimiters are not defined.' else @log.warn 'Field delimiters are defined but no template is given.' end @log.warn 'Using default delimiters ' << @fdelim << ', ' << @fdelim.reverse warn ||= true end # ----------- template is defined -------- dictionaries = @config[:dictionaries] @dicts = Array.new if(dictionaries) dictionaries.each do |d| @dicts << Dictionary.new(d[:name], d[:url], d[:xpath], d[:color]) end @log.debug('dicts are from config' << @dicts.to_s) else @log.warn( %~NO DICTIONARIES have been set in the configuration! Will use the defaults, which is probably NOT what you want! Defaults are: %s~ %[URL_DICT1.dup << ', ' << URL_DICT2.dup]) warn ||= true @dicts << Dictionary.new(NAME_DICT1, URL_DICT1,XPATH_DICT1, DICT_COLORS[0]) @dicts << Dictionary.new(NAME_DICT2, URL_DICT2, XPATH_DICT2, DICT_COLORS[1]) @log.debug('dicts are from constants' << @dicts.to_s) end @log.warn "HINT: Adapt #{@@config_file} to avoid warnings in the future." if warn end