commit c4c43664436d7ac1f94b22a4ac7c41f2d7ef7a50
parent 9cac7b14eef40e6d1ed68a9cb9a303aa352ae517
Author: Rich Lane <rlane@club.cc.cmu.edu>
Date: Thu, 25 Mar 2010 20:35:19 -0700
Source#poll interface
Diffstat:
5 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/bin/sup-sync b/bin/sup-sync
@@ -179,7 +179,7 @@ begin
if Time.now - last_info_time > PROGRESS_UPDATE_INTERVAL
last_info_time = Time.now
elapsed = last_info_time - start_time
- pctdone = source.pct_done
+ pctdone = 0.0 * 100.0
remaining = (100.0 - pctdone) * (elapsed.to_f / pctdone)
printf "## read %dm (~%.0f%%) @ %.1fm/s. %s elapsed, ~%s remaining\n", num_scanned, pctdone, num_scanned / elapsed, elapsed.to_time_s, remaining.to_time_s
end
diff --git a/lib/sup/maildir.rb b/lib/sup/maildir.rb
@@ -86,26 +86,41 @@ class Maildir < Source
end
## XXX use less memory
- def each
+ def poll
@mtimes.each do |d,prev_mtime|
subdir = File.join @dir, d
raise FatalSourceError, "#{subdir} not a directory" unless File.directory? subdir
mtime = File.mtime subdir
next if prev_mtime >= mtime
@mtimes[d] = mtime
- old_ids = benchmark(:index) { Enumerator.new(Index, :each_source_info, self.id, "#{subdir}/").to_a }
- new_ids = benchmark(:glob) { Dir.glob("#{subdir}/*").map { |x| x[@dir.length..-1] }.sort }
+
+ old_ids = benchmark(:index) { Enumerator.new(Index, :each_source_info, self.id, "#{d}/").to_a }
+ new_ids = benchmark(:glob) { Dir.glob("#{subdir}/*").map { |x| File.basename x }.sort }
added = new_ids - old_ids
deleted = old_ids - new_ids
- info "#{added.size} added, #{deleted.size} deleted"
+ debug "#{added.size} added, #{deleted.size} deleted"
+
added.each do |id|
- yield id, @labels + (seen?(id) ? [] : [:unread]) + (trashed?(id) ? [:deleted] : []) + (flagged?(id) ? [:starred] : [])
+ yield :add,
+ :info => File.join(d,id),
+ :labels => @labels + maildir_labels(id),
+ :progress => 0.0
+ end
+
+ deleted.each do |id|
+ yield :delete,
+ :info => File.join(d,id),
+ :progress => 0.0
end
end
nil
end
- def pct_done; 0.0; end
+ def maildir_labels id
+ (seen?(id) ? [] : [:unread]) +
+ (trashed?(id) ? [:deleted] : []) +
+ (flagged?(id) ? [:starred] : [])
+ end
def draft? id; maildir_data(id)[2].include? "D"; end
def flagged? id; maildir_data(id)[2].include? "F"; end
diff --git a/lib/sup/mbox.rb b/lib/sup/mbox.rb
@@ -121,11 +121,14 @@ class MBox < Source
0.0
end
- def each
+ def poll
offset = first_new_message
end_offset = File.size @f
while offset and offset < end_offset
- yield offset, (labels + [:unread])
+ yield :add,
+ :info => offset,
+ :labels => (labels + [:unread]),
+ :progress => 0.0
offset = next_offset offset
end
end
diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb
@@ -161,14 +161,17 @@ EOS
begin
return if source.has_errors?
- source.each do |offset, source_labels|
+ source.poll do |sym, args|
if source.has_errors?
warn "error loading messages from #{source}: #{source.error.message}"
return
end
- m = Message.build_from_source source, offset
- m.labels += source_labels + (source.archived? ? [] : [:inbox])
+ next if sym == :delete
+ fail unless sym == :add
+
+ m = Message.build_from_source source, args[:info]
+ m.labels += args[:labels] + (source.archived? ? [] : [:inbox])
m.labels.delete :unread if m.source_marked_read? # preserve read status if possible
m.labels.each { |l| LabelManager << l }
diff --git a/lib/sup/source.rb b/lib/sup/source.rb
@@ -79,8 +79,10 @@ class Source
def == o; o.uri == uri; end
def is_source_for? uri; uri == @uri; end
- ## yields successive offsets and labels
- def each
+ ## Yields values of the form [Symbol, Hash]
+ ## add: info, labels, progress
+ ## delete: info, progress
+ def poll
unimplemented
end