blib/lib/Image/MetaData/GQview.pm

filter: Statementstmt 83.4% 131 / 157 Branchbran 48.5% 34 / 70 Conditioncond 30.9% 13 / 42 MC/DCmcdc 3.8% 1 / 26 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 63.6% 203 / 319 SCAR 50.9
CC
53
Cov
66%
save
136.9
load
16.6
new
8.4
CRAP 161.7
↑ Summary
1 package Image::MetaData::GQview;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #use warnings;
6 ## no critic (RequireUseWarnings);
7  
8 3 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Fatal qw(:void open close);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use PerlIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Image::MetaData::GQview - Perl extension for GQview image metadata
17  
18 =head1 SYNOPSIS
19  
20 use Image::MetaData::GQview;
21  
22 my $md = Image::MetaData::GQview->new("test.jpg");
23 my $md2 = Image::MetaData::GQview->new("test2.jpg", {fields => ['keywords', 'comment', 'picture info']});
24 my $md3 = Image::MetaData::GQview->new({file => "test2.jpg", fields => ['keywords', 'comment', 'picture info']});
25 $md->load("test.jpg");
26 my $comment = $md->comment;
27 my @keywords = $md->keywords;
28 my $raw = $md->raw;
29 $md->comment("This is a comment");
30 $md->keywords(@keywords);
31 $md->save("test.jpg");
32  
33 =head1 DESCRIPTION
34  
35 This module is a abstraction to the image meta data of GQview.
36  
37 All internal errors will trow an error!
38  
39 =head2 METHODS
40  
41 =over
42  
43 =cut
44  
45 3 ▶use version; our $VERSION = qv("v2.0.0");
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 =item new
48  
49 This is a class method and the only one. It is used to get a object of Image::MetaData::GQview. It can be called without parameter or with the image as only option in witch case it try to load the meta data.
50  
51 You can provide a hash reference as second or as only parameter which specify file and/or fields. The fields are default "keywords" and "comment" in this order.
52  
53 =cut
54  
55 sub new
56 {
57 2 ▶ my $param = shift;
Subroutine new: called CC=8 SCAR=21.2
Pod documented
58 2 ▶ my $class = ref($param) || $param;
Condition33%1/3
ref $param || $param
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $param || $param
ref $param $param
Truth table33%1/3
A || B
A= ref $paramB= $param
ABresult
1X1
011
000
59 2 my $file = shift;
60 2 ▶ my $opts = shift || {};
Condition50%1/2
shift() || {}
l!l
02
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
61  
62 2 ▶ if (ref($file) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq "HASH") 1 1 2
63 {
64 1 $opts = $file;
65 1 $file = undef;
66 }
67  
68 2 my $self = {fields => [qw(keywords comment)],};
69 2 ▶ $self->{opts}->{file} = $file if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 0 2 2
70  
71 2 bless $self, $class;
72  
73 2 foreach (qw(file fields))
74 {
75 4 ▶ $self->{$_} = $opts->{$_} if exists($opts->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{$_} 1 3 4
76 }
77  
78 2 $file = $self->{opts}->{file};
79  
80 2 ▶ $self->load($file) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 0 2 2
81  
82 2 return $self;
83 } ## end sub new
84  
85 =item load
86  
87 If you didn't load the data with new you can do that with this method. If the parameter is left out the one setted before is used.
88  
89 You can also specify the location for the meta file as second parameter.
90  
91 =cut
92  
93 sub load
94 {
95 2 ▶ my $self = shift;
Subroutine load: called CC=11 SCAR=28.1
Pod documented
96 2 ▶ my $image = shift || $self->{imagefile};
Condition33%1/3
shift() || $self->{'imagefile'}
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || $self->{'imagefile'}
shift() $self->{'imagefile'}
Truth table33%1/3
A || B
A= shift()B= $self->{'imagefile'}
ABresult
1X1
011
000
97 2 my $metafile = shift;
98  
99 2 ▶ croak("No File given!") unless $image;
Branch: 1 branch
BranchTrueFalseTotal
unless $image 0 2 2
100 2 $image = abs_path($image);
101 2 ▶ croak("No such file ($image)!") unless -e $image;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $image 1 1 2
102  
103 1 $self->{imagefile} = $image;
104  
105 1 ▶ unless ($metafile)
Branch: 1 branch
BranchTrueFalseTotal
unless ($metafile) 0 1 1
106 {
107 0 (my $metadata1 = $image) =~ s#/([^/]*)$#/.metadata/$1.meta#;
108 0 my $metadata2 = abs_path($ENV{HOME}) . ".gqview/metadata$image.meta";
109  
110 0 ▶ $metafile = $metadata1 if -r $metadata1;
Branch: 1 branch
BranchTrueFalseTotal
if -r $metadata1 0 0 0
111 0 ▶ $metafile ||= $metadata2 if -r $metadata2;
Branch: 1 branch
BranchTrueFalseTotal
if -r $metadata2 0 0 0
Condition0%0/3
$metafile ||= $metadata2
l!l&&r!l&&!r
000
MC/DC0%0/2
$metafile ||= $metadata2
$metafile $metadata2
Truth table0%0/3
A ||= B
A= $metafileB= $metadata2
ABresult
1X1
011
000
112 } ## end unless ($metafile)
113 1 $self->{metafile} = $metafile;
114  
115 1 ▶ croak("No metadata found for image '$image'!") unless $metafile;
Branch: 1 branch
BranchTrueFalseTotal
unless $metafile 0 1 1
116  
117 1 open my $in, "<:utf8", $metafile;
118 1 $self->{metadata} = eval { local $/ = undef; <$in> };
119 1 close $in;
120  
121 # Aufbau:
122 # #GQview comment (<version>)
123 #
124 # [keywords]
125 # ...
126 #
127 # [comment]
128 # ...
129 #
130 # #end
131 1 my $select = join("|", @{$self->{fields}});
132 1 my @fields_ext = split(/^\[($select)\]\n/m, $self->{metadata});
133  
134 # trow away the head
135 1 shift @fields_ext;
136 1 ▶ die "Internal Error: Metadata are not parsable" if (@fields_ext % 2) != 0; ## no critic (RequireCarping);
Branch: 1 branch
BranchTrueFalseTotal
if @fields_ext % 2 != 0 0 1 1
137  
138 # Cleanup the last field if it exists
139 1 ▶ $fields_ext[-1] =~ s/\n*#end\n?\z/\n/ if @fields_ext > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @fields_ext > 0 1 0 1
140  
141 # Now they can be put into $self
142 1 my %fields = @fields_ext;
143 1 $self->{data} = \%fields;
144  
145 1 return 1;
146 } ## end sub load
147  
148 =item comment
149  
150 Get or set the comment.
151  
152 =cut
153  
154 sub comment
155 {
156 2 ▶ my $self = shift;
Subroutine comment: called CC=3 SCAR=11.0
Pod documented
157 2 my $comment = shift;
158  
159 2 ▶ $comment =~ s/^\[/ [/mg if $comment;
Branch: 1 branch
BranchTrueFalseTotal
if $comment 1 1 2
160 2 ▶ $self->set_field('comment', $comment) if $comment;
Branch: 1 branch
BranchTrueFalseTotal
if $comment 1 1 2
161  
162 2 return scalar($self->get_field('comment'));
163 } ## end sub comment
164  
165 =item keywords
166  
167 Get or set the keywords. This is the preferred method for the keywords as it shift out empty keywords.
168  
169 =cut
170  
171 sub keywords ## no critic (RequireArgUnpacking);
172 {
173 2 ▶ my $self = shift;
Subroutine keywords: called CC=2 SCAR=6.9
Pod documented
174  
175 2 ▶ $self->set_field('keywords', @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
176  
177 2 my @keywords = grep {$_} $self->get_field('keywords');
178  
179 2 return @keywords;
180 } ## end sub keywords
181  
182 =item raw
183  
184 Get the raw data
185  
186 =cut
187  
188 sub raw
189 {
190 2 ▶ my $self = shift;
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
191  
192 2 return $self->{metadata};
193 }
194  
195 =item save
196  
197 Save the data to disk. This will read the location from the gqview configuration. If there is none, the info will be saved in local directory.
198  
199 You can also specify the location for the meta file as second parameter.
200  
201 =cut
202  
203 sub save
204 {
205 1 ▶ my $self = shift;
Subroutine save: called CC=24 SCAR=49.2
Pod documented
206 1 my $image = shift;
207 1 my $newimage = $image;
208 1 my $metafile = shift;
209 1 my $newmetafile = $metafile;
210 1 ▶ $image ||= $self->{imagefile};
Condition33%1/3
$image ||= $self->{"imagefile"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$image ||= $self->{"imagefile"}
$image $self->{"imagefile"}
Truth table33%1/3
A ||= B
A= $imageB= $self->{"imagefile"}
ABresult
1X1
011
000
211 1 ▶ $metafile ||= $self->{metafile};
Condition33%1/3
$metafile ||= $self->{"metafile"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$metafile ||= $self->{"metafile"}
$metafile $self->{"metafile"}
Truth table33%1/3
A ||= B
A= $metafileB= $self->{"metafile"}
ABresult
1X1
011
000
212  
213 1 ▶ croak("No File given!") unless $image;
Branch: 1 branch
BranchTrueFalseTotal
unless $image 0 1 1
214 1 $image = abs_path($image);
215 1 ▶ croak("No such file ($image)!") unless -e $image;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $image 0 1 1
216  
217 1 (my $metadata1 = $image) =~ s#/([^/]*)$#/.metadata/$1.meta#;
218 1 my $metadata2 = abs_path($ENV{HOME}) . ".gqview/metadata$image.meta";
219  
220 1 my $metadata;
221  
222 # Read the gqviewrc
223 1 ▶ if (open my $in, "<", $ENV{HOME} . "/.gqview/gqviewrc") ## no critic (RequireBriefOpen);
Branch: 1 branch
BranchTrueFalseTotal
if (&Image::MetaData::GQview::open(my $in, "<", $ENV{"HOME"} . '/.gqview/gqviewrc')) 0 1 1
224 {
225 0 ▶ while (my $line = <$in>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 0 0 0
226 {
227 0 chomp $line;
228 0 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
229 0 ▶ if ($line =~ /^local_metadata: (true|false)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^local_metadata: (true|false)$/) 0 0 0
230 {
231 0 ▶ $metadata = ($1 eq "true") ? $metadata1 : $metadata2;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'true' ? : 0 0 0
232 0 last;
233 }
234 } ## end while (my $line = <$in>)
235 0 close $in;
236 } ## end if (open my $in, "<", ...
237 1 ▶ if ($newimage and not $newmetafile)
Branch: 1 branch
BranchTrueFalseTotal
if ($newimage and not $newmetafile) 0 1 1
Condition33%1/3
$newimage and not $newmetafile
!ll&&!rl&&r
010
MC/DC0%0/2
$newimage and not $newmetafile
$newimage not $newmetafile
Truth table33%1/3
A and not(B)
A= $newimageB= not $newmetafile
ABresult
0X0
100
111
238 {
239 0 $metafile = $metadata;
240 }
241  
242 1 my $false;
243 1 my @metadirs = split(/\//, $metafile);
244 1 pop @metadirs;
245 1 my $metadir = "";
246 1 ▶ while (@metadirs)
Branch: 1 branch
BranchTrueFalseTotal
if (@metadirs) 0 1 1
247 {
248 0 $metadir .= shift(@metadirs) . "/";
249 0 ▶ unless (-d $metadir or mkdir($metadir))
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $metadir or mkdir $metadir) 0 0 0
Condition0%0/3
-d $metadir or mkdir $metadir
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $metadir or mkdir $metadir
-d $metadir mkdir $metadir
Truth table0%0/3
A or B
A= -d $metadirB= mkdir $metadir
ABresult
1X1
011
000
250 {
251 0 $false = 1;
252 0 last;
253 }
254 } ## end while (@metadirs)
255 1 ▶ if ($false and not $newmetafile and $metafile ne $metadata2)
Branch: 1 branch
BranchTrueFalseTotal
if ($false and not $newmetafile and $metafile ne $metadata2) 0 1 1
Condition33%2/6
$false and not $newmetafile and $metafile ne $metadata2
!ll&&!rl&&r
100
$false and not $newmetafile
!ll&&!rl&&r
100
MC/DC0%0/3
$false and not $newmetafile and $metafile ne $metadata2
$false not $newmetafile $metafile ne $metadata2
Truth table25%1/4
A and not(B) and C
A= $falseB= not $newmetafileC= $metafile ne $metadata2
ABCresult
0XX0
10X0
1100
1111
256 {
257 0 $false = 0;
258 0 $metafile = $metadata2;
259 0 @metadirs = split(/\//, $metadata2);
260 0 pop @metadirs;
261 0 $metadir = "";
262 0 ▶ while (@metadirs)
Branch: 1 branch
BranchTrueFalseTotal
if (@metadirs) 0 0 0
263 {
264 0 $metadir .= shift(@metadirs) . "/";
265 0 ▶ unless (-d $metadir or mkdir($metadir))
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $metadir or mkdir $metadir) 0 0 0
Condition0%0/3
-d $metadir or mkdir $metadir
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $metadir or mkdir $metadir
-d $metadir mkdir $metadir
Truth table0%0/3
A or B
A= -d $metadirB= mkdir $metadir
ABresult
1X1
011
000
266 {
267 0 $false = 1;
268 0 last;
269 }
270 } ## end while (@metadirs)
271 } ## end if ($false and not $newmetafile...
272 1 ▶ croak("Cannot create directory structure for meta file '$metafile'!") if ($false);
Branch: 1 branch
BranchTrueFalseTotal
if $false 0 1 1
273 1 $self->_sync;
274 1 ▶ if ($self->raw)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 1 0 1
275 {
276 1 open my $meta, ">:utf8", $metafile;
277 1 ▶ print $meta $self->raw or die("Faulty metadata"); ## no critic (RequireCarping);
Branch: 1 branch
BranchTrueFalseTotal
print $meta $self->raw or die "Faulty metadata" 0 1 1
278 1 close $meta;
279 } ## end if ($self->raw)
280  
281 1 $self->{imagefile} = $image;
282 1 $self->{metafile} = $metafile;
283  
284 1 return 1;
285 } ## end sub save
286  
287 =item get_field
288  
289 This will extract the information of one field and return it as single sting (in scalar context) or as array splitted in lines.
290  
291 Please note, it array context also empty lines can be returned!
292  
293 =cut
294  
295 sub get_field
296 {
297 4 ▶ my $self = shift;
Subroutine get_field: called CC=5 SCAR=16.7
Pod documented
298 4 ▶ my $field = shift || croak("get_field has to be called with a field as the first parameter");
Condition33%1/3
shift() || croak('get_field has to be called with a field as the first parameter')
l!l&&r!l&&!r
400
MC/DC0%0/2
shift() || croak('get_field has to be called with a field as the first parameter')
shift() croak('get_field has to be called with a field as the first parameter')
Truth table33%1/3
A || B
A= shift()B= croak('get_field has to be called with a field as the first parameter')
ABresult
1X1
011
000
299  
300 4 ▶ croak("get_field has to be called with a known field '$field' as first parameter") unless grep {/^\Q$field\E$/s} @{$self->{fields}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^\Q$field\E$/s;} @{$$self{"fields"};} 0 4 4
301  
302 4 ▶ my $data = $self->{data}->{$field} || "";
Condition50%1/2
$self->{'data'}{$field} || ''
l!l
40
MC/DC0%0/1
$self->{'data'}{$field} || ''
$self->{'data'}{$field}
Truth table50%1/2
A
A= $self->{'data'}{$field}
Aresult
11
00
303 4 $data =~ s/\n*\z//;
304  
305 4 ▶ return wantarray ? split(/\n/, $data) : "$data\n";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
306 } ## end sub get_field
307  
308 =item set_field
309  
310 Well, of cause if you can get a field you have to be able to set it.
311  
312 The arguments are the field name and the data.
313  
314 The data can be a single value or a array.
315  
316 =cut
317  
318 sub set_field ## no critic (RequireArgUnpacking);
319 {
320 2 ▶ my $self = shift;
Subroutine set_field: called CC=3 SCAR=11.2
Pod documented
321 2 ▶ my $field = shift || croak("set_field has to be called with a field as the first parameter");
Condition33%1/3
shift() || croak('set_field has to be called with a field as the first parameter')
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || croak('set_field has to be called with a field as the first parameter')
shift() croak('set_field has to be called with a field as the first parameter')
Truth table33%1/3
A || B
A= shift()B= croak('set_field has to be called with a field as the first parameter')
ABresult
1X1
011
000
322  
323 2 ▶ croak("set_field has to be called with a known field '$field' }as first parameter") unless grep {/^\Q$field\E$/s} @{$self->{fields}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^\Q$field\E$/s;} @{$$self{"fields"};} 0 2 2
324  
325 2 my $data = join("\n", @_);
326 2 $data =~ s/\n*\z/\n/;
327  
328 2 $self->{data}->{$field} = $data;
329  
330 2 $self->_sync;
331  
332 2 return 1;
333 } ## end sub set_field
334  
335 #
336 # Internal method _sync
337 #
338 # This will hold the metadata in sync with the single elements
339 #
340  
341 sub _sync
342 {
343 3 ▶ my $self = shift;
Subroutine _sync: called CC=4 SCAR=13.9
344  
345 3 $self->{metadata} = "#GQview comment (2.0.0)\n\n";
346  
347 3 foreach my $field (@{$self->{fields}})
348 {
349 6 ▶ my $data = $self->{data}->{$field} || "";
Condition100%2/2
$self->{'data'}{$field} || ''
l!l
51
MC/DC100%1/1
$self->{'data'}{$field} || ''
$self->{'data'}{$field}
Truth table100%2/2
A
A= $self->{'data'}{$field}
Aresult
11
00
350 6 $data =~ s/\n*\z/\n\n/s;
351 6 ▶ $data = "\n" if $data eq "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "\n\n" 1 5 6
352 6 $self->{metadata} .= "[$field]\n" . $data;
353 } ## end foreach my $field (@{$self->...
354  
355 3 $self->{metadata} .= "#end\n";
356  
357 3 return 1;
358 } ## end sub _sync
359  
360 3 1;
361  
362 __END__
↑ Summary