App/picadata.pm

filter: Statementstmt 31.6% 100 / 316 Branchbran 16.3% 35 / 214 Conditioncond 15.3% 15 / 98 MC/DCmcdc 8.7% 5 / 57 Subroutinesub 74.0% 20 / 27 Podpod 0.0% 0 / 7 totaltotal 24.3% 175 / 719 CC 156 SCAR 69.7
CC
156
Cov
24%
run
2862.0
__ANON__
702.0
new
397.1
CRAP 1063.6
↑ Summary PICA/Writer/PPXML.pm »
1 package App::picadata;
2 1 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '2.12';
5  
6 1 ▶use Getopt::Long qw(GetOptionsFromArray :config bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use PICA::Data qw(pica_parser pica_writer pica_annotation);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PICA::Patch qw(pica_diff pica_patch);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use PICA::Schema qw(field_identifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use PICA::Schema::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Getopt::Long qw(:config bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use List::Util qw(any all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Text::Abbrev;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 my %TYPES = (
21 bin => 'Binary',
22 dat => 'Binary',
23 binary => 'Binary',
24 extpp => 'Binary',
25 ext => 'Binary',
26 plain => 'Plain',
27 pp => 'Plain',
28 import => 'Import',
29 plus => 'Plus',
30 norm => 'Plus',
31 normpp => 'Plus',
32 xml => 'XML',
33 ppxml => 'PPXML',
34 pixml => 'PIXML',
35 json => 'JSON',
36 ndjson => 'JSON',
37 patch => 'Patch',
38 );
39  
40 1 my %COLORS = (
41 tag => 'magenta',
42 occurrence => 'magenta',
43 code => 'red',
44 value => 'green'
45 );
46  
47 6 ▶sub new {
Subroutine new: called CC=54 SCAR=59.8
Pod undocumented
48 6 my ($class, @argv) = @_;
49  
50 6 my $terminal = -t *STDIN; ## no critic
51 6 ▶ my $command = (!@argv && $terminal) ? 'help' : '';
Branch: 1 branch
BranchTrueFalseTotal
!@argv && $terminal ? : 0 6 6
Condition33%1/3
not @argv and $terminal
!ll&&!rl&&r
600
MC/DC0%0/2
not @argv and $terminal
not @argv $terminal
Truth table33%1/3
not(A) and B
A= not @argvB= $terminal
ABresult
0X0
100
111
52  
53 6 my $number = 0;
54 6 ▶ if (my ($i) = grep {$argv[$_] =~ /^-(\d+)$/} (0 .. @argv - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($i) = grep({$argv[$_] =~ /^-(\d+)$/u;} 0 .. @argv - 1)) 1 5 6
55 1 $number = -(splice @argv, $i, 1);
56 }
57  
58 6 my $noAnnotate = grep {$_ eq '-A'} @argv;
59  
60 6 my @path;
61  
62 my $opt = {
63 number => \$number,
64 0 ▶ help => sub {$command = 'help'},
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 1 ▶ version => sub {$command = 'version'},
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 ▶ build => sub {$command = 'build'},
Subroutine __ANON__: called CC=1 SCAR=0.0
67 1 ▶ count => sub {$command = 'count'}, # for backwards compatibility
Subroutine __ANON__: called CC=1 SCAR=0.0
68 6 path => \@path,
69 modify => [],
70 };
71  
72 6 my %cmd = abbrev
73 qw(convert get count levels fields filter subfields sf explain validate build diff patch help version modify join);
74 6 ▶ if ($cmd{$argv[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd{$argv[0]}) 0 6 6
75 0 $command = $cmd{shift @argv};
76 0 $command =~ s/^sf$/subfields/;
77 }
78  
79 GetOptionsFromArray(
80 6 ▶ \@argv, $opt, 'from|f=s', 'to|t:s',
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@argv, $opt, "from|f=s", "to|t:s", "schema|s=s", "annotate|A|a", "abbrev|B", "build|b", "unknown|u!", "count|c", "order|o", "path|p=s", "level|l:i", "number|n:i", "color|C", "mono|M", "help|h|?", "version|V") or pod2usage(2) 6 0 6
81 'schema|s=s', 'annotate|A|a', 'abbrev|B', 'build|b',
82 'unknown|u!', 'count|c', 'order|o', 'path|p=s',
83 'level|l:i', 'number|n:i', 'color|C', 'mono|M',
84 'help|h|?', 'version|V',
85 ) or pod2usage(2);
86  
87 6 $opt->{number} = $number;
88 6 ▶ $opt->{annotate} = 0 if $noAnnotate;
Branch: 1 branch
BranchTrueFalseTotal
if $noAnnotate 0 6 6
89 $opt->{color}
90 6 ▶ = !$opt->{mono} && ($opt->{color} || -t *STDOUT); ## no critic
Condition33%2/6
!$opt->{'mono'} && ($opt->{'color'} || -t *STDOUT)
!ll&&!rl&&r
060
$opt->{"color"} or -t *STDOUT
l!l&&r!l&&!r
006
MC/DC0%0/3
!$opt->{'mono'} && ($opt->{'color'} || -t *STDOUT)
!$opt->{'mono'} $opt->{"color"} -t *STDOUT
Truth table25%1/4
not(A) && B or C
A= !$opt->{'mono'}B= $opt->{"color"}C= -t *STDOUT
ABCresult
0XX0
1000
11X1
1011
91  
92 6 delete $opt->{$_} for qw(count build help version);
93  
94 6 ▶ if ($command eq 'modify') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "modify") 0 6 6
95 0 ▶ die "missing path argument!\n" unless @argv;
Branch: 1 branch
BranchTrueFalseTotal
unless @argv 0 0 0
96 0 push @{$opt->{modify}}, parse_path(shift @argv);
97 0 my $value = shift @argv;
98 0 ▶ die "missing value argument!\n" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
99 0 utf8::decode($value);
100 0 push @{$opt->{modify}}, $value;
101 }
102  
103 6 my $pattern = '[012.][0-9.][0-9.][A-Z@.](\$[^|]+|/[0-9.-]+)?';
104 6 ▶ while (@argv && $argv[0] =~ /^$pattern(\s*\|\s*($pattern)?)*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv and $argv[0] =~ /^$pattern(\s*\|\s*($pattern)?)*$/u) 4 6 10
Condition100%3/3
@argv and $argv[0] =~ /^$pattern(\s*\|\s*($pattern)?)*$/u
!ll&&!rl&&r
514
MC/DC100%2/2
@argv and $argv[0] =~ /^$pattern(\s*\|\s*($pattern)?)*$/u
@argv $argv[0] =~ /^$pattern(\s*\|\s*($pattern)?)*$/u
Truth table100%3/3
A and B
A= @argvB= $argv[0] =~ /^$pattern(\s*\|\s*($pattern)?)*$/u
ABresult
0X0
100
111
105 4 push @path, shift @argv;
106 }
107  
108 6 @path = map {parse_path($_)}
109 6 grep {$_ ne ""} map {split /\s*[\|,]\s*/, $_} @path;
110 6 ▶ if (@path && all {$_->subfields ne ""} @path) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (@path and &all(sub { $_->subfields ne ""; } , @path)) 1 5 6
Condition100%3/3
@path and &all(sub { $_->subfields ne ""; } , @path)
!ll&&!rl&&r
411
MC/DC100%2/2
@path and &all(sub { $_->subfields ne ""; } , @path)
@path &all(sub { $_->subfields ne ""; } , @path)
Truth table100%3/3
A and B
A= @pathB= &all(sub { $_->subfields ne ""; } , @path)
ABresult
0X0
100
111
111 1 ▶ $command = 'get' unless $command;
Branch: 1 branch
BranchTrueFalseTotal
unless $command 1 0 1
112 }
113  
114 6 ▶ $opt->{order} = 1 if $command =~ /(diff|patch|levels)/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /(diff|patch|levels)/u 0 6 6
115  
116 6 ▶ unless ($command) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 2 4 6
117 2 ▶ if ($opt->{schema} && !$opt->{annotate}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->{'schema'} and not $opt->{'annotate'}) { } 0 2 2
elsif ($opt->{'abbrev'}) { } 0 2 2
Condition33%1/3
$opt->{"schema"} and not $opt->{"annotate"}
!ll&&!rl&&r
200
MC/DC0%0/2
$opt->{"schema"} and not $opt->{"annotate"}
$opt->{"schema"} not $opt->{"annotate"}
Truth table33%1/3
A and not(B)
A= $opt->{"schema"}B= not $opt->{"annotate"}
ABresult
0X0
100
111
118 0 $command = 'validate';
119 }
120 elsif ($opt->{abbrev}) {
121 0 $command = 'build';
122 }
123 else {
124 2 $command = 'convert';
125 }
126 }
127  
128 6 ▶ if ($command =~ /validate|explain|fields|subfields/ && !$opt->{schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /validate|explain|fields|subfields/u and not $opt->{"schema"}) 0 6 6
Condition33%1/3
$command =~ /validate|explain|fields|subfields/u and not $opt->{"schema"}
!ll&&!rl&&r
600
MC/DC0%0/2
$command =~ /validate|explain|fields|subfields/u and not $opt->{"schema"}
$command =~ /validate|explain|fields|subfields/u not $opt->{"schema"}
Truth table33%1/3
A and not(B)
A= $command =~ /validate|explain|fields|subfields/uB= not $opt->{"schema"}
ABresult
0X0
100
111
129 0 ▶ if ($ENV{PICA_SCHEMA}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'PICA_SCHEMA'}) { } 0 0 0
elsif ($command =~ /validate|explain/u) { } 0 0 0
130 0 $opt->{schema} = $ENV{PICA_SCHEMA};
131 }
132 elsif ($command =~ /validate|explain/) {
133 $opt->{error}
134 0 = "$command requires an Avram Schema (via option -s or environment variable PICA_SCHEMA)";
135 }
136 }
137  
138 6 ▶ $opt->{annotate} = 1 if $command eq 'diff';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "diff" 0 6 6
139 6 ▶ $opt->{annotate} = 0 if $command eq 'patch';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "patch" 0 6 6
140  
141 6 ▶ if ($opt->{schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"schema"}) 0 6 6
142 0 $opt->{schema} = load_schema($opt->{schema});
143 0 $opt->{schema}{ignore_unknown} = $opt->{unknown};
144  
145 0 ▶ if ($command eq 'explain' && !@path && !@argv && $terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "explain" and not @path and not @argv and $terminal) 0 0 0
Condition0%0/9
$command eq "explain" and not @path and not @argv and $terminal
!ll&&!rl&&r
000
$command eq "explain" and not @path and not @argv
!ll&&!rl&&r
000
$command eq "explain" and not @path
!ll&&!rl&&r
000
MC/DC0%0/4
$command eq "explain" and not @path and not @argv and $terminal
$command eq "explain" not @path not @argv $terminal
Truth table0%0/5
A and not(B) and not(C) and D
A= $command eq "explain"B= not @pathC= not @argvD= $terminal
ABCDresult
0XXX0
10XX0
110X0
11100
11111
146 0 ▶ while (my ($id, $field) = each %{$opt->{schema}{fields} || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$opt->{"schema"}{"fields"} or {};}) 0 0 0
Condition0%0/2
$opt->{'schema'}{'fields'} || {}
l!l
00
MC/DC0%0/1
$opt->{'schema'}{'fields'} || {}
$opt->{'schema'}{'fields'}
Truth table0%0/2
A
A= $opt->{'schema'}{'fields'}
Aresult
11
00
147 0 push @path, parse_path($id);
148  
149 # see <https://github.com/gbv/k10plus-avram-api/issues/12>
150 my $sf
151 = ref $field->{subfields} eq 'HASH'
152 ? $field->{subfields}
153 0 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $field->{'subfields'} eq 'HASH' ? : 0 0 0
154 0 push @path, map {parse_path("$id\$$_")} keys %$sf;
155 }
156 }
157 }
158  
159 # all path expressions have been initialized as PICA::Path objects
160 6 $opt->{path} = \@path;
161  
162 6 ▶ if ($command =~ qr{diff|patch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ qr/diff|patch/u) 0 6 6
163 0 ▶ unshift @argv, '-' if @argv == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @argv == 1 0 0 0
164 0 ▶ $opt->{error} = "$command requires two input files" if @argv != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @argv != 2 0 0 0
165  
166 0 ▶ if ($command eq 'diff') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "diff") 0 0 0
167  
168 # only Plain and JSON support annotations
169 0 ▶ $opt->{to} = 'plain' unless $TYPES{lc $opt->{to}} eq 'JSON';
Branch: 1 branch
BranchTrueFalseTotal
unless $TYPES{lc $opt->{"to"}} eq "JSON" 0 0 0
170 }
171 }
172  
173 6 ▶ $opt->{input} = @argv ? \@argv : ['-'];
Branch: 1 branch
BranchTrueFalseTotal
@argv ? : 1 5 6
174  
175 6 ▶ if (my $name = $opt->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $opt->{"from"}) 0 6 6
176 $opt->{from} = $TYPES{lc $opt->{from}}
177 0 ▶ or $opt->{error} = "unknown serialization type: $name";
Branch: 1 branch
BranchTrueFalseTotal
$opt->{"from"} = $TYPES{lc $opt->{"from"}} or $opt->{"error"} = "unknown serialization type: $name" 0 0 0
178 }
179  
180 # default output format
181 6 ▶ unless ($opt->{to}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"to"}) 6 0 6
182 6 ▶ if ($command =~ /(convert|levels|filter|diff|patch|modify|join)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command =~ /(convert|levels|filter|diff|patch|modify|join)/u) { } 2 4 6
elsif ($command eq 'validate' and $opt->{'annotate'}) { } 0 8 8
183 2 $opt->{to} = $opt->{from};
184 $opt->{to} ||= $TYPES{lc $1}
185 2 ▶ if $opt->{input}->[0] =~ /\.([a-z]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"input"}[0] =~ /\.([a-z]+)$/u 1 1 2
Condition33%1/3
$opt->{"to"} ||= $TYPES{lc $1}
l!l&&r!l&&!r
010
MC/DC0%0/2
$opt->{"to"} ||= $TYPES{lc $1}
$opt->{"to"} $TYPES{lc $1}
Truth table33%1/3
A ||= B
A= $opt->{"to"}B= $TYPES{lc $1}
ABresult
1X1
011
000
186 2 ▶ $opt->{to} ||= 'plain';
Condition100%2/2
$opt->{"to"} ||= "plain"
l!l
11
MC/DC100%1/1
$opt->{"to"} ||= "plain"
$opt->{"to"}
Truth table100%2/2
A
A= $opt->{"to"}
Aresult
11
00
187 }
188 ▶ elsif ($command eq 'validate' && $opt->{annotate}) {
Condition33%1/3
$command eq "validate" and $opt->{"annotate"}
!ll&&!rl&&r
400
MC/DC0%0/2
$command eq "validate" and $opt->{"annotate"}
$command eq "validate" $opt->{"annotate"}
Truth table33%1/3
A and B
A= $command eq "validate"B= $opt->{"annotate"}
ABresult
0X0
100
111
189 0 $opt->{to} = 'plain';
190 }
191 }
192  
193 6 ▶ if (my $name = $opt->{to}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $opt->{"to"}) 2 4 6
194 $opt->{to} = $TYPES{lc $opt->{to}}
195 2 ▶ or $opt->{error} = "unknown serialization type: $name";
Branch: 1 branch
BranchTrueFalseTotal
$opt->{"to"} = $TYPES{lc $opt->{"to"}} or $opt->{"error"} = "unknown serialization type: $name" 2 0 2
196 }
197  
198 6 $opt->{command} = $command;
199  
200 6 bless $opt, $class;
201 }
202  
203 0 ▶sub parser_from_input {
Subroutine parser_from_input: not called CC=7 SCAR=40.3
Pod undocumented
204 0 my ($self, $in, $format) = @_;
205  
206 0 ▶ if ($in eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($in eq '-') { } 0 0 0
207 0 $in = *STDIN;
208 0 binmode $in, ':encoding(UTF-8)';
209 }
210 else {
211 0 ▶ die "File not found: $in\n" unless -e $in;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $in 0 0 0
212 }
213  
214 0 ▶ $format ||= $self->{from};
Condition0%0/3
$format ||= $self->{"from"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$format ||= $self->{"from"}
$format $self->{"from"}
Truth table0%0/3
A ||= B
A= $formatB= $self->{"from"}
ABresult
1X1
011
000
215 0 ▶ $format ||= $TYPES{lc $1} if $in =~ /\.([a-z]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /\.([a-z]+)$/u 0 0 0
Condition0%0/3
$format ||= $TYPES{lc $1}
l!l&&r!l&&!r
000
MC/DC0%0/2
$format ||= $TYPES{lc $1}
$format $TYPES{lc $1}
Truth table0%0/3
A ||= B
A= $formatB= $TYPES{lc $1}
ABresult
1X1
011
000
216  
217 0 ▶ return pica_parser($format || 'plain', $in);
Condition0%0/2
$format || 'plain'
l!l
00
MC/DC0%0/1
$format || 'plain'
$format
Truth table0%0/2
A
A= $format
Aresult
11
00
218 }
219  
220 0 ▶sub load_schema {
Subroutine load_schema: not called CC=4 SCAR=30.0
Pod undocumented
221 0 my ($schema) = @_;
222 0 my $json;
223 0 ▶ if ($schema =~ qr{^https?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema =~ qr"^https?://"u) { } 0 0 0
224 0 require HTTP::Tiny;
225 0 my $res = HTTP::Tiny->new->get($schema);
226 0 ▶ die "HTTP request failed: $schema\n" unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 0 0
227 0 $json = $res->{content};
228 }
229 else {
230 0 ▶ open(my $fh, "<", $schema)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $schema or die "Failed to open schema file: $schema\n" 0 0 0
231 or die "Failed to open schema file: $schema\n";
232 0 $json = join "\n", <$fh>;
233 }
234 0 return PICA::Schema->new(JSON::PP->new->decode($json));
235 }
236  
237 0 ▶sub run {
Subroutine run: not called CC=53 SCAR=79.6
Pod undocumented
238 0 my ($self) = @_;
239 0 my $command = $self->{command};
240 0 my $schema = $self->{schema};
241 0 ▶ my @pathes = @{$self->{path} || []};
Condition0%0/2
$self->{'path'} || []
l!l
00
MC/DC0%0/1
$self->{'path'} || []
$self->{'path'}
Truth table0%0/2
A
A= $self->{'path'}
Aresult
11
00
242  
243 # commands that don't parse any input data
244 0 ▶ if ($self->{error}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'error'}) { } 0 0 0
elsif ($command eq 'help') { } 0 0 0
elsif ($command eq 'version') { } 0 0 0
elsif ($command eq 'explain') { } 0 0 0
245 0 pod2usage($self->{error});
246 }
247 elsif ($command eq 'help') {
248 0 pod2usage(
249 -verbose => 99,
250 -sections => "SYNOPSIS|COMMANDS|OPTIONS|DESCRIPTION|EXAMPLES"
251 );
252 }
253 elsif ($command eq 'version') {
254 0 say $PICA::Data::VERSION;
255 0 exit;
256 }
257 elsif ($command eq 'explain') {
258 0 $self->explain($schema, $_) for @pathes;
259 0 ▶ unless (@pathes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@pathes) 0 0 0
260 0 ▶ while (<STDIN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline STDIN)) 0 0 0
261 0 ▶ if ($_ =~ /^([^0-9a-z]\s+)?([^ ]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^([^0-9a-z]\s+)?([^ ]+)/u) 0 0 0
262 0 my $path = eval {parse_path($_)};
263 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 0 0 0
264 0 $self->explain($schema, $path);
265 }
266 else {
267 0 warn "invalid PICA Path: $_\n";
268 }
269  
270 }
271 }
272 }
273 0 exit;
274 }
275  
276 # initialize writer and schema builder
277 0 my $writer;
278 0 ▶ if ($self->{to}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"to"}) 0 0 0
279 $writer = pica_writer(
280 $self->{to},
281 color => ($self->{color} ? \%COLORS : undef),
282 schema => $schema,
283 annotate => $self->{annotate},
284 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'color'} ? : 0 0 0
285 }
286 0 binmode *STDOUT, ':encoding(UTF-8)';
287  
288 0 ▶ my $builder
Branch: 2 branches
BranchTrueFalseTotal
$command =~ /(build|fields|subfields|explain)/u ? : 0 0 0
$schema ? : 0 0 0
289 = $command =~ /(build|fields|subfields|explain)/
290 ? PICA::Schema::Builder->new($schema ? %$schema : ())
291 : undef;
292  
293 0 ▶ my $joined = $command eq 'join' ? PICA::Data->new : undef;
Branch: 1 branch
BranchTrueFalseTotal
$command eq 'join' ? : 0 0 0
294  
295 # additional options
296 0 my $number = $self->{number};
297 0 my $level = $self->{level};
298 0 my $stats = {records => 0, holdings => 0, items => 0, fields => 0};
299 0 my $invalid = 0;
300  
301 0 my @getFields = grep {$_->subfields eq ""} @pathes;
302 0 my @getSubfields = grep {$_->subfields ne ""} @pathes;
303  
304 0 ▶ my $process = sub {
Subroutine __ANON__: not called CC=26 SCAR=65.5
305 0 my $record = shift;
306  
307 0 ▶ if ($command eq 'get' && @getSubfields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "get" and @getSubfields) 0 0 0
Condition0%0/3
$command eq "get" and @getSubfields
!ll&&!rl&&r
000
MC/DC0%0/2
$command eq "get" and @getSubfields
$command eq "get" @getSubfields
Truth table0%0/3
A and B
A= $command eq "get"B= @getSubfields
ABresult
0X0
100
111
308 say $_
309 0 ▶ for map {@{$record->match($_, split => 1) // []}}
Condition0%0/2
$record->match($_, "split", 1) // []
l!l
00
MC/DC0%0/1
$record->match($_, "split", 1) // []
$record->match($_, "split", 1)
Truth table0%0/2
A
A= $record->match($_, "split", 1)
Aresult
11
00
310 @getSubfields;
311 }
312  
313 0 ▶ $record = $record->sort if $self->{order};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"order"} 0 0 0
314  
315 0 ▶ if ($command eq 'filter' && @pathes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq 'filter' and @pathes) { } 0 0 0
Condition0%0/3
$command eq "filter" and @pathes
!ll&&!rl&&r
000
MC/DC0%0/2
$command eq "filter" and @pathes
$command eq "filter" @pathes
Truth table0%0/3
A and B
A= $command eq "filter"B= @pathes
ABresult
0X0
100
111
316 my @values
317 0 ▶ = map {@{$record->match($_, split => 1) // []}} @pathes;
Condition0%0/2
$record->match($_, "split", 1) // []
l!l
00
MC/DC0%0/1
$record->match($_, "split", 1) // []
$record->match($_, "split", 1)
Truth table0%0/2
A
A= $record->match($_, "split", 1)
Aresult
11
00
318 0 ▶ return unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 0 0
319 }
320 else {
321 # reduce record to selected fields
322 0 ▶ $record->{record} = $record->fields(@getFields) if @getFields;
Branch: 1 branch
BranchTrueFalseTotal
if @getFields 0 0 0
323 }
324 0 ▶ return if $record->empty;
Branch: 1 branch
BranchTrueFalseTotal
if $record->empty 0 0 0
325  
326 0 ▶ if ($command eq 'modify') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "modify") 0 0 0
327 0 ▶ if ($self->{annotate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"annotate"}) 0 0 0
328  
329 # can't annotate an already annoted record
330 0 pica_annotation($_, undef) for @{$record->{record}};
331 }
332 0 my $before = [map {[@$_]} @{$record->{record}}]; # deep copy
333 0 my @modify = @{$self->{modify}};
334  
335 0 ▶ while (@modify) {
Branch: 1 branch
BranchTrueFalseTotal
if (@modify) 0 0 0
336 0 my $path = shift @modify;
337 0 my $value = shift @modify;
338 0 $record->update($path, $value);
339 }
340  
341 0 ▶ if ($self->{annotate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"annotate"}) 0 0 0
342 0 $record = pica_diff($before, $record, keep => 1);
343 }
344 }
345  
346 # TODO: also validate on other commands?
347 0 ▶ if ($command eq 'validate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "validate") 0 0 0
348 my @errors = $schema->check(
349 $record,
350 ignore_unknown => !$self->{unknown},
351 annotate => $self->{annotate}
352 0 );
353 0 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 0 0
354 0 ▶ unless ($self->{annotate}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"annotate"}) 0 0 0
355 say(defined $record->{_id} ? $record->{_id} . ": $_" : $_)
356 0 ▶ for @errors;
Branch: 1 branch
BranchTrueFalseTotal
defined $record->{'_id'} ? : 0 0 0
357 }
358 0 $invalid++;
359 }
360 }
361  
362 0 ▶ if ($joined) {
Branch: 2 branches
BranchTrueFalseTotal
if ($joined) { } 0 0 0
elsif ($writer) { } 0 0 0
363 0 push @{$joined->{record}}, @{$record->{record}};
364 }
365 elsif ($writer) {
366 0 $writer->write($record);
367 }
368 0 ▶ $builder->add($record) if $builder;
Branch: 1 branch
BranchTrueFalseTotal
if $builder 0 0 0
369  
370 0 ▶ if ($command eq 'count') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "count") 0 0 0
371 $stats->{holdings}
372 0 += grep {@{$_->fields('1...')}} @{$record->holdings};
373 0 $stats->{items} += grep {!$_->empty} @{$record->items};
374 0 $stats->{fields} += @{$record->{record}};
375 }
376 0 $stats->{records}++;
377 0 };
378  
379 0 ▶ if ($command eq 'diff') {
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq 'diff') { } 0 0 0
elsif ($command eq 'patch') { } 0 0 0
380 0 my @parser = map {$self->parser_from_input($_)} @{$self->{input}};
381 0 while (1) {
382 0 my $a = $parser[0]->next;
383 0 my $b = $parser[1]->next;
384 0 ▶ if ($a or $b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a or $b) { } 0 0 0
Condition0%0/3
$a or $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$a or $b
$a $b
Truth table0%0/3
A or B
A= $aB= $b
ABresult
1X1
011
000
385 0 ▶ $writer->write(pica_diff($a || [], $b || []));
Condition0%0/4
$a || []
l!l
00
$b || []
l!l
00
MC/DC0%0/1
$a || []
$a
MC/DC0%0/1
$b || []
$b
Truth table0%0/2
A
A= $a
Aresult
11
00
Truth table0%0/2
A
A= $b
Aresult
11
00
386 }
387 else {
388 0 last;
389 }
390 0 ▶ last if $number && $number <= ++$stats->{record};
Branch: 1 branch
BranchTrueFalseTotal
if $number and $number <= ++$stats->{"record"} 0 0 0
Condition0%0/3
$number and $number <= ++$stats->{"record"}
!ll&&!rl&&r
000
MC/DC0%0/2
$number and $number <= ++$stats->{"record"}
$number $number <= ++$stats->{"record"}
Truth table0%0/3
A and B
A= $numberB= $number <= ++$stats->{"record"}
ABresult
0X0
100
111
391 }
392 }
393 elsif ($command eq 'patch') {
394 0 my $parser = $self->parser_from_input($self->{input}[0]);
395  
396 # TODO: allow to read diff in PICA/JSON
397 0 my $patches = $self->parser_from_input($self->{input}[1], 'plain');
398 0 my $diff;
399 0 ▶ while (my $record = $parser->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $parser->next) 0 0 0
400 0 ▶ $diff = $patches->next || $diff; # keep latest diff
Condition0%0/3
$patches->next || $diff
l!l&&r!l&&!r
000
MC/DC0%0/2
$patches->next || $diff
$patches->next $diff
Truth table0%0/3
A || B
A= $patches->nextB= $diff
ABresult
1X1
011
000
401 0 ▶ die "Missing patch to apply in $self->{input}[1]\n" unless $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 0 0 0
402  
403 0 my $changed = eval {pica_patch($record, $diff)};
404 0 ▶ if (!$changed || $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $changed or $@) { } 0 0 0
Condition0%0/3
not $changed or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
not $changed or $@
not $changed $@
Truth table0%0/3
not(A) or B
A= not $changedB= $@
ABresult
1X1
011
000
405 0 warn $@;
406 }
407 else {
408 0 ▶ $writer->write($changed || []);
Condition0%0/2
$changed || []
l!l
00
MC/DC0%0/1
$changed || []
$changed
Truth table0%0/2
A
A= $changed
Aresult
11
00
409 }
410  
411 0 ▶ last if $number && $number <= ++$stats->{record};
Branch: 1 branch
BranchTrueFalseTotal
if $number and $number <= ++$stats->{"record"} 0 0 0
Condition0%0/3
$number and $number <= ++$stats->{"record"}
!ll&&!rl&&r
000
MC/DC0%0/2
$number and $number <= ++$stats->{"record"}
$number $number <= ++$stats->{"record"}
Truth table0%0/3
A and B
A= $numberB= $number <= ++$stats->{"record"}
ABresult
0X0
100
111
412 }
413 }
414 else {
415 0 ▶ my $split = $command eq 'levels' || ($level // -1) >= 0;
Condition0%0/5
$command eq 'levels' || ($level // -1) >= 0
l!l&&r!l&&!r
000
$level // -1
l!l
00
MC/DC0%0/2
$command eq 'levels' || ($level // -1) >= 0
$command eq 'levels' $level
Truth table0%0/3
A || B
A= $command eq 'levels'B= $level
ABresult
1X1
011
000
416 0 RECORD: foreach my $in (@{$self->{input}}) {
417 0 my $parser = $self->parser_from_input($in);
418 0 ▶ while (my $next = $parser->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $parser->next) 0 0 0
419 0 ▶ for ($split ? $next->split($level) : $next) {
Branch: 1 branch
BranchTrueFalseTotal
$split ? : 0 0 0
420 0 $process->($_);
421 0 ▶ last RECORD if $number and $stats->{records} >= $number;
Branch: 1 branch
BranchTrueFalseTotal
if $number and $stats->{"records"} >= $number 0 0 0
Condition0%0/3
$number and $stats->{"records"} >= $number
!ll&&!rl&&r
000
MC/DC0%0/2
$number and $stats->{"records"} >= $number
$number $stats->{"records"} >= $number
Truth table0%0/3
A and B
A= $numberB= $stats->{"records"} >= $number
ABresult
0X0
100
111
422 }
423 }
424 }
425 }
426  
427 0 ▶ if ($writer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writer) 0 0 0
428 0 ▶ $writer->write($joined->sort) if $joined;
Branch: 1 branch
BranchTrueFalseTotal
if $joined 0 0 0
429 0 $writer->end();
430 }
431  
432 0 ▶ if ($command eq 'count') {
Branch: 3 branches
BranchTrueFalseTotal
if ($command eq 'count') { } 0 0 0
elsif ($command =~ /(sub)?fields/u) { } 0 0 0
elsif ($command eq 'build') { } 0 0 0
433 0 $stats->{invalid} = $invalid;
434 say $stats->{$_} . " $_"
435 0 for grep {$stats->{$_}} qw(records invalid holdings items fields);
436 }
437 elsif ($command =~ /(sub)?fields/) {
438 0 my $fields = $builder->schema->{fields};
439 0 for my $id (sort keys %$fields) {
440 0 ▶ if ($command eq 'fields') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq 'fields') { } 0 0 0
441 0 ▶ $self->document($id, $self->{abbrev} ? 0 : $fields->{$id});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'abbrev'} ? : 0 0 0
442 }
443 else {
444 0 ▶ my $sfs = $fields->{$id}->{subfields} || {};
Condition0%0/2
$fields->{$id}{'subfields'} || {}
l!l
00
MC/DC0%0/1
$fields->{$id}{'subfields'} || {}
$fields->{$id}{'subfields'}
Truth table0%0/2
A
A= $fields->{$id}{'subfields'}
Aresult
11
00
445 0 for (keys %$sfs) {
446 $self->document("$id\$$_",
447 0 ▶ $self->{abbrev} ? 0 : $sfs->{$_});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'abbrev'} ? : 0 0 0
448 }
449 }
450 }
451 }
452 elsif ($command eq 'build') {
453 0 $schema = $builder->schema;
454 print JSON::PP->new->indent->space_after->canonical->convert_blessed
455 0 ▶ ->encode($self->{abbrev} ? $schema->abbreviated : $schema);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'abbrev'} ? : 0 0 0
456 }
457  
458 0 exit !!$invalid;
459 }
460  
461 6 ▶sub parse_path {
Subroutine parse_path: called CC=2 SCAR=7.2
Pod undocumented
462 6 ▶ eval {PICA::Path->new($_[0])} || die "invalid PICA Path: $_[0]\n";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "PICA::Path"->new($_[0]) } } or die "invalid PICA Path: $_[0]\n" 6 0 6
463 }
464  
465 0 ▶sub explain {
Subroutine explain: not called CC=6 SCAR=37.4
Pod undocumented
466 0 my ($self, $schema, $path) = @_;
467  
468 0 ▶ if ($path->stringify =~ /[.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->stringify =~ /[.]/u) 0 0 0
469 0 warn "Fields with wildcards cannot be explained yet!\n";
470 0 return;
471 }
472  
473 0 my $tag = $path->fields;
474  
475 0 my ($firstocc) = grep {$_ > 0} split '-', $path->occurrences;
476 0 my $id = field_identifier($schema, [$tag, $firstocc]);
477  
478 0 my $def = $schema->{fields}{$id};
479 0 ▶ if (defined $path->subfields && $def) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path->subfields and $def) { } 0 0 0
Condition0%0/3
defined $path->subfields and $def
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path->subfields and $def
defined $path->subfields $def
Truth table0%0/3
A and B
A= defined $path->subfieldsB= $def
ABresult
0X0
100
111
480 0 ▶ my $sfdef = $def->{subfields} || {};
Condition0%0/2
$def->{'subfields'} || {}
l!l
00
MC/DC0%0/1
$def->{'subfields'} || {}
$def->{'subfields'}
Truth table0%0/2
A
A= $def->{'subfields'}
Aresult
11
00
481 0 for (split '', $path->subfields) {
482 0 $self->document("$id\$$_", $sfdef->{$_}, 1);
483 }
484 }
485 else {
486 0 $self->document($id, $def, 1);
487 }
488 }
489  
490 0 ▶sub document {
Subroutine document: not called CC=11 SCAR=48.8
Pod undocumented
491 0 my ($self, $id, $def, $warn) = @_;
492  
493 my $writer
494 0 ▶ = pica_writer('plain', color => ($self->{color} ? \%COLORS : undef));
Branch: 1 branch
BranchTrueFalseTotal
$self->{'color'} ? : 0 0 0
495  
496 0 ▶ if ($def) {
Branch: 2 branches
BranchTrueFalseTotal
if ($def) { } 0 0 0
elsif (not $self->{'unknown'}) { } 0 0 0
497 0 my $status = ' ';
498 0 ▶ if ($def->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{'required'}) { } 0 0 0
499 0 ▶ $status = $def->{repeatable} ? '+' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$def->{'repeatable'} ? : 0 0 0
500 }
501 else {
502 0 ▶ $status = $def->{repeatable} ? '*' : 'o';
Branch: 1 branch
BranchTrueFalseTotal
$def->{'repeatable'} ? : 0 0 0
503 }
504  
505 0 my ($field, $subfield) = split '\$', $id;
506 0 $writer->write_identifier([split '/', $field]);
507 0 ▶ if (defined $subfield) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subfield) 0 0 0
508 0 $writer->write_subfield($subfield, '');
509 }
510 0 print "\t";
511 0 ▶ print $self->{color} ? colored($status, $COLORS{value}) : $status;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'color'} ? : 0 0 0
512  
513 0 ▶ my $doc = "\t" . $def->{label} // '';
Condition0%0/2
"\t" . $def->{"label"} // ""
l!l
00
MC/DC0%0/1
"\t" . $def->{"label"} // ""
"\t" . $def->{"label"}
Truth table0%0/2
A
A= "\t" . $def->{"label"}
Aresult
11
00
514 0 utf8::decode($doc);
515 0 say $doc =~ s/[\s\r\n]+/ /mgr;
516 }
517 elsif (!$self->{unknown}) {
518 0 ▶ if ($warn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn) { } 0 0 0
519 0 warn "$id\t?\n";
520 }
521 else {
522 0 say $id;
523 }
524 }
525 }
526  
527 =head1 NAME
528  
529 App::picadata - Implementation of picadata command line application.
530  
531 =head1 DESCRIPTION
532  
533 This package implements the L<picadata> command line application.
534  
535 =head1 COPYRIGHT AND LICENSE
536  
537 Copyright 2020- Jakob Voss
538  
539 This library is free software; you can redistribute it and/or modify it under
540 the same terms as Perl itself.
541  
542 =cut
543  
544 1 1;
↑ Summary PICA/Writer/PPXML.pm »