summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Hecker <mail@apoc.cc>2020-04-23 23:06:01 +0200
committerMatthias Hecker <mail@apoc.cc>2020-04-23 23:06:01 +0200
commit8218e3f05e8ccd95497dd3c7aa115cfde8b01a40 (patch)
tree59c69a56e3d82a2b1020346a871ce43f692b62e2
parent452e323bfdb73d6b93ab96f5fc417891e4aa8f53 (diff)
plugin(factoids): use registry for storage see #42
-rw-r--r--data/rbot/plugins/factoids.rb156
1 files changed, 79 insertions, 77 deletions
diff --git a/data/rbot/plugins/factoids.rb b/data/rbot/plugins/factoids.rb
index 452efb98..5f31191b 100644
--- a/data/rbot/plugins/factoids.rb
+++ b/data/rbot/plugins/factoids.rb
@@ -9,78 +9,78 @@
#
# Store (and retrieve) unstructured one-sentence factoids
-class FactoidsPlugin < Plugin
- class Factoid
- def initialize(hash)
- @hash = hash.reject { |k, val| val.nil? or val.empty? rescue false }
- raise ArgumentError, "no fact!" unless @hash[:fact]
- if String === @hash[:when]
- @hash[:when] = Time.parse @hash[:when]
- end
+class ::Factoid
+ def initialize(hash)
+ @hash = hash.reject { |k, val| val.nil? or val.empty? rescue false }
+ raise ArgumentError, "no fact!" unless @hash[:fact]
+ if String === @hash[:when]
+ @hash[:when] = Time.parse @hash[:when]
end
+ end
- def to_s(opts={})
- show_meta = opts[:meta]
- fact = @hash[:fact]
- if !show_meta
- return fact
- end
- meta = ""
- metadata = []
- if @hash[:who]
- metadata << _("from %{who}" % @hash)
- end
- if @hash[:when]
- metadata << _("on %{when}" % @hash)
- end
- if @hash[:where]
- metadata << _("in %{where}" % @hash)
- end
- unless metadata.empty?
- meta << _(" [%{data}]" % {:data => metadata.join(" ")})
- end
- return fact+meta
+ def to_s(opts={})
+ show_meta = opts[:meta]
+ fact = @hash[:fact]
+ if !show_meta
+ return fact
end
-
- def [](*args)
- @hash[*args]
+ meta = ""
+ metadata = []
+ if @hash[:who]
+ metadata << _("from %{who}" % @hash)
end
-
- def []=(*args)
- @hash.send(:[]=,*args)
+ if @hash[:when]
+ metadata << _("on %{when}" % @hash)
end
-
- def to_hsh
- return @hash
+ if @hash[:where]
+ metadata << _("in %{where}" % @hash)
+ end
+ unless metadata.empty?
+ meta << _(" [%{data}]" % {:data => metadata.join(" ")})
end
- alias :to_hash :to_hsh
+ return fact+meta
end
- class FactoidList < ArrayOf
- def initialize(ar=[])
- super(Factoid, ar)
- end
+ def [](*args)
+ @hash[*args]
+ end
- def index(f)
- fact = f.to_s
- return if fact.empty?
- self.map { |fs| fs[:fact] }.index(fact)
- end
+ def []=(*args)
+ @hash.send(:[]=,*args)
+ end
- def delete(f)
- idx = index(f)
- return unless idx
- self.delete_at(idx)
- end
+ def to_hsh
+ return @hash
+ end
+ alias :to_hash :to_hsh
+end
- def grep(x)
- self.find_all { |f|
- x === f[:fact]
- }
- end
+class ::FactoidList < ArrayOf
+ def initialize(ar=[])
+ super(Factoid, ar)
+ end
+
+ def index(f)
+ fact = f.to_s
+ return if fact.empty?
+ self.map { |fs| fs[:fact] }.index(fact)
+ end
+
+ def delete(f)
+ idx = index(f)
+ return unless idx
+ self.delete_at(idx)
end
+ def grep(x)
+ self.find_all { |f|
+ x === f[:fact]
+ }
+ end
+end
+
+class FactoidsPlugin < Plugin
# TODO default should be language-specific
Config.register Config::ArrayValue.new('factoids.trigger_pattern',
:default => [
@@ -119,19 +119,28 @@ class FactoidsPlugin < Plugin
def initialize
super
- # TODO config
- @dir = datafile
- @filename = "factoids.rbot"
- @factoids = FactoidList.new
@triggers = Set.new
@learn_patterns = []
- reset_learn_patterns
- begin
- read_factfile
- rescue
- debug $!
+
+ @factoids = @registry[:factoids]
+ unless @factoids
+ @factoids = FactoidList.new
+
+ @dir = datafile
+ @filename = "factoids.rbot"
+ debug "migrate from existing factoids #{@dir}/#{@filename}"
+ reset_learn_patterns
+ begin
+ read_factfile
+ rescue
+ debug $!
+ end
+ @changed = true
+ else
+ reset_learn_patterns
+ reset_triggers
+ @changed = false
end
- @changed = false
end
def read_factfile(name=@filename,dir=@dir)
@@ -177,15 +186,8 @@ class FactoidsPlugin < Plugin
def save
return unless @changed
- Dir.mkdir(@dir) unless FileTest.directory?(@dir)
- fname = File.join(@dir,@filename)
- ar = ["when | who | where | fact"]
- @factoids.each { |f|
- ar << "%s | %s | %s | %s" % [ f[:when], f[:who], f[:where], f[:fact]]
- }
- Utils.safe_save(fname) do |file|
- file.puts ar
- end
+ @registry[:factoids] = @factoids
+ @registry.flush
@changed = false
end