lib/sup/mbox/loader.rb in sup-0.0.8 vs lib/sup/mbox/loader.rb in sup-0.1
- old
+ new
@@ -3,30 +3,40 @@
module Redwood
module MBox
class Loader < Source
- def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil
- super
+ yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
+ def initialize uri_or_fp, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
+ super uri_or_fp, start_offset, usual, archived, id
@mutex = Mutex.new
- @labels = [:unread]
+ @labels = (labels || []).freeze
case uri_or_fp
when String
uri = URI(uri_or_fp)
raise ArgumentError, "not an mbox uri" unless uri.scheme == "mbox"
raise ArgumentError, "mbox uri ('#{uri}') cannot have a host: #{uri.host}" if uri.host
- ## heuristic: use the filename as a label, unless the file
- ## has a path that probably represents an inbox.
- @labels << File.basename(uri.path).intern unless File.dirname(uri.path) =~ /\b(var|usr|spool)\b/
@f = File.open uri.path
else
@f = uri_or_fp
end
end
+ def file_path; URI(uri).path end
+
+ def self.suggest_labels_for path
+ ## heuristic: use the filename as a label, unless the file
+ ## has a path that probably represents an inbox.
+ if File.dirname(path) =~ /\b(var|usr|spool)\b/
+ []
+ else
+ [File.basename(path).intern]
+ end
+ end
+
def check
if (cur_offset ||= start_offset) > end_offset
raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client."
end
end
@@ -71,18 +81,28 @@
ret
end
def raw_full_message offset
ret = ""
+ each_raw_full_message_line(offset) { |l| ret += l }
+ ret
+ end
+
+ ## apparently it's a million times faster to call this directly if
+ ## we're just moving messages around on disk, than reading things
+ ## into memory with raw_full_message.
+ ##
+ ## i hoped never to have to move shit around on disk but
+ ## sup-sync-back has to do it.
+ def each_raw_full_message_line offset
@mutex.synchronize do
@f.seek offset
- @f.gets # skip mbox header
+ yield @f.gets
until @f.eof? || (l = @f.gets) =~ BREAK_RE
- ret += l
+ yield l
end
end
- ret
end
def next
returned_offset = nil
next_offset = cur_offset
@@ -115,13 +135,11 @@
rescue SystemCallError, IOError => e
raise FatalSourceError, "Error reading #{@f.path}: #{e.message}"
end
self.cur_offset = next_offset
- [returned_offset, @labels.clone]
+ [returned_offset, @labels]
end
end
-
-Redwood::register_yaml(Loader, %w(uri cur_offset usual archived id))
end
end