Sticker/Derive/Common.pm

filter: Statementstmt 20.9% 14 / 67 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 57.1% 4 / 7 Podpod 100% 3 / 3 totaltotal 16.6% 21 / 126 CC 21 SCAR 58.9
CC
21
Cov
13%
derive
462.0
whoami
2.0
order
2.0
CRAP 359.6
↑ Summary Sticker/Database.pm »
1 package File::Sticker::Derive::Common;
2 1 $File::Sticker::Derive::Common::VERSION = '4.9';
3 =head1 NAME
4  
5 File::Sticker::Derive::Common - derive values from existing meta-data
6  
7 =head1 VERSION
8  
9 version 4.9
10  
11 =head1 SYNOPSIS
12  
13 use File::Sticker::Derive::Common;
14  
15 my $deriver = File::Sticker::Derive::Common->new(%args);
16  
17 my $derived_meta = $deriver->derive(filename=>$filename,meta=>$meta);
18  
19 =head1 DESCRIPTION
20  
21 This will derive values from existing meta-data.
22 This is the Common plugin, which derives things common to all files,
23 such as paths, size, and date.
24  
25 =cut
26  
27 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use parent qw(File::Sticker::Derive);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 DEBUGGING
34  
35 =head2 whoami
36  
37 Used for debugging info
38  
39 =cut
40 0 ▶sub whoami { ( caller(1) )[3] }
Subroutine whoami: not called CC=1 SCAR=6.9
Pod documented
41  
42 =head1 METHODS
43  
44 =head2 order
45  
46 The order of this deriver, ranging from 0 to 99.
47 This makes sure that the deriver is applied in order;
48 useful because a later deriver may depend on data created
49 by an earlier deriver.
50  
51 =cut
52  
53 0 ▶sub order {
Subroutine order: not called CC=1 SCAR=6.9
Pod documented
54 0 return 10;
55 } # order
56  
57 =head2 derive
58  
59 Derive common values from the existing meta-data.
60 This UPDATES the passed-in meta hash, taking into account whether given fields
61 are allowed to be overwritten with the derived values, or left alone.
62 If the original field has an empty value, then the derived value will be written.
63 If the field is a readonly value, then the derived value will be written.
64  
65 $deriver->derive(filename=>$filename, meta=>$meta, overwrite=>\%overwrite);
66  
67 =cut
68  
69 0 ▶sub derive {
Subroutine derive: not called CC=21 SCAR=61.4
Pod documented
70 0 my $self = shift;
71 0 my %args = @_;
72 0 ▶ say STDERR whoami() if $self->{verbose} > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 2 0 0 0
73  
74 0 my $filename = $args{filename};
75 0 my $meta = $args{meta};
76  
77 0 my $fp = path($filename);
78 0 my $field = '';
79 0 my $value = '';
80  
81 # ---------------------------------------------------------
82 # Note that all fields related to the filename are readonly
83 # ---------------------------------------------------------
84 0 ▶ if (-r $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (-r $filename) { } 0 0 0
85 {
86 0 $meta->{file} = $fp->realpath->stringify;
87 }
88 else
89 {
90 0 $meta->{file} = $fp->absolute->stringify;
91 }
92 0 $meta->{basename} = $fp->basename();
93 0 $meta->{id_name} = $fp->basename(qr/\.\w+/);
94  
95 0 ▶ if ($meta->{basename} =~ /\.(\w+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"basename"} =~ /\.(\w+)$/u) 0 0 0
96 {
97 0 $meta->{ext} = $1;
98 }
99  
100 # If we have a topdir, we want various path information
101 # relative to that top dir.
102 0 ▶ if ($self->{topdir})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"topdir"}) 0 0 0
103 {
104 0 my $relpath = '';
105 # the topdir information may be an array
106 0 ▶ if (ref $self->{topdir} eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'topdir'} eq 'ARRAY') { } 0 0 0
107 {
108 0 my @topdirs = @{$self->{topdir}};
109 0 ▶ for (my $i=0; $i < @topdirs and !$relpath; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @topdirs and not $relpath) 0 0 0
Condition0%0/3
$i < @topdirs and not $relpath
!ll&&!rl&&r
000
MC/DC0%0/2
$i < @topdirs and not $relpath
$i < @topdirs not $relpath
Truth table0%0/3
A and not(B)
A= $i < @topdirsB= not $relpath
ABresult
0X0
100
111
110 {
111 0 my $td = $topdirs[$i];
112 # first check the given filename against this topdir
113 0 ▶ if ($filename =~ m!^${td}!)
Branch: 2 branches
BranchTrueFalseTotal
if ($filename =~ /^$td/u) { } 0 0 0
elsif ($meta->{'file'} =~ /^$td/u) { } 0 0 0
114 {
115 0 $relpath = $fp->relative($td)->stringify;
116 }
117 # then check the real absolute filename against this topdir
118 elsif ($meta->{file} =~ m!^${td}!)
119 {
120 0 $relpath = path($meta->{file})->relative($td)->stringify;
121 }
122 }
123 }
124 else
125 {
126 0 $relpath = $fp->relative($self->{topdir})->stringify;
127 }
128 0 my $rel_parent = '';
129 0 ▶ if ($relpath) # we might not have been able to find the relpath
Branch: 1 branch
BranchTrueFalseTotal
if ($relpath) 0 0 0
130 {
131 # Find the parent of this relpath
132 0 my $rpp = path($relpath)->parent;
133 0 $rel_parent = $rpp->stringify;
134  
135 # Check if the relpath parent makes sense
136 0 ▶ if ($relpath =~ /\.\./) # we got a problem
Branch: 1 branch
BranchTrueFalseTotal
if ($relpath =~ /\.\./u) 0 0 0
137 {
138 0 $relpath = '';
139 0 $rel_parent = '';
140 }
141 }
142  
143 # If we still have a relpath, set it, and set other things derived from it
144 0 ▶ if ($relpath)
Branch: 1 branch
BranchTrueFalseTotal
if ($relpath) 0 0 0
145 {
146 0 $meta->{relpath} = $relpath;
147  
148 # IF we have a relpath, check if a thumbnail exists
149 # It could be a jpg or a png
150 # Note that if the file itself is a jpg or png, we can use it as the thumbnail
151 0 ▶ if ($rel_parent and
Branch: 3 branches
BranchTrueFalseTotal
if ($rel_parent and -r $fp->parent . '/.thumbnails/' . $meta->{'id_name'} . '.jpg') { } 0 0 0
elsif ($rel_parent and -r $fp->parent . '/.thumbnails/' . $meta->{'id_name'} . '.png') { } 0 0 0
elsif ($meta->{'ext'} =~ /jpg|png|gif/u) { } 0 0 0
Condition0%0/3
$rel_parent and -r $fp->parent . '/.thumbnails/' . $meta->{"id_name"} . '.jpg'
!ll&&!rl&&r
000
MC/DC0%0/2
$rel_parent and -r $fp->parent . '/.thumbnails/' . $meta->{"id_name"} . '.jpg'
$rel_parent -r $fp->parent . '/.thumbnails/' . $meta->{"id_name"} . '.jpg'
Truth table0%0/3
A and B
A= $rel_parentB= -r $fp->parent . '/.thumbnails/' . $meta->{"id_name"} . '.jpg'
ABresult
0X0
100
111
152 -r $fp->parent . '/.thumbnails/' . $meta->{id_name} . '.jpg')
153 {
154 0 $meta->{thumbnail} = $rel_parent . '/.thumbnails/' . $meta->{id_name} . '.jpg';
155 }
156 ▶ elsif ($rel_parent
Condition0%0/3
$rel_parent and -r $fp->parent . '/.thumbnails/' . $meta->{"id_name"} . '.png'
!ll&&!rl&&r
000
MC/DC0%0/2
$rel_parent and -r $fp->parent . '/.thumbnails/' . $meta->{"id_name"} . '.png'
$rel_parent -r $fp->parent . '/.thumbnails/' . $meta->{"id_name"} . '.png'
Truth table0%0/3
A and B
A= $rel_parentB= -r $fp->parent . '/.thumbnails/' . $meta->{"id_name"} . '.png'
ABresult
0X0
100
111
157 and -r $fp->parent . '/.thumbnails/' . $meta->{id_name} . '.png')
158 {
159 0 $meta->{thumbnail} = $rel_parent . '/.thumbnails/' . $meta->{id_name} . '.png';
160 }
161 elsif ($meta->{ext} =~ /jpg|png|gif/)
162 {
163 0 $meta->{thumbnail} = $relpath;
164 }
165 }
166  
167 # Make this grouping stuff simple:
168 # take it as the *directory* where the file is;
169 # this is because that's how it is *grouped* together with other files, yes?
170 # But use the directory relative to the "top" directory, the first two or three parts of it.
171 0 ▶ if ($rel_parent)
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_parent) 0 0 0
172 {
173 0 my @bits = split(/\//, $rel_parent);
174  
175 # First make "section" fields, which are each separate bit of the entire rel_parent
176 0 ▶ for (my $i=0; $i < @bits; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @bits) 0 0 0
177 {
178 0 my $id = $i + 1;
179 0 $meta->{"section${id}"} = $bits[$i];
180 }
181  
182 # Then limit this to the top three for the grouping
183 0 splice(@bits,3);
184 0 $meta->{grouping} = join(' ', @bits);
185  
186 }
187 }
188 0 ▶ if (-r $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (-r $filename) 0 0 0
189 {
190 0 my $stat = $fp->stat;
191 # This is a readonly field, it ALWAYS overwrites any existing value
192 0 $meta->{filesize} = $stat->size;
193  
194 # This is a readonly field, it ALWAYS overwrites any existing value
195 0 $meta->{filedate} = strftime '%Y-%m-%d %H:%M:%S', localtime $stat->mtime;
196 }
197 0 return $meta;
198 } # derive
199  
200 =head1 BUGS
201  
202 Please report any bugs or feature requests to the author.
203  
204 =cut
205  
206 1 1; # End of File::Sticker::Derive::Common
207 __END__
↑ Summary Sticker/Database.pm »