Sticker/Scribe.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Sticker.pm ↑ Summary Sticker/Scribe/Mp3.pm »
1 package File::Sticker::Scribe;
2 $File::Sticker::Scribe::VERSION = '4.9';
3 =head1 NAME
4  
5 File::Sticker::Scribe - read, write and standardize meta-data from files
6  
7 =head1 VERSION
8  
9 version 4.9
10  
11 =head1 SYNOPSIS
12  
13 use File::Sticker::Scribe;
14  
15 my $scribe = File::Sticker::Scribe->new(%args);
16  
17 my $meta = $scribe->read_meta($filename);
18  
19 $scribe->write_meta(%args);
20  
21 =head1 DESCRIPTION
22  
23 This will read and write meta-data from files in various formats,
24 and standardize it to a common nomenclature,
25 such as "tags" for things called tags, or Keywords or Subject etc.
26  
27 The standard nomenclature is:
28  
29 =over
30  
31 =item url
32  
33 The source URL of this file (ref 'dublincore.source')
34  
35 =item creator
36  
37 The author or artist who created this. (ref 'dublincore.creator')
38  
39 =item title
40  
41 The title of the item. (ref 'dublincore.title')
42  
43 =item description
44  
45 The description of the item. (ref 'dublincore.description')
46  
47 =item tags
48  
49 The item's tags. (ref 'Keywords').
50  
51 =back
52  
53 Other fields will be called whatever the user has pre-configured.
54  
55 =cut
56  
57 2 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 0 ▶use File::LibMagic;
Subroutine BEGIN: called CC=1 SCAR=2.6
59 use YAML::Any;
60 use List::MoreUtils qw(uniq);
61  
62 =head1 DEBUGGING
63  
64 =head2 whoami
65  
66 Used for debugging info
67  
68 =cut
69 sub whoami { ( caller(1) )[3] }
70  
71 =head1 METHODS
72  
73 =head2 new
74  
75 Create a new object, setting global values for the object.
76  
77 my $obj = File::Sticker::Scribe->new();
78  
79 =cut
80  
81 sub new {
82 my $class = shift;
83 my %parameters = (@_);
84 my $self = bless ({%parameters}, ref ($class) || $class);
85  
86 return ($self);
87 } # new
88  
89 =head2 init
90  
91 Initialize the object.
92 Check if all the required parameters are there.
93  
94 $scribe->init(wanted_fields=>{title=>'TEXT',count=>'NUMBER',tags=>'MULTI'});
95  
96 =cut
97  
98 sub init {
99 my $self = shift;
100 my %parameters = @_;
101  
102 foreach my $key (keys %parameters)
103 {
104 $self->{$key} = $parameters{$key};
105 }
106 $self->{file_magic} = File::LibMagic->new(follow_symlinks=>1);
107  
108 # If we want to read all, then create wanted fields if we don't have them
109 if ($self->{read_all}
110 and !(exists $self->{wanted_fields}
111 and defined $self->{wanted_fields})
112 )
113 {
114 $self->{wanted_fields} = {};
115 my $known = $self->known_fields();
116 foreach my $field (keys %{$known})
117 {
118 $self->{wanted_fields}->{$field} = $known->{$field};
119 }
120 my $readonly = $self->readonly_fields();
121 foreach my $field (keys %{$readonly})
122 {
123 $self->{wanted_fields}->{$field} = $readonly->{$field};
124 }
125 }
126 # Set the writable fields from the known and readonly fields
127 if (exists $self->{wanted_fields}
128 and defined $self->{wanted_fields})
129 {
130 my %writable = ();
131 my $known = $self->known_fields();
132 my $readonly = $self->readonly_fields();
133 foreach my $field (keys %{$known})
134 {
135 # If it is Known and Not Readonly, it is writable
136 if (!(exists $readonly->{$field}
137 and defined $readonly->{$field}))
138 {
139 $writable{$field} = $known->{$field};
140 }
141 }
142 $self->{writable_fields} = \%writable;
143 }
144 } # init
145  
146 =head2 name
147  
148 The name of the scribe; this is basically the last component
149 of the module name. This works as either a class function or a method.
150  
151 $name = $self->name();
152  
153 $name = File::Sticker::Scribe::name($class);
154  
155 =cut
156  
157 sub name {
158 my $class = shift;
159
160 my $fullname = (ref ($class) ? ref ($class) : $class);
161  
162 my @bits = split('::', $fullname);
163 return pop @bits;
164 } # name
165  
166 =head2 priority
167  
168 The priority of this scribe. Scribes with higher priority
169 get tried first. This is useful where there may be more
170 than one possible meta-data format for a file, such as
171 EXIF versus XATTR.
172  
173 This works as either a class function or a method.
174  
175 This must be overridden by the specific scribe class.
176  
177 $priority = $self->priority();
178  
179 $priority = File::Sticker::Scribe::priority($class);
180  
181 =cut
182  
183 sub priority {
184 my $class = shift;
185 return 0;
186 } # priority
187  
188 =head2 allow
189  
190 If this scribe can be used for the given file and the wanted_fields,
191 then this returns true.
192  
193 if ($scribe->allow($file))
194 {
195 ....
196 }
197  
198 =cut
199  
200 sub allow {
201 my $self = shift;
202 my $file = shift;
203 say STDERR whoami() if $self->{verbose} > 2;
204  
205 my $okay = $self->allowed_file($file);
206 if ($okay) # okay so far
207 {
208 $okay = $self->allowed_fields();
209 }
210 return $okay;
211 } # allow
212  
213 =head2 allowed_file
214  
215 If this scribe can be used for the given file, then this returns true.
216 This must be overridden by the specific scribe class.
217  
218 if ($scribe->allowed_file($file))
219 {
220 ....
221 }
222  
223 =cut
224  
225 sub allowed_file {
226 my $self = shift;
227 my $file = shift;
228  
229 return 0;
230 } # allowed_file
231  
232 =head2 allowed_fields
233  
234 If this writer can be used for the known and wanted fields, then this returns true.
235 By default, if there are no wanted_fields, this returns false.
236 (But this may be overridden by subclasses)
237  
238 if ($writer->allowed_fields())
239 {
240 ....
241 }
242  
243 =cut
244  
245 sub allowed_fields {
246 my $self = shift;
247  
248 my $okay = 1;
249 if (exists $self->{wanted_fields}
250 and defined $self->{wanted_fields})
251 {
252 # the wanted fields must be a subset of the (known fields + readonly fields)
253 my $known_fields = $self->known_fields();
254 my $readonly_fields = $self->readonly_fields();
255 foreach my $fn (keys %{$self->{wanted_fields}})
256 {
257 if ((!exists $known_fields->{$fn}
258 or !defined $known_fields->{$fn}
259 or !$known_fields->{$fn})
260 and (!exists $readonly_fields->{$fn}
261 or !defined $readonly_fields->{$fn}
262 or !$readonly_fields->{$fn}))
263 {
264 $okay = 0;
265 last;
266 }
267 }
268 }
269 else
270 {
271 say STDERR 'Scribe ' . $self->name() . ' was not given wanted_fields' if $self->{verbose} > 1;
272 $okay = 0;
273 }
274 return $okay;
275 } # allowed_fields
276  
277 =head2 known_fields
278  
279 Returns the fields which this scribe knows about.
280  
281 This must be overridden by the specific scribe class.
282  
283 my $known_fields = $scribe->known_fields();
284  
285 =cut
286  
287 sub known_fields {
288 my $self = shift;
289  
290 return undef;
291 } # known_fields
292  
293 =head2 readonly_fields
294  
295 Returns the fields which this scribe knows about, which can't be overwritten,
296 but are allowed to be "wanted" fields. Things like file-size etc.
297  
298 my $readonly_fields = $scribe->readonly_fields();
299  
300 =cut
301  
302 sub readonly_fields {
303 my $self = shift;
304  
305 return {filesize=>'NUMBER'};
306 } # readonly_fields
307  
308 =head2 writable_fields
309  
310 Returns the fields which this scribe knows about, which can be written into.
311  
312 my $writable_fields = $scribe->writable_fields();
313  
314 =cut
315  
316 sub writable_fields {
317 my $self = shift;
318  
319 return $self->{writable_fields};
320 } # writable_fields
321  
322 =head2 read_meta
323  
324 Read the meta-data from the given file.
325  
326 This must be overridden by the specific scribe class.
327  
328 my $meta = $scribe->read_meta($filename);
329  
330 =cut
331  
332 sub read_meta {
333 my $self = shift;
334 my $filename = shift;
335  
336 } # read_meta
337  
338 =head2 add_field_to_file
339  
340 Adds a field to a file, taking account of whether it is a multi-value field or not.
341 This requires the old meta-data for the file to be passed in.
342  
343 $scribe->add_field_to_file(filename=>$filename,
344 field=>$field,
345 value=>$value,
346 old_meta=>\%meta);
347  
348 =cut
349 sub add_field_to_file {
350 my $self = shift;
351 my %args = @_;
352 say STDERR whoami() if $self->{verbose} > 2;
353  
354 my $filename = $args{filename};
355 my $field = $args{field};
356 my $value = $args{value};
357 my $old_meta = $args{old_meta};
358  
359 my $type = (
360 exists $self->{wanted_fields}->{$field}
361 and defined $self->{wanted_fields}->{$field}
362 ? $self->{wanted_fields}->{$field}
363 : 'UNKNOWN'
364 );
365 say STDERR "field=$field value=$value type=$type" if $self->{verbose} > 2;
366 if ($type =~ /multi/i)
367 {
368 return $self->update_multival_field(
369 filename=>$filename,
370 field=>$field,
371 value=>$value,
372 old_vals=>$old_meta->{$field});
373 }
374 else
375 {
376 $self->replace_one_field(
377 filename=>$filename,
378 field=>$field,
379 value=>$value);
380 }
381 } # add_field_to_file
382  
383 =head2 delete_field_from_file
384  
385 Completely remove the given field.
386 For multi-value fields, it removes ALL the values.
387  
388 This must be overridden by the specific scribe class.
389  
390 $scribe->delete_field_from_file(filename=>$filename,field=>$field);
391  
392 =cut
393  
394 sub delete_field_from_file {
395 my $self = shift;
396 my %args = @_;
397 my $filename = $args{filename};
398 my $field = $args{field};
399  
400 } # delete_field_from_file
401  
402 =head2 replace_all_meta
403  
404 Overwrite the existing meta-data with that given.
405  
406 $scribe->replace_all_meta(filename=>$filename,meta=>\%meta);
407  
408 =cut
409  
410 sub replace_all_meta {
411 my $self = shift;
412 my %args = @_;
413 say STDERR whoami() if $self->{verbose} > 2;
414  
415 my $filename = $args{filename};
416 my $meta = $args{meta};
417  
418 # overwrite the known writable fields
419 # ignore the unknown fields
420 my $writable = $self->writable_fields();
421 foreach my $field (sort keys %{$writable})
422 {
423 if (exists $meta->{$field}
424 and defined $meta->{$field})
425 {
426 $self->replace_one_field(filename=>$filename,
427 field=>$field,
428 value=>$meta->{$field});
429 }
430 else # not there, remove it
431 {
432 $self->delete_field_from_file(filename=>$filename,field=>$field);
433 }
434 }
435 } # replace_all_meta
436  
437 =head1 Helper Functions
438  
439 Private interface.
440  
441 =head2 update_multival_field
442  
443 A multi-valued field could have individual values added or removed from it.
444 This expects a comma-separated list of individual values, prefixed with an operation:
445 '+' or nothing -- add the values
446 '-' -- remove the values
447 '=' -- replace the values
448  
449 This also needs to know the existing values of the multi-valued field.
450 The old values are either a reference to an array, or a string with comma-separated values.
451  
452 $scribe->update_multival_field(filename=>$filename,
453 field=>$field_name,
454 value=>$value,
455 old_vals=>$old_vals);
456  
457 =cut
458 sub update_multival_field {
459 my $self = shift;
460 my %args = @_;
461 say STDERR whoami() if $self->{verbose} > 2;
462  
463 my $filename = $args{filename};
464 my $field = $args{field};
465 my $value = $args{value};
466 my $old_vals = $args{old_vals};
467  
468 if ($self->{verbose} > 2 and ref $value)
469 {
470 say STDERR "$field is ", ref $value, " ", Dump($value);
471 }
472 my $prefix = '+';
473 if ($value =~ /^([+=-])(.*)/)
474 {
475 $prefix = $1;
476 $value = $2;
477 }
478 say STDERR "prefix='$prefix'" if $self->{verbose} > 2;
479 if ($prefix eq '=')
480 {
481 $self->replace_one_field(
482 filename=>$filename,
483 field=>$field,
484 value=>$value);
485 }
486 else
487 {
488 if ($prefix eq '-')
489 {
490 $self->delete_multival_from_file(
491 filename=>$filename,
492 field=>$field,
493 value=>$value,
494 old_vals=>$old_vals);
495 }
496 else
497 {
498 $self->add_multival_to_file(
499 filename=>$filename,
500 field=>$field,
501 value=>$value,
502 old_vals=>$old_vals);
503 }
504 }
505 } # update_multival_field
506  
507 =head2 add_multival_to_file
508  
509 Add a multi-valued field to the file.
510 Needs to know the existing values of the multi-valued field.
511 The old values are either a reference to an array, or a string with comma-separated values.
512  
513 $scribe->add_multival_to_file(filename=>$filename,
514 field=>$field_name,
515 value=>$value,
516 old_vals=>$old_vals);
517  
518 =cut
519 sub add_multival_to_file {
520 my $self = shift;
521 my %args = @_;
522 say STDERR whoami() if $self->{verbose} > 2;
523  
524 my $filename = $args{filename};
525 my $fname = $args{field};
526 my $old_vals = $args{old_vals};
527  
528 # allow for multiple values, comma-separated
529 my @vals = ($args{value});
530 if ($args{value} =~ /,/)
531 {
532 @vals = split(/,/, $args{value});
533 }
534  
535 # add new value(s) to existing taglike-values
536 my @old_values = ();
537 if (ref $old_vals eq 'ARRAY')
538 {
539 @old_values = @{$old_vals};
540 }
541 elsif (!ref $old_vals)
542 {
543 @old_values = split(/,/, $old_vals);
544 }
545 my @newvals = @old_values;
546 push @newvals, @vals;
547 @newvals = uniq @newvals;
548 my $newvals = join(',', @newvals);
549  
550 $self->replace_one_field(filename=>$filename,
551 field=>$fname,
552 value=>$newvals);
553 } # add_multival_to_file
554  
555 =head2 delete_multival_from_file
556  
557 Remove one value of a multi-valued field.
558 Needs to know the existing values of the multi-valued field.
559 The old values are either a reference to an array, or a string with comma-separated values.
560  
561 $scribe->delete_multival_from_file(filename=>$filename,
562 value=>$value,
563 field=>$field_name,
564 old_vals=>$old_vals);
565  
566 =cut
567 sub delete_multival_from_file ($%) {
568 my $self = shift;
569 my %args = @_;
570 say STDERR whoami() if $self->{verbose} > 2;
571  
572 my $filename = $args{filename};
573 my $fname = $args{field};
574 my $old_vals = $args{old_vals};
575  
576 # allow for multiple values, comma-separated
577 my @vals = ($args{value});
578 if ($args{value} =~ /,/)
579 {
580 @vals = split(/,/, $args{value});
581 }
582 my %to_delete = ();
583 foreach my $t (@vals)
584 {
585 $to_delete{$t} = 1;
586 }
587  
588 # remove value from existing values
589 # preserving the existing order
590 my @old_values = ();
591 if (ref $old_vals eq 'ARRAY')
592 {
593 @old_values = @{$old_vals};
594 }
595 elsif (!ref $old_vals)
596 {
597 @old_values = split(/,/, $old_vals);
598 }
599 my @newvals = ();
600 foreach my $t (@old_values)
601 {
602 if (! exists $to_delete{$t})
603 {
604 push @newvals, $t;
605 }
606 }
607 my $newvals = join(',', @newvals);
608  
609 $self->replace_one_field(filename=>$filename,
610 field=>$fname,
611 value=>$newvals);
612 } # delete_multival_from_file
613  
614 =head2 replace_one_field
615  
616 Overwrite the given field. This does no checking.
617  
618 This must be overridden by the specific scribe class.
619  
620 $scribe->replace_one_field(filename=>$filename,field=>$field,value=>$value);
621  
622 =cut
623  
624 sub replace_one_field {
625 my $self = shift;
626 my %args = @_;
627 my $filename = $args{filename};
628 my $field = $args{field};
629 my $value = $args{value};
630  
631 } # replace_one_field
632  
633 =head1 BUGS
634  
635 Please report any bugs or feature requests to the author.
636  
637 =cut
638  
639 1; # End of File::Sticker::Scribe
640 __END__
« Sticker.pm ↑ Summary Sticker/Scribe/Mp3.pm »