X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fconfig.rb;h=e6145a82ee95637d6a31528bf2a89598c6e0e1b8;hb=53c82a94714e439c63a65fccdaf2512383cffb7a;hp=360cba01867c548339e2daca78dbfe9ac324926c;hpb=1ad496b1f55715c33593b545299f5c5e877e25f0;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/config.rb b/lib/rbot/config.rb index 360cba01..e6145a82 100644 --- a/lib/rbot/config.rb +++ b/lib/rbot/config.rb @@ -40,7 +40,9 @@ module Config @order = @@order @@order += 1 @key = key.to_sym - if params.has_key? :default + if @manager.overrides.key?(@key) + @default = @manager.overrides[@key] + elsif params.has_key? :default @default = params[:default] else @default = false @@ -63,18 +65,20 @@ module Config end def get return @manager.config[@key] if @manager.config.has_key?(@key) - return @default + return default end alias :value :get def set(value, on_change = true) @manager.config[@key] = value @manager.changed = true @on_change.call(@manager.bot, value) if on_change && @on_change + return self end def unset @manager.config.delete(@key) @manager.changed = true @on_change.call(@manager.bot, value) if @on_change + return self end # set string will raise ArgumentErrors on failed parse/validate @@ -182,7 +186,7 @@ module Config unless newval.include? val newval << val validate_item(val) or raise ArgumentError, "invalid item: #{val}" - validate(newval) or raise ArgumentError, "invalid value: #{newval.to_s}" + validate(newval) or raise ArgumentError, "invalid value: #{newval.inspect}" set(newval) end end @@ -224,6 +228,7 @@ module Config attr_reader :bot attr_reader :items attr_reader :config + attr_reader :overrides attr_accessor :changed def initialize @@ -233,6 +238,21 @@ module Config def reset_config @items = Hash.new @config = Hash.new(false) + + # We allow default values for config keys to be overridden by + # the config file /etc/rbot.conf + # The main purpose for this is to allow distro or system-wide + # settings such as external program paths (figlet, toilet, ispell) + # to be set once for all the bots. + @overrides = Hash.new + etcfile = "/etc/rbot.conf" + if File.exist?(etcfile) + log "Loading defaults from #{etcfile}" + etcconf = YAML::load_file(etcfile) + etcconf.each { |k, v| + @overrides[k.to_sym] = v + } + end end # Associate with bot _bot_ @@ -242,9 +262,10 @@ module Config return unless @bot @changed = false - if(File.exist?("#{@bot.botclass}/conf.yaml")) + conf = @bot.path 'conf.yaml' + if File.exist? conf begin - newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml") + newconfig = YAML::load_file conf newconfig.each { |key, val| @config[key.to_sym] = val } @@ -287,9 +308,13 @@ module Config return false end - # TODO should I implement this via Value or leave it direct? - # def []=(key, value) - # end + def []=(key, value) + return @items[key.to_sym].set(value) if @items.has_key?(key.to_sym) + if @config.has_key?(key.to_sym) + warning _("Unregistered lookup #{key.to_sym.inspect}") + return @config[key.to_sym] = value + end + end # pass everything else through to the hash def method_missing(method, *args, &block) @@ -303,8 +328,10 @@ module Config return end begin + conf = @bot.path 'conf.yaml' + fnew = conf + '.new' debug "Writing new conf.yaml ..." - File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file| + File.open(fnew, "w") do |file| savehash = {} @config.each { |key, val| savehash[key.to_s] = val @@ -312,8 +339,7 @@ module Config file.puts savehash.to_yaml end debug "Officializing conf.yaml ..." - File.rename("#{@bot.botclass}/conf.yaml.new", - "#{@bot.botclass}/conf.yaml") + File.rename(fnew, conf) @changed = false rescue => e error "failed to write configuration file conf.yaml! #{$!}" @@ -342,6 +368,7 @@ module Config end def run() + $stdout.sync = true puts _("First time rbot configuration wizard") puts "====================================" puts _("This is the first time you have run rbot with a config directory of: #{@bot.botclass}")