diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2009-02-15 01:30:51 +0100 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2009-02-15 02:06:27 +0100 |
commit | 91a9024e21ec8b429605a036b5c9193442a580e3 (patch) | |
tree | f1524dd5595a80e32d0702c0d8939b1001c695fb /data/rbot | |
parent | 38be7f6511447d98780a069bccefecd933238e30 (diff) |
+ @bot.path and datafile methods
We provide two methods that make it more simple and elegant for
botmodules to define paths relative to the bot's own directory
(botclass) and to the BotModule's (assumed) non-registry directory.
The first method is Irc::Bot#path(), which joins its arguments with the
botclass. This method can be used to access datafiles in the bot
directory with a much cleaner syntax; and since it uses File.join, the
resulting paths are also properly formatted on each platform, which
doesn't hurt.
Each BotModule now also carries a dirname() method that should return the
directory under botclass that holds the BotModule's datafiles. dirname()
defaults to the BotModule's name(), but it can be overridden, e.g. for
backwards compatibility (see the patch for the quotes plugin), or
for BotModules that share their datafiles.
Datafiles can be accessed using the BotModule#datafile() method that
joins the botclass, the dirname() and whatever other argument is passed.
Diffstat (limited to 'data/rbot')
-rw-r--r-- | data/rbot/plugins/alias.rb | 4 | ||||
-rw-r--r-- | data/rbot/plugins/debugger.rb | 10 | ||||
-rw-r--r-- | data/rbot/plugins/factoids.rb | 2 | ||||
-rw-r--r-- | data/rbot/plugins/games/azgame.rb | 2 | ||||
-rw-r--r-- | data/rbot/plugins/games/quiz.rb | 11 | ||||
-rw-r--r-- | data/rbot/plugins/games/shiritori.rb | 2 | ||||
-rw-r--r-- | data/rbot/plugins/karma.rb | 7 | ||||
-rw-r--r-- | data/rbot/plugins/keywords.rb | 40 | ||||
-rw-r--r-- | data/rbot/plugins/lart.rb | 11 | ||||
-rw-r--r-- | data/rbot/plugins/quotes.rb | 10 | ||||
-rw-r--r-- | data/rbot/plugins/salut.rb | 8 |
11 files changed, 57 insertions, 50 deletions
diff --git a/data/rbot/plugins/alias.rb b/data/rbot/plugins/alias.rb index f947d81c..58d0ef01 100644 --- a/data/rbot/plugins/alias.rb +++ b/data/rbot/plugins/alias.rb @@ -38,8 +38,8 @@ class AliasPlugin < Plugin def initialize super - @data_path = "#{@bot.botclass}/alias/" - @data_file = "#{@data_path}/aliases.yaml" + @data_path = datafile + @data_file = File.join(@data_path, 'aliases.yaml') # hash of alias => command entries data = nil aliases = if File.exist?(@data_file) && diff --git a/data/rbot/plugins/debugger.rb b/data/rbot/plugins/debugger.rb index c83aea4a..2895cd0b 100644 --- a/data/rbot/plugins/debugger.rb +++ b/data/rbot/plugins/debugger.rb @@ -18,12 +18,16 @@ class DebugPlugin < Plugin :default => "", :desc => "Directory where profile/string dumps are to be stored") + def dirname + @bot.config['debug.logdir'] + end + def initialize super @prev = Hash.new(0) @curr = Hash.new(0) @delta = Hash.new(0) - @file = File.open("#{@bot.botclass}/#{@bot.config['debug.logdir']}/memory_profiler.log",'w') + @file = File.open(datafile("memory_profiler.log"), 'w') @thread = @bot.timer.add(@bot.config['debug.interval']) { begin GC.start @@ -39,7 +43,7 @@ class DebugPlugin < Plugin end if @bot.config['debug.dump_strings'] - File.open("#{@bot.botclass}/#{@bot.config['debug.logdir']}/memory_profiler_strings.log.#{Time.now.to_i}",'w') do |f| + File.open(datafile("memory_profiler_strings.log.#{Time.now.to_i}"), 'w') do |f| curr_strings.sort.each do |s| f.puts s end @@ -107,7 +111,7 @@ class DebugPlugin < Plugin end end - File.open("#{@bot.botclass}/#{@bot.config['debug.logdir']}/memory_profiler_strings.log.#{Time.now.to_i}",'w') do |f| + File.open(datafile("memory_profiler_strings.log.#{Time.now.to_i}"), 'w') do |f| curr_strings.sort.each do |s| f.puts s end diff --git a/data/rbot/plugins/factoids.rb b/data/rbot/plugins/factoids.rb index 3192aa00..689b6d78 100644 --- a/data/rbot/plugins/factoids.rb +++ b/data/rbot/plugins/factoids.rb @@ -120,7 +120,7 @@ class FactoidsPlugin < Plugin super # TODO config - @dir = File.join(@bot.botclass,"factoids") + @dir = datafile @filename = "factoids.rbot" @factoids = FactoidList.new @triggers = Set.new diff --git a/data/rbot/plugins/games/azgame.rb b/data/rbot/plugins/games/azgame.rb index cb95228d..6e2526e2 100644 --- a/data/rbot/plugins/games/azgame.rb +++ b/data/rbot/plugins/games/azgame.rb @@ -150,7 +150,7 @@ class AzGamePlugin < Plugin }, } - @autoadd_base = "#{@bot.botclass}/azgame/autoadd-" + @autoadd_base = datafile "autoadd-" end def initialize_wordlist(params) diff --git a/data/rbot/plugins/games/quiz.rb b/data/rbot/plugins/games/quiz.rb index 7577e7ce..9159a4c4 100644 --- a/data/rbot/plugins/games/quiz.rb +++ b/data/rbot/plugins/games/quiz.rb @@ -186,8 +186,9 @@ class QuizPlugin < Plugin def fetch_data( m ) # Read the winning messages file @win_messages = Array.new - if File.exists? "#{@bot.botclass}/quiz/win_messages" - IO.foreach("#{@bot.botclass}/quiz/win_messages") { |line| @win_messages << line.chomp } + winfile = datafile 'win_messages' + if File.exists? winfile + IO.foreach(winfile) { |line| @win_messages << line.chomp } else warning( "win_messages file not found!" ) # Fill the array with a least one message or code accessing it would fail @@ -212,13 +213,13 @@ class QuizPlugin < Plugin m.reply "Failed to download questions from #{p}, ignoring sources" end else - path = "#{@bot.botclass}/quiz/#{p}" + path = datafile p debug "Fetching from #{path}" # Local data begin - datafile = File.new( path, File::RDONLY ) - data << "\n\n" << datafile.read + file = File.new( path, File::RDONLY ) + data << "\n\n" << file.read rescue m.reply "Failed to read from local database file #{p}, skipping." end diff --git a/data/rbot/plugins/games/shiritori.rb b/data/rbot/plugins/games/shiritori.rb index 84ee9620..0b77871e 100644 --- a/data/rbot/plugins/games/shiritori.rb +++ b/data/rbot/plugins/games/shiritori.rb @@ -390,7 +390,7 @@ class ShiritoriPlugin < Plugin if ruleset.has_key?(:wordlist_file) begin ruleset[:words] = - File.new("#{@bot.botclass}/shiritori/#{ruleset[:wordlist_file]}").grep( + File.new(datafile ruleset[:wordlist_file]).grep( ruleset[:listen]) {|l| ruleset[:normalize].call l.chomp} rescue raise "unable to load word list" diff --git a/data/rbot/plugins/karma.rb b/data/rbot/plugins/karma.rb index d8d378e3..ad5f57a2 100644 --- a/data/rbot/plugins/karma.rb +++ b/data/rbot/plugins/karma.rb @@ -14,16 +14,17 @@ class KarmaPlugin < Plugin @registry.set_default(0) # import if old file format found - if(File.exist?("#{@bot.botclass}/karma.rbot")) + oldkarma = @bot.path 'karma.rbot' + if File.exist? oldkarma log "importing old karma data" - IO.foreach("#{@bot.botclass}/karma.rbot") do |line| + IO.foreach(oldkarma) do |line| if(line =~ /^(\S+)<=>([\d-]+)$/) item = $1 karma = $2.to_i @registry[item] = karma end end - File.delete("#{@bot.botclass}/karma.rbot") + File.delete oldkarma end end diff --git a/data/rbot/plugins/keywords.rb b/data/rbot/plugins/keywords.rb index 97fe4258..8671e32f 100644 --- a/data/rbot/plugins/keywords.rb +++ b/data/rbot/plugins/keywords.rb @@ -116,9 +116,10 @@ class Keywords < Plugin scan # import old format keywords into DBHash - if(File.exist?("#{@bot.botclass}/keywords.rbot")) + olds = @bot.path 'keywords.rbot' + if File.exist? olds log "auto importing old keywords.rbot" - IO.foreach("#{@bot.botclass}/keywords.rbot") do |line| + IO.foreach(olds) do |line| if(line =~ /^(.*?)\s*<=(is|are)?=?>\s*(.*)$/) lhs = $1 mhs = $2 @@ -129,7 +130,7 @@ class Keywords < Plugin @keywords[lhs] = Keyword.new(mhs, values).dump end end - File.rename("#{@bot.botclass}/keywords.rbot", "#{@bot.botclass}/keywords.rbot.old") + File.rename(olds, olds + ".old") end end @@ -137,15 +138,12 @@ class Keywords < Plugin # have been added def scan # first scan for old DBHash files, and convert them - Dir["#{@bot.botclass}/keywords/*"].each {|f| + Dir[datafile '*'].each {|f| next unless f =~ /\.db$/ log "upgrading keyword db #{f} (rbot 0.9.5 or prior) database format" newname = f.gsub(/\.db$/, ".kdb") - old = BDB::Hash.open f, nil, - "r+", 0600 - new = BDB::CIBtree.open(newname, nil, - BDB::CREATE | BDB::EXCL, - 0600) + old = BDB::Hash.open f, nil, "r+", 0600 + new = BDB::CIBtree.open(newname, nil, BDB::CREATE | BDB::EXCL, 0600) old.each {|k,v| new[k] = v } @@ -155,7 +153,7 @@ class Keywords < Plugin } # then scan for current DBTree files, and load them - Dir["#{@bot.botclass}/keywords/*"].each {|f| + Dir[@bot.path 'keywords', '*'].each {|f| next unless f =~ /\.kdb$/ hsh = DBTree.new @bot, f, true key = File.basename(f).gsub(/\.kdb$/, "") @@ -164,7 +162,7 @@ class Keywords < Plugin } # then scan for non DB files, and convert/import them and delete - Dir["#{@bot.botclass}/keywords/*"].each {|f| + Dir[@bot.path 'keywords', '*'].each {|f| next if f =~ /\.kdb$/ next if f =~ /CVS$/ log "auto converting keywords from #{f}" @@ -192,28 +190,28 @@ class Keywords < Plugin # upgrade data files found in old rbot formats to current def upgrade_data - if File.exist?("#{@bot.botclass}/keywords.db") + olds = @bot.path 'keywords.db' + if File.exist? olds log "upgrading old keywords (rbot 0.9.5 or prior) database format" - old = BDB::Hash.open "#{@bot.botclass}/keywords.db", nil, - "r+", 0600 + old = BDB::Hash.open olds, nil, "r+", 0600 old.each {|k,v| @keywords[k] = v } old.close @keywords.flush - File.rename("#{@bot.botclass}/keywords.db", "#{@bot.botclass}/keywords.db.old") + File.rename(olds, olds + ".old") end - if File.exist?("#{@bot.botclass}/keyword.db") + olds.replace(@bot.path 'keyword.db') + if File.exist? olds log "upgrading old keywords (rbot 0.9.9 or prior) database format" - old = BDB::CIBtree.open "#{@bot.botclass}/keyword.db", nil, - "r+", 0600 + old = BDB::CIBtree.open olds, nil, "r+", 0600 old.each {|k,v| @keywords[k] = v } old.close @keywords.flush - File.rename("#{@bot.botclass}/keyword.db", "#{@bot.botclass}/keyword.db.old") + File.rename(olds, olds + ".old") end end @@ -223,7 +221,7 @@ class Keywords < Plugin end def oldsave - File.open("#{@bot.botclass}/keywords.rbot", "w") do |file| + File.open(@bot.path "keywords.rbot", "w") do |file| @keywords.each do |key, value| file.puts "#{key}<=#{value.type}=>#{value.dump}" end @@ -484,7 +482,7 @@ class Keywords < Plugin # TODO check factoids config # also TODO: runtime export - dir = File.join(@bot.botclass,"factoids") + dir = @bot.path 'factoids' fname = File.join(dir,"keyword_factoids.rbot") Dir.mkdir(dir) unless FileTest.directory?(dir) diff --git a/data/rbot/plugins/lart.rb b/data/rbot/plugins/lart.rb index 0e995efb..4c945889 100644 --- a/data/rbot/plugins/lart.rb +++ b/data/rbot/plugins/lart.rb @@ -41,12 +41,12 @@ class LartPlugin < Plugin # We may be on an old installation, so on the first run read non-language-specific larts unless defined?(@oldlart) - @oldlart = "#{@bot.botclass}/lart/larts" - @oldpraise = "#{@bot.botclass}/lart/praise" + @oldlart = datafile 'larts' + @oldpraise = datafile 'praise' end - @lartfile.replace "#{@bot.botclass}/lart/larts-#{lang}" - @praisefile.replace "#{@bot.botclass}/lart/praises-#{lang}" + @lartfile.replace(datafile "larts-#{lang}") + @praisefile.replace(datafile "praises-#{lang}") @larts.clear @praises.clear if File.exists? @lartfile @@ -72,8 +72,7 @@ class LartPlugin < Plugin def save return unless @changed - Dir.mkdir("#{@bot.botclass}/lart") if not FileTest.directory? "#{@bot.botclass}/lart" - # TODO implement safe saving here too + Dir.mkdir(datafile) unless FileTest.directory? datafile Utils.safe_save(@lartfile) { |file| file.puts @larts } diff --git a/data/rbot/plugins/quotes.rb b/data/rbot/plugins/quotes.rb index c631527f..2a646789 100644 --- a/data/rbot/plugins/quotes.rb +++ b/data/rbot/plugins/quotes.rb @@ -8,11 +8,15 @@ define_structure :Quote, :num, :date, :source, :quote class QuotePlugin < Plugin + def dirname + 'quotes' + end + def initialize super @lists = Hash.new @changed = Hash.new - Dir["#{@bot.botclass}/quotes/*"].each {|f| + Dir[datafile '*'].each {|f| next if File.directory?(f) channel = File.basename(f) @lists[channel] = Array.new if(!@lists.has_key?(channel)) @@ -27,12 +31,12 @@ class QuotePlugin < Plugin end def save - Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes")) + Dir.mkdir(datafile) unless FileTest.directory? datafile @lists.each {|channel, quotes| begin if @changed[channel] debug "Writing new quotefile for channel #{channel} ..." - Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|file| + Utils.safe_save(datafile channel) {|file| quotes.compact.each {|q| file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}" } diff --git a/data/rbot/plugins/salut.rb b/data/rbot/plugins/salut.rb index c169d138..e80a02b0 100644 --- a/data/rbot/plugins/salut.rb +++ b/data/rbot/plugins/salut.rb @@ -168,7 +168,7 @@ class SalutPlugin < Plugin @all_langs = @bot.config['salut.all_languages'] if @all_langs # Get all available languages - langs = Dir.new("#{@bot.botclass}/salut").collect {|f| + langs = Dir.new(datafile).collect {|f| f =~ /salut-([^.]+)/ ? $1 : nil }.compact langs.each { |lang| @@ -183,11 +183,11 @@ class SalutPlugin < Plugin end def load_lang(lang) - dir = "#{@bot.botclass}/salut" + dir = datafile if not File.exist?(dir) Dir.mkdir(dir) end - file = "#{@bot.botclass}/salut/salut-#{lang}" + file = File.join dir, "salut-#{lang}" if File.exist?(file) begin salutations = Hash.new @@ -214,7 +214,7 @@ class SalutPlugin < Plugin end def save_lang(lang, val) - fn = "#{@bot.botclass}/salut/salut-#{lang}" + fn = datafile "salut-#{lang}" Utils.safe_save(fn) { |file| file.puts val.to_yaml } |