tree.rb 0.3.10 → 0.3.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/examples/ruby_examples/find_directory_without_subdirectory.rb +2 -2
  3. data/examples/ruby_examples/find_files.rb +4 -5
  4. data/examples/ruby_examples/print_files.rb +14 -2
  5. data/lib/tree_rb.rb +17 -15
  6. data/lib/tree_rb/cli/cli_tree.rb +32 -37
  7. data/lib/tree_rb/{abs_node.rb → core/abs_node.rb} +0 -1
  8. data/lib/tree_rb/{basic_tree_node_visitor.rb → core/basic_tree_node_visitor.rb} +0 -0
  9. data/lib/tree_rb/{leaf_node.rb → core/leaf_node.rb} +0 -0
  10. data/lib/tree_rb/{tree_node.rb → core/tree_node.rb} +1 -0
  11. data/lib/tree_rb/{tree_node_visitor.rb → core/tree_node_visitor.rb} +0 -0
  12. data/lib/tree_rb/exception.rb +7 -0
  13. data/lib/tree_rb/{util → input_file_system}/dir_processor.rb +0 -0
  14. data/lib/tree_rb/{directory_walker.rb → input_file_system/directory_walker.rb} +3 -3
  15. data/lib/tree_rb/input_html_page/dom_walker.rb +36 -0
  16. data/lib/tree_rb/output_dircat/dircat_helper.rb +24 -0
  17. data/lib/tree_rb/output_dircat/dircat_visitor.rb +78 -0
  18. data/lib/tree_rb/output_dircat/entry.rb +65 -0
  19. data/lib/tree_rb/{d3js_helper.rb → output_html/d3js_helper.rb} +10 -2
  20. data/lib/tree_rb/output_html/directory_to_hash2_visitor.rb +105 -0
  21. data/lib/tree_rb/{erb_render.rb → output_html/erb_render.rb} +0 -0
  22. data/lib/tree_rb/{visitors → output_sqlite}/sqlite_dir_tree_visitor.rb +0 -1
  23. data/lib/tree_rb/output_sqlite/sqlite_helper.rb +29 -0
  24. data/lib/tree_rb/version.rb +1 -1
  25. data/lib/tree_rb/visitors/callback_tree_node_visitor2.rb +0 -1
  26. data/lib/tree_rb/visitors/clone_tree_node_visitor.rb +0 -1
  27. data/lib/tree_rb/visitors/depth_tree_node_visitor.rb +0 -1
  28. data/lib/tree_rb/visitors/{flat_print_tree_node_visitors.rb → flat_print_tree_node_visitor.rb} +0 -0
  29. data/lib/tree_rb/{visitors → visitors_file_system}/build_dir_tree_visitor.rb +0 -0
  30. data/lib/tree_rb/{visitors → visitors_file_system}/directory_to_hash_visitor.rb +0 -0
  31. data/lib/tree_rb_cli.rb +0 -2
  32. data/spec/fixtures/html_pages/test_1.html +22 -0
  33. data/spec/fixtures/tmp/dircat +34 -0
  34. data/spec/tree_rb/cli/cli_tree_generic_spec.rb +2 -2
  35. data/spec/tree_rb/{tree_dsl_spec.rb → core/tree_dsl_spec.rb} +3 -3
  36. data/spec/tree_rb/{tree_dsl_with_derived_class1_spec.rb → core/tree_dsl_with_derived_class1_spec.rb} +1 -1
  37. data/spec/tree_rb/{tree_dsl_with_derived_class_spec.rb → core/tree_dsl_with_derived_class_spec.rb} +1 -1
  38. data/spec/tree_rb/{tree_node_paths_spec.rb → core/tree_node_paths_spec.rb} +2 -2
  39. data/spec/tree_rb/{tree_node_spec.rb → core/tree_node_spec.rb} +1 -1
  40. data/spec/tree_rb/{tree_node_visitor_delegate_spec.rb → core/tree_node_visitor_delegate_spec.rb} +1 -1
  41. data/spec/tree_rb/{tree_node_visitor_dsl_spec.rb → core/tree_node_visitor_dsl_spec.rb} +1 -1
  42. data/spec/tree_rb/{util → input_file_system}/dir_processor_spec.rb +2 -2
  43. data/spec/tree_rb/{directory_walker_spec.rb → input_file_system/directory_walker_spec.rb} +11 -12
  44. data/spec/tree_rb/input_html_page/dom_walker_spec.rb +18 -0
  45. data/tree.rb.gemspec +12 -11
  46. metadata +95 -79
  47. data/lib/tree_rb/visitors/directory_to_hash2_visitor.rb +0 -418
  48. data/spec/fixtures/test_dir_1/.dir_with_dot/dummy.txt +0 -1
  49. data/spec/fixtures/test_dir_2/[Dsube]/sub/.gitkeep +0 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2cb987c6a880527ec3110fa658983b5d60caa431
