tree.rb 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,99 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
5
+ </head>
6
+ <body>
7
+
8
+
9
+ <div id="viz"></div>
10
+
11
+ <div id="#chart"></div>
12
+
13
+ <script type="text/javascript">
14
+
15
+ var sampleSVG = d3.select("#viz")
16
+ .append("svg")
17
+ .attr("width", 100)
18
+ .attr("height", 100);
19
+
20
+ sampleSVG.append("circle")
21
+ .style("stroke", "gray")
22
+ .style("fill", "white")
23
+ .attr("r", 40)
24
+ .attr("cx", 50)
25
+ .attr("cy", 50)
26
+ .on("mouseover", function () {d3.select(this).style("fill", "aliceblue");})
27
+ .on("mouseout", function () {d3.select(this).style("fill", "white");});
28
+
29
+ </script>
30
+
31
+
32
+
33
+ <script type="text/javascript" src="flare.js">
34
+
35
+ </script>
36
+
37
+
38
+ <script type="text/javascript">
39
+
40
+
41
+ var width = 960,
42
+ height = 500,
43
+ color = d3.scale.category20c();
44
+
45
+ var treemap = d3.layout.treemap()
46
+ .size([width, height])
47
+ .sticky(true)
48
+ .value(function (d) { return d.size; });
49
+
50
+ var div = d3.select("#chart").append("div")
51
+ .style("position", "relative")
52
+ .style("width", width + "px")
53
+ .style("height", height + "px");
54
+
55
+
56
+ d3.json("flare.json", function (json) {
57
+ div.data([json]).selectAll("div")
58
+ .data(treemap.nodes)
59
+ .enter().append("div")
60
+ .attr("class", "cell")
61
+ .style("background", function (d) { return d.children ? color(d.name) : null; })
62
+ .call(cell)
63
+ .text(function (d) { return d.children ? null : d.name; });
64
+
65
+ d3.select("#size").on("click", function () {
66
+ div.selectAll("div")
67
+ .data(treemap.value(function (d) { return d.size; }))
68
+ .transition()
69
+ .duration(1500)
70
+ .call(cell);
71
+
72
+ d3.select("#size").classed("active", true);
73
+ d3.select("#count").classed("active", false);
74
+ });
75
+
76
+ d3.select("#count").on("click", function () {
77
+ div.selectAll("div")
78
+ .data(treemap.value(function (d) { return 1; }))
79
+ .transition()
80
+ .duration(1500)
81
+ .call(cell);
82
+
83
+ d3.select("#size").classed("active", false);
84
+ d3.select("#count").classed("active", true);
85
+ });
86
+ });
87
+
88
+ function cell() {
89
+ this
90
+ .style("left", function (d) { return d.x + "px"; })
91
+ .style("top", function (d) { return d.y + "px"; })
92
+ .style("width", function (d) { return Math.max(0, d.dx - 1) + "px"; })
93
+ .style("height", function (d) { return Math.max(0, d.dy - 1) + "px"; });
94
+ }
95
+
96
+
97
+ </script>
98
+ </body>
99
+ </html>
@@ -0,0 +1,320 @@
1
+ # -*- coding: utf-8 -*-
2
+ module TreeRb
3
+
4
+ #
5
+ #
6
+ #
7
+ class CliJson
8
+
9
+ SUCCESS=0
10
+ FAILURE=1
11
+
12
+ def self.run
13
+ self.new.parse_args(ARGV)
14
+ end
15
+
16
+ def options_parser(options)
17
+ parser = OptionParser.new
18
+ parser.banner = "Usage: rjson.rb [options] [directory]"
19
+ parser.separator "pretty format json file"
20
+ parser.separator "Code https://github.com/tokiro/treevisitor. Feedback to tokiro.oyama@gmail.com"
21
+
22
+ #parser.separator ""
23
+ #parser.separator "Generic options: "
24
+ #
25
+ #parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
26
+ # options[:verbose] = v
27
+ #end
28
+ #
29
+ #parser.on("-q", "--quiet", "quiet mode as --no-verbose") do
30
+ # options[:verbose] = false
31
+ #end
32
+ #
33
+ parser.on("-o [FILE]", "--output [FILE]", String) do |v|
34
+ if options[:output]
35
+ puts "only one file of output can be used"
36
+ options[:exit] = true
37
+ end
38
+ options[:output] = v
39
+ end
40
+
41
+ options[:force_overwrite_output] = false
42
+ parser.on("--force", "overwrite output") do
43
+ options[:force_overwrite_output] = true
44
+ end
45
+ #
46
+ #parser.separator ""
47
+ #parser.separator "Filter options: "
48
+ #
49
+ #parser.on("-a", "All file are listed i.e. include dot files") do
50
+ # options[:all_files] = true
51
+ #end
52
+ #
53
+ #parser.on("-d", "List directories only") do
54
+ # options[:only_directories] = true
55
+ #end
56
+ #
57
+ #options[:only_files] = false
58
+ #parser.on("--only-files", "show only file implies -i") do
59
+ # options[:only_files] = true
60
+ # options[:show_indentation] = false
61
+ #end
62
+ #
63
+ #algos = %w[build-dir print-dir json json2 yaml sqlite]
64
+ #algo_aliases = { "b" => "build-dir", "v" => "print-dir", "j" => "json", "y" => "yaml", "s" => "sqlite" }
65
+ #
66
+ #algo_list = (algo_aliases.keys + algos).join(',')
67
+ #parser.on("--format ALGO", algos, algo_aliases, "select an algo", " (#{algo_list})") do |algo|
68
+ # options[:algo] = algo
69
+ #end
70
+ #
71
+ #parser.separator ""
72
+ #parser.separator "print options:"
73
+ #
74
+ ##
75
+ ## begin colorize
76
+ ##
77
+ ## copied from tree man page
78
+ #parser.on("-n", "Turn colorization off always, over-ridden by the -C option.") do
79
+ # options[:colorize_force] = false
80
+ #end
81
+ #
82
+ ## copied from tree man page
83
+ #parser.on("-C",
84
+ # "Turn colorization on always, using built-in color",
85
+ # " defaults if the LS_COLORS environment variable is not set.",
86
+ # " Useful to colorize output to a pipe.") do
87
+ # options[:colorize_force] = true
88
+ #end
89
+ #
90
+ ##
91
+ ## end colorize
92
+ ##
93
+ #
94
+ #options[:max_level] = nil
95
+ #parser.on("-L [LEVEL]", Integer, "Max display depth of the directory tree.") do |l|
96
+ # options[:max_level] = l
97
+ #end
98
+ #
99
+ #options[:show_full_path] = false
100
+ #parser.on("-f", "Prints the full path prefix for each file.") do
101
+ # options[:show_full_path] = true
102
+ #end
103
+ #
104
+ #parser.on("-s", "Print the size of each file in bytes along with the name.") do
105
+ # options[:show_size] = true
106
+ #end
107
+ #
108
+ #parser.on("-h",
109
+ # "Print the size of each file but in a more human readable way,",
110
+ # " e.g. appending a size letter for kilobytes (K), megabytes (M),",
111
+ # " gigabytes (G), terrabytes (T), petabytes (P) and exabytes (E).") do
112
+ # options[:show_size_human] = true
113
+ #end
114
+ #
115
+ #options[:show_indentation] = true
116
+ #parser.on("-i",
117
+ # "Makes tree not print the indentation lines, ",
118
+ # " useful when used in conjunction with the -f option.") do
119
+ # options[:show_indentation] = false
120
+ #end
121
+ #
122
+ #options[:show_md5] = false
123
+ #parser.on("--md5", "show md5 of file") do
124
+ # options[:show_md5] = true
125
+ #end
126
+ #
127
+ #options[:show_sha1] = false
128
+ #parser.on("--sha1", "show sha1 of a file") do
129
+ # options[:show_sha1] = true
130
+ #end
131
+ #
132
+ #options[:show_report] = true
133
+ #parser.on("--noreport", "Omits printing of the file and directory report at the end of the tree listing.") do
134
+ # options[:show_report] = false
135
+ #end
136
+ #
137
+ #parser.separator ""
138
+ #parser.separator "shortcut options:"
139
+ #
140
+ #options[:show_md5sum] = false
141
+ #parser.on("--md5sum", "show ake md5sum implies -i and --only-files") do
142
+ # options[:only_files] = true
143
+ # options[:show_md5sum] = true
144
+ # options[:show_indentation] = false
145
+ # options[:show_report] = false
146
+ #end
147
+ #
148
+ #options[:show_sha1sum] = false
149
+ #parser.on("--sha1sum", "show ake sha1sum output implies -i and --only-files") do
150
+ # options[:only_files] = true
151
+ # options[:show_sha1sum] = true
152
+ # options[:show_indentation] = false
153
+ # options[:show_report] = false
154
+ #end
155
+ #
156
+ #parser.separator ""
157
+ #parser.separator "Other options:"
158
+ #
159
+ parser.on_tail("--help", "Show this message") do
160
+ puts parser
161
+ options[:exit] = SUCCESS
162
+ end
163
+
164
+ parser.on_tail("--version", "Show the version") do
165
+ puts "tree.rb version #{TreeRb::VERSION}"
166
+ options[:exit] = SUCCESS
167
+ end
168
+
169
+ parser
170
+ end
171
+
172
+ def parse_args(argv)
173
+ options = { :verbose => true, :force => false, :algo => 'build-dir' }
174
+ parser = options_parser(options)
175
+
176
+ begin
177
+ rest = parser.parse(argv)
178
+ rescue OptionParser::InvalidOption => e
179
+ $stderr.puts e.to_s
180
+ $stderr.puts "try --help for help"
181
+ return FAILURE
182
+ rescue OptionParser::MissingArgument => e
183
+ $stderr.puts e.to_s
184
+ $stderr.puts "try --help for help"
185
+ return FAILURE
186
+ end
187
+
188
+ unless options[:exit].nil?
189
+ return options[:exit]
190
+ end
191
+
192
+ ##
193
+ ## option: output, force
194
+ ##
195
+ output = $stdout
196
+ if options[:output]
197
+ filename = options[:output]
198
+ if File.exist?(filename) and not options[:force_overwrite_output]
199
+ $stderr.puts "catalog '#{filename}' exists use --force to overwrite"
200
+ return FAILURE
201
+ end
202
+ output = File.open(filename, "w")
203
+ $stderr.puts "Writing file '#{filename}'"
204
+ end
205
+ #
206
+ #if options[:colorize_force]
207
+ # options[:colorize] = options[:colorize_force]
208
+ #else
209
+ # options[:colorize] = output.isatty
210
+ #end
211
+ #
212
+ ## TODO: capture CTRL^C to avoid show the stack trace
213
+ ## http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-trap
214
+ #Signal.trap('INT') { puts "intercepted ctr+c"; exit }
215
+ #
216
+
217
+ if rest.length < 1
218
+ $stderr.puts "missing arg"
219
+ return FAILURE
220
+ else
221
+ filename = rest[0]
222
+ end
223
+
224
+ #
225
+ ##
226
+ ## 1. build dir tree walker
227
+ ##
228
+ #dirname = File.expand_path(dirname)
229
+ #dtw = DirTreeWalker.new(dirname, options)
230
+ #unless options[:all_files]
231
+ # dtw.ignore(/^\.[^.]+/) # ignore all file starting with "."
232
+ #end
233
+ #dtw.visit_file = !options[:only_directories]
234
+ #
235
+ #case options[:algo]
236
+ #
237
+ # when 'build-dir'
238
+ #
239
+ # visitor = BuildDirTreeVisitor.new(options)
240
+ # dtw.run(visitor)
241
+ #
242
+ # output.puts visitor.root.to_str('', options)
243
+ #
244
+ # if options[:show_report]
245
+ # output.puts
246
+ # output.puts "#{visitor.nr_directories} directories, #{visitor.nr_files} files"
247
+ # end
248
+ #
249
+ # when 'print-dir'
250
+ # visitor = PrintDirTreeVisitor.new
251
+ # dtw.run(visitor)
252
+ #
253
+ # when 'json'
254
+ # visitor = DirectoryToHashVisitor.new(dirname)
255
+ # root = dtw.run(visitor).root
256
+ # output.puts JSON.pretty_generate(root)
257
+ #
258
+ # when 'json2'
259
+ # visitor = DirectoryToHash2Visitor.new(dirname)
260
+ # root = dtw.run(visitor).root
261
+ # output.puts JSON.pretty_generate(root)
262
+ #
263
+ # when 'yaml'
264
+ # visitor = DirectoryToHashVisitor.new(dirname)
265
+ # root = dtw.run(visitor).root
266
+ # output.puts root.to_yaml
267
+ #
268
+ # when 'sqlite'
269
+ # begin
270
+ # require 'sqlite3'
271
+ # unless options[:output]
272
+ # $stderr.puts "need to specify the -o options"
273
+ # else
274
+ # output.close
275
+ # filename = options[:output]
276
+ # visitor = SqliteDirTreeVisitor.new(filename)
277
+ # #start = Time.now
278
+ # #me = self
279
+ # #bytes = 0
280
+ # dtw.run(visitor)
281
+ # end
282
+ #
283
+ # rescue LoadError
284
+ # puts 'You must gem install sqlite3 to use this output format'
285
+ # end
286
+ #
287
+ # else
288
+ # puts "unknown algo #{options[:algo]} specified"
289
+ #end
290
+
291
+
292
+ show_input = false
293
+
294
+ unless File.exists?(filename)
295
+ $stderr.puts "input file not exists '#{filename}'"
296
+ exit
297
+ end
298
+
299
+ my_json_str = File.read(filename)
300
+
301
+ if show_input
302
+ $stderr.puts "*************************"
303
+ $stderr.puts my_json_str
304
+ $stderr.puts "*************************"
305
+ end
306
+
307
+ begin
308
+ my_json = JSON(my_json_str)
309
+ rescue JSON::ParserError
310
+ $stderr.puts "json is malformed"
311
+ return FAILURE
312
+ end
313
+
314
+ output.puts JSON.pretty_generate(my_json)
315
+
316
+ return SUCCESS
317
+ end
318
+
319
+ end # end class
320
+ end # end module
@@ -1,5 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module TreeRb
3
+
3
4
  #
