Class | Irc::Bot::Config::ManagerClass |
In: |
lib/rbot/config.rb
|
Parent: | Object |
container for bot configuration
bot | [R] | |
changed | [RW] | |
config | [R] | |
items | [R] | |
overrides | [R] |
currently we store values in a hash but this could be changed in the future. We use hash semantics, however. components that register their config keys and setup defaults are supported via []
# File lib/rbot/config.rb, line 295 295: def [](key) 296: # return @items[key].value if @items.has_key?(key) 297: return @items[key.to_sym].value if @items.has_key?(key.to_sym) 298: # try to still support unregistered lookups 299: # but warn about them 300: # if @config.has_key?(key) 301: # warning "Unregistered lookup #{key.inspect}" 302: # return @config[key] 303: # end 304: if @config.has_key?(key.to_sym) 305: warning _("Unregistered lookup #{key.to_sym.inspect}") 306: return @config[key.to_sym] 307: end 308: return false 309: end
# File lib/rbot/config.rb, line 311 311: def []=(key, value) 312: return @items[key.to_sym].set(value) if @items.has_key?(key.to_sym) 313: if @config.has_key?(key.to_sym) 314: warning _("Unregistered lookup #{key.to_sym.inspect}") 315: return @config[key.to_sym] = value 316: end 317: end
Associate with bot bot
# File lib/rbot/config.rb, line 259 259: def bot_associate(bot, reset=false) 260: reset_config if reset 261: @bot = bot 262: return unless @bot 263: 264: @changed = false 265: conf = @bot.path 'conf.yaml' 266: if File.exist? conf 267: begin 268: newconfig = YAML::load_file conf 269: newconfig.each { |key, val| 270: @config[key.to_sym] = val 271: } 272: return 273: rescue 274: error "failed to read conf.yaml: #{$!}" 275: end 276: end 277: # if we got here, we need to run the first-run wizard 278: Wizard.new(@bot).run 279: # save newly created config 280: @changed = true 281: save 282: end
pass everything else through to the hash
# File lib/rbot/config.rb, line 320 320: def method_missing(method, *args, &block) 321: return @config.send(method, *args, &block) 322: end
# File lib/rbot/config.rb, line 284 284: def register(item) 285: unless item.kind_of?(Value) 286: raise ArgumentError,"item must be an Irc::Bot::Config::Value" 287: end 288: @items[item.key] = item 289: end
# File lib/rbot/config.rb, line 238 238: def reset_config 239: @items = Hash.new 240: @config = Hash.new(false) 241: 242: # We allow default values for config keys to be overridden by 243: # the config file /etc/rbot.conf 244: # The main purpose for this is to allow distro or system-wide 245: # settings such as external program paths (figlet, toilet, ispell) 246: # to be set once for all the bots. 247: @overrides = Hash.new 248: etcfile = "/etc/rbot.conf" 249: if File.exist?(etcfile) 250: log "Loading defaults from #{etcfile}" 251: etcconf = YAML::load_file(etcfile) 252: etcconf.each { |k, v| 253: @overrides[k.to_sym] = v 254: } 255: end 256: end
write current configuration to #{botclass}/conf.yaml
# File lib/rbot/config.rb, line 325 325: def save 326: if not @changed 327: debug "Not writing conf.yaml (unchanged)" 328: return 329: end 330: begin 331: conf = @bot.path 'conf.yaml' 332: fnew = conf + '.new' 333: debug "Writing new conf.yaml ..." 334: File.open(fnew, "w") do |file| 335: savehash = {} 336: @config.each { |key, val| 337: savehash[key.to_s] = val 338: } 339: file.puts savehash.to_yaml 340: end 341: debug "Officializing conf.yaml ..." 342: File.rename(fnew, conf) 343: @changed = false 344: rescue => e 345: error "failed to write configuration file conf.yaml! #{$!}" 346: error "#{e.class}: #{e}" 347: error e.backtrace.join("\n") 348: end 349: end