lib/Serge/Command/import.pm

filter: Statementstmt 20.6% 25 / 121 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 7 totaltotal 17.3% 33 / 190 SCAR 64.9
CC
32
Cov
15%
run
420.0
print_formatted_table
56.0
severity_sort
42.0
CRAP 661.5
« lib/Serge/Engine/Plugin/parse_rc.pm ↑ Summary lib/Serge/Application.pm »
1 package Serge::Command::import;
2 1 ▶use parent Serge::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Serge::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Serge::Importer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Serge::Engine::Processor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Serge::Util qw(xml_escape_strref);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub get_commands {
14 return {
15 0 ▶ import => {need_config => 1, handler => \&run, info => 'Import translations from already existing resource files'},
Subroutine get_commands: not called CC=1 SCAR=6.9
Pod undocumented
16 }
17 }
18  
19 sub init {
20 0 ▶ my ($self, $command) = @_;
Subroutine init: not called CC=2 SCAR=17.9
Pod undocumented
21  
22 0 $self->SUPER::init($command);
23  
24 GetOptions(
25 "dry-run" => \$self->{dry_run},
26 "lang|language|languages=s" => \$self->{languages},
27 "disambiguate-keys" => \$self->{disambiguate_keys},
28 "no-report" => \$self->{no_report},
29 0 ▶ ) or die "Failed to parse some command-line parameters.";
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("dry-run", \$self->{"dry_run"}, "lang|language|languages=s", \$self->{"languages"}, "disambiguate-keys", \$self->{"disambiguate_keys"}, "no-report", \$self->{"no_report"}) or die "Failed to parse some command-line parameters." 0 0 0
30 }
31  
32 sub run {
33 0 ▶ my ($self, $command) = @_;
Subroutine run: not called CC=20 SCAR=60.4
Pod undocumented
34  
35 0 my $importer = Serge::Importer->new();
36  
37 0 ▶ $importer->{dry_run} = 1 if $self->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dry_run"} 0 0 0
38 0 ▶ $importer->{save_report} = 1 unless $self->{no_report};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_report"} 0 0 0
39 0 ▶ $importer->{debug} = 1 if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
40 0 ▶ $importer->{disambiguate_keys} = 1 if $self->{disambiguate_keys};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"disambiguate_keys"} 0 0 0
41  
42 0 ▶ if ($self->{languages}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"languages"}) 0 0 0
43 0 my @l = split(/,/, $self->{languages});
44 0 $importer->{limit_destination_languages} = \@l;
45 }
46  
47 0 ▶ print "\n*** DRY RUN ***\n" if $self->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dry_run"} 0 0 0
48  
49 map {
50 0 print "\n*** $_ ***\n\n";
51  
52 0 my $config = Serge::Config->new($_);
53 0 my $processor = Serge::Engine::Processor->new($importer, $config);
54 0 $processor->run();
55  
56 0 } $self->{parent}->get_config_files;
57  
58 0 ▶ if (!$self->{no_report}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_report"}) 0 0 0
59 0 print "\nSaving reports:\n";
60 0 foreach my $lang (sort keys %{$importer->{report}}) {
61 0 my $filename = "serge-import-report-$lang.html";
62 0 print "\t$filename...";
63  
64 0 my $html;
65 0 my $error_totals = {};
66 0 foreach my $key (sort keys %{$importer->{report}->{$lang}}) {
67 0 $html .= qq|<tr><th colspan="2">$key</th></tr>\n|;
68 0 my $file_data = $importer->{report}->{$lang}->{$key};
69 0 foreach my $row_data (@$file_data) {
70 0 ▶ $error_totals->{$row_data->{error_status}}++ if $row_data->{error_status};
Branch: 1 branch
BranchTrueFalseTotal
if $row_data->{"error_status"} 0 0 0
71 0 my $severity = $row_data->{severity};
72 0 ▶ my $key = $row_data->{key} ne '' ? $row_data->{key} : qq|<em>Empty key</em>|;
Branch: 1 branch
BranchTrueFalseTotal
$row_data->{'key'} ne '' ? : 0 0 0
73 0 ▶ my $status = $row_data->{error_status} ne '' ? qq|<span class="status">$row_data->{error_status}</span| : '';
Branch: 1 branch
BranchTrueFalseTotal
$row_data->{'error_status'} ne '' ? : 0 0 0
74  
75 map {
76 0 $row_data->{$_} = xml_escape($row_data->{$_});
77 0 ▶ $row_data->{$_} = '&nbsp;' if $row_data->{$_} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $row_data->{$_} eq "" 0 0 0
78 } qw(key source translation);
79  
80 0 ▶ my $class = qq| class="$severity"| if $severity ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $severity ne "" 0 0 0
81 0 $html .= qq|
82 <tr class="key $severity">
83 <td colspan="2">$key$status</td>
84 </tr>
85  
86 <tr$class>
87 <td>$row_data->{source}</td>
88 <td>$row_data->{translation}</td>
89 </tr>
90 |;
91 } # end foreach row
92  
93 } # end foreach key
94  
95 0 my $html_error_totals = '';
96 0 my $class;
97 map {
98 0 ▶ $class = lc($1) if $_ =~ m/^SERGE_(.*?)_/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^SERGE_(.*?)_/ 0 0 0
99 0 $html_error_totals .= qq|<tr class="$class"><td>$_</td><td>$error_totals->{$_}</td></tr>\n|;
100 } sort severity_sort keys %$error_totals;
101  
102 0 $html = qq|
103 <html>
104 <head>
105 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
106 </head>
107 <style>
108 \@import url(http://fonts.googleapis.com/css?family=Roboto:400,300,300italic);
109 body {
110 font-family: Roboto, sans-serif;
111 font-weight: 300;
112 text-align: center;
113 }
114  
115 h1 {
116 font-weight: 400;
117 margin: 1.5em 0 0;
118 }
119  
120 table {
121 border-collapse: collapse;
122 }
123  
124 td {
125 vertical-align: top;
126 padding: 0.4em;
127 border: 1px solid #ccc;
128 }
129  
130 table.summary {
131 display: inline-block;
132 margin-top: 3em;
133 }
134  
135 table.summary tr td:last-child {
136 text-align: right;
137 }
138  
139 table.details {
140 width: 100%;
141 }
142  
143 .details th {
144 padding: 3em 0 1em;
145 font-weight: 400;
146 font-size: 120%;
147 color: #0af;
148 }
149  
150 tr.notice td {
151 background-color: #fea;
152 }
153  
154 tr.key.notice td {
155 background-color: #dc8;
156 color: #000;
157 }
158  
159 tr.warning td {
160 background-color: #fcc;
161 }
162  
163 tr.key.warning td {
164 background-color: #daa;
165 color: #000;
166 }
167  
168 tr.error td {
169 background-color: #c00;
170 color: #fff;
171 }
172  
173 tr.key.error td {
174 background-color: #a00;
175 }
176  
177 .details td {
178 width: 50%;
179 position: relative;
180 }
181  
182 tr.key.status td {
183 background: none;
184 border: none;
185 color: #999;
186 font-style: italic;
187 font-weight: bold;
188 font-size: 100%;
189 }
190  
191 tr.key.status.notice td {
192 color: #c2ae60;
193 }
194  
195 tr.key.status.error td {
196 color: #f00;
197 }
198  
199 td .status {
200 font-style: italic;
201 position: absolute;
202 right: 0.4em;
203 }
204  
205 .key td {
206 font-size: 80%;
207 color: #999;
208 background: #f8f8f8;
209 text-align: center;
210 }
211  
212 p.footer {
213 margin: 3em 0;
214 font-style: italic;
215 font-size: 80%;
216 color: #999;
217 }
218  
219 .br {
220 font-size: 70%;
221 background: #999;
222 color: white;
223 padding: 0 0.4em;
224 margin-left: 0.2em;
225 border-radius: 3px;
226 }
227  
228 .br:before {
229 content: "SEP"
230 }
231  
232 </style>
233 <body>
234  
235 <h1>Serge Import Report for '$lang' Language</h1>
236  
237 <table class="summary">
238 $html_error_totals
239 <table>
240  
241 <table class="details">
242 $html
243 </table>
244  
245 <p class="footer">&mdash; end of document &mdash;</p>
246  
247 </body>
248 </html>
249 |;
250  
251 0 ▶ open OUT, ">$filename" || die $!;
Condition0%0/2
">$filename" || die($!)
l!l
00
MC/DC0%0/1
">$filename" || die($!)
">$filename"
Truth table0%0/2
A
A= ">$filename"
Aresult
11
00
252 0 binmode OUT, ":utf8";
253 0 print OUT $html;
254 0 close OUT;
255 0 print " Done\n";
256 } # end foreach lang
257 } # end if
258  
259 0 print "\n";
260 0 print "Statistics:\n";
261 0 my $data = [
262 ['Language', 'Files', 'Notices', 'Warnings', 'Errors']
263 ];
264 0 foreach my $lang (sort keys %{$importer->{stats}}) {
265 0 my $stats = $importer->{stats}->{$lang};
266 0 ▶ $lang = 'source language' if $lang eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $lang eq "" 0 0 0
267 0 push @$data, [$lang, $stats->{files}, $stats->{notices}, $stats->{warnings}, $stats->{errors}];
268 }
269 0 print_formatted_table($data);
270  
271 0 $importer->cleanup;
272  
273 0 return 0;
274 }
275  
276 sub severity_sort {
277 0 ▶ my $a_n = 0;
Subroutine severity_sort: not called CC=6 SCAR=37.4
Pod undocumented
278 0 ▶ $a_n = 1 if $a =~ m/^SERGE_WARNING_/;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /^SERGE_WARNING_/ 0 0 0
279 0 ▶ $a_n = 2 if $a =~ m/^SERGE_NOTICE_/;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /^SERGE_NOTICE_/ 0 0 0
280  
281 0 my $b_n = 0;
282 0 ▶ $b_n = 1 if $b =~ m/^SERGE_WARNING_/;
Branch: 1 branch
BranchTrueFalseTotal
if $b =~ /^SERGE_WARNING_/ 0 0 0
283 0 ▶ $b_n = 2 if $b =~ m/^SERGE_NOTICE_/;
Branch: 1 branch
BranchTrueFalseTotal
if $b =~ /^SERGE_NOTICE_/ 0 0 0
284  
285 0 ▶ return $a_n <=> $b_n if $a_n != $b_n; # if priorities differ, sort by priority
Branch: 1 branch
BranchTrueFalseTotal
if $a_n != $b_n 0 0 0
286 0 return $a cmp $b; # otherwise, sort alphabetically
287 }
288  
289 sub xml_escape {
290 0 ▶ my $s = shift;
Subroutine xml_escape: not called CC=1 SCAR=6.9
Pod undocumented
291 0 xml_escape_strref(\$s);
292 0 $s =~ s/$Serge::Util::UNIT_SEPARATOR/<span class="br"><\/span><br\/>/sgi;
293 0 $s =~ s/\n/<br\/>/sgi;
294 0 return $s;
295 }
296  
297 sub print_formatted_table {
298 0 ▶ my ($data) = @_;
Subroutine print_formatted_table: not called CC=7 SCAR=40.3
Pod undocumented
299  
300 0 $data = dclone($data); # clone data not to alter the original data passed by reference
301  
302 0 my @widths;
303 0 foreach my $row (@$data) {
304 0 for (0..$#$row) {
305 0 my $len = length($row->[$_]);
306 0 ▶ $widths[$_] = $len if $widths[$_] < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $widths[$_] < $len 0 0 0
307 }
308 }
309  
310 0 my @delim_row;
311 map {
312 0 push @delim_row, '-' x $_;
313 } @widths;
314  
315 # insert delimiter row below the row of column names
316 0 splice @$data, 1, 0, \@delim_row;
317 # insert delimiter row above the row of column names
318 0 unshift @$data, \@delim_row;
319 # insert delimiter row at the end of the table
320 0 push @$data, \@delim_row;
321  
322 0 foreach my $row (@$data) {
323 0 for (0..$#$row) {
324 0 ▶ print (($_ > 0) ? ' ' : '');
Branch: 1 branch
BranchTrueFalseTotal
$_ > 0 ? : 0 0 0
325 0 print pad($row->[$_], $widths[$_]);
326 }
327 0 print "\n";
328 }
329 }
330  
331 # not a class method
332 sub pad {
333 0 ▶ my ($s, $len) = @_;
Subroutine pad: not called CC=1 SCAR=6.9
Pod undocumented
334 0 return $s . (' ' x ($len - length($s)));
335 }
336  
337 1 1;
« lib/Serge/Engine/Plugin/parse_rc.pm ↑ Summary lib/Serge/Application.pm »