]> git.cworth.org Git - sup/commitdiff
better yamlication (and misc comment tweaks)
authorwmorgan <wmorgan@5c8cc53c-5e98-4d25-b20a-d8db53a31250>
Wed, 30 May 2007 17:43:30 +0000 (17:43 +0000)
committerwmorgan <wmorgan@5c8cc53c-5e98-4d25-b20a-d8db53a31250>
Wed, 30 May 2007 17:43:30 +0000 (17:43 +0000)
git-svn-id: svn://rubyforge.org/var/svn/sup/trunk@423 5c8cc53c-5e98-4d25-b20a-d8db53a31250

doc/TODO
lib/sup.rb
lib/sup/draft.rb
lib/sup/imap.rb
lib/sup/index.rb
lib/sup/maildir.rb
lib/sup/mbox/loader.rb
lib/sup/mbox/ssh-loader.rb
lib/sup/sent.rb
lib/sup/source.rb
lib/sup/util.rb

index 9185e056f09affc88896f4f0ca9e9fc514666a67..72ff6757379c816e5028135eae0a4be16e9fb93c 100644 (file)
--- a/doc/TODO
+++ b/doc/TODO
@@ -1,5 +1,6 @@
 for 0.0.9
 ---------
+_ add arbitrary labels to sources
 _ detect other sup instances and do something intelligent (because ferret crashes violently with more than one index writer open)
 _ bugfix: need a way to force an address to a particular name, for things like evite addresses
 _ bugfix: read before thread-index has finished loading then hides the thread?!? wtf. (on jamie)
index afd18dbe4fc29a7fe2eae84e80cc777a92837856..45f43c0d369f5b8bf018581a2474796b609f3a48 100644 (file)
@@ -12,6 +12,24 @@ class Object
   end
 end
 
+class Module
+  def yaml_properties *props
+    props = props.map { |p| p.to_s }
+    vars = props.map { |p| "@#{p}" }
+    klass = self
+    path = klass.name.gsub(/::/, "/")
+    
+    klass.instance_eval do
+      define_method(:to_yaml_properties) { vars }
+      define_method(:to_yaml_type) { "!#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}/#{path}" }
+    end
+
+    YAML.add_domain_type("#{Redwood::YAML_DOMAIN},#{Redwood::YAML_DATE}", path) do |type, val|
+      klass.new(*props.map { |p| val[p] })
+    end
+  end
+end
+
 module Redwood
   VERSION = "0.0.8"
 
@@ -50,20 +68,6 @@ module Redwood
   module_function :reporting_thread
 
 ## one-stop shop for yamliciousness
-  def register_yaml klass, props
-    vars = props.map { |p| "@#{p}" }
-    path = klass.name.gsub(/::/, "/")
-    
-    klass.instance_eval do
-      define_method(:to_yaml_properties) { vars }
-      define_method(:to_yaml_type) { "!#{YAML_DOMAIN},#{YAML_DATE}/#{path}" }
-    end
-
-    YAML.add_domain_type("#{YAML_DOMAIN},#{YAML_DATE}", path) do |type, val|
-      klass.new(*props.map { |p| val[p] })
-    end
-  end
-
   def save_yaml_obj object, fn, compress=false
     if compress
       Zlib::GzipWriter.open(fn) { |f| f.puts object.to_yaml }
@@ -142,7 +146,7 @@ EOM
     end
   end
 
-  module_function :register_yaml, :save_yaml_obj, :load_yaml_obj, :start, :finish, :report_broken_sources
+  module_function :save_yaml_obj, :load_yaml_obj, :start, :finish, :report_broken_sources
 end
 
 ## set up default configuration file
index 59eef8c85b688e1849e370e5cb8cd02e6861d20b..e33e4791d128804448a39ef653f9d312d83e0bd2 100644 (file)
@@ -42,6 +42,7 @@ end
 
 class DraftLoader < Source
   attr_accessor :dir
+  yaml_properties :cur_offset
 
   def initialize cur_offset=0
     dir = Redwood::DRAFT_DIR
@@ -119,6 +120,4 @@ private
   end
 end
 
-Redwood::register_yaml(DraftLoader, %w(cur_offset))
-
 end
index 98e8df264241a776eedfd4aef9fbce0b8f9405c1..7370b91a3003b7fcb98f76ea237aee2479b522c3 100644 (file)
@@ -35,7 +35,8 @@ require 'rmail'
 ## of the spec actually happens in practice. you'll request flags for
 ## one message, and get it interspersed with a random bunch of flags
 ## for some other messages, including a different set of flags for the
-## same message! totally ok by the imap spec. totally retarded.
+## same message! totally ok by the imap spec. totally retarded by any
+## other metric.
 ##
 ## fuck you, imap committee. you managed to design something nearly as
 ## shitty as mbox but goddamn THIRTY YEARS LATER.
@@ -48,6 +49,8 @@ class IMAP < Source
   RECOVERABLE_ERRORS = [ Errno::EPIPE, Errno::ETIMEDOUT, OpenSSL::SSL::SSLError ]
 
   attr_accessor :username, :password
+  yaml_properties :uri, :username, :password, :cur_offset, :usual,
+                  :archived, :id
 
   def initialize uri, username, password, last_idate=nil, usual=true, archived=false, id=nil
     raise ArgumentError, "username and password must be specified" unless username && password
@@ -282,6 +285,4 @@ private
 
 end
 
-Redwood::register_yaml(IMAP, %w(uri username password cur_offset usual archived id))
-
 end