4
5
  #
5
6
  #
@@ -9,9 +10,7 @@ module TreeRb
9
10
  self.new.parse_args(ARGV)
10
11
  end
11
12
 
12
-
13
13
  def options_parser(options)
14
-
15
14
  parser = OptionParser.new
16
15
  parser.banner = "Usage: tree.rb [options] [directory]"
17
16
  parser.separator "list contents of directories in a tree-like format"
@@ -19,18 +18,32 @@ module TreeRb
19
18
  parser.separator "Code https://github.com/tokiro/treevisitor. Feedback to tokiro.oyama@gmail.com"
20
19
 
21
20
  parser.separator ""
22
- parser.separator "options: "
21
+ parser.separator "Generic options: "
23
22
 
24
- parser.on("--help", "Show this message") do
25
- puts parser
26
- options[:exit] = 1
23
+ parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
24
+ options[:verbose] = v
27
25
  end
28
26
 
29
- parser.on("--version", "Show the version") do
30
- puts "tree.rb version #{TreeRb::VERSION}"
31
- options[:exit] = 1
27
+ parser.on("-q", "--quiet", "quiet mode as --no-verbose") do
28
+ options[:verbose] = false
29
+ end
30
+
31
+ parser.on("-o [FILE]", "--output [FILE]", String) do |v|
32
+ if options[:output]
33
+ puts "only one file of output can be used"
34
+ options[:exit] = true
35
+ end
36
+ options[:output] = v
32
37
  end
