module_function :reporting_thread, :record_exception, :exceptions
## one-stop shop for yamliciousness
- def save_yaml_obj object, fn, safe=false
+ def save_yaml_obj o, fn, safe=false
+ o = if o.is_a?(Array)
+ o.map { |x| (x.respond_to?(:before_marshal) && x.before_marshal) || x }
+ else
+ o.respond_to?(:before_marshal) && o.before_marshal
+ end
+
if safe
safe_fn = "#{File.dirname fn}/safe_#{File.basename fn}"
mode = File.stat(fn).mode if File.exists? fn
- File.open(safe_fn, "w", mode) { |f| f.puts object.to_yaml }
+ File.open(safe_fn, "w", mode) { |f| f.puts o.to_yaml }
FileUtils.mv safe_fn, fn
else
- File.open(fn, "w") { |f| f.puts object.to_yaml }
+ File.open(fn, "w") { |f| f.puts o.to_yaml }
end
end
def load_yaml_obj fn, compress=false
- if File.exists? fn
+ o = if File.exists? fn
if compress
Zlib::GzipReader.open(fn) { |f| YAML::load f }
else
YAML::load_file fn
end
end
+ if o.is_a?(Array)
+ o.each { |x| x.after_unmarshal! if x.respond_to?(:after_unmarshal!) }
+ else
+ o.after_unmarshal! if o.respond_to?(:after_unmarshal!)
+ end
+ o
end
def start
module Redwood
class IMAP < Source
+ include SerializeLabelsNicely
SCAN_INTERVAL = 60 # seconds
## upon these errors we'll try to rereconnect a few times
## pathnames on disk.
class Maildir < Source
+ include SerializeLabelsNicely
SCAN_INTERVAL = 30 # seconds
MYHOSTNAME = Socket.gethostname
module MBox
class Loader < Source
+ include SerializeLabelsNicely
yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
## uri_or_fp is horrific. need to refactor.
end
end
+## if you have a @labels instance variable, include this
+## to serialize them nicely as an array, rather than as a
+## nasty set.
+module SerializeLabelsNicely
+ def before_marshal # can return an object
+ c = clone
+ c.instance_eval { @labels = @labels.to_a.map { |l| l.to_s } }
+ c
+ end
+
+ def after_unmarshal!
+ @labels = Set.new(@labels.map { |s| s.to_sym })
+ end
+end
+
class SourceManager
include Singleton
File.chmod 0600, fn
FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(fn) == 0
end
- Redwood::save_yaml_obj sources.sort_by { |s| s.id.to_i }, fn, true
+ Redwood::save_yaml_obj sources, fn, true
File.chmod 0600, fn
end
@sources_dirty = false
class Range
## only valid for integer ranges (unless I guess it's exclusive)
- def size
+ def size
last - first + (exclude_end? ? 0 : 1)
end
end