treevisitor 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +104 -0
- data/VERSION.yml +1 -1
- data/examples/{directory_without_subdirectory.rb → directory_walker/directory_without_subdirectory.rb} +6 -4
- data/examples/{find_files.rb → directory_walker/find_files.rb} +4 -3
- data/examples/{print_files.rb → directory_walker/print_files.rb} +5 -8
- data/examples/protovis/directory_to_json_visitor.rb +13 -0
- data/examples/protovis/index.html +87 -0
- data/examples/protovis/protovis-d3.2.js +15269 -0
- data/examples/protovis/treevisitor.js +33 -0
- data/examples/protovis/treevisitor.png +0 -0
- data/lib/treevisitor/basic_tree_node_visitor.rb +29 -0
- data/lib/treevisitor/cli/cli_tree.rb +43 -35
- data/lib/treevisitor/directory_walker.rb +260 -0
- data/lib/treevisitor/tree_node_visitor.rb +33 -5
- data/lib/treevisitor/{dir_processor.rb → util/dir_processor.rb} +0 -0
- data/lib/treevisitor/visitors/block_tree_node_visitor.rb +1 -1
- data/lib/treevisitor/visitors/build_dir_tree_visitor.rb +1 -1
- data/lib/treevisitor/visitors/callback_tree_node_visitor.rb +1 -2
- data/lib/treevisitor/visitors/callback_tree_node_visitor2.rb +1 -1
- data/lib/treevisitor/visitors/clone_tree_node_visitor.rb +1 -1
- data/lib/treevisitor/visitors/depth_tree_node_visitor.rb +1 -1
- data/lib/treevisitor/visitors/directory_to_hash_visitor.rb +33 -0
- data/lib/treevisitor/visitors/flat_print_tree_node_visitors.rb +1 -1
- data/lib/treevisitor/visitors/print_dir_tree_visitor.rb +1 -1
- data/lib/treevisitor/visitors/print_tree_node_visitor.rb +1 -1
- data/lib/treevisitor.rb +24 -8
- data/spec/fixtures/{test_dir → test_dir_1}/.dir_with_dot/dummy.txt +0 -0
- data/spec/fixtures/{test_dir → test_dir_1}/dir.1/dir.1.2/file.1.2.1 +0 -0
- data/spec/fixtures/{test_dir → test_dir_1}/dir.1/file.1.1 +0 -0
- data/spec/fixtures/{test_dir → test_dir_1}/dir.2/file.2.1 +0 -0
- data/spec/spec_helper.rb +6 -9
- data/spec/treevisitor/cli/cli_tree_spec.rb +5 -5
- data/spec/treevisitor/directory_walker_spec.rb +61 -0
- data/spec/treevisitor/tree_dsl_spec.rb +57 -0
- data/spec/treevisitor/tree_dsl_with_derived_class1_spec.rb +53 -0
- data/spec/treevisitor/tree_dsl_with_derived_class_spec.rb +51 -0
- data/spec/treevisitor/{dir_processor_spec.rb → util/dir_processor_spec.rb} +2 -4
- data/spec/treevisitor/visitor_dsl_spec.rb +32 -0
- data/spec/treevisitor/visitors/block_tree_node_visitor_spec.rb +27 -0
- data/spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb +38 -0
- data/spec/treevisitor/visitors/callback_tree_node_visitors_spec.rb +29 -0
- data/spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb +30 -0
- data/tasks/jeweler.rake +4 -8
- data/treevisitor.gemspec +114 -0
- metadata +48 -29
- data/README.rdoc +0 -42
- data/lib/treevisitor/dir_tree_walker.rb +0 -165
- data/spec/treevisitor/dir_tree_walker_spec.rb +0 -50
- data/spec/treevisitor/tree_node_dsl_spec.rb +0 -153
- data/spec/treevisitor/tree_node_visitor_spec.rb +0 -70
- data/tasks/rubyforge.rake +0 -28
@@ -0,0 +1,33 @@
|
|
1
|
+
var treevisitor = {
|
2
|
+
"lib": {
|
3
|
+
"tree_visitor.rb": 45,
|
4
|
+
"treevisitor": {
|
5
|
+
"abs_node.rb": 2858,
|
6
|
+
"basic_tree_node_visitor.rb": 578,
|
7
|
+
"cli": {
|
8
|
+
"cli_tree.rb": 2760
|
9
|
+
},
|
10
|
+
"directory_walker.rb": 5049,
|
11
|
+
"leaf_node.rb": 526,
|
12
|
+
"tree_node.rb": 5665,
|
13
|
+
"tree_node_visitor.rb": 1263,
|
14
|
+
"util": {
|
15
|
+
"dir_processor.rb": 928
|
16
|
+
},
|
17
|
+
"visitors": {
|
18
|
+
"block_tree_node_visitor.rb": 386,
|
19
|
+
"build_dir_tree_visitor.rb": 1167,
|
20
|
+
"callback_tree_node_visitor.rb": 1029,
|
21
|
+
"callback_tree_node_visitor2.rb": 1652,
|
22
|
+
"clone_tree_node_visitor.rb": 837,
|
23
|
+
"depth_tree_node_visitor.rb": 427,
|
24
|
+
"directory_to_hash_visitor.rb": 639,
|
25
|
+
"flat_print_tree_node_visitors.rb": 341,
|
26
|
+
"print_dir_tree_visitor.rb": 387,
|
27
|
+
"print_tree_node_visitor.rb": 707
|
28
|
+
}
|
29
|
+
},
|
30
|
+
"treevisitor.rb": 976,
|
31
|
+
"treevisitor_cli.rb": 87
|
32
|
+
}
|
33
|
+
};
|
Binary file
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module TreeVisitor
|
3
|
+
#
|
4
|
+
# Callback methods used to visit a tree
|
5
|
+
# Are empty so it is possible to define only a subset when deriving subclass
|
6
|
+
#
|
7
|
+
class BasicTreeNodeVisitor
|
8
|
+
|
9
|
+
#
|
10
|
+
# called on tree node at start of the visit i.e. we start to visit the subtree
|
11
|
+
#
|
12
|
+
def enter_tree_node( tree_node )
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
# called on tree node at end of the visit i.e. oll subtree are visited
|
17
|
+
#
|
18
|
+
def exit_tree_node( tree_node )
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# called when visit leaf node
|
23
|
+
#
|
24
|
+
def visit_leaf_node( leaf_node )
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -6,23 +6,22 @@ module TreeVisitor
|
|
6
6
|
class CliTree
|
7
7
|
|
8
8
|
def self.run
|
9
|
-
self.new.parse_args(
|
9
|
+
self.new.parse_args(ARGV)
|
10
10
|
end
|
11
11
|
|
12
|
-
def parse_args(
|
12
|
+
def parse_args(argv)
|
13
13
|
|
14
|
-
options
|
14
|
+
options = {:verbose => true, :force => false, :algo => 'build-dir'}
|
15
15
|
|
16
|
-
opts
|
16
|
+
opts = OptionParser.new
|
17
17
|
opts.banner = "Usage: tree.rb [options] [directory]"
|
18
|
-
|
19
|
-
opts.separator ""
|
20
18
|
opts.separator "list contents of directories in a tree-like format"
|
21
|
-
opts.separator "this is a clone of tree unix command written in ruby"
|
22
|
-
|
19
|
+
opts.separator "this is a almost :-) a clone of tree unix command written in ruby"
|
20
|
+
opts.separator "Code https://github.com/tokiro/treevisitor. Feedback to tokiro.oyama@gmail.com"
|
21
|
+
|
23
22
|
opts.separator ""
|
24
23
|
opts.separator "options: "
|
25
|
-
|
24
|
+
|
26
25
|
opts.on("-h", "--help", "Show this message") do
|
27
26
|
puts opts
|
28
27
|
return 0
|
@@ -48,49 +47,58 @@ module TreeVisitor
|
|
48
47
|
opts.on("-q", "--quiet", "quiet mode as --no-verbose") do |v|
|
49
48
|
options[:verbose] = false
|
50
49
|
end
|
51
|
-
|
52
|
-
algos = %w[build visit]
|
53
|
-
algo_aliases = { "b" => "build", "v" => "visit" }
|
54
50
|
|
55
|
-
|
56
|
-
|
51
|
+
algos = %w[build-dir print-dir json yaml]
|
52
|
+
algo_aliases = {"b" => "build-dir", "v" => "print-dir", "j" => "json", "y" => "yaml"}
|
53
|
+
|
54
|
+
algo_list = (algo_aliases.keys + algos).join(',')
|
55
|
+
opts.on("-f", "--format ALGO", algos, algo_aliases, "select an algo", " (#{algo_list})") do |algo|
|
57
56
|
options[:algo] = algo
|
58
57
|
end
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
59
|
+
begin
|
60
|
+
rest = opts.parse(argv)
|
61
|
+
rescue OptionParser::InvalidOption => e
|
62
|
+
$stderr.puts e.to_s
|
63
|
+
$stderr.puts "try -h for help"
|
64
|
+
return false
|
65
|
+
end
|
64
66
|
|
65
67
|
if rest.length < 1
|
66
|
-
|
67
|
-
|
68
|
+
dirname = Dir.pwd
|
69
|
+
else
|
70
|
+
dirname = rest[0]
|
68
71
|
end
|
69
72
|
|
70
|
-
dirname =
|
71
|
-
dirname = File.expand_path( dirname )
|
73
|
+
dirname = File.expand_path(dirname)
|
72
74
|
|
73
|
-
|
74
|
-
|
75
|
-
dtw = DirTreeWalker.new( dirname )
|
75
|
+
dtw = DirTreeWalker.new(dirname)
|
76
76
|
unless options[:all_files]
|
77
|
-
# TODO: la regex e' corretta ed un file che inizia con ".."??
|
78
77
|
dtw.ignore(/^\.[^.]+/) # ignore all file starting with "."
|
79
78
|
end
|
80
79
|
|
81
80
|
dtw.visit_file = !options[:only_directories]
|
82
81
|
|
83
82
|
case options[:algo]
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
83
|
+
when 'build-dir'
|
84
|
+
visitor = BuildDirTreeVisitor.new
|
85
|
+
dtw.run(visitor)
|
86
|
+
puts visitor.root.to_str
|
87
|
+
puts
|
88
|
+
puts "#{visitor.nr_directories} directories, #{visitor.nr_files} files"
|
89
|
+
when 'print-dir'
|
90
|
+
visitor = PrintDirTreeVisitor.new
|
91
|
+
dtw.run(visitor)
|
92
|
+
when 'json'
|
93
|
+
root = dtw.run(DirectoryToHashVisitor.new(dirname)).root
|
94
|
+
puts JSON.pretty_generate(root)
|
95
|
+
when 'yaml'
|
96
|
+
root = dtw.run(DirectoryToHashVisitor.new(dirname)).root
|
97
|
+
puts root.to_yaml
|
98
|
+
else
|
99
|
+
puts "unknown algo #{options[:algo]} specified"
|
93
100
|
end
|
101
|
+
|
94
102
|
0
|
95
103
|
end
|
96
104
|
|
@@ -0,0 +1,260 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module TreeVisitor
|
3
|
+
|
4
|
+
#
|
5
|
+
# Visit a file system directory
|
6
|
+
#
|
7
|
+
class DirTreeWalker
|
8
|
+
|
9
|
+
# Build a tree walker.
|
10
|
+
# Visit a director starting from dirname
|
11
|
+
# if dirname is missing, must be supplied when invoking run
|
12
|
+
#
|
13
|
+
# @yield [a, b, c] TreeNodeVisitor
|
14
|
+
# @yieldparam [optional, types, ...] argname description
|
15
|
+
# @yieldreturn [optional, types, ...] description
|
16
|
+
#
|
17
|
+
# @overload initialize(dirname)
|
18
|
+
# @param [String] dirname the root of the tree (top level directory)
|
19
|
+
#
|
20
|
+
# @overload initialize(dirname,options)
|
21
|
+
# @param [Hash] options
|
22
|
+
# @option options [String,Regex, Array<String,Regexp>] :ignore list of ignore pattern
|
23
|
+
#
|
24
|
+
#
|
25
|
+
# @example Print the contents of tmp directory
|
26
|
+
# DirTreeWalker.new("/tmp") do
|
27
|
+
# on_visit_leaf_node { |pathname| puts pathname }
|
28
|
+
# end.run
|
29
|
+
#
|
30
|
+
def initialize(dirname = nil, options = nil)
|
31
|
+
#
|
32
|
+
# arg detection
|
33
|
+
#
|
34
|
+
if dirname and dirname.respond_to?(:key?)
|
35
|
+
options = dirname
|
36
|
+
dirname = nil
|
37
|
+
end
|
38
|
+
|
39
|
+
if dirname
|
40
|
+
@dirname = dirname
|
41
|
+
unless File.directory?(dirname)
|
42
|
+
raise "#{dirname} is not a directory!"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
@visitor = nil
|
47
|
+
|
48
|
+
#
|
49
|
+
# pattern
|
50
|
+
#
|
51
|
+
@ignore_dir_patterns = []
|
52
|
+
@ignore_file_patterns = []
|
53
|
+
@match_file_patterns = []
|
54
|
+
|
55
|
+
if options and options[:ignore]
|
56
|
+
unless options[:ignore].respond_to?(:at)
|
57
|
+
options[:ignore] = [options[:ignore]]
|
58
|
+
end
|
59
|
+
options[:ignore].each { |p| ignore(p) }
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
# options
|
64
|
+
#
|
65
|
+
@visit_file = true
|
66
|
+
end
|
67
|
+
|
68
|
+
##########################################################################
|
69
|
+
# Pattern
|
70
|
+
|
71
|
+
#
|
72
|
+
# Ignore a node (leaf/Tree) matching pattern
|
73
|
+
# @param [RegEx] pattern
|
74
|
+
#
|
75
|
+
def ignore(pattern)
|
76
|
+
@ignore_dir_patterns << pattern
|
77
|
+
@ignore_file_patterns << pattern
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
#
|
82
|
+
# Ignore a directory (Tree) matching pattern
|
83
|
+
# @param [RegEx] pattern
|
84
|
+
#
|
85
|
+
def ignore_dir(pattern)
|
86
|
+
@ignore_dir_patterns << pattern
|
87
|
+
self
|
88
|
+
end
|
89
|
+
|
90
|
+
#
|
91
|
+
# Ignore a file (Leaf) matching pattern
|
92
|
+
# @param [RegEx] pattern
|
93
|
+
#
|
94
|
+
def ignore_file(pattern)
|
95
|
+
@ignore_file_patterns << pattern
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Just the opposite of ignore
|
101
|
+
# directory/file matching pattern will be visited
|
102
|
+
#
|
103
|
+
# @param [RegEx] pattern
|
104
|
+
#
|
105
|
+
def match(pattern)
|
106
|
+
@match_file_patterns << pattern
|
107
|
+
self
|
108
|
+
end
|
109
|
+
|
110
|
+
##########################################################################
|
111
|
+
# Options
|
112
|
+
|
113
|
+
#
|
114
|
+
# it is true to visit file
|
115
|
+
#
|
116
|
+
attr_accessor :visit_file
|
117
|
+
|
118
|
+
##########################################################################
|
119
|
+
|
120
|
+
#
|
121
|
+
# Test directory ignore pattern
|
122
|
+
#
|
123
|
+
# @param [String] directory name
|
124
|
+
# @return [boolean] if dirname match almost one pattern
|
125
|
+
#
|
126
|
+
def ignore_dir?(dirname)
|
127
|
+
_include?(@ignore_dir_patterns, File.basename(dirname))
|
128
|
+
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# Test file ignore pattern
|
132
|
+
#
|
133
|
+
# @param [String] file name
|
134
|
+
# @return [boolean] if filename match almost one pattern
|
135
|
+
#
|
136
|
+
def ignore_file?(filename)
|
137
|
+
_include?(@ignore_file_patterns, File.basename(filename))
|
138
|
+
end
|
139
|
+
|
140
|
+
#
|
141
|
+
# Test common ignore pattern
|
142
|
+
#
|
143
|
+
# @param [String] file name
|
144
|
+
# @return [boolean] if filename match almost one pattern
|
145
|
+
#
|
146
|
+
def match?(filename)
|
147
|
+
return true if @match_file_patterns.empty?
|
148
|
+
_include?(@match_file_patterns, File.basename(filename))
|
149
|
+
end
|
150
|
+
|
151
|
+
#
|
152
|
+
# Run the visitor through the directory tree
|
153
|
+
#
|
154
|
+
# @overload run
|
155
|
+
#
|
156
|
+
# @overload run(dirname)
|
157
|
+
# @param [String] dirname
|
158
|
+
# @yield define TreeNodeVisitor
|
159
|
+
#
|
160
|
+
# @overload run(tree_node_visitor)
|
161
|
+
# @param [TreeNodeVisitor]
|
162
|
+
# @yield define TreeNodeVisitor
|
163
|
+
#
|
164
|
+
# @overload run(dirname, tree_node_visitor)
|
165
|
+
# @param [String] dirname
|
166
|
+
# @param [TreeNodeVisitor]
|
167
|
+
# @yield define TreeNodeVisitor
|
168
|
+
#
|
169
|
+
# @return [TreeNodeVisitor] the visitor
|
170
|
+
#
|
171
|
+
# @example Print the contents of tmp directory
|
172
|
+
# w = DirTreeWalker.new
|
173
|
+
# w.run("/tmp") do
|
174
|
+
# on_visit_leaf_node { |pathname| puts pathname }
|
175
|
+
# end.run
|
176
|
+
#
|
177
|
+
def run(dirname = nil, tree_node_visitor = nil, &block)
|
178
|
+
|
179
|
+
#
|
180
|
+
# args detection
|
181
|
+
#
|
182
|
+
if dirname and dirname.respond_to?(:enter_tree_node)
|
183
|
+
tree_node_visitor = dirname
|
184
|
+
dirname = nil
|
185
|
+
end
|
186
|
+
|
187
|
+
#
|
188
|
+
# check dirname
|
189
|
+
#
|
190
|
+
if @dirname.nil? and dirname.nil?
|
191
|
+
raise "missing starting directory"
|
192
|
+
end
|
193
|
+
@dirname = dirname if dirname
|
194
|
+
|
195
|
+
#
|
196
|
+
# check visitor
|
197
|
+
#
|
198
|
+
if tree_node_visitor and block
|
199
|
+
raise "cannot use block and parameter together"
|
200
|
+
end
|
201
|
+
|
202
|
+
if tree_node_visitor
|
203
|
+
@visitor = tree_node_visitor
|
204
|
+
end
|
205
|
+
|
206
|
+
if block
|
207
|
+
@visitor = TreeNodeVisitor.new(&block)
|
208
|
+
end
|
209
|
+
|
210
|
+
unless @visitor
|
211
|
+
raise "missing visitor"
|
212
|
+
end
|
213
|
+
|
214
|
+
#
|
215
|
+
# finally starts to process
|
216
|
+
#
|
217
|
+
process_directory(File.expand_path(@dirname))
|
218
|
+
@visitor
|
219
|
+
end
|
220
|
+
|
221
|
+
private
|
222
|
+
|
223
|
+
def _include?(patterns, basename)
|
224
|
+
# return false if the patters.empty?
|
225
|
+
patterns.find { |pattern|
|
226
|
+
if pattern.kind_of? Regexp
|
227
|
+
pattern.match(basename)
|
228
|
+
else
|
229
|
+
basename == pattern
|
230
|
+
end
|
231
|
+
}
|
232
|
+
end
|
233
|
+
|
234
|
+
#
|
235
|
+
# recurse on other directories
|
236
|
+
#
|
237
|
+
def process_directory(dirname)
|
238
|
+
@visitor.enter_tree_node(dirname)
|
239
|
+
# return if ignore_dir?( dirname )
|
240
|
+
|
241
|
+
begin
|
242
|
+
Dir.entries(dirname).sort.each { |basename|
|
243
|
+
next if basename == "." or basename == ".." # ignore always "." and ".."
|
244
|
+
pathname = File.join(dirname, basename)
|
245
|
+
|
246
|
+
if File.directory?(pathname)
|
247
|
+
process_directory(pathname) unless ignore_dir?(basename)
|
248
|
+
else
|
249
|
+
if !!@visit_file && match?(basename) && !ignore_file?(basename)
|
250
|
+
@visitor.visit_leaf_node(pathname)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
}
|
254
|
+
rescue Errno::EACCES => e
|
255
|
+
$stderr.puts e
|
256
|
+
end
|
257
|
+
@visitor.exit_tree_node(dirname)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
@@ -1,27 +1,55 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
module TreeVisitor
|
3
3
|
#
|
4
|
-
#
|
5
|
-
# Are empty so it is possible to define only a subset
|
4
|
+
# More complex TreeNodeVisitor
|
6
5
|
#
|
7
6
|
class TreeNodeVisitor
|
8
7
|
|
8
|
+
def initialize(&block)
|
9
|
+
@on_enter_tree_node_blocks = []
|
10
|
+
@on_exit_tree_node_blocks = []
|
11
|
+
@on_visit_leaf_node_blocks = []
|
12
|
+
if block
|
13
|
+
instance_eval(&block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
9
17
|
#
|
10
18
|
# called on tree node at start of the visit i.e. we start to visit the subtree
|
11
19
|
#
|
12
|
-
def enter_tree_node(
|
20
|
+
def enter_tree_node(tree_node)
|
21
|
+
@on_enter_tree_node_blocks.each{ |b| b.call(tree_node) }
|
13
22
|
end
|
14
23
|
|
15
24
|
#
|
16
25
|
# called on tree node at end of the visit i.e. oll subtree are visited
|
17
26
|
#
|
18
|
-
def exit_tree_node(
|
27
|
+
def exit_tree_node(tree_node)
|
28
|
+
@on_exit_tree_node_blocks.each{ |b| b.call(tree_node) }
|
19
29
|
end
|
20
30
|
|
21
31
|
#
|
22
32
|
# called when visit leaf node
|
23
33
|
#
|
24
|
-
def visit_leaf_node(
|
34
|
+
def visit_leaf_node(leaf_node)
|
35
|
+
@on_visit_leaf_node_blocks.each{ |b| b.call(leaf_node) }
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def on_enter_tree_node(&block)
|
41
|
+
raise "block missing" unless block
|
42
|
+
@on_enter_tree_node_blocks << block
|
43
|
+
end
|
44
|
+
|
45
|
+
def on_exit_tree_node(&block)
|
46
|
+
raise "block missing" unless block
|
47
|
+
@on_exit_tree_node_blocks << block
|
48
|
+
end
|
49
|
+
|
50
|
+
def on_visit_leaf_node(&block)
|
51
|
+
raise "block missing" unless block
|
52
|
+
@on_visit_leaf_node_blocks << block
|
25
53
|
end
|
26
54
|
|
27
55
|
end
|
File without changes
|
@@ -5,10 +5,9 @@ module TreeVisitor
|
|
5
5
|
# The block are defined from on_enter_X methods
|
6
6
|
# The blocks take as argument only the node
|
7
7
|
#
|
8
|
-
class CallbackTreeNodeVisitor <
|
8
|
+
class CallbackTreeNodeVisitor < BasicTreeNodeVisitor
|
9
9
|
|
10
10
|
def initialize
|
11
|
-
super()
|
12
11
|
@root = nil
|
13
12
|
@stack = []
|
14
13
|
@action_enter_tree_node = nil
|
@@ -5,7 +5,7 @@ module TreeVisitor
|
|
5
5
|
# The block are defined from on_enter_X methods
|
6
6
|
# The blocks take as argument the node and the parent_node
|
7
7
|
#
|
8
|
-
class CallbackTreeNodeVisitor2 <
|
8
|
+
class CallbackTreeNodeVisitor2 < BasicTreeNodeVisitor
|
9
9
|
|
10
10
|
attr_reader :root
|
11
11
|
|
@@ -0,0 +1,33 @@
|
|
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_tree_node(pathname)
|
18
|
+
subnode = {}
|
19
|
+
@node[File.basename(pathname)] = subnode
|
20
|
+
@stack.push(@node)
|
21
|
+
@node = subnode
|
22
|
+
end
|
23
|
+
|
24
|
+
def exit_tree_node(pathname)
|
25
|
+
@node = @stack.pop
|
26
|
+
end
|
27
|
+
|
28
|
+
def visit_leaf_node(pathname)
|
29
|
+
@node[File.basename(pathname)] = File.stat(pathname).size
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -4,7 +4,7 @@ module TreeVisitor
|
|
4
4
|
# Visitor for DirTreeWalker
|
5
5
|
# Prints the node at enter
|
6
6
|
# TODO: join this con PrintTreeNodeVisitor
|
7
|
-
class PrintDirTreeVisitor <
|
7
|
+
class PrintDirTreeVisitor < BasicTreeNodeVisitor
|
8
8
|
|
9
9
|
def enter_tree_node( pathname )
|
10
10
|
puts pathname
|
data/lib/treevisitor.rb
CHANGED
@@ -1,10 +1,21 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
|
3
|
+
#
|
4
|
+
# std lib
|
5
|
+
#
|
5
6
|
require 'pathname'
|
6
7
|
require 'yaml'
|
7
8
|
|
9
|
+
#
|
10
|
+
# rubygems
|
11
|
+
#
|
12
|
+
require 'rubygems'
|
13
|
+
require 'json'
|
14
|
+
|
15
|
+
#
|
16
|
+
# treevisitor
|
17
|
+
#
|
18
|
+
|
8
19
|
module TreeVisitor
|
9
20
|
def self.version
|
10
21
|
cwd = Pathname(__FILE__).dirname.expand_path.to_s
|
@@ -18,13 +29,18 @@ end
|
|
18
29
|
|
19
30
|
require "treevisitor/abs_node"
|
20
31
|
require 'treevisitor/leaf_node'
|
21
|
-
require 'treevisitor/tree_node_visitor.rb'
|
22
|
-
|
23
32
|
require 'treevisitor/tree_node'
|
24
|
-
require 'treevisitor/
|
33
|
+
require 'treevisitor/basic_tree_node_visitor'
|
25
34
|
require 'treevisitor/tree_node_visitor'
|
26
|
-
require 'treevisitor/dir_processor'
|
27
35
|
|
28
|
-
require 'treevisitor/
|
36
|
+
require 'treevisitor/directory_walker'
|
37
|
+
|
38
|
+
#
|
39
|
+
# visitors
|
40
|
+
#
|
41
|
+
require 'treevisitor/visitors/block_tree_node_visitor'
|
29
42
|
require 'treevisitor/visitors/build_dir_tree_visitor'
|
30
43
|
require 'treevisitor/visitors/print_dir_tree_visitor'
|
44
|
+
require 'treevisitor/visitors/directory_to_hash_visitor'
|
45
|
+
|
46
|
+
require 'treevisitor/util/dir_processor'
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|