4
+ data.tar.gz: 7ab651eb25d463d83fbab1fd5e056a5e53499b57
5
+ SHA512:
6
+ metadata.gz: 3cab8085a32ff8ac7a4a6c588fb42135dccfed166a274f70f59d947dfd584664fcf7dbfbf2c943fa2ca909ac25d7e14664f3fabb7c56daf7bc270d1e2d4816d7
7
+ data.tar.gz: 82dc7d99808412b5254790087c9ded891b970909a0a12f29e3d69bfb9f7fa52efca1545bcb5b490d7ea3b4c748b7cf7b951f1972c2ee32cc818923d0c8028fb4
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'ostruct'
3
3
 
4
- cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
4
+ cwd = File.expand_path( File.join( File.dirname(__FILE__), '..', '..', 'lib') )
5
5
  $:.unshift(cwd) unless $:.include?(cwd)
6
6
  require 'tree_rb'
7
7
 
@@ -31,7 +31,7 @@ class DirWithoutSubDir < TreeRb::BasicTreeNodeVisitor
31
31
 
32
32
  end
33
33
 
34
- dtw = TreeRb::DirTreeWalker.new( File.join("..", ".." ))
34
+ dtw = TreeRb::DirTreeWalker.new( File.join('..', '..'))
35
35
  dtw.ignore /^\./
36
36
  dtw.visit_file=false
37
37
  dtw.run( DirWithoutSubDir.new )
@@ -1,6 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
-
3
- cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
2
+ cwd = File.expand_path( File.join( File.dirname(__FILE__), '..', '..', 'lib') )
4
3
  $:.unshift(cwd) unless $:.include?(cwd)
5
4
  require 'tree_rb'
6
5
  include TreeRb
@@ -11,8 +10,8 @@ class MyVisitor < BasicTreeNodeVisitor
11
10
  end
12
11
  end
13
12
 
14
- dtw = DirTreeWalker.new( File.join(File.dirname(__FILE__), "..", ".." ) )
15
- dtw.match "leaf_node.rb"
16
- dtw.match "abs_node.rb"
13
+ dtw = DirTreeWalker.new( File.join(File.dirname(__FILE__), '..', '..') )
14
+ dtw.match 'leaf_node.rb'
15
+ dtw.match 'abs_node.rb'
17
16
  dtw.run( MyVisitor.new )
18
17
 
@@ -4,8 +4,20 @@ $:.unshift(cwd) unless $:.include?(cwd)
4
4
  require 'tree_rb'
5
5
  include TreeRb
6
6
 
7
- dtw = DirTreeWalker.new( :ignore => ".git" )
8
- dtw.run ".." do
7
+ root_dir = '..'
8
+
9
+ puts '*** print all files excluding "*.git" and "*.ini"'
10
+ dtw = DirTreeWalker.new( :ignore => %w(.git .ini) )
11
+ dtw.run root_dir do
12
+ on_leaf do |pathname|
13
+ puts "- #{pathname}"
14
+ end
15
+ end
16
+
17
+ puts '*** print files with extension *.sh" and "*.js"'
18
+ #dtw = DirTreeWalker.new( :match => %w(/.sh/ /.jd/) )
19
+ dtw = DirTreeWalker.new( :match => /.sh/ )
20
+ dtw.run root_dir do
9
21
  on_leaf do |pathname|
10
22
  puts "- #{pathname}"
11
23
  end
data/lib/tree_rb.rb CHANGED
@@ -5,6 +5,8 @@
5
5
  #
6
6
  require 'pathname'
7
7
  require 'yaml'
8
+ require 'ostruct'
9
+ require 'nokogiri'
8
10
 
9
11
  #
10
12
  # rubygems
@@ -25,26 +27,22 @@ end
25
27
  require 'tree_rb/version'
26
28
  require 'tree_rb/extension_digest'
27
29
  require 'tree_rb/extension_numeric'
30
+ require 'tree_rb/exception'
28
31
 
