blib/lib/CPAN/Testers/WWW/Statistics/Excel.pm

filter: Statementstmt 100% 139 / 139 Branchbran 90.7% 49 / 54 Conditioncond 97.9% 48 / 49 MC/DCmcdc 96.7% 30 / 31 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 97.5% 281 / 288 CC 47 SCAR 33.8
CC
47
Cov
98%
create
41.0
_log
5.0
new
3.0
CRAP 29.3
↑ Summary
1 package CPAN::Testers::WWW::Statistics::Excel;
2  
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 $VERSION = '0.06';
8  
9 #----------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 CPAN::Testers::WWW::Statistics::Excel - CPAN Testers Statistics Excel tool.
14  
15 =head1 SYNOPSIS
16  
17 my %hash = { logfile => 'my.log' };
18 my $ct = CPAN::Testers::WWW::Statistics::Excel->new(%hash);
19 $ct->create( source => $source, target => $target );
20  
21 =head1 DESCRIPTION
22  
23 Using previously formatted data, generate Excel format files.
24  
25 =cut
26  
27 # -------------------------------------
28 # Library Modules
29  
30 9 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 9 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 9 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 ▶use Spreadsheet::WriteExcel;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # -------------------------------------
40 # Variables
41  
42 9 my %format_config = (
43 head => { border => 1, pattern => 1, color => 'black', bg_color => 'gray', bold => 1 },
44 lots => { border => 1, pattern => 1, color => 'black', bg_color => 'green', align => 'right' },
45 more => { border => 1, pattern => 1, color => 'black', bg_color => 'lime', align => 'right' },
46 some => { border => 1, pattern => 1, color => 'black', bg_color => 'yellow', align => 'right' },
47 none => { border => 1, pattern => 1, color => 'black', bg_color => 'silver', align => 'center', valign => 'middle' },
48 totals => { border => 1, pattern => 1, color => 'white', bg_color => 'black', bold => 1 },
49 );
50  
51 # -------------------------------------
52 # Subroutines
53  
54 =head1 INTERFACE
55  
56 =head2 The Constructor
57  
58 =over 4
59  
60 =item * new
61  
62 Object constructor. Takes an optional hash, which can contain initial settings
63 for log file creation:
64  
65 logfile - path to log file
66 logclean - append (0) or overwrite/create (1)
67  
68 =back
69  
70 =cut
71  
72 6 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
73 6 my $class = shift;
74 6 my %hash = @_;
75  
76 6 my $self = {};
77 6 bless $self, $class;
78  
79 6 ▶ $self->logfile( $hash{logfile} || '' );
Condition100%2/2
$hash{'logfile'} || ''
l!l
33
MC/DC100%1/1
$hash{'logfile'} || ''
$hash{'logfile'}
Truth table100%2/2
A
A= $hash{'logfile'}
Aresult
11
00
80 6 ▶ $self->logclean( $hash{logclean} || 0 );
Condition100%2/2
$hash{'logclean'} || 0
l!l
15
MC/DC100%1/1
$hash{'logclean'} || 0
$hash{'logclean'}
Truth table100%2/2
A
A= $hash{'logclean'}
Aresult
11
00
81  
82 6 $self->_log("logfile =" . $self->logfile );
83 6 $self->_log("logclean =" . $self->logclean );
84  
85 6 return $self;
86 }
87  
88 =head2 Methods
89  
90 =over 4
91  
92 =item * create
93  
94 Method to facilitate the creation of an Excel file.
95  
96 Parameter values are contained within a hash to the method:
97  
98 source - path to source HTML containing table
99 target - path to target Excel format file
100  
101 In addition the following hash values can also be passed:
102  
103 title - title for the file (Excel property)
104 author - author of the file (Excel property)
105 comments - comments string (Excel property)
106  
107 =item * logfile
108  
109 Accessor for the path to the file to use for log messages. If no path is given
110 either via this method or through the constructor, no log messages are printed.
111  
112 =item * logclean
113  
114 Accessor for log creation. If a false value will append log messages,
115 otherwise will overwrite any existing logfile.
116  
117 =back
118  
119 =cut
120  
121 9 __PACKAGE__->mk_accessors( qw( logfile logclean ) );
122  
123  
124 5 ▶sub create {
Subroutine create: called CC=41 SCAR=37.1
Pod documented
125 5 my $self = shift;
126 5 my %hash = @_;
127 5 my %opt;
128  
129 5 $self->_log("start");
130  
131 5 ▶ die "Source file not provided\n" unless( $hash{source});
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{"source"} 1 4 5
132 4 ▶ die "Target file not provided\n" unless( $hash{target});
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{"target"} 1 3 4
133 3 ▶ die "Source file [$hash{source}] not found\n" unless(-f $hash{source});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $hash{"source"} 1 2 3
134 2 mkpath(dirname($hash{target}));
135  
136 2 my $workbook = Spreadsheet::WriteExcel->new( $hash{target} );
137  
138 2 $workbook->set_custom_color(23, '#999999'); # head
139 2 $workbook->set_custom_color(17, '#00ff00'); # lots
140 2 $workbook->set_custom_color(11, '#99ff99'); # more
141 2 $workbook->set_custom_color(13, '#ddffdd'); # some
142 2 $workbook->set_custom_color(22, '#dddddd'); # none
143  
144 # Add and define a format
145 2 my %formats;
146 2 for my $format (keys %format_config) {
147 12 my $class = $workbook->add_format( %{ $format_config{$format} } ); # Add a format
148 12 $formats{$format} = $class;
149 }
150  
151 2 my $worksheet = $workbook->add_worksheet();
152  
153 2 my $cell = {};
154 2 my ($title,$table,$row,$col) = (0,0,0,0);
155 2 my $p = HTML::TokeParser->new( $hash{source}, %opt );
156 2 ▶ while(my $token = $p->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_token) 36232 0 36232
157  
158 # if no title given, use the H2 tag.
159 36232 ▶ unless($table || $hash{title}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table or $hash{"title"}) 192 36040 36232
Condition100%3/3
$table or $hash{"title"}
l!l&&r!l&&!r
3600040192
MC/DC100%2/2
$table or $hash{"title"}
$table $hash{"title"}
Truth table100%3/3
A or B
A= $tableB= $hash{"title"}
ABresult
1X1
011
000
160 192 ▶ if($token->[0] eq 'S' && $token->[1] eq 'h2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "S" and $token->[1] eq "h2") 1 191 192
Condition100%3/3
$token->[0] eq "S" and $token->[1] eq "h2"
!ll&&!rl&&r
131601
MC/DC100%2/2
$token->[0] eq "S" and $token->[1] eq "h2"
$token->[0] eq "S" $token->[1] eq "h2"
Truth table100%3/3
A and B
A= $token->[0] eq "S"B= $token->[1] eq "h2"
ABresult
0X0
100
111
161 1 $title = 1;
162 1 $cell = {text => ''};
163 1 next;
164 }
165 191 ▶ if($token->[0] eq 'E' && $token->[1] eq 'h2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "E" and $token->[1] eq "h2") 1 190 191
Condition100%3/3
$token->[0] eq "E" and $token->[1] eq "h2"
!ll&&!rl&&r
167231
MC/DC100%2/2
$token->[0] eq "E" and $token->[1] eq "h2"
$token->[0] eq "E" $token->[1] eq "h2"
Truth table100%3/3
A and B
A= $token->[0] eq "E"B= $token->[1] eq "h2"
ABresult
0X0
100
111
166 1 $title = 0;
167 1 $hash{title} = decode_entities($cell->{text});
168 1 $self->_log("TITLE: '$cell->{text}'");
169 1 next;
170 }
171 190 ▶ if($title && $token->[0] eq 'T') {
Branch: 1 branch
BranchTrueFalseTotal
if ($title and $token->[0] eq "T") 1 189 190
Condition66%2/3
$title and $token->[0] eq "T"
!ll&&!rl&&r
18901
MC/DC50%1/2
$title and $token->[0] eq "T"
$title $token->[0] eq "T"
Truth table66%2/3
A and B
A= $titleB= $token->[0] eq "T"
ABresult
0X0
100
111
172 1 ▶ $cell->{text} .= "\n" if($cell->{text});
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"text"} 0 1 1
173 1 $cell->{text} .= $token->[1];
174 }
175 }
176  
177 36230 ▶ next unless($table || $token->[1] eq 'table');
Branch: 1 branch
BranchTrueFalseTotal
unless $table or $token->[1] eq "table" 228 36002 36230
Condition100%3/3
$table or $token->[1] eq "table"
l!l&&r!l&&!r
360002228
MC/DC100%2/2
$table or $token->[1] eq "table"
$table $token->[1] eq "table"
Truth table100%3/3
A or B
A= $tableB= $token->[1] eq "table"
ABresult
1X1
011
000
178  
179 36002 ▶ if($token->[0] eq 'S' && $token->[1] eq 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "S" and $token->[1] eq "table") 2 36000 36002
Condition100%3/3
$token->[0] eq "S" and $token->[1] eq "table"
!ll&&!rl&&r
23152128482
MC/DC100%2/2
$token->[0] eq "S" and $token->[1] eq "table"
$token->[0] eq "S" $token->[1] eq "table"
Truth table100%3/3
A and B
A= $token->[0] eq "S"B= $token->[1] eq "table"
ABresult
0X0
100
111
180 2 $table = 1;
181 2 next;
182 }
183 36000 ▶ if($token->[0] eq 'E' && $token->[1] eq 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "E" and $token->[1] eq "table") 2 35998 36000
Condition100%3/3
$token->[0] eq "E" and $token->[1] eq "table"
!ll&&!rl&&r
24414115842
MC/DC100%2/2
$token->[0] eq "E" and $token->[1] eq "table"
$token->[0] eq "E" $token->[1] eq "table"
Truth table100%3/3
A and B
A= $token->[0] eq "E"B= $token->[1] eq "table"
ABresult
0X0
100
111
184 2 $table = 0;
185 2 last;
186 }
187  
188 35998 ▶ if($token->[0] eq 'S' && $token->[1] eq 'tr') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "S" and $token->[1] eq "tr") 129 35869 35998
Condition100%3/3
$token->[0] eq "S" and $token->[1] eq "tr"
!ll&&!rl&&r
2315012719129
MC/DC100%2/2
$token->[0] eq "S" and $token->[1] eq "tr"
$token->[0] eq "S" $token->[1] eq "tr"
Truth table100%3/3
A and B
A= $token->[0] eq "S"B= $token->[1] eq "tr"
ABresult
0X0
100
111
189 129 $col = 0;
190 129 next;
191 }
192 35869 ▶ if($token->[0] eq 'E' && $token->[1] eq 'tr') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "E" and $token->[1] eq "tr") 129 35740 35869
Condition100%3/3
$token->[0] eq "E" and $token->[1] eq "tr"
!ll&&!rl&&r
2428511455129
MC/DC100%2/2
$token->[0] eq "E" and $token->[1] eq "tr"
$token->[0] eq "E" $token->[1] eq "tr"
Truth table100%3/3
A and B
A= $token->[0] eq "E"B= $token->[1] eq "tr"
ABresult
0X0
100
111
193 129 $row++;
194 129 next;
195 }
196  
197 35740 ▶ if($token->[0] eq 'S' && $token->[1] eq 'th') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "S" and $token->[1] eq "th") 1116 34624 35740
Condition100%3/3
$token->[0] eq "S" and $token->[1] eq "th"
!ll&&!rl&&r
23021116031116
MC/DC100%2/2
$token->[0] eq "S" and $token->[1] eq "th"
$token->[0] eq "S" $token->[1] eq "th"
Truth table100%3/3
A and B
A= $token->[0] eq "S"B= $token->[1] eq "th"
ABresult
0X0
100
111
198 1116 $cell = { class => 'head', text => '' };
199 1116 ▶ if($token->[2]->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[2]{"class"}) 550 566 1116
200 550 $cell = { class => $token->[2]->{class}, text => '' };
201 }
202 1116 next;
203 }
204 34624 ▶ if($token->[0] eq 'E' && $token->[1] eq 'th') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "E" and $token->[1] eq "th") 1116 33508 34624
Condition100%3/3
$token->[0] eq "E" and $token->[1] eq "th"
!ll&&!rl&&r
23169103391116
MC/DC100%2/2
$token->[0] eq "E" and $token->[1] eq "th"
$token->[0] eq "E" $token->[1] eq "th"
Truth table100%3/3
A and B
A= $token->[0] eq "E"B= $token->[1] eq "th"
ABresult
0X0
100
111
205 # write cell
206 1116 $self->_log("CELL: TH: [$row/$col] $cell->{class} '$cell->{text}'");
207 1116 $worksheet->write($row, $col, decode_entities($cell->{text}), $formats{$cell->{class}});
208 1116 $col++;
209 1116 next;
210 }
211  
212 33508 ▶ if($token->[0] eq 'S' && $token->[1] eq 'td') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "S" and $token->[1] eq "td") 9075 24433 33508
Condition100%3/3
$token->[0] eq "S" and $token->[1] eq "td"
!ll&&!rl&&r
2190525289075
MC/DC100%2/2
$token->[0] eq "S" and $token->[1] eq "td"
$token->[0] eq "S" $token->[1] eq "td"
Truth table100%3/3
A and B
A= $token->[0] eq "S"B= $token->[1] eq "td"
ABresult
0X0
100
111
213 9075 $cell = { class => 'none', text => '' };
214 9075 ▶ if($token->[2]->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[2]{"class"}) 9075 0 9075
215 9075 $cell = { class => $token->[2]->{class}, text => '' };
216 }
217 9075 next;
218 }
219 24433 ▶ if($token->[0] eq 'E' && $token->[1] eq 'td') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "E" and $token->[1] eq "td") 9075 15358 24433
Condition100%3/3
$token->[0] eq "E" and $token->[1] eq "td"
!ll&&!rl&&r
1409412649075
MC/DC100%2/2
$token->[0] eq "E" and $token->[1] eq "td"
$token->[0] eq "E" $token->[1] eq "td"
Truth table100%3/3
A and B
A= $token->[0] eq "E"B= $token->[1] eq "td"
ABresult
0X0
100
111
220 # write cell
221 9075 $self->_log("CELL: TD: [$row/$col] $cell->{class} '$cell->{text}'");
222 9075 $worksheet->write($row, $col, decode_entities($cell->{text}), $formats{$cell->{class}});
223 9075 $col++;
224 9075 next;
225 }
226  
227 15358 ▶ if($token->[0] eq 'T') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "T") 11566 3792 15358
228 11566 ▶ $cell->{text} .= "\n" if($cell->{text});
Branch: 1 branch
BranchTrueFalseTotal
if $cell->{"text"} 1388 10178 11566
229 11566 $cell->{text} .= $token->[1];
230 }
231 }
232  
233 2 ▶ $hash{title} ||= 'CPAN Testers Matrix';
Condition100%2/2
$hash{"title"} ||= "CPAN Testers Matrix"
l!l
11
MC/DC100%1/1
$hash{"title"} ||= "CPAN Testers Matrix"
$hash{"title"}
Truth table100%2/2
A
A= $hash{"title"}
Aresult
11
00
234 2 ▶ $hash{author} ||= 'CPAN Testers';
Condition100%2/2
$hash{"author"} ||= "CPAN Testers"
l!l
11
MC/DC100%1/1
$hash{"author"} ||= "CPAN Testers"
$hash{"author"}
Truth table100%2/2
A
A= $hash{"author"}
Aresult
11
00
235 2 ▶ $hash{comments} ||= 'Copyright (C) 2009 The Perl Foundation';
Condition100%2/2
$hash{"comments"} ||= "Copyright (C) 2009 The Perl Foundation"
l!l
11
MC/DC100%1/1
$hash{"comments"} ||= "Copyright (C) 2009 The Perl Foundation"
$hash{"comments"}
Truth table100%2/2
A
A= $hash{"comments"}
Aresult
11
00
236  
237 2 $worksheet->set_landscape(); # Landscape mode
238 2 $worksheet->set_paper(9); # A4
239 2 $worksheet->fit_to_pages(0, 1); # 1 page deep, many wide
240 2 $worksheet->set_header('&L&D&C'.$hash{title}.'&R&T');
241 2 $worksheet->set_footer('&RPage &P of &N');
242  
243 2 $worksheet->repeat_rows(0, 1); # Repeat the first two rows
244 2 $worksheet->repeat_columns(0, 1); # Repeat the first two columns
245  
246  
247 $workbook->set_properties(
248 title => $hash{title},
249 author => $hash{author},
250 comments => $hash{comments},
251 2 );
252  
253 2 ▶ $workbook->close() or die "Error closing file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$workbook->close or die "Error closing file: $!" 2 0 2
254 2 $self->_log("finish");
255 }
256  
257 # -------------------------------------
258 # Private Methods
259  
260 10215 ▶sub _log {
Subroutine _log: called CC=5 SCAR=16.1
261 10215 my $self = shift;
262 10215 ▶ my $log = $self->logfile or return;
Branch: 1 branch
BranchTrueFalseTotal
my $log = $self->logfile or return 10 10205 10215
263 10 ▶ mkpath(dirname($log)) unless(-f $log);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $log 1 9 10
264  
265 10 ▶ my $mode = $self->logclean ? 'w+' : 'a+';
Branch: 1 branch
BranchTrueFalseTotal
$self->logclean ? : 1 9 10
266 10 $self->logclean(0);
267  
268 10 my @dt = localtime(time);
269 10 my $dt = sprintf "%04d/%02d/%02d %02d:%02d:%02d", $dt[5]+1900,$dt[4]+1,$dt[3],$dt[2],$dt[1],$dt[0];
270  
271 10 ▶ my $fh = IO::File->new($log,$mode) or die "Cannot write to log file [$log]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($log, $mode) or die "Cannot write to log file [$log]: $!\n" 10 0 10
272 10 print $fh "$dt ", @_, "\n";
273 10 $fh->close;
274 }
275  
276 9 q('This module is dedicated to the Birmingham Perl Mongers');
277  
278 __END__
↑ Summary