blib/lib/App/picadata.pm

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