lib/Weather/GHCN/Options.pm

filter: Statementstmt 53.5% 113 / 211 Branchbran 26.0% 23 / 100 Conditioncond 16.9% 9 / 53 MC/DCmcdc 5.7% 2 / 35 Subroutinesub 66.6% 14 / 21 Podpod 100% 11 / 11 totaltotal 40.6% 172 / 431 CC 78 SCAR 58.4
CC
78
Cov
41%
validate
975.9
initialize
156.0
options_as_string
30.0
CRAP 343.8
« blib/lib/Weather/GHCN/App/Fetch.pm ↑ Summary lib/Weather/GHCN/StationTable.pm »
1 # Weather::GHCN::Options.pm - class for GHCN options
2
3 ## no critic (Documentation::RequirePodAtEnd)
4
5 =head1 NAME
6
7 Weather::GHCN::Options - create and manage option lists/objects used by GHCN modules and scripts
8  
9 =head1 VERSION
10  
11 version v0.0.002
12
13 =head1 SYNOPSIS
14
15 use Weather::GHCN::Options;
16
17
18 =head1 DESCRIPTION
19
20 The B<Weather::GHCN::Options> module provides a class and methods that are
21 used within GHCN modules or from application scripts that use GHCN
22 modules to create and manage options that determine the behaviour of
23 GHCN methods.
24
25 The module is primarily for use by module Weather::GHCN::StationTable.
26
27 =cut
28
29 4 ▶use v5.18; # minimum for Object::Pad
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use Object::Pad 0.66 qw( :experimental(init_expr) );
Subroutine BEGIN: called CC=1 SCAR=0.0
31
32 package Weather::GHCN::Options;
33 class Weather::GHCN::Options;
34
35 4 our $VERSION = 'v0.0.002';
36
37 4 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Hash::Wrap {-lvalue => 1, -defined => 1, -as => '_wrap_hash'};
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use Text::Abbrev;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use Weather::GHCN::CountryCodes qw( search_country );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use Weather::GHCN::Common qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
44
45 ######################################################################
46 # Constants
47 ######################################################################
48
49 4 const my $SPACE => q( );
50 4 const my $EMPTY => q();
51 4 const my $DASH => q(-);
52 4 const my $NEWLINE => qq(\n);
53
54 4 const my $ALIAS_NAME_RE => qr{ \A [_]?[[:lower:]]+ \Z }xms;
55
56 =head1 METHODS
57
58 =head2 new
59
60 Create a new Options object.
61
62 =cut
63
64 ######################################################################
65 # Set up the default Tk::Getopt option table, which we will use for
66 # both Tk::Getopt and to derive an options list of Getopt::Long
67 # for when Tk::Getopt is not installed.
68 ######################################################################
69
70 4 my $Tk_opt_table = [
71 'Basic options',
72 ['location', '=s', undef, label => 'Filter stations by their location name (regex)'],
73 ['state', '=s', undef, label => 'Filter stations by state or province',
74 alias => ['province'] ],
75 ['country', '=s', undef, label => 'Filter station by the country they are in'],
76 ['gsn', '!', undef, label => 'Include only GSN reference stations'],
77 ['', '', '-'],
78 ['report', '=s', '',
79 label => 'Type of report',
80 strict => 1,
81 choices => [
82 [ 'station list', '' ],
83 [ 'yearly summary', 'yearly' ],
84 [ 'monthly summary', 'monthly' ],
85 [ 'daily summary', 'daily' ],
86 [ 'station (id) level', 'id' ],
87 ]
88 ],
89 ['', '', '-'],
90 ['nonetwork', '=i', -1,
91 help => 'Set the NoNetwork option in URI::Fetch',
92 label => 'Cache refresh option',
93 choices => [
94 ['Refresh if stale this year', -1 ], ## no critic [ProhibitMagicNumbers]
95 ['Check for fresher copy', 0 ],
96 [q(Don't refresh cache), 1 ],
97 ],
98 ],
99 ['performance', '!', undef, label => 'Report performance statistics'],
100 ['verbose', '!', undef, label => 'Print information messages'],
101 ['dataonly', '!', undef, label => 'Only print the data table'],
102 ['config', '=s', undef, label => 'Configuration options (for testing only)', nogui => 1],
103
104 'Date filters',
105 ['range', '=s', undef, label => 'Filter selected station data by year range'],
106 ['active', '=s', undef, label => 'Filter stations by their active year range'],
107 ['partial', '!', undef, label => 'Allow stations only active for part of the active range'],
108 ['quality', '=i', 90, label => 'Quality threshold (percent as an integer)'],
109 ['', '','-'],
110 ['fday', '=s', undef, label => 'Filter output to include a specific day'],
111 ['fmonth', '=s', undef, label => 'Filter output to include a specific month'],
112
113 'GIS filters',
114 ['gps', '=s', undef, label => 'Filter stations by latitude and longitude',
115 help => 'Enter decimal latitude and longitude'],
116 ['radius', '=i', 50, label => 'Radius to search for stations near coordinates'],
117
118
119 'Analysis Options',
120 ['anomalies', '!', undef, label => 'Provide calculated anomalies in the output'],
121 ['baseline', '=s', '1971-2000',
122 label => 'Baseline year range'],
123 ['precip', '!', undef, label => 'Include precipitation stats in the results'],
124 ['tavg', '!', undef, label => 'Include TAVG in the results'],
125 ['nogaps', '!', undef, label => 'Emit extra rows for missing months or days'],
126
127 'KML Output',
128 ['kml', '=s', undef, label => 'Export station coordinates to a KML file'],
129 ['color', '=s', 'red', label => 'Color to use for KML placemarks',
130 alias => ['colour'] ],
131 ['label', '!', undef, label => 'Label KML placemarks'],
132 ];
133
134 ######################################################################
135 # Class fields
136 ######################################################################
137
138 0 ▶field $opt_href :mutator; # a hashref of merged options (with default values applied))
Subroutine opt_href: not called CC=1 SCAR=2.6
Pod documented
139 0 ▶field $opt_obj :mutator; # a Hash::Wrap object derived from $opt_href
Subroutine opt_obj: not called CC=1 SCAR=2.6
Pod documented
140 0 ▶field $config_href :mutator; # a hash containing config file options
Subroutine config_href: not called CC=1 SCAR=2.6
Pod documented
141 4 field $tk_opt_aref; # the Tk:Getopt array that defines all GHCN options
142
143 =head1 FIELD ACCESSORS
144
145 Writeable (mutator) access is provided for some fields primarily so
146 an Options object can be tested independantly from StationTable.
147 In general, an Options object is set by the StationTable set_options
148 method and should not be modified directly by the consuming application
149 using these mutators.
150
151 =over 4
152
153 =item opt_href
154
155 This writable field is set by StationTable->set_options and is a
156 hashref of user options merged with default values.
157
158 For programmatic access to option values, use of B<opt_obj> is
159 preferred to prevent mispellings. (See B<opt_obj>.)
160
161 =item opt_obj
162
163 This writable field is set by StationTable->set_options and is a
164 Hash::Wrap object composed from the B<opt_href> hashref field. It
165 provides accessor field methods for user options (merged with default
166 values).
167
168 Using this object, rather than B<opt_href>, for access to option
169 values is safer programming choice as any misspelling of an option
170 name will result in a run time error. In contrast, mispelling a hash
171 key will simply result in an undef being returned.
172
173 =item config_href
174
175 This writable field is set by StationTable->set_options and contains
176 the configuration options it was given.
177
178 =back
179
180 =cut
181
182 ######################################################################
183 =head1 CLASS METHODS
184
185 The following class methods are supported. Class Options uses
186 Object::Perl, so these class methods (specified using the :common
187 method attribute) should be accessed using -> not :: because ->
188 will shift off the $class argument and :: won't.
189
190 =head2 get_tk_options_table
191
192 Returns: @tk_opttable or \@tk_opttable
193
194 Provides access to the predefined TK::Getopt options list that define
195 the Getopt::Long arguments supported by class StationTable for user
196 options.
197
198 The table is a list of lists and strings. The strings define
199 sections that Tk::Getopt renders as panels or tabs in the GUI it
200 constructs. The lists contain option names and types (in Getopt::Long
201 style) as well as default values, aliases, and labels to be displayed
202 in the GUI, choices for multi-select options, and other extensions.
203 See Tk::Getopt OPTTABLE ARGUMENTS for details.
204
205 =cut
206
207 1 ▶method get_tk_options_table :common () {
Subroutine get_tk_options_table: called CC=2 SCAR=7.0
Pod documented
208 1 ▶ return wantarray ? ( $Tk_opt_table->@* ) : $Tk_opt_table;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
209 4 }
210
211 =head2 get_getopt_list
212
213 Returns: @options or \@options
214
215 In scalar context, return a list reference to a translation of the
216 TK::Getopt options list into the simpler list used by Getopt::Long.
217 This gives application authors a choice between using Tk::Getopt and
218 the non-GUI and more traditional Getopt::Long.
219
220 In list context, this method returns a Getopt::Long-style list
221 options.
222
223 Typically, this method would be called prior to Getopt::Long in order
224 to obtain an options list for using the StationTable class; e.g.
225
226 my %opt;
227 my @options = ( Weather::GHCN::Options->get_getopt_list() );
228 GetOptions( \%opt, @options);
229
230 =cut
231
232 3 ▶method get_getopt_list :common () {
Subroutine get_getopt_list: called CC=14 SCAR=26.9
Pod documented
233 ## no critic [ProhibitDoubleSigils]
234 3 my @options_text;
235 my @options_list;
236 3 my @options;
237
238 # According to https://metacpan.org/pod/Tk::Getopt -opttable
239 # should be a reference to an array containing all options.
240 # Elements of this array may be strings, which indicate the
241 # beginning of a new group, or array references describing the
242 # options. The first element of this array is the name of the
243 # option, the second is the type (=s for string, =i for integer,
244 # ! for boolean, =f for float etc., see Getopt::Long) for a
245 # detailed list. The third element is optional and contains the
246 # default value (otherwise the default is undefined).
247 # Further elements are optional too and describe more attributes. For a
248 # complete list of these attributes refer to "OPTTABLE ARGUMENTS".
249
250 3 foreach my $row ( $Tk_opt_table->@* ) {
251 102 ▶ next if ref $row ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $row ne "ARRAY" 15 87 102
252
253 # pick off the first three values, then slurp the rest
254 87 my ($opt_kw, $opt_type, $default, @others) = $row->@*;
255 # skip the group dividers
256 87 ▶ next if not $opt_kw;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt_kw 9 78 87
257
258 # now figure out whether the slurped values are a hash of
259 # other options (including label) or just a pair of scalars
260 # ('label' and label value with no other options).
261 78 my %h;
262 78 ▶ if (@others > 1 && ref $others[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@others > 1 and ref $others[0] eq 'HASH') { } 0 78 78
elsif (@others > 1 and $others[0] eq 'label') { } 75 3 78
Condition33%1/3
@others > 1 and ref $others[0] eq "HASH"
!ll&&!rl&&r
0780
MC/DC0%0/2
@others > 1 and ref $others[0] eq "HASH"
@others > 1 ref $others[0] eq "HASH"
Truth table33%1/3
A and B
A= @others > 1B= ref $others[0] eq "HASH"
ABresult
0X0
100
111
263 0 %h = @others;
264 }
265 ▶ elsif (@others > 1 && $others[0] eq 'label') {
Condition66%2/3
@others > 1 and $others[0] eq "label"
!ll&&!rl&&r
0375
MC/DC50%1/2
@others > 1 and $others[0] eq "label"
@others > 1 $others[0] eq "label"
Truth table66%2/3
A and B
A= @others > 1B= $others[0] eq "label"
ABresult
0X0
100
111
266 75 $h{'label'} = $others[2];
267 }
268
269 78 ▶ my $label = $h{'label'} // $SPACE;
Condition66%2/3
$h{"label"} // $SPACE
l!l&&r!l&&!r
15630
MC/DC0%0/2
$h{"label"} // $SPACE
$h{"label"} $SPACE
Truth table66%2/3
A // B
A= $h{"label"}B= $SPACE
ABresult
1X1
011
000
270 78 ▶ my $alias_aref = $h{'alias'} // [];
Condition50%1/2
$h{"alias"} // []
l!l
078
MC/DC0%0/1
$h{"alias"} // []
$h{"alias"}
Truth table50%1/2
A
A= $h{"alias"}
Aresult
11
00
271 78 my $opt_kw_with_aliases = join '|', $opt_kw, $alias_aref->@*;
272
273 78 push @options_list, $opt_kw_with_aliases . $opt_type;
274 78 push @options, [$opt_kw_with_aliases, $opt_type, $label];
275 }
276
277 # calculate the width of the option spec column so the labels,
278 # which we print as comment in the text output, will line up
279
280 3 my $colwidth = 0;
281 3 foreach my $opt_aref (@options) {
282 78 my ($opt_kw_with_aliases, $opt_type, $label) = $opt_aref->@*;
283 78 my $len = length( q(') . $opt_kw_with_aliases . $opt_type . q(', ) );
284 78 ▶ $colwidth = $len if $len > $colwidth;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $colwidth 9 69 78
285 }
286
287 3 my $fmt = sprintf '%%-%ds', $colwidth;
288
289 3 foreach my $opt_aref (@options) {
290 78 my ($opt_kw_with_aliases, $opt_type, $label) = $opt_aref->@*;
291 78 my $kw = sprintf $fmt, q(') . $opt_kw_with_aliases . $opt_type . q(',);
292 78 push @options_text, $kw . '# ' . $label;
293 }
294
295 3 ▶ return wantarray ? ( @options_list ) : join $NEWLINE, sort @options_text;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
296 4 }
297
298
299 =head2 get_option_choices ( $option )
300
301 Returns: \%choices
302
303 Find all the options which have a multiple choice response, and return
304 a hash keyed on the option name and with a values consisting
305 of a hash of the valid responses as value/label pairs.
306
307 =cut
308
309 1 ▶method get_option_choices :common () {
Subroutine get_option_choices: called CC=10 SCAR=23.8
Pod documented
310 1 my %choices;
311
312 1 foreach my $row ( $Tk_opt_table->@* ) {
313 34 ▶ next if ref $row ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $row ne "ARRAY" 5 29 34
314
315 # pick off the first three values, then slurp the rest
316 29 my ($opt_kw, $opt_type, $default, @others) = $row->@*;
317 # skip the group dividers
318 29 ▶ next if not $opt_kw;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt_kw 3 26 29
319
320 26 my $href;
321 26 ▶ if (@others and ref $others[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@others and ref $others[0] eq 'HASH') { } 0 26 26
elsif (@others % 2 == 0) { } 26 0 26
Condition33%1/3
@others and ref $others[0] eq "HASH"
!ll&&!rl&&r
0260
MC/DC0%0/2
@others and ref $others[0] eq "HASH"
@others ref $others[0] eq "HASH"
Truth table33%1/3
A and B
A= @othersB= ref $others[0] eq "HASH"
ABresult
0X0
100
111
322 0 $href = $others[0];
323 } elsif (@others % 2 == 0) {
324 26 $href = { @others };
325 } else {
326 0 croak "*E* unable to parse opttable: @others";
327 }
328
329 26 my %hv;
330 26 ▶ if ( $href->{'choices'} and ref $href->{'choices'} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href->{"choices"} and ref $href->{"choices"} eq "ARRAY") 2 24 26
Condition66%2/3
$href->{"choices"} and ref $href->{"choices"} eq "ARRAY"
!ll&&!rl&&r
2402
MC/DC50%1/2
$href->{"choices"} and ref $href->{"choices"} eq "ARRAY"
$href->{"choices"} ref $href->{"choices"} eq "ARRAY"
Truth table66%2/3
A and B
A= $href->{"choices"}B= ref $href->{"choices"} eq "ARRAY"
ABresult
0X0
100
111
331 2 foreach my $aref ( $href->{'choices'}->@* ) {
332 8 $hv{ $aref->[1] } = $aref->[0];
333 }
334 2 $choices{$opt_kw} = \%hv;
335 }
336 }
337
338 1 return \%choices;
339 4 }
340
341 =head2 get_option_defaults
342
343 Returns: \%defaults
344
345 Returns the option defaults, obtained from the same predefined list
346 of lists/strings returned by get_tk_options_table.
347
348 =cut
349
350 4 ▶method get_option_defaults :common () {
Subroutine get_option_defaults: called CC=4 SCAR=13.9
Pod documented
351
352 1 my %defaults = ();
353 1 foreach my $slot ($Tk_opt_table->@*) {
354 34 ▶ next if ref $slot ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $slot ne "ARRAY" 5 29 34
355 29 my $key = $slot->[0];
356 29 ▶ next if not $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 3 26 29
357 26 my $default_value = $slot->[2];
358 26 $defaults{$key} = $default_value;
359 }
360
361 1 return \%defaults;
362 4 }
363
364
365 ######################################################################
366 =head1 INSTANCE METHODS
367
368 =over 4
369
370 =item combine_options ($user_opt_href)
371
372 Returns: ($opt_href, $opt_obj)
373
374 This method takes a hash reference containing user options, merges it
375 with the full set of supported options, and applies any necessary
376 default values. The end result is a complete set of all the options
377 supported by Weather::GHCN::StationTable with user-specified options taking
378 precedence over all other.
379
380 This set of options is returned as both a hash reference and as a
381 Hash::Wrap object. The latter is preferred for use by consuming
382 applications, because it provides accessor methods for each option.
383 In addition, an ->defined( "<option>" ) method is provided so that
384 your code can determine whether an option value was set to B<undef>.
385
386 The advantage to using an option object rather than an option hash
387 is that a misspelled option name will cause a runtime error.
388
389 =back
390
391 =cut
392
393 3 ▶method combine_options ($user_opt_href) {
Subroutine combine_options: called CC=3 SCAR=16.9
Pod documented
394 # assign the class-level tk_options_table aref, generated before BUILD, to the instance field
395 3 $tk_opt_aref = $Tk_opt_table;
396
397 3 my $defaults_href = get_option_defaults();
398
399 0 my %merged_options;
400 0 ▶ while ( my ($k,$v) = each $defaults_href->%* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$defaults_href) 0 0 0
401 0 ▶ $merged_options{$k} = $user_opt_href->{$k} // $v;
Condition0%0/3
$user_opt_href->{$k} // $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$user_opt_href->{$k} // $v
$user_opt_href->{$k} $v
Truth table0%0/3
A // B
A= $user_opt_href->{$k}B= $v
ABresult
1X1
011
000
402 }
403
404 0 $opt_href = \%merged_options;
405 0 $opt_obj = _wrap_hash \%merged_options;
406
407 0 return ($opt_href, $opt_obj);
408 4 }
409
410 =head2 initialize
411
412 Returns: @errors
413
414 This method initialize various user and configuration options that
415 can't simply be initialized by constants. Specifically:
416
417 =over 4
418
419 =item Aliases
420
421 Alias entries defined in configuration options are matched against
422 the -location option value. If a match is found to the alias name,
423 the alias value is substituted for the location value.
424
425 Alias names must be lowercase letters only. An optional underscore
426 prefix is permitted. Names not matching this rule will be silently
427 ignored by initialize().
428
429 =item country
430
431 The B<country> option value can be:
432
433 * a 2-character GEC (FIPS) country code
434
435 * a 3-character alpha ISO 3166 country code
436
437 * a 3-digit numeric ISO 3166 country number
438
439 * an internet domain country suffix (e.g. '.ca')
440
441 * a 3-character regex string
442
443 If a regex string is given, then it will be matched (unanchored and
444 case insensitve) against country names. If multiple matches are
445 found, then an error is returned and the user will need to provide a
446 more specific pattern.
447
448 =item active
449
450 The B<active> option filters stations according to the years that
451 they were active. If the B<range> option is specified, but the
452 B<active> option is not, then B<initialize> will set the B<active>
453 option value to the B<range> option value so that only stations that
454 were active during the requested data range will be selected.
455
456 =item quality
457
458 The B<quality> option determines whether a station's data will be
459 included in the output when it has missing data. Quality is
460 expressed as a number between 0 and 100, representing the percentage
461 of data that cannot be missing; 90% is the default For example, if
462 you have a range of 3 years (1095 days) when B<quality> is 90, then
463 you need 90% x 1095 = 985 days of data. Anything less and the
464 station is rejected.
465
466 When filters fmonth and fday are used, the amount of data included
467 will typically drop far below 90% thereby rejecting all stations.
468 To avoid this nuisance, B<initialize> will set quality to 0% if
469 either the B<fmonth> or B<fday> options are present.
470
471 =back
472
473 =cut
474
475 0 ▶method initialize () {
Subroutine initialize: not called CC=12 SCAR=50.5
Pod documented
476 0 my @errors;
477
478 # substitute any aliases found in -location string into their station id's
479 0 ▶ if ( $opt_obj->location and $config_href->{'aliases'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->location and $config_href->{"aliases"}) 0 0 0
Condition0%0/3
$opt_obj->location and $config_href->{"aliases"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_obj->location and $config_href->{"aliases"}
$opt_obj->location $config_href->{"aliases"}
Truth table0%0/3
A and B
A= $opt_obj->locationB= $config_href->{"aliases"}
ABresult
0X0
100
111
480 0 ▶ if ( $opt_obj->location =~ m{ \A [_]?[[:lower:]]+ \Z }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->location =~ / \A [_]?[[:lower:]]+ \Z /msux) 0 0 0
481 0 ▶ while ( my ($k,$v) = each $config_href->{'aliases'}->%* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$config_href{"aliases"}}) 0 0 0
482 0 $opt_obj->location =~ s{$k}{$v}xms;
483 }
484 }
485 }
486
487 0 ▶ if ( $opt_obj->country ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->country) 0 0 0
488 # using undef as the search type so it will figure it out based
489 # on the value pattern and length
490 0 my @cou = search_country( $opt_obj->country, undef );
491
492 0 ▶ push @errors, '*E* unrecognized country code or name'
Branch: 1 branch
BranchTrueFalseTotal
unless @cou 0 0 0
493 if not @cou;
494
495 0 ▶ push @errors, '*E* ambiguous country code or name'
Branch: 1 branch
BranchTrueFalseTotal
if @cou > 1 0 0 0
496 if @cou > 1;
497
498 # return the GEC (FIPS) country code, which is what GHCN uses
499 0 $opt_obj->country = $cou[0]->{gec};
500 }
501
502 # default the station active range to the year filter range if its value is an empty string
503 0 ▶ if ( $opt_obj->defined('active') and $opt_obj->active eq $EMPTY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->defined("active") and $opt_obj->active eq $EMPTY) 0 0 0
Condition0%0/3
$opt_obj->defined("active") and $opt_obj->active eq $EMPTY
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_obj->defined("active") and $opt_obj->active eq $EMPTY
$opt_obj->defined("active") $opt_obj->active eq $EMPTY
Truth table0%0/3
A and B
A= $opt_obj->defined("active")B= $opt_obj->active eq $EMPTY
ABresult
0X0
100
111
504 0 $opt_obj->active = $opt_obj->range;
505 }
506
507 0 ▶ $opt_obj->quality = 0
Branch: 1 branch
BranchTrueFalseTotal
if $opt_obj->fmonth or $opt_obj->fday 0 0 0
Condition0%0/3
$opt_obj->fmonth or $opt_obj->fday
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt_obj->fmonth or $opt_obj->fday
$opt_obj->fmonth $opt_obj->fday
Truth table0%0/3
A or B
A= $opt_obj->fmonthB= $opt_obj->fday
ABresult
1X1
011
000
508 if $opt_obj->fmonth or $opt_obj->fday;
509
510 0 return @errors;
511 4 }
512
513 =head2 options_as_string
514
515 This option returns a string that contains all the options and their
516 values, in a format similar to what they would look like when entered
517 as command-line arguments. For boolean options only the option name
518 is include (no value). Option values containing whitespace are
519 enclosed in double quotes. Option/value pairs are separated by
520 two spaces.
521
522 This method is primarily provided so the consuming application can
523 print the options that were used during a run, perhaps to a log or
524 in the output.
525
526 =cut
527
528 0 ▶method options_as_string () {
Subroutine options_as_string: not called CC=5 SCAR=34.0
Pod documented
529 0 my @options;
530 0 my $boolean = _get_boolean_options($Tk_opt_table);
531
532 0 foreach my $k ( sort keys $opt_href->%* ) {
533 0 my $v = $opt_href->{$k};
534 0 ▶ next if not defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
535
536 0 ▶ if ( $boolean->{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean->{$k}) 0 0 0
537 0 push @options, $DASH . $k;
538 0 next;
539 }
540
541 0 my $val = $v;
542
543 0 ▶ if ( $val =~ m{\A \s* \Z}xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /\A \s* \Z/msux) 0 0 0
544 0 $val = q(") . $val . q(");
545 }
546 0 push @options, $DASH . $k. $SPACE . $val;
547 }
548 0 return join $SPACE x 2, @options;
549 4 }
550
551 =head2 validate
552
553 Returns: @errors
554
555 This method is called by StationTable->set_options to make sure all
556 the options and configuration values that were provided to
557 B<set_options> are valid. It also handles abbreviations for options
558 color and report. Any errors arising from invalid value or from
559 problems detected during B<intialize> (which is called at the end of
560 B<validate>) are retuned in a list.
561
562 =cut
563
564 0 ▶method validate () {
Subroutine validate: not called CC=32 SCAR=68.8
Pod documented
565 0 my @errors;
566 0 my $bad_range_cnt = 0;
567
568 0 ▶ if ( $config_href->{aliases} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_href->{"aliases"}) 0 0 0
569 0 foreach my $alias_name ( keys $config_href->{aliases}->%* ) {
570 0 my $errmsg = '*E* alias names in configuration must be lowercase letters with optional underscore prefix: ' . $alias_name;
571 0 ▶ push @errors, $errmsg
Branch: 1 branch
BranchTrueFalseTotal
unless $alias_name =~ /$ALIAS_NAME_RE/u 0 0 0
572 unless $alias_name =~ $ALIAS_NAME_RE;
573 }
574 }
575
576 0 ▶ if ( $opt_obj->active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->active) 0 0 0
577 0 ▶ if ( not $opt_obj->active =~ m{ \A (18|19|20)\d\d [-] (18|19|20)\d\d }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_obj->active =~ / \A (18|19|20)\d\d [-] (18|19|20)\d\d /msux) 0 0 0
578 0 push @errors, '*E* invalid -active year range ' . $opt_obj->active;
579 0 $bad_range_cnt++;
580 }
581 }
582
583 0 ▶ if ( $opt_obj->range ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->range) 0 0 0
584 0 ▶ if ( not $opt_obj->range =~ m{ \A (18|19|20)\d\d [-,] (18|19|20)\d\d }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_obj->range =~ / \A (18|19|20)\d\d [-,] (18|19|20)\d\d /msux) 0 0 0
585 0 push @errors, '*E* invalid -range ' . $opt_obj->range;
586 0 $bad_range_cnt++;
587 }
588 }
589
590 0 ▶ push @errors, '*E* invalid 2-character state or province code ' . $opt_obj->state
Branch: 1 branch
BranchTrueFalseTotal
if $opt_obj->defined("state") and not $opt_obj->state =~ / \A [[:alpha:]]{2} \Z /msux 0 0 0
Condition0%0/3
$opt_obj->defined("state") and not $opt_obj->state =~ / \A [[:alpha:]]{2} \Z /msux
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_obj->defined("state") and not $opt_obj->state =~ / \A [[:alpha:]]{2} \Z /msux
$opt_obj->defined("state") not $opt_obj->state =~ / \A [[:alpha:]]{2} \Z /msux
Truth table0%0/3
A and not(B)
A= $opt_obj->defined("state")B= not $opt_obj->state =~ / \A [[:alpha:]]{2} \Z /msux
ABresult
0X0
100
111
591 if $opt_obj->defined('state') and not $opt_obj->state =~ m{ \A [[:alpha:]]{2} \Z }xms;
592
593 0 ▶ push @errors, '*E* -partial only allowed if -active specified'
Branch: 1 branch
BranchTrueFalseTotal
if $opt_obj->partial and not $opt_obj->defined("active") 0 0 0
Condition0%0/3
$opt_obj->partial and not $opt_obj->defined("active")
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_obj->partial and not $opt_obj->defined("active")
$opt_obj->partial not $opt_obj->defined("active")
Truth table0%0/3
A and not(B)
A= $opt_obj->partialB= not $opt_obj->defined("active")
ABresult
0X0
100
111
594 if $opt_obj->partial and not $opt_obj->defined('active');
595
596 # Note: full Condition Coverage in Devel::Cover seems impossible if these two ifs are combined
597 # (I tried every combination of uncoverable branch and condition I could think of to
598 # to suppress the missing case. In the end, this was the only thing that worked.)
599 0 ▶ if ( $opt_obj->range and $opt_obj->active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->range and $opt_obj->active) 0 0 0
Condition0%0/3
$opt_obj->range and $opt_obj->active
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_obj->range and $opt_obj->active
$opt_obj->range $opt_obj->active
Truth table0%0/3
A and B
A= $opt_obj->rangeB= $opt_obj->active
ABresult
0X0
100
111
600 # uncoverable branch false
601 0 ▶ if ( $bad_range_cnt == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad_range_cnt == 0) 0 0 0
602 0 my $r = rng_new( $opt_obj->range );
603 0 my $a = rng_new( $opt_obj->active );
604
605 0 ▶ push @errors, '*E* -range must be a subset of -active'
Branch: 1 branch
BranchTrueFalseTotal
unless $r->subset($a) 0 0 0
606 if not $r->subset($a);
607 }
608 }
609
610 0 ▶ push @errors, '*E* -gps argument must be decimal lat/long, separated by spaces or punctuation'
Branch: 1 branch
BranchTrueFalseTotal
if $opt_obj->gps and not $opt_obj->gps =~ / \A [+-]? \d{1,3} [.] \d+ ( [[:punct:]] | \s+ ) [+-]? \d{1,3} [.] \d+ \Z /msux 0 0 0
Condition0%0/3
$opt_obj->gps and not $opt_obj->gps =~ / \A [+-]? \d{1,3} [.] \d+ ( [[:punct:]] | \s+ ) [+-]? \d{1,3} [.] \d+ \Z /msux
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_obj->gps and not $opt_obj->gps =~ / \A [+-]? \d{1,3} [.] \d+ ( [[:punct:]] | \s+ ) [+-]? \d{1,3} [.] \d+ \Z /msux
$opt_obj->gps not $opt_obj->gps =~ / \A [+-]? \d{1,3} [.] \d+ ( [[:punct:]] | \s+ ) [+-]? \d{1,3} [.] \d+ \Z /msux
Truth table0%0/3
A and not(B)
A= $opt_obj->gpsB= not $opt_obj->gps =~ / \A [+-]? \d{1,3} [.] \d+ ( [[:punct:]] | \s+ ) [+-]? \d{1,3} [.] \d+ \Z /msux
ABresult
0X0
100
111
611 if $opt_obj->gps and $opt_obj->gps !~ m{ \A [+-]? \d{1,3} [.] \d+ ( [[:punct:]] | \s+ ) [+-]? \d{1,3} [.] \d+ \Z }xms;
612
613 #-----------------------------------------------------------------
614 # Maintenance Note
615 #-----------------------------------------------------------------
616 #
617 # ghcn_fetch.pl uses Tk::Getopt rather than Getopt::Long and as
618 # a result it does it's own validation checking of -report before
619 # the validate() method of this module gets called. Unfortunately,
620 # the error message is useless because it fails to print out the
621 # choices correctly. So, special code was written in ghcn_fetch
622 # to allow the -report option to be abbreviated, and to validate
623 # it, and to issue a proper error message when it's invalid.
624 #
625 # Consequently, by the time this code is reached, any abbreviation
626 # to $opt_obj->report has already be replaced, and validation
627 # and error reporting done.
628
629 0 my %report_abbrev = abbrev( qw(id daily monthly yearly) );
630
631 0 my $report = $opt_obj->report;
632
633 # uncoverable branch true
634 0 ▶ croak "*E* undef report type"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $report 0 0 0
635 if not defined $report;
636
637 push @errors, '*E* invalid report option: ' . $report
638 0 ▶ if $report and not $report_abbrev{ $report };
Branch: 1 branch
BranchTrueFalseTotal
if $report and not $report_abbrev{$report} 0 0 0
Condition0%0/3
$report and not $report_abbrev{$report}
!ll&&!rl&&r
000
MC/DC0%0/2
$report and not $report_abbrev{$report}
$report not $report_abbrev{$report}
Truth table0%0/3
A and not(B)
A= $reportB= not $report_abbrev{$report}
ABresult
0X0
100
111
639
640 0 $opt_obj->report = $report_abbrev{ $report };
641
642 #-----------------------------------------------------------------
643 # end of noted section
644 #-----------------------------------------------------------------
645
646 0 my %color_abbrev = abbrev( qw(blue green azure purple red white yellow) );
647
648 # uncoverable branch false
649 0 ▶ if ( $opt_obj->defined('color') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->defined("color")) 0 0 0
650 0 my $color = $opt_obj->color;
651 0 ▶ if ( $color eq $EMPTY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color eq $EMPTY) { } 0 0 0
652 0 push @errors, '*E* invalid -color value ""'
653 } else {
654 push @errors, '*E* invalid -color value'
655 0 ▶ if not $color_abbrev{ $color };
Branch: 1 branch
BranchTrueFalseTotal
unless $color_abbrev{$color} 0 0 0
656 }
657 0 $opt_obj->color = $color_abbrev{ $color };
658 }
659
660
661 0 ▶ push @errors, '*E* -label/-nolabel only allowed if -kml specified'
Branch: 1 branch
BranchTrueFalseTotal
if $opt_obj->defined("label") and not $opt_obj->defined("kml") 0 0 0
Condition0%0/3
$opt_obj->defined("label") and not $opt_obj->defined("kml")
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_obj->defined("label") and not $opt_obj->defined("kml")
$opt_obj->defined("label") not $opt_obj->defined("kml")
Truth table0%0/3
A and not(B)
A= $opt_obj->defined("label")B= not $opt_obj->defined("kml")
ABresult
0X0
100
111
662 if $opt_obj->defined('label') and not $opt_obj->defined('kml');
663
664 0 ▶ if ( $opt_obj->defined('fmonth') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->defined("fmonth")) 0 0 0
665 0 ▶ push @errors, '*E* -fmonth must be a single number or valid range spec (e.g. 1-5,9)'
Branch: 1 branch
BranchTrueFalseTotal
unless rng_valid($opt_obj->fmonth) and rng_within($opt_obj->fmonth, "1-12") 0 0 0
Condition0%0/3
rng_valid($opt_obj->fmonth) and rng_within($opt_obj->fmonth, "1-12")
!ll&&!rl&&r
000
MC/DC0%0/2
rng_valid($opt_obj->fmonth) and rng_within($opt_obj->fmonth, "1-12")
rng_valid($opt_obj->fmonth) rng_within($opt_obj->fmonth, "1-12")
Truth table0%0/3
A and B
A= rng_valid($opt_obj->fmonth)B= rng_within($opt_obj->fmonth, "1-12")
ABresult
0X0
100
111
666 if not rng_valid($opt_obj->fmonth)
667 or not rng_within($opt_obj->fmonth, '1-12');
668 }
669
670 0 ▶ if ( $opt_obj->defined('fday') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_obj->defined("fday")) 0 0 0
671 0 ▶ push @errors, '*E* -fday must be a single number or valid range spec (e.g. 3,15,20-31)'
Branch: 1 branch
BranchTrueFalseTotal
unless rng_valid($opt_obj->fday) and rng_within($opt_obj->fday, "1-31") 0 0 0
Condition0%0/3
rng_valid($opt_obj->fday) and rng_within($opt_obj->fday, "1-31")
!ll&&!rl&&r
000
MC/DC0%0/2
rng_valid($opt_obj->fday) and rng_within($opt_obj->fday, "1-31")
rng_valid($opt_obj->fday) rng_within($opt_obj->fday, "1-31")
Truth table0%0/3
A and B
A= rng_valid($opt_obj->fday)B= rng_within($opt_obj->fday, "1-31")
ABresult
0X0
100
111
672 if not rng_valid($opt_obj->fday)
673 or not rng_within($opt_obj->fday, '1-31');
674 }
675
676 0 my @init_errors = $self->initialize();
677
678 0 return (@errors, @init_errors);
679 4 }
680
681 =head2 DOES
682
683 Defined by Object::Pad. Included for POD::Coverage.
684
685 =head2 META
686
687 Defined by Object::Pad. Included for POD::Coverage.
688
689 =cut
690
691 ######################################################################
692 # Subroutines
693 ######################################################################
694
695 ▶sub _get_boolean_options ($tk_opt_aref) {
Subroutine _get_boolean_options: not called CC=4 SCAR=30.0
696
697 0 my %boolean;
698
699 0 foreach my $row ( $tk_opt_aref->@* ) {
700 0 ▶ next unless ref $row eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 0 0 0
701 0 my ($name, $type) = $row->@*;
702 0 ▶ $boolean{$name}++ if $type eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "!" 0 0 0
703 }
704
705 0 return \%boolean;
706 }
707
708
709 4 1;
« blib/lib/Weather/GHCN/App/Fetch.pm ↑ Summary lib/Weather/GHCN/StationTable.pm »