scripts/fv2xml

filter: Statementstmt 87.8% 101 / 115 Branchbran 52.1% 24 / 46 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 74.4% 140 / 188 CC 24 SCAR 32.4
CC
24
Cov
75%
convert_file
41.4
process_args
4.3
BEGIN
1.0
CRAP 25.6
« blib/lib/Tie/FieldVals/Row.pm ↑ Summary blib/lib/Tie/FieldVals/Join.pm »
1 #!/usr/bin/env perl
2 # vim:ts=8 sw=4 sts=4 ai
3 1 require v5.6.1;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 fv2xml - convert a Tie::FieldVals datafile into XML data.
10  
11 =head1 VERSION
12  
13 This describes version B<0.6203> of fv2xml.
14  
15 =cut
16  
17 1 our $VERSION = '0.6203';
18  
19 =head1 SYNOPSIS
20  
21 fv2xml --help | --manpage | --version
22  
23 fv2xml { --match I<field>=I<pattern> } [ --match_any I<pattern> ]
24 [ --num_recs I<num> ] [ --start_rec I<num> ]
25 { --sort_by I<field> } { --sort_numeric I<field> } { --sort_reversed I<field> }
26 I<datafile> [ I<outfile> ]
27  
28 =head1 DESCRIPTION
29  
30 This script converts a (subset of) a Tie::FieldVals datafile into
31 XML data.
32  
33 =head1 OPTIONS
34  
35 =over
36  
37 =item --datafile I<filename>
38  
39 The input data file (in Tie::FieldVals format).
40  
41 =item --help
42  
43 Print help message and exit.
44  
45 =item --manpage
46  
47 Print the full help documentation (manual page) and exit.
48  
49 =item --match I<field>=I<pattern>
50  
51 Extract a subset of records from the file, by only including
52 those which match the given pattern for the given field. This option
53 can be repeated for multiple fields.
54 For example:
55  
56 --match Author=Mary
57  
58 will give the records for authors which contain the string "Mary".
59  
60 =item --match_any I<pattern>
61  
62 Extract a subset of records from the file by only including
63 those which match the given pattern in any field.
64  
65 =item --num_recs I<n>
66  
67 Extract a subset of at most n records from the collection (it may
68 be less).
69  
70 =item --sort_by I<field>
71  
72 Sort by this field. Can be repeated to sort by multiple fields.
73 Thus, if one wished to sort by Author and then Title, one would give:
74  
75 --sort_by Author --sort_by Title
76  
77 =item --sort_numeric I<field>
78  
79 If sorting by this field, use this to alter the type of the sort, to
80 make it numeric.
81  
82 For example:
83  
84 --sort_by SeriesOrder --sort_numeric SeriesOrder
85  
86 To switch off numeric sort for a field, give the value of 0 to the
87 argument.
88  
89 For example:
90 --sort_by SeriesOrder --sort_numeric SeriesOrder=0
91  
92 =item --sort_reversed I<field>
93  
94 If sorting by this field, use this to alter the direction of the sort, to
95 make it sort reversed.
96  
97 For example:
98 --sort_by Author --sort_reversed Author
99  
100 To switch off reversed sort for a field, give the value of 0 to the
101 argument.
102  
103 For example:
104 --sort_by Author --sort_reversed Author=0
105  
106 =item --start_rec I<n>
107  
108 Extract a subset of records starting from the nth record.
109  
110 =item --verbose
111  
112 Print informational messages.
113  
114 =item --version
115  
116 Print version information and exit.
117  
118 =back
119  
120 =head1 FILE FORMATS
121  
122 =head2 FieldVals Format
123  
124 The input data file is in the form of Field:Value pairs, with each
125 record separated by a line with '=' on it.
126  
127 See L<Tie::FieldVals/FILE FORMAT> for more information.
128  
129 =head2 XML FORMAT
130  
131 The format of the output XML file is as follows:
132  
133 <fv_data>
134 <record>
135 <Field>Value</Field>
136 <AnotherField>AnotherValue</AnotherField>
137 ...
138 </record>
139 ...
140 </fv_data>
141  
142 Each field name is given its own tag, and the element contains
143 the value for that field.
144  
145 =head1 REQUIRES
146  
147 Getopt::Long
148 Pod::Usage
149 Getopt::ArgvFile
150 Data::Dumper
151 Tie::FieldVals
152 Tie::FieldVals::Row
153 Tie::FieldVals::Select
154  
155 =head1 SEE ALSO
156  
157 perl(1)
158 Getopt::Long
159 Getopt::ArgvFile
160 Pod::Usage
161  
162 =cut
163  
164 1 ▶use Getopt::Long 2.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 1 ▶use Getopt::ArgvFile qw(argvFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
166 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 1 ▶use Tie::FieldVals;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 1 ▶use Tie::FieldVals::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 ▶use Tie::FieldVals::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 #========================================================
173 # Subroutines
174 1 ▶sub init_data ($) {
Subroutine init_data: called CC=1 SCAR=0.0
175 1 my $data_ref = shift;
176  
177 # options
178 1 my %default_conf = ();
179 1 $default_conf{debug} = 0;
180 1 $default_conf{manpage} = 0;
181 1 $default_conf{version} = 0;
182 1 $default_conf{verbose} = 0;
183 1 $default_conf{outfile} = '-';
184 1 $default_conf{datafile} = '';
185 1 $default_conf{sort_by} = [];
186 1 $default_conf{sort_numeric} = {};
187 1 $default_conf{sort_reversed} = {};
188 1 $default_conf{start_rec} = 0;
189 1 $default_conf{num_recs} = -1;
190 1 $default_conf{match} = {};
191 1 $default_conf{match_any} = '';
192 1 $data_ref->{options} = \%default_conf;
193 } # init_data
194  
195 1 ▶sub process_args ($) {
Subroutine process_args: called CC=4 SCAR=14.6
196 1 my $data_ref = shift;
197  
198 1 my $ok = 1;
199  
200 1 argvFile(home=>1,current=>1,startupFilename=>'.fv2xmlrc');
201  
202 1 ▶ pod2usage(2) unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 1 1
203  
204 1 my $op = new Getopt::Long::Parser;
205 1 $op->configure(qw(auto_version auto_help));
206 $op->getoptions($data_ref->{options},
207 1 ▶ 'verbose!',
Branch: 1 branch
BranchTrueFalseTotal
$op->getoptions($data_ref->{"options"}, "verbose!", "manpage", "debug!", "datafile=s", "sort_by=s\@", "sort_numeric:1%", "sort_reversed:1%", "start_rec=i", "num_recs=i", "match=s%", "match_any=s", "outfile=s") or pod2usage(2) 1 0 1
208 'manpage',
209 'debug!',
210 'datafile=s',
211 'sort_by=s@',
212 'sort_numeric:1%',
213 'sort_reversed:1%',
214 'start_rec=i',
215 'num_recs=i',
216 'match=s%',
217 'match_any=s',
218 'outfile=s',
219 ) or pod2usage(2);
220  
221 1 ▶ if ($data_ref->{options}->{'manpage'})
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref->{"options"}{"manpage"}) 0 1 1
222 {
223 0 pod2usage({ -message => "$0 version $VERSION",
224 -exitval => 0,
225 -verbose => 2,
226 });
227 }
228  
229 } # process_args
230  
231 1 ▶sub convert_file ($) {
Subroutine convert_file: called CC=21 SCAR=37.2
232 1 my $data_ref = shift;
233  
234 1 my $outfile = $data_ref->{options}->{outfile};
235  
236 print STDERR "datafile: ", $data_ref->{options}->{datafile},
237 " outfile: ", $outfile,
238 1 ▶ "\n" if ($data_ref->{options}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $data_ref->{"options"}{"verbose"} 0 1 1
239 1 ▶ if ($data_ref->{options}->{verbose}
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref->{"options"}{"verbose"} and %{$data_ref->{"options"}{"match"};}) 0 1 1
Condition33%1/3
$data_ref->{"options"}{"verbose"} and %{$data_ref->{"options"}{"match"};}
!ll&&!rl&&r
100
MC/DC0%0/2
$data_ref->{"options"}{"verbose"} and %{$data_ref->{"options"}{"match"};}
$data_ref->{"options"}{"verbose"} %{$data_ref->{"options"}{"match"};}
Truth table33%1/3
A and B
A= $data_ref->{"options"}{"verbose"}B= %{$data_ref->{"options"}{"match"};}
ABresult
0X0
100
111
240 0 && %{$data_ref->{options}->{match}})
241 {
242 0 print STDERR Data::Dumper->Dump([$data_ref->{options}->{match}],
243 [qw(match)]);
244 }
245 1 ▶ if ($data_ref->{options}->{verbose}
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref->{"options"}{"verbose"} and $data_ref->{"options"}{"match_any"}) 0 1 1
Condition33%1/3
$data_ref->{"options"}{"verbose"} and $data_ref->{"options"}{"match_any"}
!ll&&!rl&&r
100
MC/DC0%0/2
$data_ref->{"options"}{"verbose"} and $data_ref->{"options"}{"match_any"}
$data_ref->{"options"}{"verbose"} $data_ref->{"options"}{"match_any"}
Truth table33%1/3
A and B
A= $data_ref->{"options"}{"verbose"}B= $data_ref->{"options"}{"match_any"}
ABresult
0X0
100
111
246 && $data_ref->{options}->{match_any})
247 {
248 0 print STDERR "match any: ", $data_ref->{options}->{match_any}, "\n";
249 }
250 1 ▶ if ($data_ref->{options}->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref->{"options"}{"debug"}) 0 1 1
251 {
252 0 print STDERR Data::Dumper->Dump([$data_ref], [qw(fv2xml)]);
253 }
254 # get the records, sort them, and grab them
255 1 my @sel_recs = ();
256 1 Tie::FieldVals::Select::debug($data_ref->{options}->{'debug'});
257 my $sel_obj = tie @sel_recs, 'Tie::FieldVals::Select',
258 datafile=>$data_ref->{options}->{'datafile'},
259 selection=>$data_ref->{options}->{'match'},
260 1 match_any=>$data_ref->{options}->{'match_any'};
261 1 $data_ref->{_sel_recs} = \@sel_recs;
262 1 $data_ref->{_sel_obj} = $sel_obj;
263 1 ▶ if (@{$data_ref->{options}->{sort_by}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$data_ref->{"options"}{"sort_by"};}) 0 1 1
264 {
265 $sel_obj->sort_records(
266 sort_by=>$data_ref->{options}->{'sort_by'},
267 sort_numeric=>$data_ref->{options}->{'sort_numeric'},
268 0 sort_reversed=>$data_ref->{options}->{'sort_reversed'});
269 }
270  
271 1 my $count = @sel_recs;
272 1 ▶ print STDERR "$count records found\n" if ($data_ref->{options}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $data_ref->{"options"}{"verbose"} 0 1 1
273  
274 1 my $first_rec = 0;
275 1 ▶ if ($data_ref->{options}->{start_rec} >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref->{"options"}{"start_rec"} >= 0 and $data_ref->{"options"}{"start_rec"} < $count) 1 0 1
Condition33%1/3
$data_ref->{"options"}{"start_rec"} >= 0 and $data_ref->{"options"}{"start_rec"} < $count
!ll&&!rl&&r
001
MC/DC0%0/2
$data_ref->{"options"}{"start_rec"} >= 0 and $data_ref->{"options"}{"start_rec"} < $count
$data_ref->{"options"}{"start_rec"} >= 0 $data_ref->{"options"}{"start_rec"} < $count
Truth table33%1/3
A and B
A= $data_ref->{"options"}{"start_rec"} >= 0B= $data_ref->{"options"}{"start_rec"} < $count
ABresult
0X0
100
111
276 and $data_ref->{options}->{start_rec} < $count)
277 {
278 1 $first_rec = $data_ref->{options}->{start_rec};
279 }
280 1 my $last_rec = $first_rec + $data_ref->{options}->{num_recs};
281 1 ▶ if ($data_ref->{options}->{num_recs} <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref->{"options"}{"num_recs"} <= 0) 1 0 1
282 {
283 1 $last_rec = $count;
284 }
285 1 ▶ if ($last_rec > $count)
Branch: 1 branch
BranchTrueFalseTotal
if ($last_rec > $count) 0 1 1
286 {
287 0 $last_rec = $count;
288 }
289 1 ▶ if ($first_rec > $last_rec)
Branch: 1 branch
BranchTrueFalseTotal
if ($first_rec > $last_rec) 0 1 1
290 {
291 0 warn "record number $first_rec > $last_rec -- aborting\n";
292 0 return 0;
293 }
294 1 ▶ print STDERR "records from #$first_rec to #$last_rec\n" if ($data_ref->{options}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $data_ref->{"options"}{"verbose"} 0 1 1
295  
296 # for each record, create the XML version
297 1 ▶ if ($outfile ne '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile ne '-') { } 1 0 1
298 {
299 1 ▶ open(OUTFILE, ">$outfile") || die "Can't open '$outfile' for writing.";
Branch: 1 branch
BranchTrueFalseTotal
open OUTFILE, ">$outfile" or die "Can't open '${outfile}' for writing." 1 0 1
300 1 print OUTFILE "<fv_data>\n";
301 }
302 else
303 {
304 0 print "<fv_data>\n";
305 }
306 1 ▶ for (my $i=$first_rec; $i < $last_rec; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $last_rec) 119 1 120
307 {
308 119 my $vals = $sel_recs[$i];
309 119 my $row_obj = tied %{$vals};
310 119 my $xml_str = $row_obj->get_xml_string();
311 119 ▶ if ($outfile eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile eq '-') { } 0 119 119
312 {
313 0 print $xml_str;
314 }
315 else
316 {
317 119 print OUTFILE $xml_str;
318 }
319 }
320 1 ▶ if ($outfile ne '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile ne '-') { } 1 0 1
321 {
322 1 print OUTFILE "</fv_data>\n";
323 1 close(OUTFILE);
324 }
325 else
326 {
327 0 print "</fv_data>\n";
328 }
329 1 ▶ print STDERR "done!\n" if ($data_ref->{options}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $data_ref->{"options"}{"verbose"} 0 1 1
330 } # convert_file
331  
332 #========================================================
333 # Main
334  
335 MAIN: {
336 1 my %data = ();
337  
338 1 init_data(\%data);
339 1 process_args(\%data);
340  
341 # first argument is the datafile
342 1 ▶ if (@ARGV)
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 1 0 1
343 {
344 1 $data{options}->{datafile} = shift @ARGV;
345 }
346 1 ▶ if (!$data{options}->{datafile})
Branch: 1 branch
BranchTrueFalseTotal
unless ($data{"options"}{"datafile"}) 0 1 1
347 {
348 0 print STDERR "$0 no datafile\n";
349 0 return 0;
350 }
351  
352 # remaining argument is the output file
353 1 ▶ if (@ARGV)
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 1 0 1
354 {
355 1 $data{options}->{outfile} = shift @ARGV;
356 }
357 1 convert_file(\%data);
358 }
359  
360 =head1 BUGS
361  
362 Please report any bugs or feature requests to the author.
363  
364 =head1 AUTHOR
365  
366 Kathryn Andersen (RUBYKAT)
367 perlkat AT katspace dot com
368 http://www.katspace.com
369  
370 =head1 COPYRIGHT AND LICENCE
371  
372 Copyright (c) 2004 by Kathryn Andersen
373  
374 This program is free software; you can redistribute it and/or modify it
375 under the same terms as Perl itself.
376  
377  
378 =cut
379  
380 __END__
« blib/lib/Tie/FieldVals/Row.pm ↑ Summary blib/lib/Tie/FieldVals/Join.pm »