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

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