treevisitor 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/README.md +6 -1
  2. data/Rakefile +6 -11
  3. data/lib/tree_visitor.rb +1 -1
  4. data/lib/treevisitor.rb +1 -38
  5. data/lib/treevisitor_cli.rb +2 -4
  6. data/treevisitor.gemspec +16 -28
  7. metadata +11 -143
  8. data/.gemtest +0 -0
  9. data/bin/tree.rb +0 -9
  10. data/examples/directory_walker/directory_without_subdirectory.rb +0 -37
  11. data/examples/directory_walker/find_files.rb +0 -18
  12. data/examples/directory_walker/print_files.rb +0 -12
  13. data/examples/protovis/directory_to_json_visitor.rb +0 -15
  14. data/examples/protovis/index.html +0 -87
  15. data/examples/protovis/protovis-r3.2.js +0 -277
  16. data/examples/protovis/treevisitor.js +0 -33
  17. data/examples/protovis/treevisitor.png +0 -0
  18. data/lib/treevisitor/abs_node.rb +0 -144
  19. data/lib/treevisitor/basic_tree_node_visitor.rb +0 -44
  20. data/lib/treevisitor/cli/cli_tree.rb +0 -106
  21. data/lib/treevisitor/directory_walker.rb +0 -300
  22. data/lib/treevisitor/leaf_node.rb +0 -33
  23. data/lib/treevisitor/tree_node.rb +0 -251
  24. data/lib/treevisitor/tree_node_visitor.rb +0 -119
  25. data/lib/treevisitor/util/dir_processor.rb +0 -46
  26. data/lib/treevisitor/version.rb +0 -4
  27. data/lib/treevisitor/visitors/block_tree_node_visitor.rb +0 -21
  28. data/lib/treevisitor/visitors/build_dir_tree_visitor.rb +0 -57
  29. data/lib/treevisitor/visitors/callback_tree_node_visitor2.rb +0 -48
  30. data/lib/treevisitor/visitors/clone_tree_node_visitor.rb +0 -39
  31. data/lib/treevisitor/visitors/depth_tree_node_visitor.rb +0 -27
  32. data/lib/treevisitor/visitors/directory_to_hash_visitor.rb +0 -33
  33. data/lib/treevisitor/visitors/flat_print_tree_node_visitors.rb +0 -20
  34. data/lib/treevisitor/visitors/print_dir_tree_visitor.rb +0 -21
  35. data/lib/treevisitor/visitors/print_tree_node_visitor.rb +0 -40
  36. data/spec/fixtures/test_dir_1/.dir_with_dot/dummy.txt +0 -0
  37. data/spec/fixtures/test_dir_1/dir.1/dir.1.2/file.1.2.1 +0 -0
  38. data/spec/fixtures/test_dir_1/dir.1/file.1.1 +0 -0
  39. data/spec/fixtures/test_dir_1/dir.2/file.2.1 +0 -0
  40. data/spec/fixtures/test_dir_2/[Dsube]/sub/.gitkeep +0 -0
  41. data/spec/spec_helper.rb +0 -28
  42. data/spec/treevisitor/cli/cli_tree_spec.rb +0 -69
  43. data/spec/treevisitor/directory_walker_spec.rb +0 -99
  44. data/spec/treevisitor/tree_dsl_spec.rb +0 -57
  45. data/spec/treevisitor/tree_dsl_with_derived_class1_spec.rb +0 -53
  46. data/spec/treevisitor/tree_dsl_with_derived_class_spec.rb +0 -51
  47. data/spec/treevisitor/tree_node_paths_spec.rb +0 -70
  48. data/spec/treevisitor/tree_node_spec.rb +0 -135
  49. data/spec/treevisitor/tree_node_visitor_delegate_spec.rb +0 -35
  50. data/spec/treevisitor/tree_node_visitor_dsl_spec.rb +0 -66
  51. data/spec/treevisitor/util/dir_processor_spec.rb +0 -13
  52. data/spec/treevisitor/visitors/block_tree_node_visitor_spec.rb +0 -25
  53. data/spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb +0 -38
  54. data/spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb +0 -28
  55. data/spec/treevisitor/visitors/tree_node_visitors_spec.rb +0 -27
  56. data/tasks/rspec.rake +0 -34
  57. data/tasks/yard.rake +0 -36