29
- require 'tree_rb/abs_node'
30
- require 'tree_rb/leaf_node'
31
- require 'tree_rb/tree_node'
32
- require 'tree_rb/basic_tree_node_visitor'
33
- require 'tree_rb/tree_node_visitor'
32
+ require 'tree_rb/core/abs_node'
33
+ require 'tree_rb/core/leaf_node'
34
+ require 'tree_rb/core/tree_node'
35
+ require 'tree_rb/core/basic_tree_node_visitor'
36
+ require 'tree_rb/core/tree_node_visitor'
34
37
 
35
- require 'tree_rb/directory_walker'
38
+ require 'tree_rb/input_file_system/directory_walker'
39
+ require 'tree_rb/input_file_system/dir_processor'
40
+
41
+ require 'tree_rb/input_html_page/dom_walker'
36
42
 
37
43
  #
38
44
  # visitors
39
45
  #
40
- #require 'tree_rb/visitors/block_tree_node_visitor'
41
- #require 'tree_rb/visitors/build_dir_tree_visitor'
42
- #require 'tree_rb/visitors/callback_tree_node_visitor2'
43
- #require 'tree_rb/visitors/clone_tree_node_visitor'
44
- #require 'tree_rb/visitors/depth_tree_node_visitor'
45
- #require 'tree_rb/visitors/print_dir_tree_visitor'
46
- #require 'tree_rb/visitors/directory_to_hash_visitor'
47
- #require 'tree_rb/visitors/sqlite_dir_tree_visitor'
48
46
 
49
47
  visitors_dir = File.join(File.dirname(__FILE__), "tree_rb", "visitors")
50
48
  unless Dir.exist? visitors_dir
@@ -52,5 +50,9 @@ unless Dir.exist? visitors_dir
52
50
  end
53
51
  Dir[ File.join(visitors_dir, "*.rb") ].each { |f|require f }
54
52
 
53
+ visitors_dir = File.join(File.dirname(__FILE__), "tree_rb", "visitors_file_system")
54
+ unless Dir.exist? visitors_dir
55
+ raise "cannot found directory '#{visitors_dir}'"
56
+ end
57
+ Dir[ File.join(visitors_dir, "*.rb") ].each { |f|require f }
55
58
 
56
- require 'tree_rb/util/dir_processor'
@@ -74,7 +74,7 @@ module TreeRb
74
74
  options[:show_indentation] = false
75
75
  end
76
76
 
77
- algos = %w[build-dir print-dir json d3js html_partition html_tree html_treemap yaml sqlite]
77
+ algos = %w[build-dir print-dir json d3js html_partition html_tree html_treemap yaml sqlite dircat]
78
78
  # algo_aliases = { "b" => "build-dir", "v" => "print-dir", "j" => "json", "y" => "yaml", "s" => "sqlite" }
79
79
  # algo_list = (algo_aliases.keys + algos).join(',')
80
80
  parser.on("--format ALGO", algos, "select an algo", " (#{algos})") do |algo|
@@ -191,6 +191,10 @@ module TreeRb
191
191
  $stderr.puts e.to_s
192
192
  $stderr.puts "try --help for help"
193
193
  return false
194
+ rescue OptionParser::InvalidArgument => e
195
+ $stderr.puts e.to_s
196
+ $stderr.puts "try --help for help"
197
+ return false
194
198
  end
195
199
 
196
200
  unless options[:exit].nil?
@@ -230,8 +234,14 @@ module TreeRb
230
234
  #
231
235
  # 1. build dir tree walker
232
236
  #
237
+
233
238
  dirname = File.expand_path(dirname)
234
- directory_tree_walker = DirTreeWalker.new(dirname, options)
239
+ begin
240
+ directory_tree_walker = DirTreeWalker.new(dirname, options)
241
+ rescue ArgumentError => e
242
+ $stderr.puts e.to_s
243
+ return false
244
+ end
235
245
  unless options[:all_files]
236
246
  directory_tree_walker.ignore(/^\.[^.]+/) # ignore all file starting with "."
237
247
  end
@@ -255,6 +265,11 @@ module TreeRb
255
265
  visitor = PrintDirTreeVisitor.new
256
266
  directory_tree_walker.run(visitor)
257
267
 
268
+ when 'yaml'
269
+ visitor = DirectoryToHashVisitor.new(dirname)
270
+ root = directory_tree_walker.run(visitor).root
271
+ output.puts root.to_yaml
272
+
258
273
  when 'json'
259
274
  visitor = DirectoryToHashVisitor.new(dirname)
260
275
  root = directory_tree_walker.run(visitor).root
@@ -265,51 +280,31 @@ module TreeRb
265
280
  end
