From df66a19276091678b01f1f9c147cb6a58f3dd6f0 Mon Sep 17 00:00:00 2001 From: Rich Lane Date: Sat, 20 Jun 2009 13:50:06 -0700 Subject: [PATCH] move source-related methods to SourceManager --- bin/sup | 10 ++++---- bin/sup-add | 10 ++++---- bin/sup-config | 14 +++++----- bin/sup-recover-sources | 7 ++--- bin/sup-sync | 6 ++--- bin/sup-sync-back | 4 +-- bin/sup-tweak-labels | 4 +-- lib/sup.rb | 5 ++-- lib/sup/index.rb | 52 +++---------------------------------- lib/sup/poll.rb | 2 +- lib/sup/source.rb | 57 +++++++++++++++++++++++++++++++++++++++++ 11 files changed, 92 insertions(+), 79 deletions(-) diff --git a/bin/sup b/bin/sup index 302ad7c..1febefd 100755 --- a/bin/sup +++ b/bin/sup @@ -160,17 +160,17 @@ begin Redwood::start Index.load - if(s = Index.source_for DraftManager.source_name) + if(s = Redwood::SourceManager.source_for DraftManager.source_name) DraftManager.source = s else Redwood::log "no draft source, auto-adding..." - Index.add_source DraftManager.new_source + Redwood::SourceManager.add_source DraftManager.new_source end - if(s = Index.source_for SentManager.source_uri) + if(s = Redwood::SourceManager.source_for SentManager.source_uri) SentManager.source = s else - Index.add_source SentManager.default_source + Redwood::SourceManager.add_source SentManager.default_source end HookManager.run "startup" @@ -190,7 +190,7 @@ begin bm.draw_screen - Index.usual_sources.each do |s| + Redwood::SourceManager.usual_sources.each do |s| next unless s.respond_to? :connect reporting_thread("call #connect on #{s}") do begin diff --git a/bin/sup-add b/bin/sup-add index 50bbb29..3ab7c4d 100755 --- a/bin/sup-add +++ b/bin/sup-add @@ -82,12 +82,12 @@ index = Redwood::Index.new index.lock_or_die begin - index.load_sources + Redwood::SourceManager.load_sources ARGV.each do |uri| labels = $opts[:labels] ? $opts[:labels].split(/\s*,\s*/).uniq : [] - if !$opts[:force_new] && index.source_for(uri) + if !$opts[:force_new] && Redwood::SourceManager.source_for(uri) say "Already know about #{uri}; skipping." next end @@ -99,10 +99,10 @@ begin when "mbox+ssh" say "For SSH connections, if you will use public key authentication, you may leave the username and password blank." say "" - username, password = get_login_info uri, index.sources + username, password = get_login_info uri, Redwood::SourceManager.sources Redwood::MBox::SSHLoader.new uri, username, password, nil, !$opts[:unusual], $opts[:archive], nil, labels when "imap", "imaps" - username, password = get_login_info uri, index.sources + username, password = get_login_info uri, Redwood::SourceManager.sources Redwood::IMAP.new uri, username, password, nil, !$opts[:unusual], $opts[:archive], nil, labels when "maildir" Redwood::Maildir.new uri, nil, !$opts[:unusual], $opts[:archive], nil, labels @@ -114,7 +114,7 @@ begin Trollop::die "Unknown source type #{parsed_uri.scheme.inspect}" end say "Adding #{source}..." - index.add_source source + Redwood::SourceManager.add_source source end ensure index.save diff --git a/bin/sup-config b/bin/sup-config index 398197f..9fcbee6 100755 --- a/bin/sup-config +++ b/bin/sup-config @@ -152,7 +152,7 @@ end $terminal.wrap_at = :auto Redwood::start index = Redwood::Index.new -index.load_sources +Redwood::SourceManager.load_sources say < source, :source_info => offset m.load_from_source! - source_id = index.source_for_id m.id + source_id = Redwood::SourceManager.source_for_id m.id next unless source_id source_ids[source_id] += 1 count += 1 @@ -85,7 +86,7 @@ ARGV.each do |fn| id = source_ids.keys.first.to_i puts "assigned #{source} to #{source_ids.keys.first}" source.id = id - index.add_source source + Redwood::SourceManager.add_source source else puts ">> unable to determine #{source}: #{source_ids.inspect}" end diff --git a/bin/sup-sync b/bin/sup-sync index 18a3cab..270524a 100755 --- a/bin/sup-sync +++ b/bin/sup-sync @@ -116,11 +116,11 @@ begin index.load sources = ARGV.map do |uri| - index.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?" + Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?" end - sources = index.usual_sources if sources.empty? - sources = index.sources if opts[:all_sources] + sources = Redwood::SourceManager.usual_sources if sources.empty? + sources = Redwood::SourceManager.sources if opts[:all_sources] unless target == :new if opts[:start_at] diff --git a/bin/sup-sync-back b/bin/sup-sync-back index 05b9e8c..f09ae5b 100755 --- a/bin/sup-sync-back +++ b/bin/sup-sync-back @@ -80,13 +80,13 @@ begin index.load sources = ARGV.map do |uri| - s = index.source_for(uri) or die "unknown source: #{uri}. Did you add it with sup-add first?" + s = Redwood::SourceManager.source_for(uri) or die "unknown source: #{uri}. Did you add it with sup-add first?" s.is_a?(Redwood::MBox::Loader) or die "#{uri} is not an mbox source." s end if sources.empty? - sources = index.usual_sources.select { |s| s.is_a? Redwood::MBox::Loader } + sources = Redwood::SourceManager.usual_sources.select { |s| s.is_a? Redwood::MBox::Loader } end unless sources.all? { |s| s.file_path.nil? } || File.executable?(dotlockfile) || opts[:dont_use_dotlockfile] diff --git a/bin/sup-tweak-labels b/bin/sup-tweak-labels index 6f603e2..95a3b03 100755 --- a/bin/sup-tweak-labels +++ b/bin/sup-tweak-labels @@ -66,10 +66,10 @@ begin source_ids = if opts[:all_sources] - index.sources + Redwood::SourceManager.sources else ARGV.map do |uri| - index.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?" + Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?" end end.map { |s| s.id } Trollop::die "nothing to do: no sources" if source_ids.empty? diff --git a/lib/sup.rb b/lib/sup.rb index 8373820..5689c2b 100644 --- a/lib/sup.rb +++ b/lib/sup.rb @@ -115,6 +115,7 @@ module Redwood Redwood::SuicideManager.new Redwood::SUICIDE_FN Redwood::CryptoManager.new Redwood::UndoManager.new + Redwood::SourceManager.new end def finish @@ -130,7 +131,7 @@ module Redwood def report_broken_sources opts={} return unless BufferManager.instantiated? - broken_sources = Index.sources.select { |s| s.error.is_a? FatalSourceError } + broken_sources = SourceManager.sources.select { |s| s.error.is_a? FatalSourceError } unless broken_sources.empty? BufferManager.spawn_unless_exists("Broken source notification for #{broken_sources.join(',')}", opts) do TextMode.new(< (query[:limit] || :all) } @@ -604,21 +573,6 @@ private q.add_query Ferret::Search::TermQuery.new("source_id", query[:source_id]), :must if query[:source_id] q end - - def save_sources fn=Redwood::SOURCE_FN - @source_mutex.synchronize do - if @sources_dirty || @sources.any? { |id, s| s.dirty? } - bakfn = fn + ".bak" - if File.exists? fn - 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 - File.chmod 0600, fn - end - @sources_dirty = false - end - end end end diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb index bbad5f2..c83290c 100644 --- a/lib/sup/poll.rb +++ b/lib/sup/poll.rb @@ -83,7 +83,7 @@ EOS from_and_subj_inbox = [] @mutex.synchronize do - Index.usual_sources.each do |source| + SourceManager.usual_sources.each do |source| # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})" begin yield "Loading from #{source}... " unless source.done? || (source.respond_to?(:has_errors?) && source.has_errors?) diff --git a/lib/sup/source.rb b/lib/sup/source.rb index fb98dbc..1bb7797 100644 --- a/lib/sup/source.rb +++ b/lib/sup/source.rb @@ -155,4 +155,61 @@ protected end end +class SourceManager + include Singleton + + def initialize + @sources = {} + @sources_dirty = false + @source_mutex = Monitor.new + self.class.i_am_the_instance self + end + + def [](id) + @source_mutex.synchronize { @sources[id] } + end + + def add_source source + @source_mutex.synchronize do + raise "duplicate source!" if @sources.include? source + @sources_dirty = true + max = @sources.max_of { |id, s| s.is_a?(DraftLoader) || s.is_a?(SentLoader) ? 0 : id } + source.id ||= (max || 0) + 1 + ##source.id += 1 while @sources.member? source.id + @sources[source.id] = source + end + end + + def sources + ## favour the inbox by listing non-archived sources first + @source_mutex.synchronize { @sources.values }.sort_by { |s| s.id }.partition { |s| !s.archived? }.flatten + end + + def source_for uri; sources.find { |s| s.is_source_for? uri }; end + def usual_sources; sources.find_all { |s| s.usual? }; end + + def load_sources fn=Redwood::SOURCE_FN + source_array = (Redwood::load_yaml_obj(fn) || []).map { |o| Recoverable.new o } + @source_mutex.synchronize do + @sources = Hash[*(source_array).map { |s| [s.id, s] }.flatten] + @sources_dirty = false + end + end + + def save_sources fn=Redwood::SOURCE_FN + @source_mutex.synchronize do + if @sources_dirty || @sources.any? { |id, s| s.dirty? } + bakfn = fn + ".bak" + if File.exists? fn + 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 + File.chmod 0600, fn + end + @sources_dirty = false + end + end +end + end -- 2.43.0