blib/lib/Music/Tag/File.pm

filter: Statementstmt 68.5% 107 / 156 Branchbran 51.5% 33 / 64 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 71.4% 10 / 14 Podpod 100% 7 / 7 totaltotal 61.9% 161 / 260 CC 37 SCAR 30.1
CC
37
Cov
62%
get_tag
37.1
_slurp_file
12.0
save_lyrics
12.0
CRAP 20.4
↑ Summary
1 package Music::Tag::File;
2 2 ▶use strict; use warnings; use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 our $VERSION = '0.4101';
4  
5 # Copyright © 2007,2010 Edward Allen III. Some rights reserved.
6 #
7 # You may distribute under the terms of either the GNU General Public
8 # License or the Artistic License, as specified in the README file.
9  
10 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use base qw(Music::Tag::Generic);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub set_values {
Subroutine set_values: not called CC=1 SCAR=6.9
Pod documented
14 0 return qw( picture artist album title booklet lyrics track discnum);
15 }
16  
17 0 ▶sub saved_values {
Subroutine saved_values: not called CC=1 SCAR=6.9
Pod documented
18 0 return qw( picture booklet lyrics );
19 }
20  
21 2 ▶sub get_tag {
Subroutine get_tag: called CC=23 SCAR=36.1
Pod documented
22 2 my $self = shift;
23 2 my $filename = $self->info->get_data('filename');
24 2 my @dirs = File::Spec->splitdir( File::Spec->rel2abs($filename) );
25 2 my $fname = pop @dirs;
26 2 my $dname = File::Spec->catdir(@dirs);
27 2 my $album = pop @dirs;
28 2 my $artist = pop @dirs;
29  
30 2 $album =~ s/_/ /g;
31 2 $album =~ s/\b(\w)/uc($1)/ge;
32 2 $album =~ s/ *$//g;
33 2 $album =~ s/^ *//g;
34  
35 2 ▶ if ( length($album) < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $album < 2) 0 2 2
36 0 $album = "";
37 }
38  
39 2 $artist =~ s/_/ /g;
40 2 $artist =~ s/\b(\w)/uc($1)/ge;
41 2 $artist =~ s/ *$//g;
42 2 $artist =~ s/ *$//g;
43 2 ▶ unless ( $self->info->has_data('track') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->info->has_data("track")) 2 0 2
44 2 ▶ if ( $fname =~ /^[^\d]*(\d+)[^\d]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname =~ /^[^\d]*(\d+)[^\d]/) { } 0 2 2
45 0 my $num = sprintf( "%d", $1 );
46 0 $self->info->set_data('track',$num);
47 0 $self->tagchange("TRACK");
48 }
49 else {
50 2 $self->info->set_data('track',0);
51 2 $self->tagchange("TRACK");
52 }
53 }
54 2 ▶ unless ( $self->info->has_data('title') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->info->has_data("title")) 2 0 2
55 2 my $title = $fname;
56 2 $title =~ s/\..+$//g;
57 2 $title =~ s/^\d+\.?\ +//g;
58 2 $title =~ s/^ *//g;
59 2 $title =~ s/ *$//g;
60 2 $self->info->set_data('title',$title);
61 2 $self->tagchange("TITLE");
62 }
63 2 ▶ unless ( $self->info->has_data('artist') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->info->has_data("artist")) 2 0 2
64 2 $self->info->set_data('artist',$artist);
65 2 $self->tagchange("ARTIST");
66 }
67 2 ▶ unless ( $self->info->has_data('album') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->info->has_data("album")) 2 0 2
68 2 $self->info->set_data('album',$album);
69 2 $self->tagchange("ALBUM");
70 }
71 2 ▶ unless ( $self->info->has_data('disc') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->info->has_data("disc")) 2 0 2
72 2 $self->info->set_data('discnum',"1/1");
73 2 $self->tagchange("DISC");
74 }
75  
76 2 ▶ if ( ( not $self->info->has_data('picture') ) or ( $self->options->{coveroverwrite} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->info->has_data("picture") or $self->options->{"coveroverwrite"}) 2 0 2
Condition33%1/3
not $self->info->has_data("picture") or $self->options->{"coveroverwrite"}
l!l&&r!l&&!r
200
MC/DC0%0/2
not $self->info->has_data("picture") or $self->options->{"coveroverwrite"}
not $self->info->has_data("picture") $self->options->{"coveroverwrite"}
Truth table33%1/3
not(A) or B
A= not $self->info->has_data("picture")B= $self->options->{"coveroverwrite"}
ABresult
1X1
011
000
77 2 my $fname = File::Spec->catdir($dname, "folder.jpg");
78 2 my $pfname = File::Spec->catdir($dname, "folder.png");
79 2 my $cfname = File::Spec->catdir($dname, "cover.jpg");
80 2 ▶ if ( -e $fname ) {
Branch: 3 branches
BranchTrueFalseTotal
if (-e $fname) { } 1 1 2
elsif (-e $pfname) { } 0 1 1
elsif (-e $cfname) { } 0 1 1
81 1 $self->tagchange( "COVER ART", "from folder.jpg" );
82 1 $self->info->set_data('picture', $self->_cover_art($fname) );
83 }
84 elsif ( -e $pfname ) {
85 0 $self->tagchange( "COVER ART", "from folder.png" );
86 0 $self->info->set_data('picture', $self->_cover_art($pfname) );
87 }
88 elsif ( -e $cfname ) {
89 0 $self->tagchange( "COVER ART", "from cover.jpg" );
90 0 $self->info->set_data('picture', $self->_cover_art($cfname) );
91 }
92  
93 }
94 2 ▶ if ( ( not $self->info->has_data('lyrics') )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->info->has_data("lyrics") or $self->options->{"lyricsoverwrite"} or length $self->info->lyrics < 10) 2 0 2
Condition33%2/6
not $self->info->has_data("lyrics") or $self->options->{"lyricsoverwrite"} or length $self->info->lyrics < 10
l!l&&r!l&&!r
200
not $self->info->has_data("lyrics") or $self->options->{"lyricsoverwrite"}
l!l&&r!l&&!r
200
MC/DC0%0/3
not $self->info->has_data("lyrics") or $self->options->{"lyricsoverwrite"} or length $self->info->lyrics < 10
not $self->info->has_data("lyrics") $self->options->{"lyricsoverwrite"} length $self->info->lyrics < 10
Truth table25%1/4
not(A) or B or C
A= not $self->info->has_data("lyrics")B= $self->options->{"lyricsoverwrite"}C= length $self->info->lyrics < 10
ABCresult
1XX1
01X1
0011
0000
95 or ( $self->options->{lyricsoverwrite} )
96 or ( length( $self->info->lyrics ) < 10 ) ) {
97 2 my $fname = $self->info->filename;
98 2 $fname =~ s/\.[^\.]*$/.txt/;
99 2 ▶ if ( -e "$fname" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$fname") 0 2 2
100 0 $self->tagchange( "LYRICS", "from $fname" );
101 0 my $l = $self->_slurp_file($fname);
102 0 $self->info->set_data('lyrics',$l);
103 0 $l =~ s/\n\r?/ \/ /g;
104 0 $self->tagchange( "LYRICS", substr( $l, 0, 40 ) );
105 }
106 }
107 2 local *DIR;
108 2 opendir(DIR, $dname);
109 2 ▶ while ( my $f = readdir(DIR) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = readdir DIR)) 11 2 13
110 11 ▶ next if $f =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /^\./ 4 7 11
111 7 my $fname = File::Spec->catdir($dname, $f);
112 7 ▶ if ($f =~ /\.pdf$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\.pdf$/i) 0 7 7
113 0 ▶ unless ($self->info->has_data('booklet')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->info->has_data("booklet")) 0 0 0
114 0 $self->tagchange( "BOOKLET", "from $f" );
115 0 $self->info->set_data('booklet',$f);
116 }
117 }
118 #if ($f =~ /\.txt$/i) {
119 #unless ($self->info->has_data('lyrics')) {
120 #$self->tagchange( "LYRICS", "from $fname" );
121 #my $l = $self->_slurp_file($fname);
122 #$self->info->set_data('lyrics',$l);
123 #$l =~ s/\n\r?/ \/ /g;
124 #$self->tagchange( "LYRICS", substr( $l, 0, 40 ) );
125 #}
126 #}
127 7 ▶ if ($f =~ /\.jpg$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\.jpg$/i) 3 4 7
128 3 ▶ unless ($self->info->has_data('picture')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->info->has_data("picture")) 1 2 3
129 1 $self->tagchange( "COVER ART", "from $f" );
130 1 $self->info->set_data('picture', $self->_cover_art($fname) );
131 }
132 }
133 }
134  
135  
136  
137 2 return $self;
138 }
139  
140 0 ▶sub _slurp_file {
Subroutine _slurp_file: not called CC=3 SCAR=24.8
141 0 my $self = shift;
142 0 my $fname = shift;
143 0 local *IN;
144 0 ▶ open( IN, $fname ) or return "";
Branch: 1 branch
BranchTrueFalseTotal
open IN, $fname or return "" 0 0 0
145 0 my $l = "";
146 0 ▶ while (<IN>) { $l .= $_ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 0 0 0
147 0 close(IN);
148 0 return $l;
149 }
150  
151 2 ▶sub _cover_art {
Subroutine _cover_art: called CC=2 SCAR=7.5
152 2 my $self = shift;
153 2 my $picture = shift;
154 2 my ($vol, $root, $file) = File::Spec->splitpath($picture);
155 2 my $pic = { "Picture Type" => "Cover (front)",
156 "MIME type" => "image/jpg",
157 Description => "",
158 filename => $file,
159 _Data => "",
160 };
161 2 ▶ if ( $picture =~ /\.png$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($picture =~ /\.png$/i) 0 2 2
162 0 $pic->{"MIME type"} = "image/png";
163 }
164 2 return $pic;
165 0 local *IN;
166 #unless ( open( IN, $picture ) ) {
167 # $self->error("Could not open $picture for read: $!");
168 # return undef;
169 #}
170 #my $n = 0;
171 #my $b = 1;
172 #while ($b) {
173 # $b = sysread( IN, $pic->{"_Data"}, 1024, $n );
174 # $n += $b;
175 #}
176 #close(IN);
177 #return $pic;
178 }
179  
180 1 ▶sub save_cover {
Subroutine save_cover: called CC=7 SCAR=21.4
Pod documented
181 1 my $self = shift;
182 1 my ( $vol, $dir, $file ) = File::Spec->splitpath( $self->info->get_data('filename') );
183 1 my $filename = File::Spec->catpath( $vol, $dir, "folder.jpg" );
184  
185 #if ($dname eq "/") { $dname = "" } else {$dname = File::Spec->catpath($vol, $dir) }
186 1 my $art = $self->info->get_data('picture');
187 1 ▶ if ( exists $art->{_Data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $art->{"_Data"}) 1 0 1
188 1 local *OUT;
189 1 ▶ if ( $art->{"MIME type"} eq "image/png" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($art->{'MIME type'} eq 'image/png') { } 0 1 1
elsif ($art->{'MIME type'} eq 'image/bmp') { } 0 1 1
190 0 $filename = File::Spec->catpath( $vol, $dir, "folder.png" );
191 }
192 elsif ( $art->{"MIME type"} eq "image/bmp" ) {
193 0 $filename = File::Spec->catpath( $vol, $dir, "folder.jpg" );
194 }
195 1 $self->status("Saving cover image to $filename");
196 1 ▶ unless ( open OUT, ">$filename" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open OUT, ">$filename") 0 1 1
197 0 $self->error("Error writing to $filename: $!, skipping.");
198 0 return undef;
199 }
200 1 my $b = 0;
201 1 my $l = length( $art->{_Data} );
202 1 ▶ while ( $b < $l ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b < $l) 4 1 5
203 4 my $s = syswrite OUT, $art->{_Data}, 1024, $b;
204 4 ▶ if ( defined $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) { } 4 0 4
205 4 $b += $s;
206 }
207 else {
208 0 $self->status("Error writing to $filename: $!, skipping.");
209 0 return undef;
210 }
211 }
212 1 close OUT;
213 }
214 1 return 1;
215 }
216  
217 0 ▶sub save_lyrics {
Subroutine save_lyrics: not called CC=3 SCAR=24.8
Pod documented
218 0 my $self = shift;
219 0 my $fname = $self->info->get_data('filename');
220 0 $fname =~ s/\.[^\.]*$/.txt/;
221 0 my $lyrics = $self->info->get_data('lyrics');
222 0 ▶ if ($lyrics) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lyrics) 0 0 0
223 0 local *OUT;
224 0 $self->status("Saving lyrics image to $fname");
225 0 ▶ unless ( open OUT, ">$fname" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open OUT, ">$fname") 0 0 0
226 0 $self->status("Error writing to $fname: $!, skipping.");
227 0 return undef;
228 }
229 0 print OUT $lyrics;
230 0 close OUT;
231 }
232 0 return 1;
233 }
234  
235 1 ▶sub set_tag {
Subroutine set_tag: called CC=4 SCAR=15.9
Pod documented
236 1 my $self = shift;
237 1 ▶ unless ( $self->options("no_savecover")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->options("no_savecover")) 1 0 1
238 1 $self->save_cover( $self->info );
239 }
240 1 ▶ unless ( $self->options("no_savelyrics") or $self->info->get_data('filename') =~ /\.mp3$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->options("no_savelyrics") or $self->info->get_data("filename") =~ /\.mp3$/i) 0 1 1
Condition33%1/3
$self->options("no_savelyrics") or $self->info->get_data("filename") =~ /\.mp3$/i
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->options("no_savelyrics") or $self->info->get_data("filename") =~ /\.mp3$/i
$self->options("no_savelyrics") $self->info->get_data("filename") =~ /\.mp3$/i
Truth table33%1/3
A or B
A= $self->options("no_savelyrics")B= $self->info->get_data("filename") =~ /\.mp3$/i
ABresult
1X1
011
000
241 0 $self->save_lyrics( $self->info );
242 }
243 1 return $self;
244 }
245  
246 2 ▶sub default_options {
Subroutine default_options: called CC=1 SCAR=0.0
Pod documented
247 {
248 2 lyricsoverwrite => 0,
249 coveroverwrite => 0,
250 no_savecover => 0,
251 no_savelyrics => 0,
252 }
253 }
254  
255 2 1;
256  
257 __END__
↑ Summary