blib/lib/Image/Nikon/Index.pm

filter: Statementstmt 44.3% 43 / 97 Branchbran 40.0% 4 / 10 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 53.8% 7 / 13 Podpod 0.0% 0 / 4 totaltotal 42.2% 60 / 142 CC 19 SCAR 20.9
CC
19
Cov
45%
print
20.0
transform
20.0
_index
6.0
CRAP 8.1
↑ Summary
1 package Image::Nikon::Index;
2  
3 #use 5.018002;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Image::ExifTool;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our @ISA = qw();
9 1 our $VERSION = '0.01';
10  
11 # methods
12 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod undocumented
13 1 my $class = shift;
14 1 my %args = @_;
15 my $self = {
16 folder => ".", # present working directory
17 prefix => $args{'prefix'} || undef,
18 suffix => $args{'suffix'} || undef,
19 exif => Image::ExifTool->new,
20 tags => [ 'SubSecDateTimeOriginal', 'ModifyDate' ],
21 tree => undef,
22 # tree => {
23 # date => {
24 # key => {
25 # name=>'original name',
26 # code=>'new date code',
27 # index=>'sequence number',
28 # },
29 # },
30 # }
31 files => undef,
32 inityear=> $args{'initialyear'} || 2011,
33 1 ▶ debug => $args{'debug'} || 1,
Condition50%4/8
$args{'prefix'} || undef
l!l
01
$args{'suffix'} || undef
l!l
01
$args{'initialyear'} || 2011
l!l
01
$args{'debug'} || 1
l!l
01
MC/DC0%0/1
$args{'prefix'} || undef
$args{'prefix'}
MC/DC0%0/1
$args{'suffix'} || undef
$args{'suffix'}
MC/DC0%0/1
$args{'initialyear'} || 2011
$args{'initialyear'}
MC/DC0%0/1
$args{'debug'} || 1
$args{'debug'}
Truth table50%1/2
A
A= $args{'prefix'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'suffix'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'initialyear'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'debug'}
Aresult
11
00
34 };
35 1 bless $self, $class;
36 1 return $self;
37 }
38  
39 # list directory and populate files
40 1 ▶sub _list {
Subroutine _list: called CC=4 SCAR=14.6
41 1 my ($self)=(@_);
42 1 my @files;
43  
44 # always open current directory
45 1 ▶ opendir DIR, $self->{'folder'} || die "$!\n";
Condition50%1/2
$self->{'folder'} || die("$!\n")
l!l
10
MC/DC0%0/1
$self->{'folder'} || die("$!\n")
$self->{'folder'}
Truth table50%1/2
A
A= $self->{'folder'}
Aresult
11
00
46 1 @files = readdir DIR;
47 1 closedir DIR;
48 1 chomp @files;
49  
50 1 @files = grep { !/^\.+/ } @files; # ignore hidden files, starting with dot
51 0 ▶ @files = grep { /^$self->{'prefix'}/ } @files if defined $self->{'prefix'}; # match prefix
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"prefix"} 0 1 1
52 0 ▶ @files = grep { /$self->{'suffix'}$/ } @files if defined $self->{'suffix'}; # match extension
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"suffix"} 0 1 1
53 1 $self->{'files'} = \@files;
54 }
55  
56 # extract file name and process
57 13 ▶sub _file {
Subroutine _file: called CC=3 SCAR=15.4
58 13 my ($self, %opts)=(@_);
59 13 my %dummy;
60 13 my $f = $opts{'file'};
61 13 my $t = $self->{'exif'};
62  
63 13 ▶ $t->ExtractInfo ( $f, \%dummy ) || return; # if not an image, skip.
Branch: 1 branch
BranchTrueFalseTotal
$t->ExtractInfo($f, \%dummy) or return 12 1 13
64 #my $value = $t->GetValue('SubSecDateTimeOriginal', 1) || return; # if not an image, skip.
65 12 ▶ my $value = $t->GetValue($self->{'tags'}->[0], 1) || return; # if not an image, skip.
Condition50%1/2
$t->GetValue($self->{'tags'}[0], 1) || (return)
l!l
012
MC/DC0%0/1
$t->GetValue($self->{'tags'}[0], 1) || (return)
$t->GetValue($self->{'tags'}[0], 1)
Truth table50%1/2
A
A= $t->GetValue($self->{'tags'}[0], 1)
Aresult
11
00
66 0 my ($date, $time) = split /\s+/, $value, 2 || die $!;
67 0 my $datestr = $self->_date ($date);
68  
69 0 $date =~ s/\://g;
70 0 $time =~ s/\:|\.//g;
71 0 $time = $date.$time;
72  
73 0 my ($updated) = $t->GetValue($self->{'tags'}->[1], 1);
74 0 $updated =~ s/\:|\s+//g;
75  
76 0 $f =~ s/\_//g;
77 0 $f =~ s/\./\_/g;
78  
79 0 $self->{'tree'}->{$date}->{$time.$updated.$f}->{'name'} = $opts{'file'};
80 0 $self->{'tree'}->{$date}->{$time.$updated.$f}->{'code'} = $datestr;
81 }
82  
83 # changing date format
84 0 ▶sub _date {
Subroutine _date: not called CC=1 SCAR=6.9
85 0 my ($self, $datestr) = (@_);
86 0 my ($y, $m, $d) = split /:/, $datestr;
87
88 0 $datestr =~ s/^\s+|\s+$//g;
89 0 ($y, $m, $d) = split /:/, $datestr;
90 0 $y = chr ( $y - $self->{'inityear'} + ord('A') );
91 0 $datestr = sprintf "%s%1X%02d", $y, $m, $d;
92 0 return $datestr;
93 }
94  
95 # indexing
96 0 ▶sub _index {
Subroutine _index: not called CC=2 SCAR=17.9
97 0 my ($self, %opts) = (@_);
98 0 my $hash = $opts{'hash'};
99 0 my $i = 1;
100  
101 0 foreach my $key ( sort keys %{$hash} ) {
102 0 $hash->{$key}->{'index'} = sprintf "%03d", $i;
103 0 $i++;
104 }
105 }
106  
107 # file extension
108 0 ▶sub _ext {
Subroutine _ext: not called CC=1 SCAR=6.9
109 0 my ($self, $name) = (@_);
110 0 my ($junk, $x) = split /\./, $name;
111 0 return $x;
112 }
113  
114 # print all files - key, oldname, newname
115 0 ▶sub print {
Subroutine print: not called CC=4 SCAR=30.0
Pod undocumented
116 0 my ($self) = (@_);
117 0 my $hash;
118 my $newname;
119  
120 0 foreach my $date ( keys %{$self->{'tree'}} ) {
121 0 ▶ print "__".$date."__\n" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
122 0 $hash = $self->{'tree'}->{$date};
123  
124 0 foreach my $key ( sort keys %{$hash} ) {
125 0 $newname = $hash->{$key}->{'code'}."_".$hash->{$key}->{'index'}.".".$self->_ext($hash->{$key}->{'name'});
126 0 print sprintf " %-45s %-15s %-15s\n", $key, $hash->{$key}->{'name'}, " ".$newname;
127 }
128 }
129 }
130  
131 # rename of each file
132 0 ▶sub _rename {
Subroutine _rename: not called CC=1 SCAR=6.9
133 0 my ($self, %opts) =(@_);
134 0 rename ($opts{'oldname'}, $opts{'newname'});
135 }
136  
137 # mass rename
138 0 ▶sub transform {
Subroutine transform: not called CC=4 SCAR=30.0
Pod undocumented
139 0 my ($self) =(@_);
140 0 my $hash;
141 my $newname;
142  
143 0 foreach my $date ( keys %{$self->{'tree'}} ) {
144 0 $hash = $self->{'tree'}->{$date};
145 0 foreach my $key ( sort keys %{$hash} ) {
146 0 $newname = $hash->{$key}->{'code'}."_".$hash->{$key}->{'index'}.".".$self->_ext($hash->{$key}->{'name'});
147 0 ▶ print $hash->{$key}->{'name'}." ".$newname."\n" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
148 0 $self->_rename ( oldname=>$hash->{$key}->{'name'}, newname=>$newname );
149 }
150 }
151 }
152  
153 1 ▶sub process {
Subroutine process: called CC=3 SCAR=11.2
Pod undocumented
154 1 my ($self) = (@_);
155 1 my $list = $self->_list;
156 1 my $hash;
157  
158 # create node for each file
159 1 foreach my $f ( @{$list} ) {
160 13 $self->_file (file=>$f);
161 }
162  
163 # do indexing for each available date
164 1 foreach my $date ( keys %{$self->{'tree'}} ) {
165 0 $hash = $self->{'tree'}->{$date};
166 0 $self->_index (hash=>$hash);
167 }
168 1 return $self;
169 }
170  
171  
172 1 1;
173 __END__
↑ Summary