index c11d137b34db0e00297c2aa83b1b40218b5c0a2a..56429d37962638cceea33c102158d91fb2f2ade8 100644 (file)
@@ -310,7 +310,7 @@ protected
         File.chmod 0600, fn
         FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(bakfn) > File.size(fn)
       end
-      Redwood::save_yaml_obj @sources.values, fn
+      Redwood::save_yaml_obj @sources.values.sort_by { |s| s.id.to_i }, fn
       File.chmod 0600, fn
     end
     @sources_dirty = false
index 012663d85eb7739173184c09009de42e9f08e106..c17e8f3191527253571f125df1e02b6a9f8d480f 100644 (file)
@@ -11,6 +11,7 @@ module Redwood
 class Maildir < Source
   SCAN_INTERVAL = 30 # seconds
 
+  yaml_properties :uri, :cur_offset, :usual, :archived, :id
   def initialize uri, last_date=nil, usual=true, archived=false, id=nil
     super
     uri = URI(uri)
@@ -122,6 +123,4 @@ private
   end
 end
 
-Redwood::register_yaml(Maildir, %w(uri cur_offset usual archived id))
-
 end
index 49eb37b13e2b0e1382cfe2acf7dde0a68fcf945a..05ddbab09f6ae90153274add4c0b9083a2c78781 100644 (file)
@@ -5,6 +5,7 @@ module Redwood
 module MBox
 
 class Loader < Source
+  yaml_properties :uri, :cur_offset, :usual, :archived, :id
   def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
     super
 
@@ -131,7 +132,5 @@ class Loader < Source
   end
 end
 
-Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))
-
 end
 end
index f9e226af25e4e900bcaed0ab7ea351b2da89fe55..42a2b98cf3c62a24cddf83f12ab7f99769950033 100644 (file)
@@ -6,6 +6,9 @@ module MBox
 class SSHLoader < Source
   attr_accessor :username, :password
 
+  yaml_properties :uri, :username, :password, :cur_offset, :usual, 
+                  :archived, :id
+
   def initialize uri, username=nil, password=nil, start_offset=nil, usual=true, archived=false, id=nil
     raise ArgumentError, "not an mbox+ssh uri: #{uri.inspect}" unless uri =~ %r!^mbox\+ssh://!
 
@@ -66,7 +69,5 @@ class SSHLoader < Source
   end
 end
 
-Redwood::register_yaml(SSHLoader, %w(uri username password cur_offset usual archived id))
-
 end
 end
index 8a546b67e768166d63acccf261bbde0f46f47bd3..1f4392d76cd2a84b2f5057d0b68884c65ddcbdb4 100644 (file)
@@ -30,6 +30,8 @@ class SentManager
 end
 
 class SentLoader < MBox::Loader
+  yaml_properties :cur_offset
+
   def initialize cur_offset=0
     filename = Redwood::SENT_FN
     File.open(filename, "w") { } unless File.exists? filename
@@ -42,6 +44,4 @@ class SentLoader < MBox::Loader
   def labels; [:sent, :inbox]; end
 end
 
-Redwood::register_yaml(SentLoader, %w(cur_offset))
-
 end
index 1bc77a7a7f1ccafbbaeca3ab602780bd88508846..00f2d072261a2f810b20a50aab0bf2656bf52a7c 100644 (file)
@@ -5,15 +5,15 @@ class OutOfSyncSourceError < SourceError; end
 class FatalSourceError < SourceError; end
 
 class Source
-  ## Implementing a new source is typically quite easy, because Sup
-  ## only needs to be able to:
+  ## Implementing a new source should be easy, because Sup only needs
+  ## to be able to:
   ##  1. See how many messages it contains
   ##  2. Get an arbitrary message
   ##  3. (optional) see whether the source has marked it read or not
   ##
   ## In particular, Sup doesn't need to move messages, mark them as
-  ## read, delete them, or anything else. (Well, at some point it will
-  ## need to delete them, but that will be an optional capability.)
+  ## read, delete them, or anything else. (Well, it's nice to be able
+  ## to delete them, but that is optional.)
   ##
   ## On the other hand, Sup assumes that you can assign each message a
   ## unique integer id, such that newer messages have higher ids than
@@ -33,7 +33,8 @@ class Source
   ## - raw_header offset
   ## - raw_full_message offset
   ## - check
-  ## - next (or each, if you prefer)
+  ## - next (or each, if you prefer): should return a message and an
+  ##   array of labels.
   ##
   ## ... where "offset" really means unique id. (You can tell I
   ## started with mbox.)
@@ -46,7 +47,7 @@ class Source
   ## else (e.g. the imap server is down or the maildir is missing.)
   ##
   ## Finally, be sure the source is thread-safe, since it WILL be
-  ## pummeled from multiple threads at once.
+  ## pummelled from multiple threads at once.
   ##
   ## Examples for you to look at: mbox/loader.rb, imap.rb, and
   ## maildir.rb.
@@ -97,6 +98,4 @@ protected
   end
 end
 
-Redwood::register_yaml(Source, %w(uri cur_offset usual archived id))
-
 end
index 6db56a3e1023ac98f19b1086faa57c7b3fc3d771..77c2cc2edb274af57f4afa78b4d1f87bc00a4a3a 100644 (file)
@@ -344,6 +344,10 @@ end
 ## h2[:a].val # => 0
 ## h2[:a].val = 1
 ## h2[:a].val # => 1
+##
+## important note: you REALLY want to use #member? to test existence,
+## because just checking h[anything] will always evaluate to true
+## (except for degenerate constructor blocks that return nil or false)
 class SavingHash
   def initialize &b
     @constructor = b