File/Spec/Dated.pm

filter: Statementstmt 22.1% 23 / 104 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 26.9% 7 / 26 Podpod 100% 10 / 10 totaltotal 25.3% 40 / 158 CC 11 SCAR 18.1
CC
11
Cov
19%
reset_name
20.0
splitpath
12.0
dates
12.0
CRAP 6.1
« File/Spec/BaseParse.pm ↑ Summary Document/Members.pm »
1 #================================ Dated.pm ===================================
2 # Filename: Dated.pm
3 # Description: Object to parse File::Spec::Dated archive filenames.
4 # Programmed by: Dale Amon
5 # Revised by: $Author: amon $
6 # Date: $Date: 2008-08-28 23:32:45 $
7 # Version: $Revision: 1.3 $
8 # License: LGPL 2.1, Perl Artistic or BSD
9 #
10 #=============================================================================
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fault::ErrorHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Fault::DebugPrinter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use DMA::ISODate;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use DMA::FSM;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec::BaseParse;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 package File::Spec::Dated;
19 1 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 @ISA = qw( File::Spec::BaseParse );
21  
22 #=============================================================================
23 # Object Methods
24 #=============================================================================
25 # Local Lexical Analyzers for splitpath method.
26  
27 0 ▶sub _nullFilename {
Subroutine _nullFilename: not called CC=1 SCAR=6.9
28 0 my ($lexeme, $bb) = @_;
29 0 Fault::ErrorHandler->warn ("Impossible state: Missing first lexeme");
30 0 $bb->{'tail'} = "startdate";
31 0 return (1,$lexeme);
32 }
33  
34 0 ▶sub _getFirstDate {
Subroutine _getFirstDate: not called CC=2 SCAR=17.9
35 0 my ($lexeme, $bb) = @_;
36  
37 # This is a a hack because my printer needs a toner cartridge and ISODate
38 # parsing is too much of bother to attempt solely on screen. It catches the
39 # special case of '<date>.<extentions>'
40 #
41 0 my ($lpar, $rpar) = ($lexeme =~ /^(.*)(\..*)?$/);
42  
43 0 my $date = DMA::ISODate->new($lpar);
44 0 ▶ if ($date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date) 0 0 0
45 0 @$bb{'startdate','tail'} = ($date->get,"startdate"); return (1,$rpar);}
46 0 $bb->_err("Filename does not begin with an ISO date"); return (0,$lexeme);
47 }
48  
49 0 ▶sub _endsWithStartDate {
Subroutine _endsWithStartDate: not called CC=1 SCAR=6.9
50 0 my ($lexeme, $bb) = @_;
51 0 $bb->_err("Name truncated after start date"); return (1,$lexeme);}
52  
53 0 ▶sub _getSecondDate {
Subroutine _getSecondDate: not called CC=2 SCAR=17.9
54 0 my ($lexeme, $bb) = @_;
55  
56 # This is a a hack because my printer needs a toner cartridge and ISODate
57 # parsing is too much of bother to attempt solely on screen. It catches the
58 # special case of '<date>.<extentions>'
59 #
60 0 my ($lpar, $rpar) = ($lexeme =~ /^(.*)(\..*)$/);
61 0 my $date = DMA::ISODate->new($lexeme);
62 0 ▶ if ($date) {@$bb{'enddate','tail'}
Branch: 1 branch
BranchTrueFalseTotal
if ($date) 0 0 0
63 0 = ($date->get,"enddate"); return (1,$rpar);}
64 0 return (0,$lexeme);
65 }
66  
67 sub _noBody
68 0 ▶ {my ($lexeme, $bb) = @_; $bb->_err("No name_body section found");
Subroutine _noBody: not called CC=1 SCAR=6.9
69 0 return (1,undef);}
70  
71 sub _getFirstBody
72 0 ▶ {my ($lexeme, $bb) = @_;
Subroutine _getFirstBody: not called CC=1 SCAR=6.9
73 0 @$bb{'name_body','tail'} = ($lexeme,"name_body"); return (1,undef);}
74  
75 sub _getBody
76 0 ▶ {my ($lexeme, $bb) = @_;
Subroutine _getBody: not called CC=1 SCAR=6.9
77 0 $bb->{'name_body'} .= "-" . $lexeme;
78 0 $bb->{'tail'} = "name_body";
79 0 return (1,undef);
80 }
81  
82 0 ▶sub _noop {my ($lexeme, $bb) = @_; return (1,$lexeme);}
Subroutine _noop: not called CC=1 SCAR=6.9
83  
84 #-----------------------------------------------------------------------------
85  
86 0 ▶sub splitpath {
Subroutine splitpath: not called CC=3 SCAR=24.8
Pod documented
87 0 my ($self,$file) = (shift, shift);
88 0 my ($hasdots,$root,@extensions,$lexeme);
89  
90 0 my $fst =
91 { 'S0' => ["E0","SAME", \&_getFirstDate, "S1","TSTL","S2","SAME"],
92 'S1' => ["D1","SAME", \&_getSecondDate, "S2","TSTL","S2","SAME"],
93 'S2' => ["D2","SAME", \&_getFirstBody, "S3","NEXT","S3","NEXT"],
94 'S3' => ["D0","SAME", \&_getBody, "S3","NEXT","S3","NEXT"],
95 'D0' => ["D0","DONE", \&_noop, "","","",""],
96 'E0' => ["E0","FAIL", \&_nullFilename, "","","",""],
97 'D1' => ["D1","DONE", \&_endsWithStartDate, "","","",""],
98 'D2' => ["D2","DONE", \&_noBody, "","","",""],
99 };
100  
101 0 $self->SUPER::splitpath ($file);
102 0 Fault::DebugPrinter->dbg (4, "Beginning parse for File::Spec::Dated");
103  
104 # This is just a fast way to get name + extensions. I could have used
105 # _append_extensions_to_tail.
106 #
107 0 $self->{'name'} = $self->{'filename'};
108  
109 # Reparse the filename. We are more persnickity about extensions at this
110 # level: it is not an extension unless it occurs after the last "-" in the
111 # name.
112 #
113 0 my @lexlst = split (/-/, $self->{'name'});
114  
115 0 my @remaining = DMA::FSM::FSM ( $fst, $self, @lexlst);
116 0 delete $self->{'state'};
117  
118 # If name_body was the tailpart, see if it has trailing extensions. If the
119 # tailpart were dates, there could not have been anything leftover.
120 #
121 0 {$_ = $self->{'tail'};
122 0 ▶ if (/name_body/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/name_body/) 0 0 0
123 0 my $lpar = $self->_parse_extensions_from_tail;
124 0 ▶ $self->{$_} = ($lpar) ? $lpar : undef;
Branch: 1 branch
BranchTrueFalseTotal
$lpar ? : 0 0 0
125 0 $self->reset_name;
126 }
127 }
128  
129 return (@$self{'volume','basepath','directory',
130 'startdate','enddate','name_body'},
131 0 (@{$self->{'extensions'}}));
132 }
133  
134 #-----------------------------------------------------------------------------
135 # Set parts of name
136  
137 0 ▶sub set_startdate {my $s=shift; @$s{'startdate','_dirty'}=(shift,1);
Subroutine set_startdate: not called CC=1 SCAR=6.9
Pod documented
138 0 return $s;}
139  
140 0 ▶sub set_enddate {my $s=shift; @$s{'enddate', '_dirty'}=(shift,1);
Subroutine set_enddate: not called CC=1 SCAR=6.9
Pod documented
141 0 return $s;}
142  
143 0 ▶sub set_name_body {my $s=shift; @$s{'name_body','_dirty'}=(shift,1);
Subroutine set_name_body: not called CC=1 SCAR=6.9
Pod documented
144 0 return $s;}
145  
146 #-----------------------------------------------------------------------------
147  
148 0 ▶sub reset_name {
Subroutine reset_name: not called CC=4 SCAR=30.0
Pod documented
149 0 my $self = shift;
150 0 my ($name,$del) = ("","");
151 0 foreach (@$self{'startdate','enddate','name_body'}) {
152 0 ▶ $_ || next;
Branch: 1 branch
BranchTrueFalseTotal
$_ or next 0 0 0
153 0 $name .= "$del$_"; $del = "-";
154 }
155 0 ▶ return $self->{'name'} = ($name) ? $name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
156 }
157  
158 #-----------------------------------------------------------------------------
159  
160 0 ▶sub startdate {return shift->{'startdate'};}
Subroutine startdate: not called CC=1 SCAR=6.9
Pod documented
161 0 ▶sub enddate {return shift->{'enddate'};}
Subroutine enddate: not called CC=1 SCAR=6.9
Pod documented
162 0 ▶sub name_body {return shift->{'name_body'};}
Subroutine name_body: not called CC=1 SCAR=6.9
Pod documented
163  
164 #-----------------------------------------------------------------------------
165  
166 0 ▶sub dates {
Subroutine dates: not called CC=3 SCAR=24.8
Pod documented
167 0 my $self = shift;
168 0 my ($beg,$end) = ($self->{'startdate'}, $self->{'enddate'});
169 0 ▶ defined $beg || (return undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $beg or return undef 0 0 0
170 0 ▶ defined $end || (return $beg);
Branch: 1 branch
BranchTrueFalseTotal
defined $end or return $beg 0 0 0
171 0 return $beg . "-" . $end;
172 }
173  
174 #-----------------------------------------------------------------------------
175  
176 0 ▶sub undated_filename {
Subroutine undated_filename: not called CC=2 SCAR=17.9
Pod documented
177 0 my $self = shift;
178 0 my $extensions = $self->extensions;
179 0 ▶ return $self->{'name_body'} . (($extensions) ? ".$extensions" : "");
Branch: 1 branch
BranchTrueFalseTotal
$extensions ? : 0 0 0
180 }
181  
182 #=============================================================================
183 # INTERNAL: Object Methods
184 #=============================================================================
185  
186 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
187 0 my $self = shift;
188 0 $self->SUPER::_init;
189 0 @$self{'startdate','enddate','name_body','extension','extensions'} =
190 ( undef,undef,undef,undef,[] );
191 }
192
193 #=============================================================================
194 # Pod Documentation
195 #=============================================================================
196 # You may extract and format the documention section with the 'perldoc' cmd.
197  
198 =head1 NAME
199  
200 File::Spec::Dated - Object to parse dated archive filenames.
201  
202 =head1 SYNOPSIS
203  
204 use File::Spec::Dated;
205  
206 ($volume, $basepath, $directory, $startdate, $enddate, $name_body, @extensions) = $self->splitpath ($filepath);
207  
208 $startdate = $self->startdate;
209 $enddate = $self->enddate;
210 $dates = $self->dates;
211 $undated = $self->undated_filename;
212 $name_body = $self->name_body;
213 $self = $self->set_startdate ($startdate);
214 $self = $self->set_enddate ($enddate);
215 $self = $self->set_name_body ($name_body);
216 $name = $self->reset_name;
217  
218  
219 =head1 Inheritance
220  
221 UNIVERSAL
222 File::Spec::Unix
223 File::Spec::BaseParse
224 File::Spec::Dated
225  
226 =head1 Description
227  
228 Split a filename path into segments used by virtually all archiver
229 applications. This includes the segmentation done by the File::Spec:Archivist
230 plus a further breakdown of the filename part it returns.
231  
232 For example, if the filename were 19901225-XMAS-Title-Subtitle-note.tar.gz
233 would be further split as:
234  
235 startdate: 19991225
236 enddate: undef
237 name_body: XMAS-Title-Subtitle-note
238 extensions: (tar gz)
239 extension: gz
240  
241 Extensions are re-parsed here with more constraints on what is considered an
242 extension. It must not only be dot delimited, but also be left of the
243 last - i the filename. Eve so, there is still an ambiguity problem to be
244 dealt with here and in child classes. We cannot always be certain that an
245 extension really is an extension. For example, if the filename were
246 DATE-XYZ.ABC, it could be parsed in a number of ways:
247  
248 name_body: XYZ
249 extension: ABC
250  
251 or
252 name_body: XYZ.ABC
253  
254 The later may seem unlikely, but here are examples showing that it isn't:
255  
256 19991225-XMAS-CardGenerator-V1.1
257 19991225-XMAS-ACarol-p100.1a
258  
259 =head1 Examples
260  
261 use File::Spec::Dated;
262 my $baz = File::Spec::Dated->new;
263 my @list = $baz->splitpath
264 ("/my/base/Cards/19901225-XMAS-Title-Subtitle-note.tar.gz");
265  
266 my $foo = File::Spec::Dated->new
267 ("/my/base/Cards/19901225-XMAS-Title-Subtitle-note.tar.gz");
268  
269 my $startdate = $foo->startdate;
270 my $enddate = $foo->enddate;
271 my $dates = $foo->dates;
272 my $undated = $obj->undated_filename;
273 my $name_body = $foo->name_body;
274 my @extensions = $foo->extensions;
275 my $extensions = $foo->extensions;
276 my $extension = $foo->extension;
277  
278 $foo->set_startdate ("19901225120000");
279 $foo->set_enddate ("20001225120000");
280 $foo->set_name_body ("XMAS-Title-Subtitle-note");
281 $foo->set_extensions ("jpeg" );
282 my $name = $foo->reset_name;
283 my $filename = $foo->reset_filename;
284 my $filepath = $foo->reset_pathname;
285 my @parts = $foo->reparse;
286  
287 $foo->set_extensions ("tar","gz");
288 $filename = $foo->reset_filename;
289 $filepath = $foo->reset_pathname;
290 @parts = $foo->reparse;
291  
292 =head1 Class Variables
293  
294 None.
295  
296 =head1 Instance Variables
297  
298 startdate The starting date string.
299 enddate The ending date string.
300 name_body The "body" portion of the filename.
301 extensions A list, left to right, of all file extensions found.
302 extension Last or rightmost of the file extensions found.
303  
304 =head1 Class Methods
305  
306 None.
307  
308 =head1 Instance Methods
309  
310 =over 4
311  
312 =item B<$dates = $obj-E<gt>dates>
313  
314 Return a $dates string suitable for use in an index or table of contents,
315 eg "19991225", "19991225103015", "19991225-20000101" or
316 "19991225103000-19991225113000".
317  
318 Returns undef if there is no date information associated with this filespec.
319  
320 =item B<$enddate = $obj-E<gt>enddate>
321  
322 Return the end date string or undef if none.
323  
324 =item B<$name_body = $obj-E<gt>name_body>
325  
326 Return the name_body or undef if none.
327  
328 =item B<$name = $obj-E<gt>reset_name>
329  
330 Regenerate name from parts:
331  
332 startdate + enddate + name_body -> name
333  
334 =item B<$obj = $obj-E<gt>set_enddate ($enddate)>
335  
336 Unconditionally set the end date of the filename.
337  
338 =item B<$obj = $obj-E<gt>set_name_body ($name_body)>
339  
340 Unconditionally set the name_body of the filename.
341  
342 =item B<$obj = $obj-E<gt>set_startdate ($startdate)>
343  
344 Unconditionally set the start date of the filename.
345  
346 =item B<($volume, $basepath, $directory, $startdate, $enddate, $name_body, @extensions) = $obj-E<gt>splitpath ($filepath)>
347  
348 Parses the filename into:
349  
350 firstdate{-lastdate}{-name_body}{.extensions}
351  
352 and returns all the elements of the pathname and filename as a list.
353 Completely reinitializes the object for the name $filepath. Chains to parent
354 class method.
355  
356 =item B<$startdate = $obj-E<gt>startdate>
357  
358 Return the start date string or undef if none.
359  
360 =item B<$undated = $obj-E<gt>undated_filename>
361  
362 If a filename has dates on the left, return the remainder; if there is no
363 date part do nothing.
364  
365 For example, whether the original filename is 20040817-filename.tar.gz or
366 just filename.tar.gz it will return filename.tar.gz. This is useful in
367 applications which deal with both Archivist and non-Archivist filenames
368 and which may need to shift a file back and forth between the two universes.
369  
370 =back 4
371  
372 =head1 Private Class Methods
373  
374 None.
375  
376 =head1 Private Instance Methods
377  
378 =over 4
379  
380 =item B<$obj = $obj-E<gt>_init>
381  
382 Internal initializer. Chains to parent class method.
383  
384 This method is for the subclass initializer chaining and should not be used
385 otherwise.
386  
387 =back 4
388  
389 =head1 KNOWN BUGS
390  
391 See TODO.
392  
393 =head1 SEE ALSO
394  
395 Fault::ErrorHandler, Fault::DebugPrinter, DMA::ISODate, DMA::FSM, File::Spec::BaseParse
396  
397 =head1 AUTHOR
398  
399 Dale Amon <amon@vnl.com>
400  
401 =cut
402
403 #=============================================================================
404 # CVS HISTORY
405 #=============================================================================
406 # $Log: Dated.pm,v $
407 # Revision 1.3 2008-08-28 23:32:45 amon
408 # perldoc section regularization.
409 #
410 # Revision 1.2 2008-08-16 17:49:06 amon
411 # Update source format, documentation; switch to Fault package
412 #
413 # Revision 1.1.1.1 2004-09-17 16:52:34 amon
414 # File Spec extensions for doc name formats.
415 #
416 # 20040917 Dale Amon <amon@islandone.org>
417 # Added undated_filename method.
418 #
419 # 20040821 Dale Amon <amon@islandone.org>
420 # Switched to Finite State Machine for parsing.
421 #
422 # 20040820 Dale Amon <amon@islandone.org>
423 # Modified the name of the parent to File::Spec::BaseParse.
424 #
425 # 20040815 Dale Amon <amon@islandone.org>
426 # Changed from Archivist::FileSpec to File::Spec::Dated.
427 #
428 # 20021208 Dale Amon <amon@vnl.com>
429 # Hacked it apart into a Class hierarchy.
430 #
431 # 20021121 Dale Amon <amon@vnl.com>
432 # Created.
433 #
434 1 1;
« File/Spec/BaseParse.pm ↑ Summary Document/Members.pm »