33
38
 
39
+ options[:force_overwrite_output] = false
40
+ parser.on("--force", "overwrite output") do
41
+ options[:force_overwrite_output] = true
42
+ end
43
+
44
+ parser.separator ""
45
+ parser.separator "Filter options: "
46
+
34
47
  parser.on("-a", "All file are listed i.e. include dot files") do
35
48
  options[:all_files] = true
36
49
  end
@@ -45,15 +58,7 @@ module TreeRb
45
58
  options[:show_indentation] = false
46
59
  end
47
60
 
48
- parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
49
- options[:verbose] = v
50
- end
51
-
52
- parser.on("-q", "--quiet", "quiet mode as --no-verbose") do
53
- options[:verbose] = false
54
- end
55
-
56
- algos = %w[build-dir print-dir json yaml sqlite]
61
+ algos = %w[build-dir print-dir json json2 yaml sqlite]
57
62
  algo_aliases = { "b" => "build-dir", "v" => "print-dir", "j" => "json", "y" => "yaml", "s" => "sqlite" }
58
63
 
59
64
  algo_list = (algo_aliases.keys + algos).join(',')
@@ -61,10 +66,8 @@ module TreeRb
61
66
  options[:algo] = algo
62
67
  end
63
68
 
64
- options[:show_full_path] = false
65
- parser.on("-f", "Prints the full path prefix for each file.") do
66
- options[:show_full_path] = true
67
- end
69
+ parser.separator ""
70
+ parser.separator "print options:"
68
71
 
