blib/lib/File/Sticker/Derive/Common.pm

filter: Statementstmt 21.8% 14 / 64 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 17.0% 21 / 123 CC 21 SCAR 58.9
CC
21
Cov
13%
derive
462.0
whoami
2.0
order
2.0
CRAP 359.6
↑ Summary blib/lib/File/Sticker/Database.pm »
1 package File::Sticker::Derive::Common;
2 1 $File::Sticker::Derive::Common::VERSION = '4.8';
3 =head1 NAME
4  
5 File::Sticker::Derive::Common - derive values from existing meta-data
6  
7 =head1 VERSION
8  
9 version 4.8
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 sub order {
54 0 ▶ return 10;
Subroutine order: not called CC=1 SCAR=6.9
Pod documented
55 } # order
56  
57 =head2 derive
58  
59 Derive common values from the existing meta-data.
60 This is expected to return a new hash.
61 DOES NOT alter the passed-in data.
62  
63 $deriver->derive(filename=>$filename, meta=>$meta);
64  
65 =cut
66  
67 sub derive {
68 0 ▶ my $self = shift;
Subroutine derive: not called CC=21 SCAR=61.4
Pod documented
69 0 my %args = @_;
70 0 ▶ say STDERR whoami() if $self->{verbose} > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 2 0 0 0
71  
72 0 my $filename = $args{filename};
73 0 my $oldmeta = $args{meta};
74  
75 0 my $newmeta = {};
76 0 my $fp = path($filename);
77 0 ▶ if (-r $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (-r $filename) { } 0 0 0
78 {
79 0 $newmeta->{file} = $fp->realpath->stringify;
80 }
81 else
82 {
83 0 $newmeta->{file} = $fp->absolute->stringify;
84 }
85 0 $newmeta->{basename} = $fp->basename();
86 0 $newmeta->{id_name} = $fp->basename(qr/\.\w+/);
87 0 ▶ if ($newmeta->{basename} =~ /\.(\w+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($newmeta->{"basename"} =~ /\.(\w+)$/u) 0 0 0
88 {
89 0 $newmeta->{ext} = $1;
90 }
91  
92 # If we have a topdir, we want various path information
93 # relative to that top dir.
94 0 ▶ if ($self->{topdir})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"topdir"}) 0 0 0
95 {
96 # the topdir information may be an array
97 0 ▶ if (ref $self->{topdir} eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'topdir'} eq 'ARRAY') { } 0 0 0
98 {
99 0 my @topdirs = @{$self->{topdir}};
100 0 ▶ for (my $i=0; $i < @topdirs and !$newmeta->{relpath}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @topdirs and not $newmeta->{"relpath"}) 0 0 0
Condition0%0/3
$i < @topdirs and not $newmeta->{"relpath"}
!ll&&!rl&&r
000
MC/DC0%0/2
$i < @topdirs and not $newmeta->{"relpath"}
$i < @topdirs not $newmeta->{"relpath"}
Truth table0%0/3
A and not(B)
A= $i < @topdirsB= not $newmeta->{"relpath"}
ABresult
0X0
100
111
101 {
102 0 my $td = $topdirs[$i];
103 # first check the given filename against this topdir
104 0 ▶ if ($filename =~ m!^${td}!)
Branch: 2 branches
BranchTrueFalseTotal
if ($filename =~ /^$td/u) { } 0 0 0
elsif ($newmeta->{'file'} =~ /^$td/u) { } 0 0 0
105 {
106 0 $newmeta->{relpath} = $fp->relative($td)->stringify;
107 }
108 # then check the real absolute filename against this topdir
109 elsif ($newmeta->{file} =~ m!^${td}!)
110 {
111 0 $newmeta->{relpath} = path($newmeta->{file})->relative($td)->stringify;
112 }
113 }
114 }
115 else
116 {
117 0 $newmeta->{relpath} = $fp->relative($self->{topdir})->stringify;
118 }
119 0 my $rel_parent = '';
120 0 ▶ if ($newmeta->{relpath}) # we might not have been able to find the relpath
Branch: 1 branch
BranchTrueFalseTotal
if ($newmeta->{"relpath"}) 0 0 0
121 {
122 # Find the parent of this relpath
123 0 my $rpp = path($newmeta->{relpath})->parent;
124 0 $rel_parent = $rpp->stringify;
125  
126 # Check if the relpath parent makes sense
127 0 ▶ if ($newmeta->{relpath} =~ /\.\./) # we got a problem
Branch: 1 branch
BranchTrueFalseTotal
if ($newmeta->{"relpath"} =~ /\.\./u) 0 0 0
128 {
129 0 $newmeta->{relpath} = '';
130 0 $rel_parent = '';
131 }
132 }
133  
134 # If we still have a relpath, set other things derived from it
135 0 ▶ if ($newmeta->{relpath})
Branch: 1 branch
BranchTrueFalseTotal
if ($newmeta->{"relpath"}) 0 0 0
136 {
137 # IF we have a relpath, check if a thumbnail exists
138 # It could be a jpg or a png
139 # Note that if the file itself is a jpg or png, we can use it as the thumbnail
140 0 ▶ if ($rel_parent and
Branch: 3 branches
BranchTrueFalseTotal
if ($rel_parent and -r $fp->parent . '/.thumbnails/' . $newmeta->{'id_name'} . '.jpg') { } 0 0 0
elsif ($rel_parent and -r $fp->parent . '/.thumbnails/' . $newmeta->{'id_name'} . '.png') { } 0 0 0
elsif ($newmeta->{'ext'} =~ /jpg|png|gif/u) { } 0 0 0
Condition0%0/3
$rel_parent and -r $fp->parent . '/.thumbnails/' . $newmeta->{"id_name"} . '.jpg'
!ll&&!rl&&r
000
MC/DC0%0/2
$rel_parent and -r $fp->parent . '/.thumbnails/' . $newmeta->{"id_name"} . '.jpg'
$rel_parent -r $fp->parent . '/.thumbnails/' . $newmeta->{"id_name"} . '.jpg'
Truth table0%0/3
A and B
A= $rel_parentB= -r $fp->parent . '/.thumbnails/' . $newmeta->{"id_name"} . '.jpg'
ABresult
0X0
100
111
141 -r $fp->parent . '/.thumbnails/' . $newmeta->{id_name} . '.jpg')
142 {
143 0 $newmeta->{thumbnail} = $rel_parent . '/.thumbnails/' . $newmeta->{id_name} . '.jpg'
144 }
145 ▶ elsif ($rel_parent
Condition0%0/3
$rel_parent and -r $fp->parent . '/.thumbnails/' . $newmeta->{"id_name"} . '.png'
!ll&&!rl&&r
000
MC/DC0%0/2
$rel_parent and -r $fp->parent . '/.thumbnails/' . $newmeta->{"id_name"} . '.png'
$rel_parent -r $fp->parent . '/.thumbnails/' . $newmeta->{"id_name"} . '.png'
Truth table0%0/3
A and B
A= $rel_parentB= -r $fp->parent . '/.thumbnails/' . $newmeta->{"id_name"} . '.png'
ABresult
0X0
100
111
146 and -r $fp->parent . '/.thumbnails/' . $newmeta->{id_name} . '.png')
147 {
148 0 $newmeta->{thumbnail} = $rel_parent . '/.thumbnails/' . $newmeta->{id_name} . '.png'
149 }
150 elsif ($newmeta->{ext} =~ /jpg|png|gif/)
151 {
152 0 $newmeta->{thumbnail} = $newmeta->{relpath};
153 }
154 }
155  
156 # Make this grouping stuff simple:
157 # take it as the *directory* where the file is;
158 # this is because that's how it is *grouped* together with other files, yes?
159 # But use the directory relative to the "top" directory, the first two or three parts of it.
160  
161 0 ▶ if ($rel_parent)
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_parent) 0 0 0
162 {
163 0 my @bits = split(/\//, $rel_parent);
164  
165 # First make "section" fields, which are each separate bit of the entire rel_parent
166 0 ▶ for (my $i=0; $i < @bits; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @bits) 0 0 0
167 {
168 0 my $id = $i + 1;
169 0 $newmeta->{"section${id}"} = $bits[$i];
170 }
171  
172 # Then limit this to the top three for the grouping
173 0 splice(@bits,3);
174 0 $newmeta->{grouping} = join(' ', @bits);
175  
176 }
177 }
178 0 ▶ if (-r $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (-r $filename) 0 0 0
179 {
180 0 my $stat = $fp->stat;
181 0 $newmeta->{filesize} = $stat->size;
182  
183 0 $newmeta->{filedate} = strftime '%Y-%m-%d %H:%M:%S', localtime $stat->mtime;
184 }
185 0 return $newmeta;
186 } # derive
187  
188 =head1 BUGS
189  
190 Please report any bugs or feature requests to the author.
191  
192 =cut
193  
194 1 1; # End of File::Sticker::Derive::Common
195 __END__
↑ Summary blib/lib/File/Sticker/Database.pm »