Validator.pm

filter: Statementstmt 87.2% 158 / 181 Branchbran 62.5% 20 / 32 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 96.3% 26 / 27 Podpod 100% 2 / 2 totaltotal 84.4% 217 / 257 CC 26 SCAR 17.3
CC
26
Cov
83%
_process_validation
15.0
run
8.1
_use_plugins
4.2
CRAP 5.6
↑ Summary Validator/Utils.pm »
1 package App::MARC::Validator;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use App::MARC::Validator::Utils qw(obj_to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Data::MARC::Validator::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use IO::Barf qw(barf);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use IO::Uncompress::AnyUncompress qw($AnyUncompressError);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use List::Util 1.33 qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use MARC::Batch;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use MARC::File::XML (BinaryEncoding => 'utf8', RecordFormat => 'MARC21');
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use MARC::Validator 0.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use MARC::Validator::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Unicode::UTF8 qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 our $VERSION = 0.09;
22  
23 # Constructor.
24 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
25 7 my ($class, @params) = @_;
26  
27 # Create object.
28 7 my $self = bless {}, $class;
29  
30 # Process parameters.
31 7 set_params($self, @params);
32  
33 # Object.
34 6 return $self;
35 }
36  
37 # Run.
38 5 ▶sub run {
Subroutine run: called CC=8 SCAR=20.9
Pod documented
39 5 my $self = shift;
40  
41 # Process arguments.
42 5 $self->{'_opts'} = {
43 'd' => 0,
44 'f' => 0,
45 'h' => 0,
46 'i' => '001',
47 'l' => 0,
48 'o' => undef,
49 'p' => 0,
50 'r' => 0,
51 'u' => undef,
52 'v' => 0,
53 };
54 5 ▶ if (! getopts('dfhi:lo:pru:v', $self->{'_opts'})
Branch: 1 branch
BranchTrueFalseTotal
if (not &getopts("dfhi:lo:pru:v", $self->{"_opts"}) or $self->{"_opts"}{"h"}) 2 3 5
Condition100%3/3
not &getopts("dfhi:lo:pru:v", $self->{"_opts"}) or $self->{"_opts"}{"h"}
l!l&&r!l&&!r
113
MC/DC100%2/2
not &getopts("dfhi:lo:pru:v", $self->{"_opts"}) or $self->{"_opts"}{"h"}
not &getopts("dfhi:lo:pru:v", $self->{"_opts"}) $self->{"_opts"}{"h"}
Truth table100%3/3
not(A) or B
A= not &getopts("dfhi:lo:pru:v", $self->{"_opts"})B= $self->{"_opts"}{"h"}
ABresult
1X1
011
000
55 || $self->{'_opts'}->{'h'}) {
56  
57 2 $self->_usage;
58 2 return 1;
59 }
60 3 ▶ if (! $self->{'_opts'}->{'f'}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_opts"}{"f"} or $self->{"_opts"}{"l"}) 2 1 3
Condition66%2/3
$self->{"_opts"}{"f"} or $self->{"_opts"}{"l"}
l!l&&r!l&&!r
012
MC/DC50%1/2
$self->{"_opts"}{"f"} or $self->{"_opts"}{"l"}
$self->{"_opts"}{"f"} $self->{"_opts"}{"l"}
Truth table66%2/3
A or B
A= $self->{"_opts"}{"f"}B= $self->{"_opts"}{"l"}
ABresult
1X1
011
000
61 && ! $self->{'_opts'}->{'l'}) {
62  
63 2 ▶ if (@ARGV < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV < 1) 1 1 2
64 1 $self->_usage;
65 1 return 1;
66 }
67 1 $self->{'_marc_xml_files'} = [@ARGV];
68 }
69  
70 2 my $exit_code;
71 2 ▶ if ($self->{'_opts'}->{'l'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_opts'}{'l'}) { } 1 1 2
elsif ($self->{'_opts'}{'f'}) { } 0 1 1
72 1 $exit_code = $self->_list_plugins;
73 } elsif ($self->{'_opts'}->{'f'}) {
74 0 $exit_code = $self->_list_filter_plugins;
75 } else {
76 1 $exit_code = $self->_process_validation;
77 }
78  
79 2 return $exit_code;
80 }
81  
82 0 ▶sub _list_filter_plugins {
Subroutine _list_filter_plugins: not called CC=1 SCAR=6.9
83 0 my $self = shift;
84  
85 0 my @plugins = MARC::Validator::Filter::plugins;
86  
87 0 print "List of filter plugins:\n";
88 0 print map { '- '.$_ } join "\n- ", @plugins;
89 0 print "\n";
90  
91 0 return 0;
92 }
93  
94 1 ▶sub _init_plugins {
Subroutine _init_plugins: called CC=4 SCAR=13.9
95 1 my $self = shift;
96  
97 # Get plugins to use.
98 1 my @use_plugins = $self->_use_plugins;
99  
100 1 $self->{'_plugins'} = [];
101 1 foreach my $plugin (MARC::Validator::plugins) {
102 12 ▶ if (@use_plugins && none { $plugin eq $_ } @use_plugins) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (@use_plugins and &none(sub { $plugin eq $_; } , @use_plugins)) 11 1 12
Condition66%2/3
@use_plugins and &none(sub { $plugin eq $_; } , @use_plugins)
!ll&&!rl&&r
0111
MC/DC50%1/2
@use_plugins and &none(sub { $plugin eq $_; } , @use_plugins)
@use_plugins &none(sub { $plugin eq $_; } , @use_plugins)
Truth table66%2/3
A and B
A= @use_pluginsB= &none(sub { $plugin eq $_; } , @use_plugins)
ABresult
0X0
100
111
103 11 next;
104 }
105 my $plugin_obj = $plugin->new(
106 'debug' => $self->{'_opts'}->{'d'},
107 'record_id_def' => $self->{'_opts'}->{'i'},
108 'recommendation' => $self->{'_opts'}->{'r'},
109 1 'verbose' => $self->{'_opts'}->{'v'},
110 );
111 1 $plugin_obj->init;
112 1 push @{$self->{'_plugins'}}, $plugin_obj;
113 }
114  
115 1 return;
116 }
117  
118 1 ▶sub _list_plugins {
Subroutine _list_plugins: called CC=1 SCAR=0.0
119 1 my $self = shift;
120  
121 1 my @plugins = MARC::Validator::plugins;
122  
123 1 print "List of plugins:\n";
124 1 print map { '- '.$_ } join "\n- ", @plugins;
125 1 print "\n";
126  
127 1 return 0;
128 }
129  
130 1 ▶sub _open_marc_input {
Subroutine _open_marc_input: called CC=2 SCAR=7.8
131 1 my ($self, $path, $fh_sr, $errno_sr) = @_;
132  
133 # Compression autodetection.
134 1 ${$fh_sr} = IO::Uncompress::AnyUncompress->new($path);
135 1 ▶ if (defined ${$fh_sr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${$fh_sr;}) 1 0 1
136 1 return 0;
137 }
138 0 ${$errno_sr} = $AnyUncompressError;
139  
140 0 return 1;
141 }
142  
143 1 ▶sub _process_validation {
Subroutine _process_validation: called CC=11 SCAR=27.1
144 1 my $self = shift;
145  
146 1 $self->_init_plugins;
147 1 foreach my $marc_xml_file (@{$self->{'_marc_xml_files'}}) {
148 1 my ($fh, $errno);
149 1 ▶ if ($self->_open_marc_input($marc_xml_file, \$fh, \$errno)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_open_marc_input($marc_xml_file, \$fh, \$errno)) 0 1 1
150 0 print STDERR "Cannot open file '$marc_xml_file'.";
151 0 ▶ if (defined $errno) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $errno) 0 0 0
152 0 print STDERR "\tErrno: $errno\n";
153 }
154 0 return 1;
155 }
156 1 my $marc_batch = eval {
157 1 MARC::Batch->new('XML', $fh);
158 };
159 1 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::MARC::Validator::EVAL_ERROR) 0 1 1
160 0 print STDERR "Cannot open MARC XML stream.\n";
161 0 print STDERR "\tError: $EVAL_ERROR\n";
162 0 return 1;
163 }
164  
165 # XXX strict mode returns undef in case of warning.
166 1 $marc_batch->strict_off;
167  
168 # Don't print warnings on stdout.
169 1 $marc_batch->warnings_off;
170  
171 1 my $num = 0;
172 1 my $previous_record;
173 1 while (1) {
174 2 $num++;
175 2 my $record = eval {
176 2 $marc_batch->next;
177 };
178 2 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::MARC::Validator::EVAL_ERROR) 0 2 2
179 0 ▶ print STDERR "Cannot process file '$marc_xml_file', record '$num'.".
Branch: 1 branch
BranchTrueFalseTotal
defined $previous_record ? : 0 0 0
180 (
181 defined $previous_record
182 ? "Previous record is ".encode_utf8($previous_record->title)."\n"
183 : ''
184 );
185 0 print STDERR "Error: $EVAL_ERROR\n";
186 0 next;
187 }
188 2 ▶ if (! defined $record) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $record) 1 1 2
189 1 last;
190 }
191 1 $previous_record = $record;
192  
193 # Collect statistics.
194 1 foreach my $plugin_obj (@{$self->{'_plugins'}}) {
195 1 $plugin_obj->process($record);
196 }
197 }
198 }
199 1 $self->_postprocess_plugins;
200  
201 1 my @plugin_reports;
202 1 foreach my $plugin_obj (@{$self->{'_plugins'}}) {
203 1 push @plugin_reports, $plugin_obj->report;
204 }
205 1 my $report = Data::MARC::Validator::Report->new(
206 'datetime' => DateTime->now,
207 'plugins' => \@plugin_reports,
208 );
209  
210 1 my $json = obj_to_json($self, $report);
211  
212 # Save to file.
213 1 ▶ if (defined $self->{'_opts'}->{'o'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_opts'}{'o'}) { } 0 1 1
214 0 barf($self->{'_opts'}->{'o'}, encode_utf8($json));
215  
216 # Print to STDOUT.
217 } else {
218 1 print encode_utf8($json);
219 }
220  
221 1 return 0;
222 }
223  
224 1 ▶sub _postprocess_plugins {
Subroutine _postprocess_plugins: called CC=2 SCAR=6.9
225 1 my $self = shift;
226  
227 1 foreach my $plugin_obj (@{$self->{'_plugins'}}) {
228 1 $plugin_obj->postprocess;
229 }
230  
231 1 return;
232 }
233  
234 3 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
235 3 my $self = shift;
236  
237 3 print STDERR "Usage: $0 [-d] [-f] [-h] [-i id] [-l] [-o output_file] [-p] [-r] [-u use_string] [-v] [--version] marc_xml_file..\n";
238 3 print STDERR "\t-d\t\tDebug mode.\n";
239 3 print STDERR "\t-f\t\tList of filter plugins.\n";
240 3 print STDERR "\t-h\t\tPrint help.\n";
241 3 print STDERR "\t-i id\t\tRecord identifier (default value is 001).\n";
242 3 print STDERR "\t-l\t\tList of plugins.\n";
243 3 print STDERR "\t-o output_file\tOutput file (default is STDOUT).\n";
244 3 print STDERR "\t-p\t\tPretty print JSON output.\n";
245 3 print STDERR "\t-r\t\tRecommendations.\n";
246 3 print STDERR "\t-u use_string\tUse string to prefer plugin or filter (default situation is use all).\n";
247 3 print STDERR "\t\t\te.g. plugin:MARC::Validator::Plugin::Field008\n";
248 3 print STDERR "\t-v\t\tVerbose mode.\n";
249 3 print STDERR "\t--version\tPrint version.\n";
250 3 print STDERR "\tmarc_xml_file..\tMARC XML file(s).\n";
251  
252 3 return;
253 }
254  
255 1 ▶sub _use_plugins {
Subroutine _use_plugins: called CC=4 SCAR=14.2
256 1 my $self = shift;
257  
258 1 ▶ if (! defined $self->{'_opts'}->{'u'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_opts"}{"u"}) 0 1 1
259 0 return ();
260 }
261 1 my @use_options = split m/,/, $self->{'_opts'}->{'u'};
262 1 my @use_plugins;
263 1 foreach my $use_option (@use_options) {
264 1 my ($type, $name) = split m/:/ms, $use_option, 2;
265 1 ▶ if ($type eq 'plugin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "plugin") 1 0 1
266 1 push @use_plugins, $name;
267 }
268 }
269  
270 1 return @use_plugins;
271 }
272  
273 4 1;
274  
275 __END__
↑ Summary Validator/Utils.pm »