69
72
  #
70
73
  # begin colorize
@@ -75,7 +78,10 @@ module TreeRb
75
78
  end
76
79
 
77
80
  # copied from tree man page
78
- parser.on("-C", "Turn colorization on always, using built-in color defaults if the LS_COLORS environment variable is not set. Useful to colorize output to a pipe.") do
81
+ parser.on("-C",
82
+ "Turn colorization on always, using built-in color",
83
+ " defaults if the LS_COLORS environment variable is not set.",
84
+ " Useful to colorize output to a pipe.") do
79
85
  options[:colorize_force] = true
80
86
  end
81
87
 
@@ -83,21 +89,31 @@ module TreeRb
83
89
  # end colorize
84
90
  #
85
91
 
92
+ options[:max_level] = nil
93
+ parser.on("-L [LEVEL]", Integer, "Max display depth of the directory tree.") do |l|
94
+ options[:max_level] = l
95
+ end
96
+
97
+ options[:show_full_path] = false
98
+ parser.on("-f", "Prints the full path prefix for each file.") do
99
+ options[:show_full_path] = true
100
+ end
101
+
86
102
  parser.on("-s", "Print the size of each file in bytes along with the name.") do
87
103
  options[:show_size] = true
88
104
  end
89
105
 
90
- msg =<<-EOS
91
- Print the size of each file but in a more human readable way, e.g. appending a size letter for kilobytes (K),
92
- megabytes (M), gigabytes (G), terrabytes (T), petabytes (P) and exabytes (E).
93
- EOS
94
-
95
- parser.on("-h", msg) do
106
+ parser.on("-h",
107
+ "Print the size of each file but in a more human readable way,",
108
+ " e.g. appending a size letter for kilobytes (K), megabytes (M),",
109
+ " gigabytes (G), terrabytes (T), petabytes (P) and exabytes (E).") do
96
110
  options[:show_size_human] = true
