class DownloadHistory
A singleton of this type manages the previously unfinished or queued downloads.
Attributes
history_file[R]
Public Class Methods
new()
click to toggle source
load an existing list of unfinished downloads
# File lib/download_history.rb, line 60 def initialize @log = @@log @log.level = $log_level home = ENV['HOME'] @history = nil @hthread = nil if home hist_dir = home.dup << File::SEPARATOR << ".#{File::basename($0) }" if !File.exist?(hist_dir) begin Dir.mkdir(hist_dir, 0700) rescue Exception => ex @log.error('Cannot create history in ' << hist_dir.dup << ': ' << ex.message) @log.error('Aborting') exit false end end @history_file = hist_dir.dup << File::SEPARATOR << @@history_base load_history @log.debug("history-file is in #{@history_file}") else @log.error('Cannot determine the current users home-directory') exit false end end
Public Instance Methods
[](ele)
click to toggle source
# File lib/download_history.rb, line 105 def [](ele) @log.debug('checking for element ' << ele.to_s) prev_dl = nil if(@history && !@history.empty?) if(ele.respond_to?(:to_str)) @history[ele] elsif(ele.respond_to?(:to_int)) @log.debug('looking for queued item #' << (ele).to_s) a = @history.to_a if(ele <= a.size) a[ele - 1][1] else(a.size > 0) raise StandardError.new('Queued items range from 1 to ' << a.size.to_s << '!') end end end end
delete(url)
click to toggle source
# File lib/download_history.rb, line 149 def delete(url) @log.debug('deleting from queue : ' << @history[url].to_s) @history.delete(url) dump load_history end
empty?()
click to toggle source
is there a history?
# File lib/download_history.rb, line 56 def empty? !@history || @history.empty? end
length()
click to toggle source
# File lib/download_history.rb, line 156 def length() return @history.length end
load_history()
click to toggle source
# File lib/download_history.rb, line 86 def load_history if(File.exist?(@history_file) && !File.zero?(@history_file) ) File.open(@history_file, 'r') do |hf| @history = Marshal.load(hf) @log.debug ('history is ' << @history.to_s) end end end
previous_download(url)
click to toggle source
find and keep for later reference a previous attempt to load the file from url
# File lib/download_history.rb, line 101 def previous_download(url) @history ? @history[url] : nil end
queued_URLs()
click to toggle source
# File lib/download_history.rb, line 95 def queued_URLs @history ? @history.keys : [] end
update(url, opts = {})
click to toggle source
update te download-history for the file at url.
# File lib/download_history.rb, line 124 def update(url, opts = {}) begin @log.debug('update') @history ||= Hash.new # produces a lot of output if ! @history.has_key?(url) @history[url] = Download.new(url, opts[:local]) elsif(opts[:local]) @history[url].local_file = opts[:local] end @history[url].position = opts[:position] if opts[:position] @history[url].size = opts[:size] if opts[:size] @history[url].updated=DateTime.now @history[url].file_hash @log.debug('dumping history ' << @history.inspect) dump rescue Exception => ex @log.error('CANNOT update history : ' << ex.message) @log.error('history is ' << @history.to_s) raise ex end @log.debug('download is ' << @history[url].to_s) end
Private Instance Methods
dump()
click to toggle source
# File lib/download_history.rb, line 38 def dump() @@log.debug('dump') unless @hthread && @hthread.alive? @hthread = Thread.new do begin File.delete(@history_file) if File.exist?(@history_file) File.open(@history_file, 'w') do |hf| Marshal.dump(@history, hf) end rescue Exception => ex @log.error 'cannot write history-file: ' << ex.message end end @hthread = nil end end