Class: TreeRb::DirCatVisitor

Inherits:
BasicTreeNodeVisitor show all
Defined in:
lib/tree_rb/output_plugins/dircat/dircat_visitor.rb

Constant Summary collapse

CR =
"\r"
CLEAR =
"\e[K"

Instance Method Summary collapse

Methods inherited from BasicTreeNodeVisitor

#cannot_enter_node, #cannot_visit_leaf, #enter_node, #exit_node

Constructor Details

#initialize(out_filename, options = { }) ⇒ DirCatVisitor

Returns a new instance of DirCatVisitor.



9
10
11
12
13
14
15
16
17
# File 'lib/tree_rb/output_plugins/dircat/dircat_visitor.rb', line 9

def initialize(out_filename, options = { })
  @start          = Time.now
  @bytes          = 0
  @entries        = Array.new
  @md5_to_entries = Hash.new
  @out_filename   = out_filename
  @verbose_level = 1
  @show_progress = true
end

Instance Method Details

#add_entry(e) ⇒ Object

add entry to this catalog



35
36
37
38
39
40
41
42
# File 'lib/tree_rb/output_plugins/dircat/dircat_visitor.rb', line 35

def add_entry(e)
  @entries.push(e)
  if @md5_to_entries.has_key?(e.md5)
    @md5_to_entries[e.md5].push(e)
  else
    @md5_to_entries[e.md5] = [e]
  end
end

#save_toObject

Save serialized catalog to file

Parameters:

  • file (String, File)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tree_rb/output_plugins/dircat/dircat_visitor.rb', line 62

def save_to
  if @out_filename.kind_of?(String)
    begin
      File.open(@out_filename, "w") do |f|
        f.puts to_ser.to_yaml
      end
    rescue Errno::ENOENT
      raise DirCatException.new, "DirCat: cannot write into '#{file}'", caller
    end
  else
    @out_filename.puts to_ser.to_yaml
  end
end

#to_serDirCatSer

Returns serialized catalog.

Returns:

  • (DirCatSer)

    serialized catalog



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tree_rb/output_plugins/dircat/dircat_visitor.rb', line 46

def to_ser
  dircat_ser = DirCat::DirCatSer.new
  dircat_ser.dircat_version = DirCat::FORMAT_VERSION
  dircat_ser.dirname = @dirname
  dircat_ser.ctime = @ctime
  dircat_ser.entries = []
  @entries.each do |entry|
    dircat_ser.entries << entry.to_ser
  end
  dircat_ser
end

#visit_leaf(filename) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tree_rb/output_plugins/dircat/dircat_visitor.rb', line 19

def visit_leaf(filename)
  entry = DirCat::Entry.from_filename(filename)
  add_entry(entry)
  @bytes += entry.size
  if @verbose_level > 0
    print "#{CR}#{filename}#{CLEAR}"
  end
  if @show_progress
    sec = Time.now - @start
    print "#{CR}bytes: #{@bytes.to_human} time: #{sec} bytes/sec #{@bytes/sec} #{CLEAR}"
  end
end