97
111
  end
98
112
 
99
113
  options[:show_indentation] = true
100
- parser.on("-i", "Makes tree not print the indentation lines, useful when used in conjunction with the -f option.") do
114
+ parser.on("-i",
115
+ "Makes tree not print the indentation lines, ",
116
+ " useful when used in conjunction with the -f option.") do
101
117
  options[:show_indentation] = false
102
118
  end
103
119
 
@@ -111,6 +127,14 @@ module TreeRb
111
127
  options[:show_sha1] = true
112
128
  end
113
129
 
130
+ options[:show_report] = true
131
+ parser.on("--noreport", "Omits printing of the file and directory report at the end of the tree listing.") do
132
+ options[:show_report] = false
133
+ end
134
+
135
+ parser.separator ""
136
+ parser.separator "shortcut options:"
137
+
114
138
  options[:show_md5sum] = false
115
139
  parser.on("--md5sum", "show ake md5sum implies -i and --only-files") do
116
140
  options[:only_files] = true
@@ -127,29 +151,23 @@ module TreeRb
127
151
  options[:show_report] = false
128
152
  end
129
153
 
130
- parser.on("-o [FILE]", "--output [FILE]", String) do |v|
131
- if options[:output]
132
- puts "only one file of output can be used"
133
- options[:exit] = true
134
- end
135
- options[:output] = v
136
- end
154
+ parser.separator ""
155
+ parser.separator "Other options:"
137
156
 
