Astro/Catalog/Query/SkyCat.pm

filter: Statementstmt 51.1% 112 / 219 Branchbran 36.7% 39 / 106 Conditioncond 38.8% 7 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 56.0% 14 / 25 Podpod 100% 3 / 3 totaltotal 46.1% 176 / 381 CC 68 SCAR 32.8
CC
68
Cov
46%
_parse_query
72.0
_load_config
50.6
_build_query
30.0
CRAP 26.6
« Astro/Catalog/Item.pm ↑ Summary Astro/Catalog/IO/Cluster.pm »
1 package Astro::Catalog::Query::SkyCat;
2  
3 =head1 NAME
4  
5 Astro::Catalog::Query::SkyCat - Generate SkyCat catalog query clients
6  
7 =head1 SYNOPSIS
8  
9 =head1 DESCRIPTION
10  
11 On load, automatically parse the SkyCat server specification file
12 from C<~/.skycat/skycat.cfg>, if available, and dynamically
13 generate query classes that can send queries to each catalog
14 server and parse the results.
15  
16 =cut
17  
18 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use base qw/Astro::Catalog::Transport::REST/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 our $VERSION = '4.38';
29 2 our $DEBUG = 0;
30  
31 # Controls whether we follow 'directory' config entries and recursively
32 # expand those. Default to false at the moment.
33 2 our $FOLLOW_DIRS = 0;
34  
35 # This is the name of the config file that was used to generate
36 # the content in %CONFIG. Can be different to the contents ofg_file
37 # if that
38 2 my $CFG_FILE;
39  
40 # This is the content of the config file
41 # organized as a hash indexed by remote server shortname
42 # this has the advantage of removing duplicates
43 my %CONFIG;
44  
45 =head1 METHODS
46  
47 =head2 Constructor
48  
49 =over 4
50  
51 =item B<new>
52  
53 Simple constructor. Forces read of config file if one can be found and
54 the config has not been read previously. If no config file can be located
55 the query object can not be instantiated since it will not know the
56 location of any servers.
57  
58 $q = new Astro::Catalog::Query::SkyCat(catalog => 'gsc', %options);
59 $q = new Astro::Catalog::Query::SkyCat(catalog => 'gsc@eso', %options);
60  
61 The C<catalog> field must be present, otherwise the new object will
62 not know which remote server to use and which options are mandatory
63 in the query. Note that the remote catalog can not be changed after
64 the object is instantiated. In general it is probably not wise to
65 try to change the remote host via either the C<query_url> or
66 C<url> methods unless you know what you are doing. Modifying your
67 C<skycat.cfg> file is safer.
68  
69 Currently only one config file is supported at any given time.
70 If a config file is changed (see the C<cfg_file> class method)
71 the current config is overwritten automatically.
72  
73 It is not possible to override the catalog file in the
74 constructor. Use the C<cfg_file> class method instead.
75  
76 Obviously a config per object can be supported but this is
77 probably not that helpful. This will be reconsidered if demand
78 is high.
79  
80 =cut
81  
82 1 ▶sub new {
Subroutine new: called CC=2 SCAR=8.4
Pod documented
83 1 my $proto = shift;
84 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
85  
86 # Instantiate via base class
87 1 my $block = $class->SUPER::new(@_);
88  
89 0 return $block;
90 }
91  
92 =back
93  
94 =head2 Accessor methods
95  
96 =over 4
97  
98 =item B<_selected_catalog>
99  
100 Catalog name selected by the user and currently configured for
101 this object. Not to be used outside this class..
102  
103 =cut
104  
105 0 ▶sub _selected_catalog {
Subroutine _selected_catalog: not called CC=2 SCAR=17.9
106 0 my $self = shift;
107 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
108 # The class has to be configured as a hash!!!
109 0 $self->{SKYCAT_CATALOG} = shift;
110 }
111 0 return $self->{SKYCAT_CATALOG};
112 }
113  
114 =back
115  
116 =head2 General methods
117  
118 =over 4
119  
120 =item C<configure>
121  
122 Configure the object. This calls the base class configure , after it has
123 made sure that a sky cat config file has been read (otherwise we will
124 not be able to vet the incoming arguments.
125  
126 =cut
127  
128 1 ▶sub configure {
Subroutine configure: called CC=9 SCAR=28.0
Pod documented
129 1 my $self = shift;
130  
131 # load a config if we do not have one read yet
132 # Note that this may force a remote URL read via directory
133 # directives even though we do not have a user agent configured...
134 1 ▶ $self->_load_config() unless %CONFIG;
Branch: 1 branch
BranchTrueFalseTotal
unless %CONFIG 0 1 1
135  
136 # Error if we have no config yet
137 1 ▶ croak "Error instantiating SkyCat object since no config was located"
Branch: 1 branch
BranchTrueFalseTotal
unless %CONFIG 0 1 1
138 unless %CONFIG;
139  
140 # Now we need to configure this object based on the
141 # supplied catalog name. This is not really a public interface
142 # let's call it a protected interface available to subclases
143 # even though we are not technically a subclass...
144 1 my %args = Astro::Catalog::_normalize_hash(@_);
145  
146 croak "A remote service catalog name must be provided using a 'catalog' key"
147 1 ▶ unless exists $args{catalog};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"catalog"} 0 1 1
148  
149 # case-insensitive
150 1 my $cat = lc($args{catalog});
151  
152 # if we have an entry in %CONFIG then we can use it directly
153 # else we may have a root name without a remote server
154 1 ▶ if (! exists $CONFIG{$cat}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $CONFIG{$cat}) 1 0 1
155 1 my $name = $cat;
156 # clear it and look for another
157 1 $cat = undef;
158  
159 # if name does not include an @ we probably have a generic catalog
160 # and just need to choose a random specific version
161 1 ▶ if ($name !~ /\@/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /\@/) 0 1 1
162 # look through the catalog
163 0 for my $rmt (keys %CONFIG) {
164 0 ▶ if ($rmt =~ /^$name\@/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rmt =~ /^$name\@/) 0 0 0
165 # a match
166 0 $cat = $rmt;
167 }
168 }
169 }
170 # No luck finding catalog name
171 1 ▶ croak "unable to find a remote service named $name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cat 1 0 1
172 unless defined $cat;
173 }
174  
175 # Now we know the details we need to store this somewhere in
176 # the object so that it won't get clobbered. Otherwise the
177 # super class configure will not be able to get the information
178 # it needs. We can not simply store this in options since configure
179 # does not know it is an allowed option...
180 0 $self->_selected_catalog( $cat );
181  
182 # delete catalog from list
183 0 delete $args{catalog};
184  
185 # Configure
186 0 $self->SUPER::configure(%args);
187 }
188  
189 =item B<_build_query>
190  
191 Construct a query URL based on the options.
192  
193 $url = $q->_build_query();
194  
195 =cut
196  
197 0 ▶sub _build_query {
Subroutine _build_query: not called CC=5 SCAR=34.0
198 0 my $self = shift;
199  
200 0 my $cat = $self->_selected_catalog();
201  
202 # Get the URL
203 0 my $url = $CONFIG{$cat}->{url};
204  
205 # Translate all the options to the internal skycat format
206 0 my %translated = $self->_translate_options();
207  
208 0 ▶ print "Translated query: ".Dumper(\%translated,$url) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 0 0
209  
210 # Now for each token replace it in the URL
211 0 for my $key (keys %translated) {
212 0 my $tok = "%". $key;
213 croak "Token $tok is mandatory but was not specified"
214 0 ▶ unless defined $translated{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $translated{$key} 0 0 0
215 0 $url =~ s/$tok/$translated{$key}/;
216 }
217  
218 0 ▶ print "Final URL: $url\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 0 0
219  
220 0 return $url;
221 }
222  
223  
224 =item B<_parse_query>
225  
226 All the SkyCat servers return data in TST format.
227 Need to make sure that column information is passed
228 into the TST parser.
229  
230 =cut
231  
232 0 ▶sub _parse_query {
Subroutine _parse_query: not called CC=8 SCAR=42.8
233 0 my $self = shift;
234  
235 # Get the catalog info
236 0 my $cat = $self->_selected_catalog();
237  
238 # and extract formatting information needed by the TST parser
239 0 my %params;
240 0 for my $key (keys %{ $CONFIG{$cat} }) {
241 0 ▶ if ($key =~ /_col$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /_col$/) 0 0 0
242 0 ▶ print "FOUND column specified $key\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 0 0
243 0 $params{$key} = $CONFIG{$cat}->{$key};
244 }
245 }
246  
247 # If this catalog is a GSC, pass in a GSC parameter
248 0 ▶ $params{gsc} = 1 if $cat =~ /^gsc/i;
Branch: 1 branch
BranchTrueFalseTotal
if $cat =~ /^gsc/i 0 0 0
249  
250 0 ▶ print $self->{BUFFER} ."\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 0 0
251  
252 # Make sure we set origin and field centre if we know it
253 my $newcat = new Astro::Catalog(
254 Format => 'TST',
255 Data => $self->{BUFFER},
256 ReadOpt => \%params,
257 Origin => $CONFIG{$cat}->{long_name},
258 0 );
259  
260 # set the field centre
261 0 my %allow = $self->_get_allowed_options();
262 0 my %field;
263 0 for my $key ("ra","dec","radius") {
264 0 ▶ if (exists $allow{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $allow{$key}) 0 0 0
265 0 $field{$key} = $self->query_options($key);
266 }
267 }
268 0 $newcat->fieldcentre(%field);
269  
270 0 return $newcat;
271 }
272  
273 =item B<_get_allowed_options>
274  
275 This method declares which options can be configured by the user
276 of this service. Generated automatically by the skycat config
277 file and keyed to the requested catalog.
278  
279 =cut
280  
281 0 ▶sub _get_allowed_options {
Subroutine _get_allowed_options: not called CC=1 SCAR=6.9
282 0 my $self = shift;
283 0 my $cat = $self->_selected_catalog();
284  
285 0 return %{ $CONFIG{$cat}->{allow} };
286 }
287  
288 =item B<_get_default_options>
289  
290 Get the default options that are relevant for the selected
291 catalog.
292  
293 %defaults = $q->_get_default_options();
294  
295 =cut
296  
297 0 ▶sub _get_default_options {
Subroutine _get_default_options: not called CC=1 SCAR=6.9
298 0 my $self = shift;
299  
300 # Global skycat defaults
301 0 my %defaults = (
302 # Target information
303 ra => undef,
304 dec => undef,
305 id => undef,
306  
307 # Limits
308 radmin => 0,
309 radmax => 5,
310 width => 10,
311 height => 10,
312  
313 magfaint => 100,
314 magbright => 0,
315  
316 nout => 20000,
317 cond => '',
318 );
319  
320 # Get allowed options
321 0 my %allow = $self->_get_allowed_options();
322  
323 # Trim the defaults (could do with hash slice?)
324 0 my %trim = map {$_ => $defaults{$_}} keys %allow;
325  
326 0 return %trim;
327 }
328  
329 =item B<_get_supported_init>
330  
331 =cut
332  
333 0 ▶sub _get_supported_init {
Subroutine _get_supported_init: not called CC=1 SCAR=6.9
334 0 croak "xxx - get supported init";
335 }
336  
337 =back
338  
339 =head2 Class methods
340  
341 These methods are not associated with any particular object.
342  
343 =over 4
344  
345 =item B<cfg_file>
346  
347 Location of the skycat config file. Default location is
348 C<$SKYCAT_CFG>, if defined, else C<$HOME/.skycat/skycat.cfg>,
349 or C<$PERLPREFIX/etc/skycat.cfg> if there isn't a version
350 in the users home directory
351  
352 This could be made per-class if there is a demand for running
353 queries with different catalogs. This would also move the config
354 contents into the query object itself.
355  
356 =cut
357  
358 0 ▶sub _set_cfg_file {
Subroutine _set_cfg_file: not called CC=5 SCAR=34.0
359 0 my $cfg_file;
360  
361 0 ▶ if (exists $ENV{SKYCAT_CFG}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{'SKYCAT_CFG'}) { } 0 0 0
elsif (-f 'File::Spec'->catfile($ENV{'HOME'}, '.skycat', 'skycat.cfg')) { } 0 0 0
362 0 $cfg_file = $ENV{SKYCAT_CFG};
363 }
364 elsif (-f File::Spec->catfile($ENV{HOME}, ".skycat", "skycat.cfg")) {
365 0 $cfg_file = File::Spec->catfile($ENV{HOME}, ".skycat", "skycat.cfg");
366 }
367 else {
368 # generate the default path to the $PERLPRFIX/etc/skycat.cfg file,
369 # this is a horrible hack, there is probably an elegant way to do
370 # this but I can't be bothered looking it up right now.
371 0 my $perlbin = $^X;
372 0 my ($volume, $dir, $file) = File::Spec->splitpath($perlbin);
373 0 my @dirs = File::Spec->splitdir($dir);
374 0 my @path;
375 0 foreach my $i (0 .. $#dirs-2) {
376 0 push @path, $dirs[$i];
377 }
378 0 my $directory = File::Spec->catdir(@path, 'etc');
379  
380 # reset to the default
381 0 $cfg_file = File::Spec->catfile($directory, "skycat.cfg");
382  
383 # debugging and testing purposes
384 0 ▶ unless (-f $cfg_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $cfg_file) 0 0 0
385 # use blib version!
386 0 $cfg_file = File::Spec->catfile('.', 'etc', 'skycat.cfg');
387 }
388 }
389 0 return $cfg_file;
390 }
391  
392 1 ▶sub cfg_file {
Subroutine cfg_file: called CC=6 SCAR=20.5
Pod documented
393 1 my $class = shift;
394 1 my $cfg_file;
395 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
396 1 $cfg_file = shift;
397 1 ▶ if (((defined $CFG_FILE) && $cfg_file ne $CFG_FILE)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $CFG_FILE and $cfg_file ne $CFG_FILE or not defined $CFG_FILE) 1 0 1
Condition33%2/6
defined $CFG_FILE and $cfg_file ne $CFG_FILE or not defined $CFG_FILE
l!l&&r!l&&!r
010
defined $CFG_FILE and $cfg_file ne $CFG_FILE
!ll&&!rl&&r
100
MC/DC0%0/3
defined $CFG_FILE and $cfg_file ne $CFG_FILE or not defined $CFG_FILE
defined $CFG_FILE $cfg_file ne $CFG_FILE not defined $CFG_FILE
Truth table20%1/5
A and B or not(C)
A= defined $CFG_FILEB= $cfg_file ne $CFG_FILEC= not defined $CFG_FILE
ABCresult
11X1
0X11
1011
0X00
1000
398 || ! (defined $CFG_FILE)) {
399 # We were given a new config file, so load it.
400 1 $class->_load_config($cfg_file);
401 1 $CFG_FILE = $cfg_file;
402 }
403 }
404 1 ▶ unless (defined $CFG_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $CFG_FILE) 0 1 1
405 0 $CFG_FILE = _set_cfg_file;
406 }
407 1 return $CFG_FILE;
408 }
409  
410 =back
411  
412 =begin __PRIVATE_METHODS__
413  
414 =head2 Internal methods
415  
416 =over 4
417  
418 =item B<_load_config>
419  
420 Method to load the skycat config information into
421 the class and configure the modules.
422  
423 $q->_load_config() or die "Error loading config";
424  
425 The config file name is obtained from the C<cfg_file> method.
426 Returns true if the file was read successfully and contained at
427 least one catalog server. Otherwise returns false.
428  
429 Requires an object to attach itself to (mainly for the useragent
430 remote directory follow up). The results of this load are
431 visible to all instances of this class.
432  
433 Usually called automatically from the constructor if a config
434 has not previously been read.
435  
436 =cut
437  
438 1 ▶sub _load_config {
Subroutine _load_config: called CC=23 SCAR=39.2
439 1 my $self = shift;
440 1 my $cfg = shift;
441  
442 1 ▶ print "SkyCat.pm: \$cfg = $cfg\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 1 1
443  
444 1 ▶ unless (defined $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg) 0 1 1
445 0 $cfg = _set_cfg_file;
446 0 $self->cfg_file( $cfg );
447 }
448  
449 1 ▶ unless (-e $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $cfg) 0 1 1
450 0 ▶ my $xcfg = (defined $cfg ? $cfg : "<undefined>" );
Branch: 1 branch
BranchTrueFalseTotal
defined $cfg ? : 0 0 0
451 0 return;
452 }
453  
454 1 my $fh;
455 1 ▶ unless (open $fh, "<$cfg") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<$cfg") 0 1 1
456 0 warnings::warnif( "Specified config file, $cfg, could not be opened: $!");
457 0 return;
458 }
459  
460 # Need to read the contents into an array
461 1 my @lines = <$fh>;
462  
463 # Process the config file and extract the raw content
464 1 my @configs = $self->_extract_raw_info(\@lines);
465  
466 1 ▶ print "Pre-filtering has \@configs " . @configs . " entries\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 1 1
467  
468 # Close file
469 1 ▶ close( $fh ) or do {
Branch: 1 branch
BranchTrueFalseTotal
close $fh or do { warnings::warnif("Error closing config file, ${cfg}: $!"); return } 1 0 1
470 0 warnings::warnif("Error closing config file, $cfg: $!");
471 0 return;
472 };
473  
474 # Get the token mapping for validation
475 1 my %map = $self->_token_mapping;
476  
477 # Currently we are only interested in catalog, namesvr and archive
478 # so throw everything else away
479 1 @configs = grep {$_->{serv_type} =~ /(namesvr|catalog|archive)/ } @configs;
480  
481 1 ▶ print "Post-filtering has \@configs " . @configs . " entries\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 1 1
482  
483 # Process each entry. Mainly URL processing
484 1 for my $entry ( @configs ) {
485 # Skip if we have already analysed this server
486 33 ▶ if (exists $CONFIG{lc($entry->{short_name})}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CONFIG{lc $entry->{"short_name"}}) 0 33 33
487 0 ▶ print "Already know about " . $entry->{short_name} . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 0 0
488 if $DEBUG;
489 0 next;
490 }
491  
492 33 ▶ print " Processing " . $entry->{short_name} . "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 33 33
493 33 ▶ print Dumper( $entry ) if( $DEBUG );
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 33 33
494  
495 # Extract info from the 'url'. We need to extract the following info:
496 # - Host name and port
497 # - remaining url path
498 # - all the CGI options including the static options
499 # Note that at the moment we do not do token replacement (the
500 # rest of the REST architecture expects to get the above
501 # information separately). This might well prove to be silly
502 # since we can trivially replace the tokens without having to
503 # reconstruct the url. Of course, this does allow us to provide
504 # mandatory keywords. $url =~ s/\%ra/$ra/;
505 33 ▶ if ($entry->{url} =~ m|^http:// # Standard http:// prefix
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"url"} =~ m[^http:// # Standard http:// prefix ([\w\.\-]+ # remote host (?::\d+)?) # Optional port number / # path separator ([\w\/\-\.]+\?) # remaining URL path and ? (.*) # CGI options without trailing space ]x) 33 0 33
506 ([\w\.\-]+ # remote host
507 (?::\d+)?) # Optional port number
508 / # path separator
509 ([\w\/\-\.]+\?) # remaining URL path and ?
510 (.*) # CGI options without trailing space
511 |x) {
512 33 $entry->{remote_host} = $1;
513 33 $entry->{url_path} = $2;
514 33 my $options = $3;
515  
516 # if first character is & we append that to url_path since it
517 # is an empty argument
518 33 ▶ $entry->{url_path} .= "&" if $options =~ s/^\&//;
Branch: 1 branch
BranchTrueFalseTotal
if $options =~ s/^\&// 2 31 33
519  
520 # In general the options from skycat files are a real pain
521 # Most of them have nice blah=%blah format but there are some cases
522 # that do ?%ra%dec or coords=%ra %dec that just cause more trouble
523 # than they are worth given the standard URL constructor that we
524 # are attempting to inherit from REST
525 # Best idea is not to fight against it. Extract the host, path
526 # and options separately but simply use token replacement when it
527 # comes time to build the URL. This will require that the url
528 # is moved into its own method in REST.pm for subclassing.
529 # We still need to extract the tokens themselves so that we
530 # can generate an allowed options list.
531  
532 # tokens have the form %xxx but we have to make sure we allow
533 # %mime-type. Use the /g modifier to get all the matches
534 33 my @tokens = ( $options =~ /(\%[\w\-]+)/g);
535  
536 # there should always be tokens. No obvious way to reomve the anomaly
537 33 ▶ warnings::warnif("No tokens found in $options!!!")
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 0 33 33
538 unless @tokens;
539  
540 # Just need to make sure that these are acceptable tokens
541 # Get the lookup table and store that as the allowed options
542 33 my %allow;
543 33 for my $tok (@tokens) {
544 # only one token. See if we recognize it
545 145 my $strip = $tok;
546 145 $strip =~ s/%//;
547  
548 145 ▶ if (exists $map{$strip}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map{$strip}) { } 145 0 145
549 145 ▶ unless (defined $map{$strip}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $map{$strip}) { } 145 0 145
550 0 warnings::warnif("Do not know how to process token $tok" );
551 }
552 else {
553 145 $allow{ $map{$strip} } = $strip;
554 }
555 }
556 else {
557 warnings::warnif("Token $tok not currently recognized")
558 0 ▶ unless exists $map{$strip};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map{$strip} 0 0 0
559 }
560 }
561  
562 # Store them
563 33 $entry->{tokens} = \@tokens;
564 33 $entry->{allow} = \%allow;
565  
566 33 ▶ print Dumper($entry) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 33 33
567  
568 # And store this in the config. Only store it if we have
569 # tokens
570 33 $CONFIG{lc($entry->{short_name})} = $entry;
571 }
572 }
573  
574 # Debug
575 1 ▶ print Dumper(\%CONFIG) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 1 1
576  
577 1 return;
578 }
579  
580 =item B<_extract_raw_info>
581  
582 Go through a skycat.cfg file and extract the raw unprocessed entries
583 into an array of hashes. The actual content of the file is passed
584 in as a reference to an array of lines.
585  
586 @entries = $q->_extract_raw_info(\@lines);
587  
588 This routine is separate from the main load routine to allow recursive
589 calls to remote directory entries.
590  
591 =cut
592  
593 1 ▶sub _extract_raw_info {
Subroutine _extract_raw_info: called CC=6 SCAR=18.0
594 1 my $self = shift;
595 1 my $lines = shift;
596  
597 # Now read in the contents
598 1 my $current; # Current server spec
599 my @configs; # Somewhere temporary to store the entries
600  
601 1 for my $line (@$lines) {
602 # Skip comment lines and blank lines
603 354 ▶ next if $line =~ /^\s*\#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*\#/ 23 331 354
604 331 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 55 276 331
605  
606 276 ▶ if ($line =~ /^(\w+):\s*(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\w+):\s*(.*?)\s*$/) { } 276 0 276
607 # This is content
608 276 my $key = $1;
609 276 my $value = $2;
610 # Assume that serv_type is always first
611 276 ▶ if ($key eq 'serv_type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'serv_type') { } 45 231 276
612 # Store previous config if it contains something
613 # If it actually contains information on a serv_type of
614 # directory we can follow the URL and recursively expand
615 # the content
616 45 push(@configs, $self->_dir_check($current));
617  
618 # Clear the config and store the serv_type
619 45 $current = {$key => $value};
620 }
621 else {
622 # Just store the key value pair
623 231 $current->{$key} = $value;
624 }
625 }
626 else {
627 # do not know what this line signifies since it is
628 # not a comment and not a content line
629 0 warnings::warnif("Unexpected line in config file: $line\n");
630 }
631 }
632  
633 # Last entry will still be in %$current so store it if it contains
634 # something.
635 1 push(@configs, $self->_dir_check( $current ));
636  
637 # Return the entries
638 1 return @configs;
639 }
640  
641 =item B<_dir_check>
642  
643 If the supplied hash reference has content, look at the content
644 and decide whether you simply want to keep that content or
645 follow up directory specifications by doing a remote URL call
646 and expanding that directory specification to many more remote
647 catalog server configs.
648  
649 @configs = $q->_dir_check(\%current);
650  
651 Returns the supplied argument, additional configs derived from
652 that argument or nothing at all.
653  
654 Do not follow a 'directory' link if we have already followed a link with
655 the same short name. This prevents infinite recursion when the catalog
656 pointed to by 'catalogs@eso' itself contains a reference to 'catalogs@eso'.
657  
658 =cut
659  
660 2 my %followed_dirs;
661 46 ▶sub _dir_check {
Subroutine _dir_check: called CC=8 SCAR=25.1
662 46 my $self = shift;
663 46 my $current = shift;
664  
665 46 ▶ if (defined $current && %$current) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current and %$current) 45 1 46
Condition66%2/3
defined $current and %$current
!ll&&!rl&&r
1045
MC/DC50%1/2
defined $current and %$current
defined $current %$current
Truth table66%2/3
A and B
A= defined $currentB= %$current
ABresult
0X0
100
111
666 45 ▶ if ($current->{serv_type} eq 'directory') {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{'serv_type'} eq 'directory') { } 4 41 45
667 # Get the content of the URL unless we are not
668 # reading directories
669 4 ▶ if ($FOLLOW_DIRS && defined $current->{url} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($Astro::Catalog::Query::SkyCat::FOLLOW_DIRS and defined $current->{"url"} and not exists $followed_dirs{$current->{"short_name"}}) 0 4 4
Condition33%2/6
$Astro::Catalog::Query::SkyCat::FOLLOW_DIRS and defined $current->{"url"} and not exists $followed_dirs{$current->{"short_name"}}
!ll&&!rl&&r
400
$Astro::Catalog::Query::SkyCat::FOLLOW_DIRS and defined $current->{"url"}
!ll&&!rl&&r
400
MC/DC0%0/3
$Astro::Catalog::Query::SkyCat::FOLLOW_DIRS and defined $current->{"url"} and not exists $followed_dirs{$current->{"short_name"}}
$Astro::Catalog::Query::SkyCat::FOLLOW_DIRS defined $current->{"url"} not exists $followed_dirs{$current->{"short_name"}}
Truth table25%1/4
A and B and not(C)
A= $Astro::Catalog::Query::SkyCat::FOLLOW_DIRSB= defined $current->{"url"}C= not exists $followed_dirs{$current->{"short_name"}}
ABCresult
0XX0
10X0
1100
1111
670 !exists $followed_dirs{$current->{short_name}}) {
671 print "Following directory link to ". $current->{short_name}.
672 0 ▶ "[".$current->{url}."]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Astro::Catalog::Query::SkyCat::DEBUG 0 0 0
673 if $DEBUG;
674  
675 # Indicate that we have followed this link
676 0 $followed_dirs{$current->{short_name}} = $current->{url};
677  
678 # Retrieve the url, pass that array to the raw parser and then
679 # return any new configs to our caller
680 # Must force scalar context to get array ref
681 # back rather than a simple list.
682 0 return $self->_extract_raw_info(scalar $self->_get_directory_url( $current->{url} ));
683 }
684 }
685 else {
686 # Not a 'directory' so this is a simple config entry. Simply return it.
687 41 return ($current);
688 }
689 }
690  
691 # return empty list since we have no value
692 5 return ();
693 }
694  
695  
696 =item B<_get_directory_url>
697  
698 Returns the content of the remote directory URL supplied as
699 argument. In scalar context returns reference to array of lines. In
700 list context returns the lines in a list.
701  
702 $lines = $q->_get_directory_url($url);
703 @lines = $q->_get_directory__url($url);
704  
705 If we have an error retrieving the file, just return an empty
706 array (ie skip it).
707  
708 =cut
709  
710 0 ▶sub _get_directory_url {
Subroutine _get_directory_url: not called CC=3 SCAR=24.8
711 0 my $self = shift;
712 0 my $url = shift;
713  
714 # Call the base class to get the actual content
715 0 my $content = '';
716 0 eval {
717 0 $content = $self->_fetch_url( $url );
718 };
719  
720 # Need an array
721 0 my @lines;
722 0 ▶ @lines = split("\n", $content) if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 0 0 0
723  
724 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
725 0 return @lines;
726 }
727 else {
728 0 return \@lines;
729 }
730 }
731  
732 =item B<_token_mapping>
733  
734 Provide a mapping of tokens found in SkyCat config files to the
735 internal values used generically by Astro::Catalog::Query classes.
736  
737 %map = $class->_token_mappings;
738  
739 Keys are skycat tokens.
740  
741 =cut
742  
743 1 ▶sub _token_mapping {
Subroutine _token_mapping: called CC=1 SCAR=0.0
744 return (
745 1 id => 'id',
746  
747 ra => 'ra',
748 dec => 'dec',
749  
750 # Arcminutes
751 r1 => 'radmin',
752 r2 => 'radmax',
753 w => 'width',
754 h => 'height',
755  
756 n => 'nout',
757  
758 # which filter???
759 m1 => 'magfaint',
760 m2 => 'magbright',
761  
762 # Is this a conditional?
763 cond => 'cond',
764  
765 # Not Yet Supported
766 cols => undef,
767 'mime-type' => undef,
768 ws => undef,
769 );
770 }
771  
772 =back
773  
774 =head2 Translations
775  
776 SkyCat specific translations from the internal format to URL format
777 go here.
778  
779 RA/Dec must match format described in
780 http://vizier.u-strasbg.fr/doc/asu.html
781 (at least for GSC) ie hh:mm:ss.s+/-dd:mm:ss
782 or decimal degrees.
783  
784 =over 4
785  
786 =cut
787  
788 0 ▶sub _from_dec {
Subroutine _from_dec: not called CC=2 SCAR=17.9
789 0 my $self = shift;
790 0 my $dec = $self->query_options("dec");
791 0 my %allow = $self->_get_allowed_options();
792  
793 # Need colons
794 0 $dec =~ s/\s+/:/g;
795  
796 # Need a + preprended
797 0 ▶ $dec = "+" . $dec if $dec !~ /^[\+\-]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dec =~ /^[\+\-]/ 0 0 0
798  
799 0 return ($allow{dec}, $dec);
800 }
801  
802 0 ▶sub _from_ra {
Subroutine _from_ra: not called CC=1 SCAR=6.9
803 0 my $self = shift;
804 0 my $ra = $self->query_options("ra");
805 0 my %allow = $self->_get_allowed_options();
806  
807 # need colons
808 0 $ra =~ s/\s+/:/g;
809  
810 0 return ($allow{ra}, $ra);
811 }
812  
813 =item B<_translate_one_to_one>
814  
815 Return a list of internal options (as defined in C<_get_allowed_options>)
816 that are known to support a one-to-one mapping of the internal value
817 to the external value.
818  
819 %one = $q->_translate_one_to_one();
820  
821 Returns a hash with keys and no values (this makes it easy to
822 check for the option).
823  
824 This method also returns, the values from the parent class.
825  
826 =cut
827  
828 0 ▶sub _translate_one_to_one {
Subroutine _translate_one_to_one: not called CC=1 SCAR=6.9
829 0 my $self = shift;
830 # convert to a hash-list
831 return ($self->SUPER::_translate_one_to_one,
832 0 map { $_, undef }(qw/
833 cond
834 /)
835 );
836 }
837  
838 2 1;
839  
840 __END__
« Astro/Catalog/Item.pm ↑ Summary Astro/Catalog/IO/Cluster.pm »