All.pm

filter: Statementstmt 68.3% 110 / 161 Branchbran 52.0% 26 / 50 Conditioncond 16.6% 5 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 62.9% 17 / 27 Podpod 0.0% 0 / 17 totaltotal 52.3% 158 / 302 CC 44 SCAR 19.6
CC
44
Cov
59%
store
20.0
write
20.0
count
12.0
CRAP 7.1
« All/Format/Delim.pm ↑ Summary All/IO/FTP.pm »
1 package Data::All;
2  
3 # Data::All - Access to data in many formats source many places
4  
5 # TODO: Create Data::All::IO::Hash for internal storage
6 # TODO: Add checking for output field names that aren't present in input field names
7 # TODO: Auto reset file/db cursors? Call read() then convert() causes error;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 #use diagnostics;
12
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::All::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::All::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $VERSION = 0.042;
18 1 our @EXPORT = qw(collection);
19  
20 ## PUBLIC INTERFACE
21 sub show_fields; # returns an arrayref of field names
22 sub collection; # A shortcut for open() and read()
23 sub getrecord;
24 sub putrecord;
25 sub is_open;
26 sub convert; # Change formats
27 sub store; # save in memory records
28 sub count; # Record count
29 sub close;
30 sub read;
31 sub open;
32  
33 ## PUBLIC ATTRIBUTES
34 ## i.e. $da->source()
35 1 attribute 'source';
36 1 attribute 'target';
37 1 attribute 'print_fields' => 0;
38 1 attribute 'atomic' => 1;
39  
40  
41 ## PRIVATE ATTRIBUTES
42  
43 # Contains Data::All::IO::* object by moniker
44 1 internal 'collection' => {};
45  
46 1 internal 'profile' =>
47 # Hardcoded/commonly used format configs
48 {
49 csv => ['delim', "\n", ',', '"', '\\'],
50 tab => ['delim', "\n", "\t", '', '']
51 };
52  
53 1 internal 'a2h_template' =>
54 # Templates for converting arrayref configurations to
55 # internally used, easy to handle hashref configs. See _parse_args().
56 # TODO: move this functionality into a generic arg parsing library
57 {
58 'format.delim' => ['type','break','delim','quote','escape'],
59 'format.fixed' => ['type','break','lengths'],
60 'ioconf.file' => ['type','perm','with_original'],
61 'ioconf.ftp' => ['type','perm','with_original'],
62 'ioconf.db' => ['type','perm','with_original']
63 };
64  
65 1 internal 'default' =>
66 # Default values for configuration variables
67 {
68 profile => 'csv',
69 filters => '',
70 ioconf =>
71 {
72 type => 'file',
73 perm => 'r',
74 with_original => 0
75 },
76 format =>
77 {
78 type => 'delim'
79 }
80 };
81  
82  
83 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 Data::All::IO->register_factory_type( file => 'Data::All::IO::File');
85 #Data::All::IO->register_factory_type( xml => 'Data::All::IO::XML');
86 1 Data::All::IO->register_factory_type( db => 'Data::All::IO::Database');
87 #Data::All::IO->register_factory_type( ftp => 'Data::All::IO::FTP');
88 }
89  
90  
91 # CONSTRUCTOR RELATED
92 sub init()
93 # Rekindle all that we are
94 1 ▶{
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
95 1 my $self = shift;
96
97 1 my $args = $self->reinit(@_);
98 1 return $self;
99 }
100  
101 sub reinit
102 2 ▶{
Subroutine reinit: called CC=3 SCAR=11.2
Pod undocumented
103 2 my $self = shift;
104 2 my $args;
105
106 2 ▶ return undef unless ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 2 2
107
108 # Allow for hash or hashref args
109 2 ▶ $args = (ref($_[0]) eq 'HASH') ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 2 2
110
111 2 populate($self, $args);
112
113 2 $self->prep_collections();
114
115 2 return $args;
116 }
117  
118  
119  
120 sub prep_collections()
121 # Prepare and store an instance of Data::All::IO::* for source and to configs
122 2 ▶{
Subroutine prep_collections: called CC=3 SCAR=11.2
Pod undocumented
123 2 my $self = shift;
124
125 2 foreach (qw(source target))
126 {
127 4 ▶ $self->__collection()->{$_} = $self->_load_IO($self->$_())
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$_ 4 0 4
128 if (defined($self->$_()));
129 }
130 }
131  
132  
133 sub _load_IO(\%)
134 # Load an instance of Data::All::IO::? to memory
135 4 ▶{
Subroutine _load_IO: called CC=1 SCAR=0.0
136 4 my $self = shift;
137 4 my $args = shift;
138  
139 4 $self->_parse_args($args);
140 4 my ($ioconf, $format, $path, $fields) = @{ $args }{'ioconf','format','path','fields'};
141
142  
143 4 my $IO = Data::All::IO->new($ioconf->{'type'},
144 {
145 ioconf => $ioconf,
146 format => $format,
147 path => $path,
148 fields => $fields
149 });
150
151 4 return $IO;
152 }
153  
154 sub _parse_args()
155 # Convert arrayref args into hashref, process determinable values,
156 # and apply defaults to the rest. We can also through a horrible
157 # error at this point if there isn't enoguh info for Data::All to
158 # continue.
159 4 ▶{
Subroutine _parse_args: called CC=8 SCAR=20.9
160 4 my $self = shift;
161 4 my $args = shift;
162
163 # TODO: Allow collection('filename.csv', 'profile'); usage
164 4 $self->_apply_profile_to_args($args);
165
166 # Make sure path is an array ref
167 4 ▶ $args->{'path'} = [$args->{'path'}] if (ref($args->{'path'}) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{"path"} ne "ARRAY" 2 2 4
168
169 4 for my $a (keys %{ $self->__default() })
170 # Apply default values to data collection configuration. Amplify arrayref
171 # configs into hashref configs using the a2h_templates where appropriate.
172 {
173 16 ▶ next if $a eq 'path';
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "path" 0 16 16
174  
175 16 ▶ if (ref($args->{$a}) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{$a} eq "ARRAY") 4 12 16
176 {
177 4 my (%hash, $templ);
178 4 $templ = join '', $a, '.', $args->{$a}->[0];
179
180 4 ▶ $self->error("Wasn't expecting: $templ"), next unless(exists($self->__a2h_template()->{$templ}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->__a2h_template->{$templ} 0 4 4
181
182 4 @hash{@{$self->__a2h_template()->{$templ}}} = @{ $args->{$a} };
183
184 4 $args->{$a} = \%hash;
185 }
186
187 16 $self->_apply_default_to($a, $args);
188 }
189
190 4 ▶ return if ($args->{'moniker'});
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"moniker"} 2 2 4
191
192 $args->{'moniker'} = ($args->{'ioconf'}->{'type'} ne 'db')
193 2 ▶ ? join('', @{ $args->{'path'} })
Branch: 1 branch
BranchTrueFalseTotal
$args->{'ioconf'}{'type'} ne 'db' ? : 2 0 2
194 : '_';
195
196 }
197  
198  
199 sub _apply_profile_to_args(\%)
200 # Populate format within args based on a preconfigured profile
201 4 ▶{
Subroutine _apply_profile_to_args: called CC=5 SCAR=16.7
202 4 my $self = shift;
203 4 my $args = shift;
204 #print Dumper($args);
205 4 ▶ my $p = $args->{'profile'} || $self->__default()->{'profile'};
Condition66%2/3
$args->{'profile'} || $self->__default->{'profile'}
l!l&&r!l&&!r
310
MC/DC0%0/2
$args->{'profile'} || $self->__default->{'profile'}
$args->{'profile'} $self->__default->{'profile'}
Truth table66%2/3
A || B
A= $args->{'profile'}B= $self->__default->{'profile'}
ABresult
1X1
011
000
206
207 4 ▶ return if (exists($args->{'format'}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"format"} 3 1 4
208
209 die("There is no profile for type $p ")
210 1 ▶ unless ($p && exists($self->__profile()->{$p}));
Branch: 1 branch
BranchTrueFalseTotal
unless $p and exists $self->__profile->{$p} 0 1 1
Condition33%1/3
$p and exists $self->__profile->{$p}
!ll&&!rl&&r
001
MC/DC0%0/2
$p and exists $self->__profile->{$p}
$p exists $self->__profile->{$p}
Truth table33%1/3
A and B
A= $pB= exists $self->__profile->{$p}
ABresult
0X0
100
111
211
212 # Set the format using the requested profile
213 1 $args->{'format'} = $self->__profile()->{$p};
214 1 return;
215 }
216  
217 sub _apply_default_to()
218 # Set a default value to a particular attribute.
219 # TODO: Allow setting of individual attribute fields
220 16 ▶{
Subroutine _apply_default_to: called CC=5 SCAR=16.1
221 16 my $self = shift;
222 16 my ($a, $args) = @_;
223 $args->{$a} = $self->__default()->{$a}
224 16 ▶ unless (exists($args->{$a}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$a} 3 13 16
225
226 16 ▶ return unless (ref($args->{$a}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{$a} eq "HASH" 8 8 16
227
228 8 foreach my $c (keys %{ $self->__default()->{$a} })
229 {
230 $args->{$a}->{$c} = $self->__default()->{$a}->{$c}
231 16 ▶ unless (defined($args->{$a}->{$c}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$a}{$c} 2 14 16
232 }
233  
234 }
235  
236  
237  
238 sub count(;$)
239 # get a record count
240 0 ▶{
Subroutine count: not called CC=3 SCAR=24.8
Pod undocumented
241 0 my $self = shift;
242 0 ▶ my $which = shift || 'source';
Condition0%0/2
shift() || 'source'
l!l
00
MC/DC0%0/1
shift() || 'source'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
243
244 0 ▶ $self->open() unless ($self->is_open($which));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open($which) 0 0 0
245 0 return $self->__collection()->{$which}->count();
246 }
247  
248  
249 sub count_to(;$)
250 # get a record count for the source config
251 0 ▶{
Subroutine count_to: not called CC=1 SCAR=6.9
Pod undocumented
252 0 my $self = shift;
253 0 return $self->count('target');
254 }
255  
256  
257 sub count_source(;$)
258 # get a record count for the source config
259 0 ▶{
Subroutine count_source: not called CC=1 SCAR=6.9
Pod undocumented
260 0 my $self = shift;
261 0 return $self->count('source');
262 }
263  
264 sub getrecord(;$$)
265 # Get a single, consecutive record
266 0 ▶{
Subroutine getrecord: not called CC=2 SCAR=17.9
Pod undocumented
267 0 my $self = shift;
268 0 ▶ my $type = shift || 'hash';
Condition0%0/2
shift() || 'hash'
l!l
00
MC/DC0%0/1
shift() || 'hash'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
269 0 my $meth = 'getrecord_' . $type;
270 0 my $record;
271
272 # $record = ($self->__collection()->{'source'}->can($meth))
273 # ? $self->__collection()->{'source'}->$meth()
274 # : undef;
275  
276 0 return $self->__collection()->{'source'}->getrecord_hash();
277 }
278  
279 sub putrecord()
280 # Put a single, consecutive record
281 0 ▶{
Subroutine putrecord: not called CC=2 SCAR=17.9
Pod undocumented
282 0 my $self = shift;
283 0 ▶ my $record = shift || return undef;
Condition0%0/2
shift() || (return undef)
l!l
00
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
284
285 0 $self->__collection()->{'target'}->putrecord()
286 }
287  
288  
289 sub collection(%)
290 # Shorthand for creating a Data::All instance, openning, reading
291 # and closing the data source
292 0 ▶{
Subroutine collection: not called CC=3 SCAR=24.8
Pod undocumented
293 0 my ($conf1, $conf2) = @_;
294 0 my ($myself, $rec);
295
296 # We can accept standard-arg style, but we will also make provisions
297 # for a single hashref arg which we'll assume is the 'source' config
298 0 ▶ $myself = (ref($_[0]) ne 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ne 'HASH' ? : 0 0 0
299 ? new('Data::All', @_)
300 : new('Data::All', source => $_[0]);
301
302 0 $myself->open();
303 0 $rec = $myself->read();
304 0 $myself->close();
305
306 0 ▶ return (!wantarray) ? $rec : @{ $rec };
Branch: 1 branch
BranchTrueFalseTotal
!wantarray ? : 0 0 0
307 }
308  
309 sub open(;$)
310 2 ▶{
Subroutine open: called CC=3 SCAR=11.8
Pod undocumented
311 2 my $self = shift;
312 #my $which = shift || 'source';
313
314 2 foreach my $source (keys %{ $self->__collection() })
315 {
316 4 $self->__collection()->{$source}->open();
317
318 4 ▶ unless ($self->__collection()->{$source}->is_open())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->__collection->{$source}->is_open) 0 4 4
319 {
320 0 $self->__ERROR($self->__collection()->{$source}->__ERROR());
321 0 die "Cannot open ", $self->__collection()->{$source}->create_path();
322 }
323 }
324
325 2 return;
326 }
327  
328 sub close(;$)
329 1 ▶{
Subroutine close: called CC=2 SCAR=6.9
Pod undocumented
330 1 my $self = shift;
331 #my $which = shift || 'source';
332
333 1 foreach my $source (keys %{ $self->__collection() })
334 {
335 2 $self->__collection()->{$source}->close();
336 }
337
338 1 return;
339 }
340  
341 sub show_fields(;$)
342 0 ▶{
Subroutine show_fields: not called CC=2 SCAR=17.9
Pod undocumented
343 0 my $self = shift;
344 0 ▶ my $which = shift || 'source';
Condition0%0/2
shift() || 'source'
l!l
00
MC/DC0%0/1
shift() || 'source'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
345 0 $self->__collection()->{$which}->fields();
346 }
347  
348 sub read(;$$)
349 1 ▶{
Subroutine read: called CC=3 SCAR=11.8
Pod undocumented
350 1 my $self = shift;
351 1 ▶ my $which = shift || 'source';
Condition50%1/2
shift() || 'source'
l!l
01
MC/DC0%0/1
shift() || 'source'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
352
353 1 $self->open();
354 1 my $records = $self->__collection()->{$which}->getrecords();
355
356 0 ▶ return !wantarray ? $records : @{ $records };
Branch: 1 branch
BranchTrueFalseTotal
!wantarray ? : 1 0 1
357 }
358  
359 sub store
360 # Store data source an array ref (of hashes) into a Data::All enabled source
361 # IN: (arrayref) of hashes -- your records
362 # [ standard parameters ]
363 # OUT:
364 0 ▶{
Subroutine store: not called CC=4 SCAR=30.0
Pod undocumented
365 0 my $self = shift;
366 0 my $source = shift;
367 0 my ($target, $bool);
368
369 0 my $args = $self->reinit(@_);
370
371 0 $target = $self->__collection()->{'target'};
372
373 0 $target->open();
374
375 0 $target->fields([keys %{ $source->[0] }])
376 0 ▶ unless ($target->fields() && $#{ $target->fields() });
Branch: 1 branch
BranchTrueFalseTotal
unless $target->fields and $#{$target->fields;} 0 0 0
Condition0%0/3
$target->fields and $#{$target->fields;}
!ll&&!rl&&r
000
MC/DC0%0/2
$target->fields and $#{$target->fields;}
$target->fields $#{$target->fields;}
Truth table0%0/3
A and B
A= $target->fieldsB= $#{$target->fields;}
ABresult
0X0
100
111
377
378 0 ▶ $target->putfields() if ($self->print_fields);
Branch: 1 branch
BranchTrueFalseTotal
if $self->print_fields 0 0 0
379  
380 # Convert data in a wholesome fashion (rather than piecemeal)
381 # There is no point in doing it record by record b/c the
382 # records we are storing are already in memory.
383 0 $bool = $target->putrecords($source, $args) ;
384
385 0 $target->close();
386
387 0 return 1;
388 }
389  
390 sub convert
391 # Move data source one Data::All collection to another, using a simple
392 # source (source) and to (target) metaphor
393 # TODO: need error detection
394 1 ▶{
Subroutine convert: called CC=6 SCAR=20.1
Pod undocumented
395 1 my $self = shift;
396 1 my ($source, $target, $bool);
397
398 1 my $args = $self->reinit(@_);
399  
400 1 ($source, $target) = @{ $self->__collection() }{'source','target'};
401  
402 1 $source->open();
403 1 $target->open();
404
405 # TODO: Get fields source db SELECT before we copy to the $target->fields()
406
407 # Use the source's field names if the target's has none
408 1 ▶ $target->fields($source->fields) unless ($target->fields() && $#{ $target->fields() });
Branch: 1 branch
BranchTrueFalseTotal
unless $target->fields and $#{$target->fields;} 1 0 1
Condition33%1/3
$target->fields and $#{$target->fields;}
!ll&&!rl&&r
010
MC/DC0%0/2
$target->fields and $#{$target->fields;}
$target->fields $#{$target->fields;}
Truth table33%1/3
A and B
A= $target->fieldsB= $#{$target->fields;}
ABresult
0X0
100
111
409
410  
411 # Print the field names into the target
412 # TODO: If the field list is in the source collection, then the
413 # fields will appear twice in the target file.
414 1 ▶ $target->putfields() if ($self->print_fields);
Branch: 1 branch
BranchTrueFalseTotal
if $self->print_fields 0 1 1
415
416 1 ▶ if ($self->atomic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->atomic) { } 1 0 1
417 # Convert data in a wholesome fashion (rather than piecemeal)
418 1 $bool = $target->putrecords([$source->getrecords()], $args) ;
419 }
420 else {
421 # Convert record by record (great for large family members!!!!!!!)
422 0 ▶ while (my $rec = $source->getrecord_hash())
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = $source->getrecord_hash) 0 0 0
423 0 { $bool = $target->putrecord($rec, $args) }
424 }
425
426 # BUG: I commented this out for the extract specifically (delano - May 9)
427 #$target->close();
428 #$source->close();
429
430 1 return $bool;
431 }
432  
433  
434 sub write(;$$)
435 0 ▶{
Subroutine write: not called CC=4 SCAR=30.0
Pod undocumented
436 0 my $self = shift;
437 0 ▶ my $which = shift || 'source';
Condition0%0/2
shift() || 'source'
l!l
00
MC/DC0%0/1
shift() || 'source'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
438 0 ▶ my ($start, $count) = (shift || 0, shift || 0);
Condition0%0/4
shift() || 0
l!l
00
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
Truth table0%0/2
A
A= shift()
Aresult
11
00
439
440 }
441  
442  
443 sub is_open(;$)
444 0 ▶{
Subroutine is_open: not called CC=2 SCAR=17.9
Pod undocumented
445 0 my $self = shift;
446 0 ▶ my $which = shift || 'source';
Condition0%0/2
shift() || 'source'
l!l
00
MC/DC0%0/1
shift() || 'source'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
447
448 0 return $self->__collection()->{'source'}->is_open();
449 }
450  
451  
452  
453  
454  
455  
456  
457  
458  
459  
460 1 1;
461 __END__
« All/Format/Delim.pm ↑ Summary All/IO/FTP.pm »