blib/lib/App/LTSVUtils.pm

filter: Statementstmt 65.3% 49 / 75 Branchbran 37.5% 9 / 24 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 6 / 8 Podpod 75.0% 3 / 4 totaltotal 54.9% 67 / 122 CC 20 SCAR 37.6
CC
20
Cov
55%
ltsvutil
66.9
__ANON__
4.0
ltsv2csv
2.0
CRAP 43.0
↑ Summary
1 package App::LTSVUtils;
2  
3 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 2 our $DATE = '2020-05-03'; # DATE
5 2 our $DIST = 'App-LTSVUtils'; # DIST
6 2 our $VERSION = '0.002'; # VERSION
7  
8 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our %SPEC;
13  
14 2 my %args_common = (
15 );
16  
17 2 my %arg_filename_0 = (
18 filename => {
19 summary => 'Input LTSV file',
20 schema => 'filename*',
21 description => <<'_',
22  
23 Use `-` to read from stdin.
24  
25 _
26 req => 1,
27 pos => 0,
28 cmdline_aliases => {f=>{}},
29 },
30 );
31  
32 2 my %arg_filename_1 = (
33 filename => {
34 summary => 'Input LTSV file',
35 description => <<'_',
36  
37 Use `-` to read from stdin.
38  
39 _
40 schema => 'filename*',
41 req => 1,
42 pos => 1,
43 cmdline_aliases => {f=>{}},
44 },
45 );
46  
47 $SPEC{ltsvutil} = {
48 2 v => 1.1,
49 summary => 'Perform action on a LTSV file',
50 'x.no_index' => 1,
51 args => {
52 %args_common,
53 action => {
54 schema => ['str*', in=>[
55 'dump',
56 '2csv',
57 ]],
58 req => 1,
59 pos => 0,
60 cmdline_aliases => {a=>{}},
61 },
62 %arg_filename_1,
63 },
64 args_rels => {
65 },
66 };
67 1 ▶sub ltsvutil {
Subroutine ltsvutil: called CC=17 SCAR=42.0
Pod undocumented
68 1 my %args = @_;
69 1 my $action = $args{action};
70  
71 1 my $res = "";
72 1 my $i = 0;
73  
74 1 my $fh;
75 1 ▶ if ($args{filename} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'filename'} eq '-') { } 0 1 1
76 0 $fh = *STDIN;
77 } else {
78 open $fh, "<", $args{filename} or
79 1 ▶ return [500, "Can't open input filename '$args{filename}': $!"];
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $args{"filename"} or return [500, qq[Can't open input filename '$args{"filename"}': $!]] 1 0 1
80 }
81 1 binmode $fh, ":encoding(utf8)";
82  
83 3 ▶ my $code_getline = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
84 3 my $row0 = <$fh>;
85 3 ▶ return undef unless defined $row0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row0 1 2 3
86 2 chomp($row0);
87 2 my $row = {};
88 2 for my $col0 (split /\t/, $row0) {
89 4 ▶ $col0 =~ /(.+?):(.*)/ or die "Row $i: Invalid column '$col0': must be in LABEL:VAL format\n";
Branch: 1 branch
BranchTrueFalseTotal
$col0 =~ /(.+?):(.*)/ or die "Row ${i}: Invalid column '${col0}': must be in LABEL:VAL format\n" 4 0 4
90 4 $row->{$1} = $2;
91 }
92 2 $row;
93 1 };
94  
95 1 my $rows = [];
96 1 my %col_idxs;
97  
98 1 ▶ while (my $row = $code_getline->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$code_getline()) 2 1 3
99 2 $i++;
100 2 ▶ if ($action eq 'dump') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'dump') { } 2 0 2
elsif ($action eq '2csv' or $action eq '2tsv') { } 0 0 0
101 2 push @$rows, $row;
102 ▶ } elsif ($action eq '2csv' || $action eq '2tsv') {
Condition0%0/3
$action eq "2csv" or $action eq "2tsv"
l!l&&r!l&&!r
000
MC/DC0%0/2
$action eq "2csv" or $action eq "2tsv"
$action eq "2csv" $action eq "2tsv"
Truth table0%0/3
A or B
A= $action eq "2csv"B= $action eq "2tsv"
ABresult
1X1
011
000
103 0 push @$rows, $row;
104 0 for my $k (sort keys %$row) {
105 0 ▶ next if defined $col_idxs{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $col_idxs{$k} 0 0 0
106 0 $col_idxs{$k} = keys(%col_idxs);
107 }
108 } else {
109 0 return [400, "Unknown action '$action'"];
110 }
111 } # while getline()
112  
113 0 my @cols = sort { $col_idxs{$a} <=> $col_idxs{$b} } keys %col_idxs;
114  
115 1 ▶ if ($action eq 'dump') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'dump') { } 1 0 1
elsif ($action eq '2csv') { } 0 0 0
elsif ($action eq '2tsv') { } 0 0 0
116 1 return [200, "OK", $rows];
117 } elsif ($action eq '2csv') {
118 0 require Text::CSV_XS;
119 0 my $csv = Text::CSV_XS->new({binary=>1});
120 0 $csv->print(\*STDOUT, \@cols);
121 0 print "\n";
122 0 for my $row (@$rows) {
123 0 ▶ $csv->print(\*STDOUT, [map {$row->{$_} // ''} @cols]);
Condition0%0/2
$row->{$_} // ""
l!l
00
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table0%0/2
A
A= $row->{$_}
Aresult
11
00
124 0 print "\n";
125 }
126 } elsif ($action eq '2tsv') {
127 0 ▶ if (@cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cols) 0 0 0
128 0 print join("\t", @cols) . "\n";
129 0 for my $row (@$rows) {
130 0 ▶ print join("\t", map { $row->{$_} // '' } @cols) . "\n";
Condition0%0/2
$row->{$_} // ""
l!l
00
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table0%0/2
A
A= $row->{$_}
Aresult
11
00
131 }
132 }
133 } else {
134 0 return [500, "Unknown action '$action'"];
135 }
136  
137 0 [200, "OK", $res, {"cmdline.skip_format"=>1}];
138 } # ltsvutil
139  
140 $SPEC{ltsv_dump} = {
141 2 v => 1.1,
142 summary => 'Dump LTSV as data structure (array of hashes)',
143 args => {
144 %args_common,
145 %arg_filename_0,
146 },
147 };
148 1 ▶sub ltsv_dump {
Subroutine ltsv_dump: called CC=1 SCAR=0.0
Pod documented
149 1 my %args = @_;
150 1 ltsvutil(%args, action=>'dump');
151 }
152  
153 $SPEC{ltsv2csv} = {
154 2 v => 1.1,
155 summary => 'Convert LTSV to CSV',
156 args => {
157 %args_common,
158 %arg_filename_0,
159 },
160 };
161 0 ▶sub ltsv2csv {
Subroutine ltsv2csv: not called CC=1 SCAR=6.9
Pod documented
162 0 my %args = @_;
163 0 ltsvutil(%args, action=>'2csv');
164 }
165  
166 $SPEC{ltsv2tsv} = {
167 2 v => 1.1,
168 summary => 'Convert LTSV to TSV',
169 args => {
170 %args_common,
171 %arg_filename_0,
172 },
173 };
174 0 ▶sub ltsv2tsv {
Subroutine ltsv2tsv: not called CC=1 SCAR=6.9
Pod documented
175 0 my %args = @_;
176 0 ltsvutil(%args, action=>'2tsv');
177 }
178  
179 2 1;
180 # ABSTRACT: CLI utilities related to LTSV
181  
182 __END__
↑ Summary