266
281
 
267
282
  when 'd3js'
268
- visitor = DirectoryToHash2Visitor.new(dirname)
269
- root = directory_tree_walker.run(visitor).root
270
- begin
271
- str_json = JSON.pretty_generate(root)
272
- str_json = "var data = " + str_json
273
- output.puts str_json
274
- rescue JSON::NestingError => e
275
- $stderr.puts "#{File.basename(__FILE__)}:#{__LINE__} #{e.to_s}"
276
- end
283
+ require 'tree_rb/output_html/d3js_helper'
284
+ D3jsHelper.new.run(directory_tree_walker, dirname, nil, output)
277
285
 
278
286
  when 'html_partition'
279
- D3jsHelper.new.run(directory_tree_walker, dirname,"d3js_layout_partition.erb", output)
287
+ require 'tree_rb/output_html/d3js_helper'
288
+ D3jsHelper.new.run(directory_tree_walker, dirname, "d3js_layout_partition.erb", output)
280
289
 
281
290
  when 'html_tree'
282
- D3jsHelper.new.run(directory_tree_walker, dirname,"d3js_layout_tree.erb", output)
291
+ require 'tree_rb/output_html/d3js_helper'
292
+ D3jsHelper.new.run(directory_tree_walker, dirname, "d3js_layout_tree.erb", output)
283
293
 
284
294
  when 'html_treemap'
285
- D3jsHelper.new.run(directory_tree_walker, dirname,"d3js_layout_treemap.erb", output)
286
-
287
- when 'yaml'
288
- visitor = DirectoryToHashVisitor.new(dirname)
289
- root = directory_tree_walker.run(visitor).root
290
- output.puts root.to_yaml
295
+ require 'tree_rb/output_html/d3js_helper'
296
+ D3jsHelper.new.run(directory_tree_walker, dirname, "d3js_layout_treemap.erb", output)
291
297
 
292
298
  when 'sqlite'
293
- begin
294
- require 'sqlite3'
295
- unless options[:output]
296
- $stderr.puts "need to specify the -o options"
297
- else
298
- output.close
299
- filename = options[:output]
300
- visitor = SqliteDirTreeVisitor.new(filename)
301
- #start = Time.now
302
- #me = self
303
- #bytes = 0
304
- directory_tree_walker.run(visitor)
305
- end
306
-
307
- rescue LoadError
308
- puts 'You must gem install sqlite3 to use this output format'
309
- end
299
+ require 'tree_rb/output_sqlite/sqlite_helper'
300
+ SqliteHelper.new.run(directory_tree_walker, output, options)
301
+
302
+ when 'dircat'
303
+ require 'tree_rb/output_dircat/dircat_helper'
304
+ DirCatHelper.new.run(directory_tree_walker, options)
310
305
 
311
306
  else
312
- puts "unknown algo #{options[:algo]} specified"
307
+ puts "unknown format #{options[:algo]} specified"
313
308
  end
314
309
 
315
310
  0
@@ -120,7 +120,6 @@ module TreeRb
120
120
  def depth
121
121
  return @depth unless @depth.nil?
122
122
  @depth = @parent.nil? ? 1 : @parent.depth + 1
123
- @depth
124
123
  end
125
124
 
126
125
  #
File without changes
@@ -266,6 +266,7 @@ module TreeRb
266
266
  visitor
267
267
  end
268
268
 
269
+ ###################################################################################################################
269
270
  #
270
271
  # Format the content of tree
271
272
  #
@@ -0,0 +1,7 @@
1
+ # -*- coding: utf-8 -*-
2
+ module TreeRb
3
+
4
+ class TreeRbException < StandardError
5
+ end
6
+
7
+ end # end module
@@ -42,7 +42,7 @@ module TreeRb
42
42
  if dirname
43
43
  @dirname = dirname
44
44
  unless File.directory?(dirname)
45
- raise "#{dirname} is not a directory!"
45
+ raise ArgumentError.new "#{dirname} is not a directory!"
46
46
  end
47
47
  end
48
48
 
@@ -217,7 +217,7 @@ module TreeRb
217
217
  # check dirname
218
218
  #
219
219
  if @dirname.nil? and dirname.nil?
220
- raise "missing starting directory"
220
+ raise 'missing starting directory'
221
221
  end
222
222
  @dirname = dirname if dirname
223
223
 
@@ -225,7 +225,7 @@ module TreeRb
225
225
  # check visitor
226
226
  #
227
227
  if tree_node_visitor and block
