Class: DirCat::CatOnYaml
- Inherits:
-
Object
- Object
- DirCat::CatOnYaml
- Defined in:
- lib/dircat/cat_on_yaml/cat_on_yaml.rb
Overview
Catalog of files (contained into directory :-))
Constant Summary collapse
- CR =
"\r"
- CLEAR =
"\e[K"
Instance Attribute Summary collapse
-
#ctime ⇒ Object
creation date.
-
#dirname ⇒ Object
readonly
Directory name.
-
#show_progress ⇒ Object
readonly
Returns the value of attribute show_progress.
-
#verbose_level ⇒ Object
readonly
verbose level used to print message on $stdout.
Class Method Summary collapse
-
.from_dir(dirname, options = {}) ⇒ Object
Build catalog from a directory.
-
.from_file(filename, options = {}) ⇒ Object
Load catalog from a serialize file.
- .load(file_or_dir, options = {}) ⇒ Object
Instance Method Summary collapse
-
#-(right) ⇒ CatOnYaml
return differences from this catalog and right catalog param [Cat] right.
- #_load_from_dir ⇒ Object
-
#add_entry(e) ⇒ Object
add entry to this catalog.
-
#bytes ⇒ Number
total size number of file cataloged.
- #contains(e) ⇒ Object
-
#duplicates ⇒ Array
Entries representing duplicate files.
-
#empty? ⇒ Boolean
number of entries == 0.
-
#fmt_report(*columns) ⇒ Object
print a complex report on stdout.
- #fmt_ruby(dst) ⇒ Object
-
#fmt_simple ⇒ Object
(also: #to_s)
list of entries on stdout @return.
-
#from_dir(dirname) ⇒ Object
Build a catalog from a directory.
-
#from_file(filename) ⇒ Object
Load catalog from a file.
-
#initialize(options = {}) ⇒ CatOnYaml
constructor
A new instance of CatOnYaml.
- #list_dup ⇒ Object
- #load(file_or_dir) ⇒ Object
-
#report ⇒ String
simple report with essential information about this catalog.
-
#save_to(file) ⇒ Object
Save serialized catalog to file.
-
#script_dup ⇒ String
return ruby script to eliminate duplicated.
-
#size ⇒ Number
number of entries (files).
-
#to_ser ⇒ DirCatSer
serialize catalog.
Constructor Details
#initialize(options = {}) ⇒ CatOnYaml
Returns a new instance of CatOnYaml.
27 28 29 30 31 32 33 34 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 27 def initialize( = {}) @verbose_level = .delete(:verbose) || 0 @show_progress = .delete(:show_progress) @dirname = "" @ctime = DateTime.now @entries = Array.new @md5_to_entries = Hash.new end |
Instance Attribute Details
#ctime ⇒ Object
creation date
17 18 19 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 17 def ctime @ctime end |
#dirname ⇒ Object (readonly)
Directory name
12 13 14 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 12 def dirname @dirname end |
#show_progress ⇒ Object (readonly)
Returns the value of attribute show_progress.
23 24 25 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 23 def show_progress @show_progress end |
#verbose_level ⇒ Object (readonly)
verbose level used to print message on $stdout
22 23 24 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 22 def verbose_level @verbose_level end |
Class Method Details
.from_dir(dirname, options = {}) ⇒ Object
Build catalog from a directory
41 42 43 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 41 def self.from_dir(dirname, = {}) new().from_dir(dirname) end |
.from_file(filename, options = {}) ⇒ Object
Load catalog from a serialize file
50 51 52 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 50 def self.from_file(filename, = {}) new().from_file(filename) end |
.load(file_or_dir, options = {}) ⇒ Object
54 55 56 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 54 def self.load(file_or_dir, = {}) new().load(file_or_dir) end |
Instance Method Details
#-(right) ⇒ CatOnYaml
return differences from this catalog and right catalog param [Cat] right
208 209 210 211 212 213 214 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 208 def -(right) result = CatOnYaml.new @entries.each do |e| result.add_entry(e) unless right.contains(e) end result end |
#_load_from_dir ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 113 def _load_from_dir start = Time.now me = self bytes = 0 TreeRb::DirTreeWalker.new.run @dirname do on_leaf do |filename| entry = Entry.from_filename(filename) me.add_entry(entry) bytes += entry.size if me.verbose_level > 0 print "#{CR}#{filename}#{CLEAR}" end if me.show_progress sec = Time.now - start print "#{CR}bytes: #{bytes.to_human} time: #{sec} bytes/sec #{bytes/sec} #{CLEAR}" end end end self end |
#add_entry(e) ⇒ Object
add entry to this catalog
191 192 193 194 195 196 197 198 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 191 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 |
#bytes ⇒ Number
total size number of file cataloged
170 171 172 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 170 def bytes @entries.inject(0) { |sum, entry| sum + entry.size } end |
#contains(e) ⇒ Object
200 201 202 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 200 def contains(e) @md5_to_entries.has_key?(e.md5) end |
#duplicates ⇒ Array
Returns entries representing duplicate files.
244 245 246 247 248 249 250 251 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 244 def duplicates list = [] @md5_to_entries.each_value do |ee| next if ee.size < 2 list.push(ee) end list end |
#empty? ⇒ Boolean
number of entries == 0
163 164 165 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 163 def empty? @entries.size == 0 end |
#fmt_report(*columns) ⇒ Object
print a complex report on stdout
228 229 230 231 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 228 def fmt_report(*columns) columns = [:md5, :name, :path, :size] if columns.empty? OptParseCommand::report(@entries, *columns) end |
#fmt_ruby(dst) ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 233 def fmt_ruby(dst) puts "require 'fileutils'" @entries.each { |entry| src = File.join(@dirname, entry.path, entry.name); puts "FileUtils.cp( \"#{src}\", \"#{dst}\" )" } end |
#fmt_simple ⇒ Object Also known as: to_s
list of entries on stdout @return
219 220 221 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 219 def fmt_simple @entries.inject('') { |s, e| s << e.to_s << "\n" } end |
#from_dir(dirname) ⇒ Object
Build a catalog from a directory
59 60 61 62 63 64 65 66 67 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 59 def from_dir(dirname) unless File.directory?(dirname) raise "'#{dirname}' is not a directory or doesn't exists" end @dirname = File. dirname @ctime = DateTime.now _load_from_dir self end |
#from_file(filename) ⇒ Object
Load catalog from a file
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 70 def from_file(filename) unless File.exist?(filename) raise DirCatException.new, "'#{filename}' not exists" end dircat_ser = File.open(filename) { |f| YAML::load(f) } @dirname = dircat_ser.dirname @ctime = dircat_ser.ctime dircat_ser.entries.each do |entry_ser| add_entry(Entry.from_ser(entry_ser)) end self end |
#list_dup ⇒ Object
253 254 255 256 257 258 259 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 253 def list_dup r = "" duplicates.flatten.each do |e| r += e.to_s + "\n" end r end |
#load(file_or_dir) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 83 def load(file_or_dir) if File.directory?(file_or_dir) from_dir(file_or_dir) elsif File.exists?(file_or_dir) from_file(file_or_dir) else raise DirCatException.new, "'#{file_or_dir}' not exists" end end |
#report ⇒ String
simple report with essential information about this catalog
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 177 def report dups = duplicates s = "Base dir: #{@dirname}\n" s += "Nr. file: #{size}" if dups.size > 0 s+= " (duplicates #{dups.size})" end s += "\nBytes: #{bytes.with_separator}" s end |
#save_to(file) ⇒ Object
Save serialized catalog to file
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 139 def save_to(file) if file.kind_of?(String) begin File.open(file, "w") do |f| f.puts to_ser.to_yaml end rescue Errno::ENOENT raise DirCatException.new, "DirCat: cannot write into '#{file}'", caller end else file.puts to_ser.to_yaml end end |
#script_dup ⇒ String
return ruby script to eliminate duplicated
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 265 def script_dup r = "require 'fileutils'\n" duplicates.each do |entries| flg_first = true r += "\n" entries.each do |entry| src = File.join(@dirname, entry.path, entry.name) if flg_first flg_first = false r += "# FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n" else r += "FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n" end end end r end |
#size ⇒ Number
number of entries (files)
156 157 158 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 156 def size @entries.size end |
#to_ser ⇒ DirCatSer
serialize catalog
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 95 def to_ser dircat_ser = DirCatSer.new dircat_ser.dircat_version = DirCat::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 |