@@ -1,4 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- VERSION="0.2.3"
4
- end
@@ -1,21 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # It calls a block when visit a tree_node or leaf_node
5
- #
6
- class BlockTreeNodeVisitor < BasicTreeNodeVisitor
7
-
8
- def initialize( &action )
9
- @block = action
10
- end
11
-
12
- def enter_node( tree_node )
13
- @block.call( tree_node )
14
- end
15
-
16
- def visit_leaf( leaf_node )
17
- @block.call( leaf_node )
18
- end
19
-
20
- end
21
- end
@@ -1,57 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # Builds a TreeNode from a filesystem directory
5
- # It similar to CloneTreeNodeVisitor
6
- #
7
- class BuildDirTreeVisitor # < BasicTreeNodeVisitor
8
-
9
- attr_reader :root
10
-
11
- #
12
- # Number of visited directory (aka nr_nodes - nr_leaf)
13
- #
14
- attr_reader :nr_directories
15
-
16
-
17
- #
18
- # Number of visited directory (nr_leaves)
19
- # @see AbsNode#nr_leaves
20
- #
21
- attr_reader :nr_files
22
-
23
- def initialize
24
- super
25
- @root = nil
26
- @stack = []
27
- @nr_directories = 0
28
- @nr_files = 0
29
- end
30
-
31
- def enter_node( pathname )
32
- if @stack.empty?
33
- tree_node = TreeNode.new( File.basename( pathname ) )
34
- @root = tree_node
35
- else
36
- tree_node = TreeNode.new( File.basename( pathname ), @stack.last )
37
- end
38
- @nr_directories += 1
39
- @stack.push( tree_node )
40
- end
41
-
42
- def cannot_enter_node(pathname, error)
43
-
44
- end
45
-
46
- def exit_node( pathname )
47
- @stack.pop
48
- end
49
-
50
- def visit_leaf( pathname )
51
- @nr_files += 1
52
- # connect the leaf_node created to the last tree_node on the stack
53
- LeafNode.new( File.basename(pathname), @stack.last )
54
- end
55
-
56
- end
57
- end # end module TreeVisitor
@@ -1,48 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # Executes a block when enter in a node
5
- # The block are defined from on_enter_X methods
6
- # The blocks take as argument the node and the parent_node
7
- #
8
- class CallbackTreeNodeVisitor2
9
-
10
- attr_reader :root
11
-
12
- def initialize(delegate = nil)
13
- super()
14
- @stack = []
15
- @root = nil
16
- @delegate = delegate
17
- end
18
-
19
- def on_enter_node(&block)
20
- raise "already defined a delegate" if @delegate
21
- @action_enter_tree_node = block
22
- end
23
-
24
- def on_visit_leaf(&block)
25
- raise "already defined a delegate" if @delegate
26
- @action_visit_leaf_node = block
27
- end
28
-
29
- def enter_node(src_tree_node)
30
- dst_parent_node = @stack.empty? ? nil : @stack.last
31
- dst_tree_node = @action_enter_tree_node.call(src_tree_node, dst_parent_node) if @action_enter_tree_node
32
- dst_tree_node = @delegate.enter_node(src_tree_node, dst_parent_node) if @delegate and @delegate.respond_to? :enter_node
33
- @root = dst_tree_node if @stack.empty?
34
- @stack.push(dst_tree_node)
35
- end
36
-
37
- def exit_node(src_tree_node)
38
- @stack.pop
39
- end
40
-
41
- def visit_leaf(src_leaf_node)
42
- parent_node = @stack.last
43
- @action_visit_leaf_node.call(src_leaf_node, parent_node) if @action_visit_leaf_node
44
- @delegate.visit_leaf(src_leaf_node, parent_node) if @delegate and @delegate.respond_to? :visit_leaf
45
- end
46
- end
47
-
48
- end
@@ -1,39 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # Clone a tree_node, nodes are duplicated.
5
- # Node content are not duplicated!
6
- #
7
- class CloneTreeNodeVisitor # < BasicTreeNodeVisitor
8
-
9
- #
10
- # Contains the cloned tree node after the visit
11
- #
12
- attr_reader :cloned_root
13
-
14
- def initialize
15
- super
16
- @cloned_root = nil
17
- @stack = []
18
- end
19
-
20
- def enter_node( tree_node )
21
- if @stack.empty?
22
- cloned_tree_node = TreeNode.new( tree_node.content )
23
- @cloned_root = cloned_tree_node
24
- else
25
- cloned_tree_node = TreeNode.new( tree_node.content, @stack.last )
26
- end
27
- @stack.push( cloned_tree_node )
28
- end
29
-
30
- def exit_node( tree_node )
31
- @stack.pop
32
- end
33
-
34
- def visit_leaf( leaf_node )
35
- LeafNode.new( leaf_node.content, @stack.last )
36
- end
37
-
38
- end
39
- end
@@ -1,27 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # Simple visitor: show how calculate the depth of a tree
5
- #
6
- class DepthTreeNodeVisitor # < BasicTreeNodeVisitor
7
-
8
- attr_reader :depth
9
-
10
- def initialize
11
- super
12
- @depth = 0
13
- end
14
-
15
- def enter_node( tree_node )
16
- @depth += 1
17
- end
18
-
19
- def exit_node( tree_node )
20
- @depth -= 1
21
- end
22
-
23
- def visit_leaf( leaf_node )
24
- end
25
-
26
- end
27
- end
@@ -1,33 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module TreeVisitor
4
- #
5
- # Build hash with directory structure
6
- #
7
- class DirectoryToHashVisitor # < TreeVisitor::BasicTreeNodeVisitor
8
-
9
- attr_reader :root
10
-
11
- def initialize(pathname)
12
- @stack = []
13
- @node = {}
14
- @root = @node
15
- end
16
-
17
- def enter_node(pathname)
18
- subnode = {}
19
- @node[File.basename(pathname)] = subnode
20
- @stack.push(@node)
21
- @node = subnode
22
- end
23
-
24
- def exit_node(pathname)
25
- @node = @stack.pop
26
- end
27
-
28
- def visit_leaf(pathname)
29
- @node[File.basename(pathname)] = File.stat(pathname).size
30
- end
31
-
32
- end
33
- end
@@ -1,20 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # Print for every node the name
5
- #
6
- class FlatPrintTreeNodeVisitor # < BasicTreeNodeVisitor
7
-
8
- def enter_node( tree_node )
9
- puts tree_node.name
10
- end
11
-
12
- def exit_node( tree_node )
13
- end
14
-
15
- def visit_leaf( leaf_node )
16
- puts leaf_node.name
17
- end
18
-
19
- end
20
- end
@@ -1,21 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # Visitor for DirTreeWalker
5
- # Prints the node at enter
6
- # TODO: join this con PrintTreeNodeVisitor
7
- class PrintDirTreeVisitor # < BasicTreeNodeVisitor
8
-
9
- def enter_node( pathname )
10
- puts pathname
11
- end
12
-
13
- def exit_node( pathname )
14
- end
15
-
16
- def visit_leaf( pathname )
17
- puts pathname
18
- end
19
-
20
- end
21
- end
@@ -1,40 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module TreeVisitor
3
- #
4
- # Prints TreeNode names indenting according to depth
5
- #
6
- class PrintTreeNodeVisitor # < BasicTreeNodeVisitor
7
-
8
- def initialize
9
- @depth = 0
10
- end
11
-
12
- def enter_node( tree_node )
13
- str = ""
14
- (0...@depth).step {
15
- str << " |-"
16
- }
17
-
18
- if @depth == 0
19
- puts str + tree_node.name.to_s
20
- else
21
- puts str + tree_node.name.to_s
22
- end
23
- @depth += 1
24
- end
25
-
26
- def exit_node( tree_node )
27
- @depth -= 1
28
- end
29
-
30
- def visit_leaf( leaf_node )
31
- str = ""
32
- (0...@depth-1).step {
33
- str << " |-"
34
- }
35
- str << " | "
36
- puts str + leaf_node.name.to_s
37
- end
38
-
39
- end
40
- end
File without changes
File without changes
File without changes
File without changes
File without changes
data/spec/spec_helper.rb DELETED
@@ -1,28 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # std lib
4
- #
5
- require "stringio"
6
-
7
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
- require 'treevisitor'
9
- require 'treevisitor_cli'
10
- include TreeVisitor
11
-
12
- FIXTURES = File.expand_path( File.join( File.dirname(__FILE__), "fixtures" ) )
13
-
14
- def with_output_captured
15
- old_stdout = $stdout
16
- old_stderr = $stderr
17
- out = StringIO.new
18
- err = StringIO.new
19
- $stdout = out
20
- $stderr = err
21
- begin
22
- yield
23
- ensure
24
- $stdout = old_stdout
25
- $stderr = old_stderr
26
- end
27
- { :stdout => out.string, :stderr => err.string }
28
- end
@@ -1,69 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
-
4
- describe CliTree do
5
-
6
- it "help message" do
7
- out = with_output_captured do
8
- args = %w{-h}
9
- CliTree.new.parse_args(args)
10
- end
11
- out[:stdout].should match /Usage:/
12
- end
13
-
14
- it "version" do
15
- out = with_output_captured do
16
- args = %w{--version}
17
- CliTree.new.parse_args(args)
18
- end
19
- version = TreeVisitor::VERSION
20
- out[:stdout].should match version
21
- end
22
-
23
- it "should accepts switch -d (directories only)" do
24
- out = with_output_captured do
25
- args = %w{-d}
26
- args << File.join(FIXTURES, "test_dir_1")
27
- CliTree.new.parse_args(args)
28
- end
29
- # puts out
30
- out[:stdout].split("\n").length.should == 6
31
-
32
- out = with_output_captured do
33
- args = %w{-da}
34
- args << File.join(FIXTURES, "test_dir_1")
35
- CliTree.new.parse_args(args)
36
- end
37
- # puts out
38
- out[:stdout].split("\n").length.should == 7
39
- end
40
-
41
- it "should accepts switch -a (all files)" do
42
- out = with_output_captured do
43
- args = %w{-a}
44
- args << File.join(FIXTURES, "test_dir_1")
45
- CliTree.new.parse_args(args)
46
- end
47
- # pp out
48
- out[:stdout].split("\n").length.should == 11
49
-
50
- out = with_output_captured do
51
- args = []
52
- args << File.join(FIXTURES, "test_dir_1")
53
- CliTree.new.parse_args(args)
54
- end
55
- # puts out
56
- out[:stdout].split("\n").length.should == 9
57
- end
58
-
59
- it "should show tree with inaccessible directories" do
60
- out = with_output_captured do
61
- args = []
62
- args << File.join(FIXTURES, "test_dir_3_with_error")
63
- CliTree.new.parse_args(args)
64
- end
65
- puts out
66
- out[:stderr].should_not be_empty
67
- out[:stdout].split("\n").length.should == 4
68
- end
69
- end
@@ -1,99 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), "..", "spec_helper")
3
-
4
- describe DirTreeWalker do
5
-
6
- it "should accept option :ignore" do
7
- walker = DirTreeWalker.new :ignore => /^\./
8
- walker.ignore_file?(".thumbnails").should be_true
9
- walker.ignore_dir?(".thumbnails").should be_true
10
- end
11
-
12
- it "should accept option :ignore" do
13
- walker = DirTreeWalker.new :ignore => ".git"
14
- walker.ignore_file?(".git").should be_true
15
- walker.ignore_dir?(".git").should be_true
16
- end
17
-
18
-
19
- it "should accept option :ignore_dir" do
20
- dtw = DirTreeWalker.new :ignore_dir => [/^\./, "private_dir" ]
21
- dtw.should be_ignore_dir ".git"
22
- dtw.should be_ignore_dir "private_dir"
23
- end
24
-
25
- it "should accept option :ignore_file" do
26
- dtw = DirTreeWalker.new :ignore_file => [/.xml/, /(ignore)|(orig)/ ]
27
- dtw.should be_ignore_file "pippo.xml"
28
- end
29
-
30
- it "should accept option :match" do
31
- dtw = DirTreeWalker.new :match => /.jpg/
32
- dtw.should be_match "foo.jpg"
33
- end
34
-
35
- it "should ignore files and directory" do
36
- walker = DirTreeWalker.new(".")
37
-
38
- walker.ignore(/^\./)
39
- walker.ignore_file?(".thumbnails").should be_true
40
- walker.ignore_dir?(".thumbnails").should be_true
41
-
42
- walker.ignore_dir("thumbnails")
43
- walker.ignore_dir?(".thumbnails").should be_true
44
- walker.ignore_dir?("thumbnails").should be_true
45
- walker.ignore_dir?("pippo").should be_false
46
-
47
- walker.ignore_file("xvpics")
48
- walker.ignore_file?("xvpics").should be_true
49
-
50
- walker.ignore("sub")
51
- walker.ignore_file?("[Dsube]").should be_false
52
- walker.ignore_dir?("[Dsube]").should be_false
53
- end
54
-
55
- it "should accumulate file names" do
56
- dir_tree_walker = DirTreeWalker.new(File.join(FIXTURES, "test_dir_1"))
57
-
58
- accumulator = []
59
- visitor = BlockTreeNodeVisitor.new { |pathname| accumulator << File.basename(pathname) }
60
- dir_tree_walker.run(visitor)
61
- accumulator.length.should == 9
62
- accumulator.sort.should == %w{ test_dir_1 dir.1 dir.1.2 file.1.2.1 file.1.1 dir.2 file.2.1 .dir_with_dot dummy.txt }.sort
63
- end
64
-
65
- it "should accumulate file names 2" do
66
- dir_tree_walker = DirTreeWalker.new(File.join(FIXTURES, "test_dir_2"))
67
- dir_tree_walker.ignore("sub")
68
-
69
- accumulator = []
70
- visitor = BlockTreeNodeVisitor.new { |pathname| accumulator << File.basename(pathname) }
71
- dir_tree_walker.run(visitor)
72
- accumulator.length.should == 2
73
- accumulator.sort.should == %w{ [Dsube] test_dir_2 }.sort
74
- end
75
-
76
- it "should ignore not accessible directory" do
77
-
78
- dir = File.join(FIXTURES, "test_dir_3_with_error")
79
-
80
- f1 = File.join(dir, "no_accessible_dir")
81
- Dir.rmdir(f1) if File.exist?(f1)
82
- Dir.mkdir(f1, 0000)
83
-
84
- f2 = File.join(dir, "accessible_dir")
85
- Dir.rmdir(f2) if File.exist?(f2)
86
- Dir.mkdir(f2)
87
-
88
- dir_tree_walker = DirTreeWalker.new(File.join(FIXTURES, "test_dir_3_with_error"))
89
- accumulator = []
90
- visitor = BlockTreeNodeVisitor.new { |pathname| accumulator << File.basename(pathname) }
91
- dir_tree_walker.run(visitor)
92
- accumulator.length.should == 2
93
- accumulator.sort.should == %w{accessible_dir test_dir_3_with_error }.sort
94
-
95
- # Dir.rmdir(f1)
96
- # Dir.rmdir(f2)
97
- end
98
-
99
- end