228
- raise "cannot use block and parameter together"
228
+ raise 'cannot use block and parameter together'
229
229
  end
230
230
 
231
231
  if tree_node_visitor
@@ -0,0 +1,36 @@
1
+ # -*- coding: utf-8 -*-
2
+ module TreeRb
3
+
4
+ class DomWalker
5
+ def initialize(node)
6
+ @node = node
7
+ end
8
+
9
+ def run(visitor)
10
+ @visitor = visitor
11
+ process_node(@node)
12
+ end
13
+
14
+
15
+ #
16
+ # recurse on nodes
17
+ #
18
+ def process_node(node, level=1)
19
+ entries = node.children
20
+ @visitor.enter_node(node)
21
+ entries.each do |entry|
22
+ unless is_leaf?(entry)
23
+ process_node(entry, level+1)
24
+ else
25
+ @visitor.visit_leaf(entry)
26
+ end
27
+ end
28
+ @visitor.exit_node(node)
29
+ end
30
+
31
+ def is_leaf?(node)
32
+ node.node_type == Nokogiri::XML::Node::TEXT_NODE
33
+ end
34
+
35
+ end # class
36
+ end # module
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'tree_rb/output_dircat/entry'
3
+ require 'tree_rb/output_dircat/dircat_visitor'
4
+
5
+ module TreeRb
6
+
7
+ class DirCatHelper
8
+
9
+ def run(directory_tree_walker, options)
10
+ unless options[:output]
11
+ $stderr.puts "need to specify the -o options"
12
+ else
13
+ filename = options[:output]
14
+ visitor = DirCatVisitor.new(filename)
15
+ #start = Time.now
16
+ #me = self
17
+ #bytes = 0
18
+ directory_tree_walker.run(visitor)
19
+ visitor.save_to
20
+ end
21
+ end
22
+
23
+ end
24
+ end # module
@@ -0,0 +1,78 @@
1
+ # -*- coding: utf-8 -*-
2
+ module TreeRb
3
+
4
+ class DirCatVisitor < BasicTreeNodeVisitor
5
+
6
+ CR = "\r"
7
+ CLEAR = "\e[K"
8
+
9
+ def initialize(out_filename, options = { })
10
+ @start = Time.now
11
+ @bytes = 0
12
+ @entries = Array.new
13
+ @md5_to_entries = Hash.new
14
+ @out_filename = out_filename
15
+ @verbose_level = 1
16
+ @show_progress = true
17
+ end
18
+
19
+ def visit_leaf(filename)
20
+ entry = DirCat::Entry.from_filename(filename)
21
+ add_entry(entry)
22
+ @bytes += entry.size
23
+ if @verbose_level > 0
24
+ print "#{CR}#{filename}#{CLEAR}"
25
+ end
26
+ if @show_progress
27
+ sec = Time.now - @start
28
+ print "#{CR}bytes: #{@bytes.to_human} time: #{sec} bytes/sec #{@bytes/sec} #{CLEAR}"
29
+ end
30
+ end
31
+
32
+ #
33
+ # add entry to this catalog
34
+ # @private
35
+ def add_entry(e)
36
+ @entries.push(e)
37
+ if @md5_to_entries.has_key?(e.md5)
38
+ @md5_to_entries[e.md5].push(e)
39
+ else
40
+ @md5_to_entries[e.md5] = [e]
41
+ end
42
+ end
43
+
44
+ # serialize catalog
45
+ # @return [DirCatSer] serialized catalog
46
+ def to_ser
47
+ dircat_ser = DirCat::DirCatSer.new
48
+ dircat_ser.dircat_version = DirCat::FORMAT_VERSION
49
+ dircat_ser.dirname = @dirname
50
+ dircat_ser.ctime = @ctime
51
+ dircat_ser.entries = []
52
+ @entries.each do |entry|
53
+ dircat_ser.entries << entry.to_ser
54
+ end
55
+ dircat_ser
56
+ end
57
+
58
+
59
+ #
60
+ # Save serialized catalog to file
61
+ # @param [String,File] file
62
+ def save_to
63
+ if @out_filename.kind_of?(String)
64
+ begin
65
+ File.open(@out_filename, "w") do |f|
66
+ f.puts to_ser.to_yaml
67
+ end
68
+ rescue Errno::ENOENT
69
+ raise DirCatException.new, "DirCat: cannot write into '#{file}'", caller
70
+ end
71
+ else
72
+ @out_filename.puts to_ser.to_yaml
73
+ end
74
+ end
75
+
76
+
77
+ end
78
+ end