blib/lib/Chess/ELO/FEDA.pm

filter: Statementstmt 29.7% 49 / 165 Branchbran 14.7% 15 / 102 Conditioncond 35.7% 5 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 52.3% 11 / 21 Podpod 83.3% 5 / 6 totaltotal 27.7% 88 / 317 CC 59 SCAR 41.0
CC
59
Cov
25%
new_xls_player
156.0
_parse_abstract_dbd
132.0
download
72.0
CRAP 60.1
↑ Summary
1 package Chess::ELO::FEDA;
2 2 $Chess::ELO::FEDA::VERSION = '0.04';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DBD::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Uncompress::Unzip qw/$UnzipError/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Spreadsheet::ParseExcel;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Encode qw/decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # ABSTRACT: Download FEDA ELO (L<http://www.feda.org>) into differents backends (SQLite)
16  
17  
18 6 ▶sub new {
Subroutine new: called CC=12 SCAR=25.0
Pod documented
19 6 my $class = shift;
20 6 my %args = @_;
21 6 my $self = {-verbose=>0, -url=>'', -target=>'', -ext=>'', -path=>''};
22 6 ▶ $self->{-path} = $args{-folder} if exists $args{-folder};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-folder"} 4 2 6
23 6 ▶ $self->{-target} = $args{-target} if exists $args{-target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-target"} 5 1 6
24 6 ▶ $self->{-url} = $args{-url} if exists $args{-url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-url"} 0 6 6
25 6 ▶ $self->{-verbose} = $args{-verbose} if exists $args{-verbose};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-verbose"} 0 6 6
26 6 ▶ $self->{-callback} = $args{-callback} if exists $args{-callback};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"-callback"} 0 6 6
27  
28 6 ▶ if( $self->{-target} && ($self->{-target} =~ m!(\w+)$!) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-target"} and $self->{"-target"} =~ /(\w+)$/) 4 2 6
Condition66%2/3
$self->{"-target"} and $self->{"-target"} =~ /(\w+)$/
!ll&&!rl&&r
204
MC/DC50%1/2
$self->{"-target"} and $self->{"-target"} =~ /(\w+)$/
$self->{"-target"} $self->{"-target"} =~ /(\w+)$/
Truth table66%2/3
A and B
A= $self->{"-target"}B= $self->{"-target"} =~ /(\w+)$/
ABresult
0X0
100
111
29 4 $self->{-ext} = lc( $1 );
30 }
31
32 6 ▶ if( ($self->{-ext} ne 'sqlite') && ($self->{-ext} ne 'csv') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-ext"} ne "sqlite" and $self->{"-ext"} ne "csv") 3 3 6
Condition100%3/3
$self->{"-ext"} ne "sqlite" and $self->{"-ext"} ne "csv"
!ll&&!rl&&r
213
MC/DC100%2/2
$self->{"-ext"} ne "sqlite" and $self->{"-ext"} ne "csv"
$self->{"-ext"} ne "sqlite" $self->{"-ext"} ne "csv"
Truth table100%3/3
A and B
A= $self->{"-ext"} ne "sqlite"B= $self->{"-ext"} ne "csv"
ABresult
0X0
100
111
33 3 ▶ if( $self->{-target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'-target'}) { } 1 2 3
34 1 die "Unsupported target: [" . $self->{-ext} . "]";
35 }
36 else {
37 2 $self->{-ext} = 'NULL';
38 }
39 }
40  
41 5 ▶ unless(-d $self->{-path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"-path"}) 2 3 5
42 2 die "Invalid path: [" . $self->{-path} . "]";
43 }
44 3 bless $self, $class;
45 }
46  
47 #-------------------------------------------------------------------------------
48  
49  
50 0 ▶sub cleanup {
Subroutine cleanup: not called CC=3 SCAR=24.8
Pod documented
51 0 my $self = shift;
52 0 ▶ if( -e $self->{-xls} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->{"-xls"}) 0 0 0
53 0 ▶ $self->{-verbose} and print "+ remove xls file: ", $self->{-xls}, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"-verbose"} and print "+ remove xls file: ", $self->{"-xls"}, "\n" 0 0 0
54 0 unlink $self->{-xls};
55 }
56 }
57  
58 #-------------------------------------------------------------------------------
59  
60  
61 0 ▶sub download {
Subroutine download: not called CC=8 SCAR=42.8
Pod documented
62 0 my $self = shift;
63 0 ▶ my $filename = $self->{-target} || 'chess_elo_feda';
Condition0%0/2
$self->{'-target'} || 'chess_elo_feda'
l!l
00
MC/DC0%0/1
$self->{'-target'} || 'chess_elo_feda'
$self->{'-target'}
Truth table0%0/2
A
A= $self->{'-target'}
Aresult
11
00
64 0 my $target_filename = catfile($self->{-path}, $filename);
65 0 my $zip_filename = $target_filename . '.zip';
66 0 my $xls_filename = $target_filename . '.xls';
67  
68 ##my $response = {content=>'', status=>200, reason=>'OK'};
69 0 my $response = HTTP::Tiny->new->get($self->{-url});
70 0 ▶ die "GET [$self->{-url}] failed" unless $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
71  
72 0 ▶ if( length $response->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $response->{"content"}) 0 0 0
73 0 ▶ open my $fhz, ">", $zip_filename or die "Cannot open file: $zip_filename";
Branch: 1 branch
BranchTrueFalseTotal
open my $fhz, ">", $zip_filename or die "Cannot open file: $zip_filename" 0 0 0
74 0 binmode $fhz;
75 0 print $fhz $response->{content} ;
76 0 close $fhz;
77 }
78 0 ▶ print "+ Download: ", $zip_filename, " => [", $response->{status}, "]: ", $response->{reason}, "\n" if $self->{-verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-verbose"} 0 0 0
79 0 $self->_extract_file_from_zip($xls_filename, $zip_filename, qr!\.xls$!i);
80 0 unlink $zip_filename;
81 0 ▶ print "+ Unzip: ", $xls_filename, "\n" if $self->{-verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-verbose"} 0 0 0
82 0 $self->{-xls} = $xls_filename;
83 0 ▶ return (-e $self->{-xls}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-e $self->{'-xls'} ? : 0 0 0
84 }
85  
86 #-------------------------------------------------------------------------------
87  
88  
89 0 ▶sub parse {
Subroutine parse: not called CC=4 SCAR=30.0
Pod documented
90 0 my $self = shift;
91
92 0 my $rc = 0;
93  
94 0 ▶ if( $self->{-ext} eq 'NULL' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'-ext'} eq 'NULL') { } 0 0 0
elsif ($self->{'-ext'} eq 'sqlite') { } 0 0 0
elsif ($self->{'-ext'} eq 'csv') { } 0 0 0
95 0 $rc = $self->_parse_null;
96 }
97 elsif( $self->{-ext} eq 'sqlite' ) {
98 0 $rc = $self->_parse_sqlite;
99 }
100 elsif( $self->{-ext} eq 'csv' ) {
101 0 $rc = $self->_parse_csv;
102 }
103 else {
104 0 die "Unsupported target. Not in [sqlite, csv]";
105 }
106  
107 0 return $rc;
108 }
109  
110 #-------------------------------------------------------------------------------
111  
112  
113 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
114 0 my $self = shift;
115 0 my $rc = 0;
116  
117 0 ▶ if( $self->download() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->download) 0 0 0
118 0 $rc = $self->parse;
119 0 $self->cleanup;
120 }
121  
122 0 return $rc;
123 }
124  
125 #-------------------------------------------------------------------------------
126  
127 0 ▶sub _extract_file_from_zip {
Subroutine _extract_file_from_zip: not called CC=7 SCAR=40.3
128 0 my ($self, $xlsfile, $zipfile, $regexpr_file_to_extract) = @_;
129 0 ▶ my $u = new IO::Uncompress::Unzip $zipfile or die "Cannot open $zipfile: $UnzipError";
Branch: 1 branch
BranchTrueFalseTotal
my $u = "IO::Uncompress::Unzip"->new($zipfile) or die "Cannot open ${zipfile}: $Chess::ELO::FEDA::UnzipError" 0 0 0
130 0 my $filename = undef;
131  
132 0 ▶ for( my $status = 1; $status > 0; $status = $u->nextStream() )
Branch: 1 branch
BranchTrueFalseTotal
if ($status > 0) 0 0 0
133 {
134 0 my $name = $u->getHeaderInfo()->{Name};
135 0 ▶ next unless $name =~ $regexpr_file_to_extract;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$regexpr_file_to_extract/ 0 0 0
136
137 0 my $buff;
138 0 ▶ open my $fh, '>', $xlsfile or die "Couldn't write to $name: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $xlsfile or die "Couldn't write to ${name}: $!" 0 0 0
139 0 binmode $fh;
140 0 ▶ while( ($status = $u->read($buff)) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($status = $u->read($buff)) > 0) 0 0 0
141 0 syswrite $fh, $buff;
142 }
143 0 close $fh;
144 0 $filename = $name;
145 0 last;
146 }
147
148 0 ▶ return ($filename) ? $xlsfile . "/$filename" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$filename ? : 0 0 0
149 }
150  
151 #-------------------------------------------------------------------------------
152  
153 0 ▶sub _parse_null {
Subroutine _parse_null: not called CC=2 SCAR=17.9
154 0 my $self = shift;
155
156 0 ▶ $self->{-verbose} and print "+ NULL target", "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"-verbose"} and print "+ NULL target", "\n" 0 0 0
157 0 my $rc = $self->_parse_abstract_dbd(undef);
158
159 0 return $rc;
160 }
161  
162 #-------------------------------------------------------------------------------
163  
164 0 ▶sub _parse_sqlite {
Subroutine _parse_sqlite: not called CC=4 SCAR=30.0
165 0 my $self = shift;
166
167 0 $self->{-dbfile} = catfile($self->{-path}, $self->{-target});
168 0 ▶ $self->{-verbose} and print "+ DB File: ", $self->{-dbfile}, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"-verbose"} and print "+ DB File: ", $self->{"-dbfile"}, "\n" 0 0 0
169 0 ▶ unlink $self->{-dbfile} if -e $self->{-dbfile};
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->{"-dbfile"} 0 0 0
170  
171 0 ▶ my $dbh = DBI->connect("dbi:SQLite:dbname=" . $self->{-dbfile}, "", "", {
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect('dbi:SQLite:dbname=' . $self->{"-dbfile"}, "", "", {"RaiseError", 1, "AutoCommit", 0}) or die $DBI::errstr 0 0 0
172 RaiseError=>1,
173 AutoCommit=>0
174 }) or die $DBI::errstr;
175 0 my $rc = $self->_parse_abstract_dbd($dbh);
176 0 $dbh->disconnect;
177 0 return $rc;
178 }
179  
180 #-------------------------------------------------------------------------------
181  
182 0 ▶sub _parse_csv {
Subroutine _parse_csv: not called CC=4 SCAR=30.0
183 0 my $self = shift;
184  
185 0 $self->{-dbfile} = catfile($self->{-path}, $self->{-target});
186 0 ▶ $self->{-verbose} and print "+ DB File: ", $self->{-dbfile}, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"-verbose"} and print "+ DB File: ", $self->{"-dbfile"}, "\n" 0 0 0
187 0 ▶ unlink $self->{-dbfile} if -e $self->{-dbfile};
Branch: 1 branch
BranchTrueFalseTotal
if -e $self->{"-dbfile"} 0 0 0
188  
189 my $dbh = DBI->connect ("dbi:CSV:", "", "", {
190 f_schema => undef,
191 f_dir => $self->{-path},
192 f_encoding => "utf8",
193 csv_eol => "\n",
194 csv_sep_char => ",",
195 csv_quote_char => '"',
196 csv_escape_char => '"',
197 csv_class => "Text::CSV_XS",
198 csv_null => 1,
199 csv_always_quote => 1,
200 csv_tables => { elo_feda => { f_file => $self->{-target} } },
201 0 ▶ RaiseError => 1,
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect("dbi:CSV:", "", "", {"f_schema", undef, "f_dir", $self->{"-path"}, "f_encoding", "utf8", "csv_eol", "\n", "csv_sep_char", ",", "csv_quote_char", "\"", "csv_escape_char", "\"", "csv_class", "Text::CSV_XS", "csv_null", 1, "csv_always_quote", 1, "csv_tables", {"elo_feda", {"f_file", $self->{"-target"}}}, "RaiseError", 1, "AutoCommit", 1}) or die $DBI::errstr 0 0 0
202 AutoCommit => 1
203 }) or die $DBI::errstr;
204 0 my $rc = $self->_parse_abstract_dbd($dbh);
205 0 $dbh->disconnect;
206 0 return $rc;
207 }
208  
209 #-------------------------------------------------------------------------------
210  
211 0 ▶sub _parse_abstract_dbd {
Subroutine _parse_abstract_dbd: not called CC=11 SCAR=48.8
212 0 my $self = shift;
213 0 my $dbh = shift;
214
215 0 ▶ $dbh and $dbh->do(qq/CREATE TABLE elo_feda(
Branch: 1 branch
BranchTrueFalseTotal
$dbh and $dbh->do("CREATE TABLE elo_feda(\nfeda_id integer primary key, \nsurname varchar(32) not null,\nname varchar(32), \nfed varchar(8), \nrating integer, \ngames integer, \nbirth integer, \ntitle varchar(16), \nflag varchar(8)\n)") 0 0 0
216 feda_id integer primary key,
217 surname varchar(32) not null,
218 name varchar(32),
219 fed varchar(8),
220 rating integer,
221 games integer,
222 birth integer,
223 title varchar(16),
224 flag varchar(8)
225 )/ );
226 0 ▶ $self->{-verbose} and print "+ Load XLS: ", $self->{-xls}, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"-verbose"} and print "+ Load XLS: ", $self->{"-xls"}, "\n" 0 0 0
227  
228 0 my $parser = Spreadsheet::ParseExcel->new;
229 my $workbook = $parser->parse( $self->{-xls} )
230 0 ▶ or die $parser->error(), "\n";
Branch: 1 branch
BranchTrueFalseTotal
my $workbook = $parser->parse($self->{"-xls"}) or die $parser->error, "\n" 0 0 0
231  
232 0 my $worksheet = $workbook->worksheet('ELO');
233 0 my ( $row_min, $row_max ) = $worksheet->row_range();
234  
235 0 my $START_XLS_ROW = 4;
236 0 my @player_keys = qw/feda_id name fed rating games birth title flag/;
237  
238 0 ▶ sub new_xls_player {
Subroutine new_xls_player: not called CC=12 SCAR=50.5
Pod undocumented
239 0 my ($worksheet, $stmt, $player_keys, $callback, $start_row, $stop_row, $verbose) = @_;
240 0 for my $row ( $start_row .. $stop_row ) {
241 0 my %feda_player;
242 0 for my $col_index( 0..7 ) {
243 0 my $cell = $worksheet->get_cell($row, $col_index);
244 0 ▶ my $value = $cell ? $cell->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cell ? : 0 0 0
245 0 $feda_player{ $player_keys->[$col_index] } = $value;
246 }
247
248 0 $feda_player{name} = decode('latin1', $feda_player{name});
249
250 0 my $name = $feda_player{name};
251 0 my ($apellidos, $nombre) = split / *, */, $name;
252  
253 0 ▶ if( $apellidos && $nombre ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($apellidos and $nombre) { } 0 0 0
elsif (index($name, '.') != -1) { } 0 0 0
elsif ($apellidos) { } 0 0 0
Condition0%0/3
$apellidos and $nombre
!ll&&!rl&&r
000
MC/DC0%0/2
$apellidos and $nombre
$apellidos $nombre
Truth table0%0/3
A and B
A= $apellidosB= $nombre
ABresult
0X0
100
111
254 0 $feda_player{surname} = $apellidos;
255 0 $feda_player{name} = $nombre;
256 }
257 elsif( index($name, '.') >= 0 ) {
258 0 ($apellidos, $nombre) = split / *\. */, $name;
259 0 $feda_player{surname} = $apellidos;
260 0 $feda_player{name} = $nombre;
261 }
262 elsif( $apellidos ) {
263 0 $feda_player{surname} = $apellidos;
264 0 $feda_player{name} = '***';
265 }
266 0 eval {
267 $stmt and $stmt->execute(
268 $feda_player{feda_id},
269 $feda_player{surname},
270 $feda_player{name},
271 $feda_player{fed},
272 $feda_player{rating},
273 $feda_player{games},
274 $feda_player{birth},
275 $feda_player{title},
276 $feda_player{flag}
277 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$stmt and $stmt->execute($feda_player{"feda_id"}, $feda_player{"surname"}, $feda_player{"name"}, $feda_player{"fed"}, $feda_player{"rating"}, $feda_player{"games"}, $feda_player{"birth"}, $feda_player{"title"}, $feda_player{"flag"}) 0 0 0
278 0 ▶ $callback and $callback->(\%feda_player);
Branch: 1 branch
BranchTrueFalseTotal
$callback and &$callback(\%feda_player) 0 0 0
279 };
280 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
281 0 ▶ $verbose and print "DB Error: $@", "\n";
Branch: 1 branch
BranchTrueFalseTotal
$verbose and print "DB Error: $@", "\n" 0 0 0
282 }
283 }
284 }
285  
286 0 my $BLOCK_TXN = 2000;
287 0 my $i = $START_XLS_ROW;
288 0 my $j = $i + $BLOCK_TXN - 1;
289
290 0 ▶ my $stmt = $dbh ?
Branch: 1 branch
BranchTrueFalseTotal
$dbh ? : 0 0 0
291 $dbh->prepare("insert into elo_feda (feda_id, surname, name, fed, rating, games, birth, title, flag) values (?,?,?,?,?,?,?,?,?)"):
292 undef;
293  
294 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$i <= $row_max and do { do { new_xls_player($worksheet, $stmt, \@player_keys, $self->{"-callback"}, $i, $j, $self->{"-verbose"}); if ($dbh and not $dbh->{"AutoCommit"}) { $dbh->commit unless $dbh->{"AutoCommit"}; } ; $i += $BLOCK_TXN; $j = $i + $BLOCK_TXN - 1 > $row_max ? $row_max : $i + $BLOCK_TXN - 1 } } 0 0 0
295 0 new_xls_player($worksheet, $stmt, \@player_keys, $self->{-callback}, $i, $j, $self->{-verbose});
296 0 ▶ if( $dbh && (! $dbh->{AutoCommit}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh and not $dbh->{"AutoCommit"}) 0 0 0
Condition0%0/3
$dbh and not $dbh->{"AutoCommit"}
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh and not $dbh->{"AutoCommit"}
$dbh not $dbh->{"AutoCommit"}
Truth table0%0/3
A and not(B)
A= $dbhB= not $dbh->{"AutoCommit"}
ABresult
0X0
100
111
297 0 ▶ $dbh->commit unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 0 0
298 }
299 0 $i += $BLOCK_TXN;
300 0 ▶ $j = ($i + $BLOCK_TXN -1) > $row_max ? $row_max : $i + $BLOCK_TXN - 1;
Branch: 1 branch
BranchTrueFalseTotal
$i + $BLOCK_TXN - 1 > $row_max ? : 0 0 0
301 } while( $i <= $row_max );
302  
303 0 ▶ $stmt and $stmt->finish;
Branch: 1 branch
BranchTrueFalseTotal
$stmt and $stmt->finish 0 0 0
304  
305 0 return 1;
306 }
307  
308 #-------------------------------------------------------------------------------
309  
310 2 1;
311  
312 __END__
↑ Summary