| 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 | use v5.18; # minimum for Object::Pad | ||
| 30 | use Object::Pad 0.66 qw( :experimental(init_expr) ); | ||
| 31 | |||
| 32 | package Weather::GHCN::Options; | ||
| 33 | class Weather::GHCN::Options; | ||
| 34 | |||
| 35 | our $VERSION = 'v0.0.002'; | ||
| 36 | |||
| 37 | use Carp qw(carp croak); | ||
| 38 | use Const::Fast; | ||
| 39 | use Hash::Wrap {-lvalue => 1, -defined => 1, -as => '_wrap_hash'}; | ||
| 40 | use Text::Abbrev; | ||
| 41 | use Weather::GHCN::CountryCodes qw( search_country ); | ||
| 42 | use Weather::GHCN::Common qw( :all ); | ||
| 43 | use YAML::Tiny; | ||
| 44 | |||
| 45 | ###################################################################### | ||
| 46 | # Constants | ||
| 47 | ###################################################################### | ||
| 48 | |||
| 49 | const my $SPACE => q( ); | ||
| 50 | const my $EMPTY => q(); | ||
| 51 | const my $DASH => q(-); | ||
| 52 | const my $NEWLINE => qq(\n); | ||
| 53 | |||
| 54 | 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 | 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 | field $opt_href :mutator; # a hashref of merged options (with default values applied)) | ||
| 139 | field $opt_obj :mutator; # a Hash::Wrap object derived from $opt_href | ||
| 140 | field $config_href :mutator; # a hash containing config file options | ||
| 141 | 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 | method get_tk_options_table :common () { | ||
| 208 | return wantarray ? ( $Tk_opt_table->@* ) : $Tk_opt_table; | ||
| 209 | } | ||
| 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 | method get_getopt_list :common () { | ||
| 233 | ## no critic [ProhibitDoubleSigils] | ||
| 234 | my @options_text; | ||
| 235 | my @options_list; | ||
| 236 | 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 | foreach my $row ( $Tk_opt_table->@* ) { | ||
| 251 | next if ref $row ne 'ARRAY'; | ||
| 252 | |||
| 253 | # pick off the first three values, then slurp the rest | ||
| 254 | my ($opt_kw, $opt_type, $default, @others) = $row->@*; | ||
| 255 | # skip the group dividers | ||
| 256 | next if not $opt_kw; | ||
| 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 | my %h; | ||
| 262 | if (@others > 1 && ref $others[0] eq 'HASH') { | ||
| 263 | %h = @others; | ||
| 264 | } | ||
| 265 | elsif (@others > 1 && $others[0] eq 'label') { | ||
| 266 | $h{'label'} = $others[2]; | ||
| 267 | } | ||
| 268 | |||
| 269 | my $label = $h{'label'} // $SPACE; | ||
| 270 | my $alias_aref = $h{'alias'} // []; | ||
| 271 | my $opt_kw_with_aliases = join '|', $opt_kw, $alias_aref->@*; | ||
| 272 | |||
| 273 | push @options_list, $opt_kw_with_aliases . $opt_type; | ||
| 274 | 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 | my $colwidth = 0; | ||
| 281 | foreach my $opt_aref (@options) { | ||
| 282 | my ($opt_kw_with_aliases, $opt_type, $label) = $opt_aref->@*; | ||
| 283 | my $len = length( q(') . $opt_kw_with_aliases . $opt_type . q(', ) ); | ||
| 284 | $colwidth = $len if $len > $colwidth; | ||
| 285 | } | ||
| 286 | |||
| 287 | my $fmt = sprintf '%%-%ds', $colwidth; | ||
| 288 | |||
| 289 | foreach my $opt_aref (@options) { | ||
| 290 | my ($opt_kw_with_aliases, $opt_type, $label) = $opt_aref->@*; | ||
| 291 | my $kw = sprintf $fmt, q(') . $opt_kw_with_aliases . $opt_type . q(',); | ||
| 292 | push @options_text, $kw . '# ' . $label; | ||
| 293 | } | ||
| 294 | |||
| 295 | return wantarray ? ( @options_list ) : join $NEWLINE, sort @options_text; | ||
| 296 | } | ||
| 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 | method get_option_choices :common () { | ||
| 310 | my %choices; | ||
| 311 | |||
| 312 | foreach my $row ( $Tk_opt_table->@* ) { | ||
| 313 | next if ref $row ne 'ARRAY'; | ||
| 314 | |||
| 315 | # pick off the first three values, then slurp the rest | ||
| 316 | my ($opt_kw, $opt_type, $default, @others) = $row->@*; | ||
| 317 | # skip the group dividers | ||
| 318 | next if not $opt_kw; | ||
| 319 | |||
| 320 | my $href; | ||
| 321 | if (@others and ref $others[0] eq 'HASH' ) { | ||
| 322 | $href = $others[0]; | ||
| 323 | } elsif (@others % 2 == 0) { | ||
| 324 | $href = { @others }; | ||
| 325 | } else { | ||
| 326 | croak "*E* unable to parse opttable: @others"; | ||
| 327 | } | ||
| 328 | |||
| 329 | my %hv; | ||
| 330 | if ( $href->{'choices'} and ref $href->{'choices'} eq 'ARRAY' ) { | ||
| 331 | foreach my $aref ( $href->{'choices'}->@* ) { | ||
| 332 | $hv{ $aref->[1] } = $aref->[0]; | ||
| 333 | } | ||
| 334 | $choices{$opt_kw} = \%hv; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | return \%choices; | ||
| 339 | } | ||
| 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 | method get_option_defaults :common () { | ||
| 351 | |||
| 352 | my %defaults = (); | ||
| 353 | foreach my $slot ($Tk_opt_table->@*) { | ||
| 354 | next if ref $slot ne 'ARRAY'; | ||
| 355 | my $key = $slot->[0]; | ||
| 356 | next if not $key; | ||
| 357 | my $default_value = $slot->[2]; | ||
| 358 | $defaults{$key} = $default_value; | ||
| 359 | } | ||
| 360 | |||
| 361 | return \%defaults; | ||
| 362 | } | ||
| 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 | method combine_options ($user_opt_href) { | ||
| 394 | # assign the class-level tk_options_table aref, generated before BUILD, to the instance field | ||
| 395 | $tk_opt_aref = $Tk_opt_table; | ||
| 396 | |||
| 397 | my $defaults_href = get_option_defaults(); | ||
| 398 | |||
| 399 | my %merged_options; | ||
| 400 | while ( my ($k,$v) = each $defaults_href->%* ) { | ||
| 401 | $merged_options{$k} = $user_opt_href->{$k} // $v; | ||
| 402 | } | ||
| 403 | |||
| 404 | $opt_href = \%merged_options; | ||
| 405 | $opt_obj = _wrap_hash \%merged_options; | ||
| 406 | |||
| 407 | return ($opt_href, $opt_obj); | ||
| 408 | } | ||
| 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 | method initialize () { | ||
| 476 | my @errors; | ||
| 477 | |||
| 478 | # substitute any aliases found in -location string into their station id's | ||
| 479 | if ( $opt_obj->location and $config_href->{'aliases'} ) { | ||
| 480 | if ( $opt_obj->location =~ m{ \A [_]?[[:lower:]]+ \Z }xms ) { | ||
| 481 | while ( my ($k,$v) = each $config_href->{'aliases'}->%* ) { | ||
| 482 | $opt_obj->location =~ s{$k}{$v}xms; | ||
| 483 | } | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | if ( $opt_obj->country ) { | ||
| 488 | # using undef as the search type so it will figure it out based | ||
| 489 | # on the value pattern and length | ||
| 490 | my @cou = search_country( $opt_obj->country, undef ); | ||
| 491 | |||
| 492 | push @errors, '*E* unrecognized country code or name' | ||
| 493 | if not @cou; | ||
| 494 | |||
| 495 | push @errors, '*E* ambiguous country code or name' | ||
| 496 | if @cou > 1; | ||
| 497 | |||
| 498 | # return the GEC (FIPS) country code, which is what GHCN uses | ||
| 499 | $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 | if ( $opt_obj->defined('active') and $opt_obj->active eq $EMPTY ) { | ||
| 504 | $opt_obj->active = $opt_obj->range; | ||
| 505 | } | ||
| 506 | |||
| 507 | $opt_obj->quality = 0 | ||
| 508 | if $opt_obj->fmonth or $opt_obj->fday; | ||
| 509 | |||
| 510 | return @errors; | ||
| 511 | } | ||
| 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 | method options_as_string () { | ||
| 529 | my @options; | ||
| 530 | my $boolean = _get_boolean_options($Tk_opt_table); | ||
| 531 | |||
| 532 | foreach my $k ( sort keys $opt_href->%* ) { | ||
| 533 | my $v = $opt_href->{$k}; | ||
| 534 | next if not defined $v; | ||
| 535 | |||
| 536 | if ( $boolean->{$k} ) { | ||
| 537 | push @options, $DASH . $k; | ||
| 538 | next; | ||
| 539 | } | ||
| 540 | |||
| 541 | my $val = $v; | ||
| 542 | |||
| 543 | if ( $val =~ m{\A \s* \Z}xms ) { | ||
| 544 | $val = q(") . $val . q("); | ||
| 545 | } | ||
| 546 | push @options, $DASH . $k. $SPACE . $val; | ||
| 547 | } | ||
| 548 | return join $SPACE x 2, @options; | ||
| 549 | } | ||
| 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 | method validate () { | ||
| 565 | my @errors; | ||
| 566 | my $bad_range_cnt = 0; | ||
| 567 | |||
| 568 | if ( $config_href->{aliases} ) { | ||
| 569 | foreach my $alias_name ( keys $config_href->{aliases}->%* ) { | ||
| 570 | my $errmsg = '*E* alias names in configuration must be lowercase letters with optional underscore prefix: ' . $alias_name; | ||
| 571 | push @errors, $errmsg | ||
| 572 | unless $alias_name =~ $ALIAS_NAME_RE; | ||
| 573 | } | ||
| 574 | } | ||
| 575 | |||
| 576 | if ( $opt_obj->active ) { | ||
| 577 | if ( not $opt_obj->active =~ m{ \A (18|19|20)\d\d [-] (18|19|20)\d\d }xms ) { | ||
| 578 | push @errors, '*E* invalid -active year range ' . $opt_obj->active; | ||
| 579 | $bad_range_cnt++; | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | if ( $opt_obj->range ) { | ||
| 584 | if ( not $opt_obj->range =~ m{ \A (18|19|20)\d\d [-,] (18|19|20)\d\d }xms ) { | ||
| 585 | push @errors, '*E* invalid -range ' . $opt_obj->range; | ||
| 586 | $bad_range_cnt++; | ||
| 587 | } | ||
| 588 | } | ||
| 589 | |||
| 590 | push @errors, '*E* invalid 2-character state or province code ' . $opt_obj->state | ||
| 591 | if $opt_obj->defined('state') and not $opt_obj->state =~ m{ \A [[:alpha:]]{2} \Z }xms; | ||
| 592 | |||
| 593 | push @errors, '*E* -partial only allowed if -active specified' | ||
| 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 | if ( $opt_obj->range and $opt_obj->active ) { | ||
| 600 | # uncoverable branch false | ||
| 601 | if ( $bad_range_cnt == 0 ) { | ||
| 602 | my $r = rng_new( $opt_obj->range ); | ||
| 603 | my $a = rng_new( $opt_obj->active ); | ||
| 604 | |||
| 605 | push @errors, '*E* -range must be a subset of -active' | ||
| 606 | if not $r->subset($a); | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | push @errors, '*E* -gps argument must be decimal lat/long, separated by spaces or punctuation' | ||
| 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 | my %report_abbrev = abbrev( qw(id daily monthly yearly) ); | ||
| 630 | |||
| 631 | my $report = $opt_obj->report; | ||
| 632 | |||
| 633 | # uncoverable branch true | ||
| 634 | croak "*E* undef report type" | ||
| 635 | if not defined $report; | ||
| 636 | |||
| 637 | push @errors, '*E* invalid report option: ' . $report | ||
| 638 | if $report and not $report_abbrev{ $report }; | ||
| 639 | |||
| 640 | $opt_obj->report = $report_abbrev{ $report }; | ||
| 641 | |||
| 642 | #----------------------------------------------------------------- | ||
| 643 | # end of noted section | ||
| 644 | #----------------------------------------------------------------- | ||
| 645 | |||
| 646 | my %color_abbrev = abbrev( qw(blue green azure purple red white yellow) ); | ||
| 647 | |||
| 648 | # uncoverable branch false | ||
| 649 | if ( $opt_obj->defined('color') ) { | ||
| 650 | my $color = $opt_obj->color; | ||
| 651 | if ( $color eq $EMPTY ) { | ||
| 652 | push @errors, '*E* invalid -color value ""' | ||
| 653 | } else { | ||
| 654 | push @errors, '*E* invalid -color value' | ||
| 655 | if not $color_abbrev{ $color }; | ||
| 656 | } | ||
| 657 | $opt_obj->color = $color_abbrev{ $color }; | ||
| 658 | } | ||
| 659 | |||
| 660 | |||
| 661 | push @errors, '*E* -label/-nolabel only allowed if -kml specified' | ||
| 662 | if $opt_obj->defined('label') and not $opt_obj->defined('kml'); | ||
| 663 | |||
| 664 | if ( $opt_obj->defined('fmonth') ) { | ||
| 665 | push @errors, '*E* -fmonth must be a single number or valid range spec (e.g. 1-5,9)' | ||
| 666 | if not rng_valid($opt_obj->fmonth) | ||
| 667 | or not rng_within($opt_obj->fmonth, '1-12'); | ||
| 668 | } | ||
| 669 | |||
| 670 | if ( $opt_obj->defined('fday') ) { | ||
| 671 | push @errors, '*E* -fday must be a single number or valid range spec (e.g. 3,15,20-31)' | ||
| 672 | if not rng_valid($opt_obj->fday) | ||
| 673 | or not rng_within($opt_obj->fday, '1-31'); | ||
| 674 | } | ||
| 675 | |||
| 676 | my @init_errors = $self->initialize(); | ||
| 677 | |||
| 678 | return (@errors, @init_errors); | ||
| 679 | } | ||
| 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) { | ||
| 696 | |||
| 697 | my %boolean; | ||
| 698 | |||
| 699 | foreach my $row ( $tk_opt_aref->@* ) { | ||
| 700 | next unless ref $row eq 'ARRAY'; | ||
| 701 | my ($name, $type) = $row->@*; | ||
| 702 | $boolean{$name}++ if $type eq '!'; | ||
| 703 | } | ||
| 704 | |||
| 705 | return \%boolean; | ||
| 706 | } | ||
| 707 | |||
| 708 | |||
| 709 | 1; |