tb 0.2 → 0.3
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 +62 -50
- data/bin/tb +22 -18
- data/lib/tb.rb +35 -19
- data/lib/tb/basic.rb +85 -86
- data/lib/tb/catreader.rb +33 -116
- data/lib/tb/cmd_cat.rb +31 -27
- data/lib/tb/cmd_consecutive.rb +45 -35
- data/lib/tb/cmd_crop.rb +86 -52
- data/lib/tb/cmd_cross.rb +113 -71
- data/lib/tb/cmd_cut.rb +49 -44
- data/lib/tb/cmd_git_log.rb +193 -0
- data/lib/tb/cmd_grep.rb +43 -32
- data/lib/tb/cmd_group.rb +63 -39
- data/lib/tb/cmd_gsub.rb +53 -43
- data/lib/tb/cmd_help.rb +51 -24
- data/lib/tb/cmd_join.rb +32 -35
- data/lib/tb/cmd_ls.rb +233 -205
- data/lib/tb/cmd_mheader.rb +47 -37
- data/lib/tb/cmd_nest.rb +94 -0
- data/lib/tb/cmd_newfield.rb +29 -33
- data/lib/tb/cmd_rename.rb +40 -32
- data/lib/tb/cmd_shape.rb +31 -24
- data/lib/tb/cmd_sort.rb +46 -25
- data/lib/tb/cmd_svn_log.rb +47 -28
- data/lib/tb/cmd_tar_tvf.rb +447 -0
- data/lib/tb/cmd_to_csv.rb +60 -0
- data/lib/tb/cmd_to_json.rb +60 -0
- data/lib/tb/cmd_to_pnm.rb +48 -0
- data/lib/tb/cmd_to_pp.rb +71 -0
- data/lib/tb/cmd_to_tsv.rb +48 -0
- data/lib/tb/cmd_to_yaml.rb +52 -0
- data/lib/tb/cmd_unnest.rb +118 -0
- data/lib/tb/cmdmain.rb +24 -20
- data/lib/tb/cmdtop.rb +33 -25
- data/lib/tb/cmdutil.rb +26 -66
- data/lib/tb/csv.rb +46 -34
- data/lib/tb/enum.rb +294 -0
- data/lib/tb/enumerable.rb +198 -7
- data/lib/tb/enumerator.rb +73 -0
- data/lib/tb/fieldset.rb +27 -19
- data/lib/tb/fileenumerator.rb +365 -0
- data/lib/tb/json.rb +50 -0
- data/lib/tb/pager.rb +6 -6
- data/lib/tb/pairs.rb +227 -0
- data/lib/tb/pnm.rb +23 -22
- data/lib/tb/reader.rb +52 -49
- data/lib/tb/record.rb +48 -19
- data/lib/tb/revcmp.rb +38 -0
- data/lib/tb/ropen.rb +74 -57
- data/lib/tb/search.rb +25 -21
- data/lib/tb/tsv.rb +31 -34
- data/sample/excel2csv +24 -20
- data/sample/poi-xls2csv.rb +24 -20
- data/sample/poi-xls2csv.sh +22 -18
- data/sample/tbplot +185 -127
- data/test-all-cov.rb +3 -3
- data/test-all.rb +1 -1
- data/test/test_basic.rb +26 -10
- data/test/test_catreader.rb +7 -6
- data/test/test_cmd_cat.rb +32 -0
- data/test/test_cmd_consecutive.rb +10 -0
- data/test/test_cmd_crop.rb +4 -4
- data/test/test_cmd_cross.rb +16 -4
- data/test/test_cmd_git_log.rb +46 -0
- data/test/test_cmd_help.rb +17 -12
- data/test/test_cmd_join.rb +21 -1
- data/test/test_cmd_ls.rb +3 -4
- data/test/test_cmd_mheader.rb +17 -11
- data/test/test_cmd_nest.rb +49 -0
- data/test/test_cmd_sort.rb +15 -0
- data/test/test_cmd_tar_tvf.rb +281 -0
- data/test/{test_cmd_csv.rb → test_cmd_to_csv.rb} +35 -21
- data/test/{test_cmd_json.rb → test_cmd_to_json.rb} +31 -3
- data/test/{test_cmd_pnm.rb → test_cmd_to_pnm.rb} +2 -2
- data/test/{test_cmd_pp.rb → test_cmd_to_pp.rb} +4 -4
- data/test/{test_cmd_tsv.rb → test_cmd_to_tsv.rb} +4 -4
- data/test/{test_cmd_yaml.rb → test_cmd_to_yaml.rb} +3 -3
- data/test/test_cmd_unnest.rb +89 -0
- data/test/test_cmdtty.rb +19 -13
- data/test/test_enumerable.rb +83 -1
- data/test/test_fileenumerator.rb +265 -0
- data/test/test_json.rb +15 -0
- data/test/test_pager.rb +3 -4
- data/test/test_pairs.rb +122 -0
- data/test/test_pnm.rb +24 -24
- data/test/test_reader.rb +35 -13
- data/test/test_revcmp.rb +10 -0
- data/test/test_tbenum.rb +173 -0
- metadata +51 -23
- data/lib/tb/cmd_csv.rb +0 -42
- data/lib/tb/cmd_json.rb +0 -60
- data/lib/tb/cmd_pnm.rb +0 -43
- data/lib/tb/cmd_pp.rb +0 -70
- data/lib/tb/cmd_tsv.rb +0 -43
- data/lib/tb/cmd_yaml.rb +0 -47
@@ -0,0 +1,60 @@
|
|
1
|
+
# Copyright (C) 2011-2012 Tanaka Akira <akr@fsij.org>
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions
|
5
|
+
# are met:
|
6
|
+
#
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above
|
10
|
+
# copyright notice, this list of conditions and the following
|
11
|
+
# disclaimer in the documentation and/or other materials provided
|
12
|
+
# with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote
|
14
|
+
# products derived from this software without specific prior
|
15
|
+
# written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
18
|
+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
23
|
+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
26
|
+
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
27
|
+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
Tb::Cmd.subcommands << 'to-csv'
|
30
|
+
|
31
|
+
def (Tb::Cmd).op_to_csv
|
32
|
+
op = OptionParser.new
|
33
|
+
op.banner = "Usage: tb to-csv [OPTS] [TABLE ...]\n" +
|
34
|
+
"Convert a table to CSV (Comma Separated Value)."
|
35
|
+
define_common_option(op, "hNo", "--no-pager")
|
36
|
+
op
|
37
|
+
end
|
38
|
+
|
39
|
+
def (Tb::Cmd).main_to_csv(argv)
|
40
|
+
op_to_csv.parse!(argv)
|
41
|
+
exit_if_help('to-csv')
|
42
|
+
argv = ['-'] if argv.empty?
|
43
|
+
creader = Tb::CatReader.open(argv, Tb::Cmd.opt_N)
|
44
|
+
header = []
|
45
|
+
ter = Tb::Enumerator.new {|y|
|
46
|
+
creader.with_cumulative_header.each {|pairs, header1|
|
47
|
+
header = header1
|
48
|
+
y.yield pairs
|
49
|
+
}
|
50
|
+
}.to_fileenumerator
|
51
|
+
er = Tb::Enumerator.new {|y|
|
52
|
+
y.set_header header
|
53
|
+
ter.each {|pairs|
|
54
|
+
y.yield Tb::Pairs.new(header.map {|f| [f, pairs[f]] })
|
55
|
+
}
|
56
|
+
}
|
57
|
+
with_output {|out|
|
58
|
+
er.write_to_csv(out, !Tb::Cmd.opt_N)
|
59
|
+
}
|
60
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Copyright (C) 2011-2012 Tanaka Akira <akr@fsij.org>
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions
|
5
|
+
# are met:
|
6
|
+
#
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above
|
10
|
+
# copyright notice, this list of conditions and the following
|
11
|
+
# disclaimer in the documentation and/or other materials provided
|
12
|
+
# with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote
|
14
|
+
# products derived from this software without specific prior
|
15
|
+
# written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
18
|
+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
23
|
+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
26
|
+
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
27
|
+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
Tb::Cmd.subcommands << 'to-json'
|
30
|
+
|
31
|
+
def (Tb::Cmd).op_to_json
|
32
|
+
op = OptionParser.new
|
33
|
+
op.banner = "Usage: tb to-json [OPTS] [TABLE]\n" +
|
34
|
+
"Convert a table to JSON (JavaScript Object Notation)."
|
35
|
+
define_common_option(op, "hNo", "--no-pager")
|
36
|
+
op
|
37
|
+
end
|
38
|
+
|
39
|
+
def (Tb::Cmd).main_to_json(argv)
|
40
|
+
require 'json'
|
41
|
+
op_to_json.parse!(argv)
|
42
|
+
exit_if_help('to-json')
|
43
|
+
argv = ['-'] if argv.empty?
|
44
|
+
with_output {|out|
|
45
|
+
out.print "["
|
46
|
+
sep = nil
|
47
|
+
argv.each {|filename|
|
48
|
+
sep = ",\n\n" if sep
|
49
|
+
tablereader_open(filename) {|tblreader|
|
50
|
+
tblreader.each {|pairs|
|
51
|
+
out.print sep if sep
|
52
|
+
out.print JSON.pretty_generate(Hash[pairs.to_a])
|
53
|
+
sep = ",\n"
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
out.puts "]"
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Copyright (C) 2011-2012 Tanaka Akira <akr@fsij.org>
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions
|
5
|
+
# are met:
|
6
|
+
#
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above
|
10
|
+
# copyright notice, this list of conditions and the following
|
11
|
+
# disclaimer in the documentation and/or other materials provided
|
12
|
+
# with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote
|
14
|
+
# products derived from this software without specific prior
|
15
|
+
# written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
18
|
+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
23
|
+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
26
|
+
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
27
|
+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
Tb::Cmd.subcommands << 'to-pnm'
|
30
|
+
|
31
|
+
def (Tb::Cmd).op_to_pnm
|
32
|
+
op = OptionParser.new
|
33
|
+
op.banner = "Usage: tb to-pnm [OPTS] [TABLE]\n" +
|
34
|
+
"Convert a table to PNM (Portable Anymap: PPM, PGM, PBM)."
|
35
|
+
define_common_option(op, "hNo", "--no-pager")
|
36
|
+
op
|
37
|
+
end
|
38
|
+
|
39
|
+
def (Tb::Cmd).main_to_pnm(argv)
|
40
|
+
op_to_pnm.parse!(argv)
|
41
|
+
exit_if_help('to-pnm')
|
42
|
+
argv = ['-'] if argv.empty?
|
43
|
+
tbl = Tb::CatReader.open(argv, Tb::Cmd.opt_N).to_tb
|
44
|
+
with_output {|out|
|
45
|
+
tbl.generate_pnm(out)
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
data/lib/tb/cmd_to_pp.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Copyright (C) 2011-2012 Tanaka Akira <akr@fsij.org>
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions
|
5
|
+
# are met:
|
6
|
+
#
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above
|
10
|
+
# copyright notice, this list of conditions and the following
|
11
|
+
# disclaimer in the documentation and/or other materials provided
|
12
|
+
# with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote
|
14
|
+
# products derived from this software without specific prior
|
15
|
+
# written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
18
|
+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
23
|
+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
26
|
+
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
27
|
+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
Tb::Cmd.subcommands << 'to-pp'
|
30
|
+
|
31
|
+
def (Tb::Cmd).op_to_pp
|
32
|
+
op = OptionParser.new
|
33
|
+
op.banner = "Usage: tb to-pp [OPTS] [TABLE]\n" +
|
34
|
+
"Convert a table to pretty printed format."
|
35
|
+
define_common_option(op, "hNo", "--no-pager")
|
36
|
+
op
|
37
|
+
end
|
38
|
+
|
39
|
+
def (Tb::Cmd).main_to_pp(argv)
|
40
|
+
op_to_pp.parse!(argv)
|
41
|
+
exit_if_help('to-pp')
|
42
|
+
argv.unshift '-' if argv.empty?
|
43
|
+
with_output {|out|
|
44
|
+
argv.each {|filename|
|
45
|
+
tablereader_open(filename) {|tblreader|
|
46
|
+
tblreader.each {|pairs|
|
47
|
+
a = pairs.reject {|f, v| v.nil? }
|
48
|
+
q = PP.new(out, 79)
|
49
|
+
q.guard_inspect_key {
|
50
|
+
q.group(1, '{', '}') {
|
51
|
+
q.seplist(a, nil, :each) {|kv|
|
52
|
+
k, v = kv
|
53
|
+
q.group {
|
54
|
+
q.pp k
|
55
|
+
q.text '=>'
|
56
|
+
q.group(1) {
|
57
|
+
q.breakable ''
|
58
|
+
q.pp v
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
q.flush
|
65
|
+
out << "\n"
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Copyright (C) 2011-2012 Tanaka Akira <akr@fsij.org>
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions
|
5
|
+
# are met:
|
6
|
+
#
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above
|
10
|
+
# copyright notice, this list of conditions and the following
|
11
|
+
# disclaimer in the documentation and/or other materials provided
|
12
|
+
# with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote
|
14
|
+
# products derived from this software without specific prior
|
15
|
+
# written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
18
|
+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
23
|
+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
26
|
+
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
27
|
+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
Tb::Cmd.subcommands << 'to-tsv'
|
30
|
+
|
31
|
+
def (Tb::Cmd).op_to_tsv
|
32
|
+
op = OptionParser.new
|
33
|
+
op.banner = "Usage: tb to-tsv [OPTS] [TABLE]\n" +
|
34
|
+
"Convert a table to TSV (Tab Separated Value)."
|
35
|
+
define_common_option(op, "hNo", "--no-pager")
|
36
|
+
op
|
37
|
+
end
|
38
|
+
|
39
|
+
def (Tb::Cmd).main_to_tsv(argv)
|
40
|
+
op_to_tsv.parse!(argv)
|
41
|
+
exit_if_help('to-tsv')
|
42
|
+
argv = ['-'] if argv.empty?
|
43
|
+
tbl = Tb::CatReader.open(argv, Tb::Cmd.opt_N).to_tb
|
44
|
+
with_output {|out|
|
45
|
+
tbl_generate_tsv(tbl, out)
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Copyright (C) 2011-2012 Tanaka Akira <akr@fsij.org>
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions
|
5
|
+
# are met:
|
6
|
+
#
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above
|
10
|
+
# copyright notice, this list of conditions and the following
|
11
|
+
# disclaimer in the documentation and/or other materials provided
|
12
|
+
# with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote
|
14
|
+
# products derived from this software without specific prior
|
15
|
+
# written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
18
|
+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
23
|
+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
26
|
+
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
27
|
+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
Tb::Cmd.subcommands << 'to-yaml'
|
30
|
+
|
31
|
+
def (Tb::Cmd).op_to_yaml
|
32
|
+
op = OptionParser.new
|
33
|
+
op.banner = "Usage: tb to-yaml [OPTS] [TABLE]\n" +
|
34
|
+
"Convert a table to YAML (YAML Ain't a Markup Language)."
|
35
|
+
define_common_option(op, "hNo", "--no-pager")
|
36
|
+
op
|
37
|
+
end
|
38
|
+
|
39
|
+
def (Tb::Cmd).main_to_yaml(argv)
|
40
|
+
require 'yaml'
|
41
|
+
op_to_yaml.parse!(argv)
|
42
|
+
exit_if_help('to-yaml')
|
43
|
+
argv = ['-'] if argv.empty?
|
44
|
+
tbl = Tb::CatReader.open(argv, Tb::Cmd.opt_N).to_tb
|
45
|
+
ary = tbl.map {|rec| rec.to_h }
|
46
|
+
with_output {|out|
|
47
|
+
YAML.dump(ary, out)
|
48
|
+
out.puts
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# Copyright (C) 2011-2012 Tanaka Akira <akr@fsij.org>
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions
|
5
|
+
# are met:
|
6
|
+
#
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above
|
10
|
+
# copyright notice, this list of conditions and the following
|
11
|
+
# disclaimer in the documentation and/or other materials provided
|
12
|
+
# with the distribution.
|
13
|
+
# 3. The name of the author may not be used to endorse or promote
|
14
|
+
# products derived from this software without specific prior
|
15
|
+
# written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
18
|
+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
23
|
+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
26
|
+
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
27
|
+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
|
29
|
+
Tb::Cmd.subcommands << 'unnest'
|
30
|
+
|
31
|
+
Tb::Cmd.default_option[:opt_unnest_prefix] = ''
|
32
|
+
Tb::Cmd.default_option[:opt_unnest_outer] = nil
|
33
|
+
|
34
|
+
def (Tb::Cmd).op_unnest
|
35
|
+
op = OptionParser.new
|
36
|
+
op.banner = "Usage: tb unnest [OPTS] FIELD [TABLE ...]\n" +
|
37
|
+
"Unnest a field."
|
38
|
+
define_common_option(op, "hNo", "--no-pager")
|
39
|
+
op.def_option('--prefix PREFIX', 'field prefix') {|prefix| Tb::Cmd.opt_unnest_prefix = prefix }
|
40
|
+
op.def_option('--outer', 'retain rows for empty nested table') { Tb::Cmd.opt_unnest_outer = true }
|
41
|
+
op
|
42
|
+
end
|
43
|
+
|
44
|
+
def (Tb::Cmd).main_unnest(argv)
|
45
|
+
op_unnest.parse!(argv)
|
46
|
+
exit_if_help('unnest')
|
47
|
+
err('no field given.') if argv.empty?
|
48
|
+
target_field = argv.shift
|
49
|
+
argv = ['-'] if argv.empty?
|
50
|
+
creader = Tb::CatReader.open(argv, Tb::Cmd.opt_N)
|
51
|
+
er = Tb::Enumerator.new {|y|
|
52
|
+
nested_fields = {}
|
53
|
+
ter = Tb::Enumerator.new {|y2|
|
54
|
+
creader.with_header {|header0|
|
55
|
+
unless header0.include? target_field
|
56
|
+
err("field not found: #{target_field.inspect}")
|
57
|
+
end
|
58
|
+
y2.set_header header0
|
59
|
+
}.each {|pairs|
|
60
|
+
pairs2 = {}
|
61
|
+
pairs.each {|f, v|
|
62
|
+
if f != target_field
|
63
|
+
pairs2[f] = v
|
64
|
+
elsif v.nil?
|
65
|
+
pairs2[f] = v
|
66
|
+
else
|
67
|
+
nested_tbl = Tb.parse_csv(v)
|
68
|
+
nested_tbl.list_fields.each {|f2|
|
69
|
+
unless nested_fields.has_key? f2
|
70
|
+
nested_fields[f2] = nested_fields.size
|
71
|
+
end
|
72
|
+
}
|
73
|
+
pairs2[f] = nested_tbl
|
74
|
+
end
|
75
|
+
}
|
76
|
+
y2.yield pairs2
|
77
|
+
}
|
78
|
+
}.to_fileenumerator
|
79
|
+
nested_fields_ary = nested_fields.keys
|
80
|
+
if Tb::Cmd.opt_unnest_prefix
|
81
|
+
nested_fields_ary.map! {|f| Tb::Cmd.opt_unnest_prefix + f }
|
82
|
+
end
|
83
|
+
ter.with_header {|header0|
|
84
|
+
header2 = []
|
85
|
+
header0.each {|f|
|
86
|
+
if f != target_field
|
87
|
+
header2 << f
|
88
|
+
else
|
89
|
+
header2.concat nested_fields_ary
|
90
|
+
end
|
91
|
+
}
|
92
|
+
y.set_header header2
|
93
|
+
}.each {|pairs|
|
94
|
+
pairs2 = pairs.reject {|f, v| f == target_field }
|
95
|
+
ntbl = pairs[target_field]
|
96
|
+
if ntbl.nil? || ntbl.empty?
|
97
|
+
if Tb::Cmd.opt_unnest_outer
|
98
|
+
y.yield pairs2
|
99
|
+
end
|
100
|
+
else
|
101
|
+
ntbl.each {|npairs|
|
102
|
+
pairs3 = pairs2.dup
|
103
|
+
npairs.each {|nf, nv|
|
104
|
+
if Tb::Cmd.opt_unnest_prefix
|
105
|
+
nf = Tb::Cmd.opt_unnest_prefix + nf
|
106
|
+
end
|
107
|
+
pairs3[nf] = nv
|
108
|
+
}
|
109
|
+
y.yield pairs3
|
110
|
+
}
|
111
|
+
end
|
112
|
+
}
|
113
|
+
}
|
114
|
+
with_output {|out|
|
115
|
+
er.write_to_csv(out, !Tb::Cmd.opt_N)
|
116
|
+
}
|
117
|
+
end
|
118
|
+
|