138
- options[:force_overwrite_output] = false
139
- parser.on("--force", "overwrite output") do
140
- options[:force_overwrite_output] = true
157
+ parser.on_tail("--help", "Show this message") do
158
+ puts parser
159
+ options[:exit] = 1
141
160
  end
142
161
 
143
- options[:show_report] = true
144
- parser.on("--noreport", "Omits printing of the file and directory report at the end of the tree listing.") do
145
- options[:show_report] = false
162
+ parser.on_tail("--version", "Show the version") do
163
+ puts "tree.rb version #{TreeRb::VERSION}"
164
+ options[:exit] = 1
146
165
  end
147
166
 
148
167
  parser
149
168
  end
150
169
 
151
170
  def parse_args(argv)
152
-
153
171
  options = { :verbose => true, :force => false, :algo => 'build-dir' }
154
172
  parser = options_parser(options)
155
173
 
@@ -189,6 +207,8 @@ module TreeRb
189
207
  options[:colorize] = output.isatty
190
208
  end
191
209
 
210
+ # TODO: capture CTRL^C to avoid show the stack trace
211
+ # http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-trap
192
212
  Signal.trap('INT') { puts "intercepted ctr+c"; exit }
193
213
 
194
214
  if rest.length < 1
@@ -198,10 +218,10 @@ module TreeRb
198
218
  end
199
219
 
200
220
  #
201
- # 1. build dirtreewalker
221
+ # 1. build dir tree walker
202
222
  #
203
223
  dirname = File.expand_path(dirname)
204
- dtw = DirTreeWalker.new(dirname)
224
+ dtw = DirTreeWalker.new(dirname, options)
205
225
  unless options[:all_files]
206
226
  dtw.ignore(/^\.[^.]+/) # ignore all file starting with "."
207
227
  end
@@ -210,8 +230,6 @@ module TreeRb
210
230
  case options[:algo]
211
231
 
212
232
  when 'build-dir'
213
- # TODO: capture CTRL^C to avoid show the stack trace
214
- # http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-trap
215
233
 
216
234
  visitor = BuildDirTreeVisitor.new(options)
217
235
  dtw.run(visitor)
@@ -232,6 +250,11 @@ module TreeRb
232
250
  root = dtw.run(visitor).root
233
251
  output.puts JSON.pretty_generate(root)
234
252
 
253
+ when 'json2'
254
+ visitor = DirectoryToHash2Visitor.new(dirname)
255
+ root = dtw.run(visitor).root
256
+ output.puts JSON.pretty_generate(root)
257
+
235
258
  when 'yaml'
236
259
  visitor = DirectoryToHashVisitor.new(dirname)
237
260
  root = dtw.run(visitor).root
@@ -245,7 +268,7 @@ module TreeRb
245
268
  else
246
269
  output.close
247
270
  filename = options[:output]
248
- visitor = SqliteDirTreeVisitor.new(filename)
271
+ visitor = SqliteDirTreeVisitor.new(filename)
249
272
  #start = Time.now
250
273
  #me = self
251
274
  #bytes = 0