lib/Serge/Engine/Plugin/parse_csv.pm

filter: Statementstmt 18.3% 27 / 147 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 45.0% 9 / 20 Podpod 0.0% 0 / 6 totaltotal 12.5% 36 / 287 SCAR 72.4
CC
44
Cov
11%
parse
1190.0
report_errors
56.0
quoted_list_str
12.0
CRAP 1397.4
« lib/Serge/Command/sync.pm ↑ Summary lib/Serge/DB/Cached.pm »
1 package Serge::Engine::Plugin::parse_csv;
2  
3 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
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  
7 1 ▶use parent 'Serge::Engine::Plugin::Base::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Serge::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Serge::Util qw(xml_escape_strref);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub name {
16 0 ▶ return 'CSV Parser Plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
17 }
18  
19 sub init {
20 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
21  
22 0 $self->SUPER::init(@_);
23  
24 0 $self->merge_schema({
25 email_from => 'STRING',
26 email_to => 'ARRAY',
27 email_subject => 'STRING',
28  
29 end_of_line => 'STRING',
30 delimiter => 'STRING',
31 escape => 'STRING',
32 quote => 'STRING',
33  
34 column_key => 'STRING',
35 column_string => 'STRING',
36 column_context => 'STRING',
37 column_comment => 'STRING',
38 });
39  
40 0 $self->add('after_job', \&report_errors);
41 }
42  
43 sub validate_data {
44 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=2 SCAR=17.9
Pod undocumented
45  
46 0 $self->SUPER::validate_data;
47  
48 0 eval('use Text::CSV_XS;');
49 0 ▶ die "ERROR: To use parse_csv parser, please install Text::CSV_XS module (run 'cpan Text::CSV_XS')\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
50 }
51  
52 sub quoted_list_str {
53 0 ▶ my ($items) = @_;
Subroutine quoted_list_str: not called CC=3 SCAR=24.8
Pod undocumented
54 0 ▶ if ($items && @$items > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($items and @$items > 0) 0 0 0
Condition0%0/3
$items and @$items > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$items and @$items > 0
$items @$items > 0
Truth table0%0/3
A and B
A= $itemsB= @$items > 0
ABresult
0X0
100
111
55 0 return q{'} . join(q{', '}, @$items) . q{'};
56 }
57 0 return '';
58 }
59  
60 sub parse {
61 0 ▶ my ($self, $textref, $callbackref, $lang) = @_;
Subroutine parse: not called CC=34 SCAR=70.8
Pod undocumented
62 0 my $has_errors = 0;
63  
64 0 eval('use Text::CSV_XS;');
65 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
66  
67 0 ▶ die 'callbackref not specified' unless $callbackref;
Branch: 1 branch
BranchTrueFalseTotal
unless $callbackref 0 0 0
68  
69 0 ▶ my $sep_char = $self->{data}->{delimiter} // q{,};
Condition0%0/2
$self->{"data"}{"delimiter"} // ","
l!l
00
MC/DC0%0/1
$self->{"data"}{"delimiter"} // ","
$self->{"data"}{"delimiter"}
Truth table0%0/2
A
A= $self->{"data"}{"delimiter"}
Aresult
11
00
70 0 ▶ my $eol = $self->{data}->{end_of_line} // $INPUT_RECORD_SEPARATOR;
Condition0%0/3
$self->{"data"}{"end_of_line"} // $Serge::Engine::Plugin::parse_csv::INPUT_RECORD_SEPARATOR
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"data"}{"end_of_line"} // $Serge::Engine::Plugin::parse_csv::INPUT_RECORD_SEPARATOR
$self->{"data"}{"end_of_line"} $Serge::Engine::Plugin::parse_csv::INPUT_RECORD_SEPARATOR
Truth table0%0/3
A // B
A= $self->{"data"}{"end_of_line"}B= $Serge::Engine::Plugin::parse_csv::INPUT_RECORD_SEPARATOR
ABresult
1X1
011
000
71 0 ▶ my $escape_char = $self->{data}->{escape} // q{"};
Condition0%0/2
$self->{"data"}{"escape"} // "\""
l!l
00
MC/DC0%0/1
$self->{"data"}{"escape"} // "\""
$self->{"data"}{"escape"}
Truth table0%0/2
A
A= $self->{"data"}{"escape"}
Aresult
11
00
72 0 ▶ my $quote_char = $self->{data}->{quote} // q{"};
Condition0%0/2
$self->{"data"}{"quote"} // "\""
l!l
00
MC/DC0%0/1
$self->{"data"}{"quote"} // "\""
$self->{"data"}{"quote"}
Truth table0%0/2
A
A= $self->{"data"}{"quote"}
Aresult
11
00
73  
74 0 ▶ my $key_col_name = lc($self->{data}->{column_key} // 'Key');
Condition0%0/2
$self->{"data"}{"column_key"} // "Key"
l!l
00
MC/DC0%0/1
$self->{"data"}{"column_key"} // "Key"
$self->{"data"}{"column_key"}
Truth table0%0/2
A
A= $self->{"data"}{"column_key"}
Aresult
11
00
75 0 ▶ my $text_col_name = lc($self->{data}->{column_string} // 'String');
Condition0%0/2
$self->{"data"}{"column_string"} // "String"
l!l
00
MC/DC0%0/1
$self->{"data"}{"column_string"} // "String"
$self->{"data"}{"column_string"}
Truth table0%0/2
A
A= $self->{"data"}{"column_string"}
Aresult
11
00
76 0 ▶ my $context_col_name = lc($self->{data}->{column_context} // $key_col_name);
Condition0%0/3
$self->{"data"}{"column_context"} // $key_col_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"data"}{"column_context"} // $key_col_name
$self->{"data"}{"column_context"} $key_col_name
Truth table0%0/3
A // B
A= $self->{"data"}{"column_context"}B= $key_col_name
ABresult
1X1
011
000
77 0 ▶ my $comment_col_name = lc($self->{data}->{column_comment} // 'Comment');
Condition0%0/2
$self->{"data"}{"column_comment"} // "Comment"
l!l
00
MC/DC0%0/1
$self->{"data"}{"column_comment"} // "Comment"
$self->{"data"}{"column_comment"}
Truth table0%0/2
A
A= $self->{"data"}{"column_comment"}
Aresult
11
00
78  
79 0 my $csv_fh = IO::String->new($textref);
80 0 my $csv = Text::CSV_XS->new({
81 allow_loose_quotes => 1,
82 binary => 1,
83 eol => $eol,
84 escape_char => $escape_char,
85 quote_char => $quote_char,
86 sep_char => $sep_char,
87 });
88  
89 0 my @out_lines;
90  
91 my @headers;
92 0 my $header_line;
93 # Construct hashref of { col_name => position }
94 0 my $col_for = do {
95 0 my $tokens = $csv->getline($csv_fh);
96 0 ▶ die "Could not parse the header" if $self->_push_csv_error_and_print($csv);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_push_csv_error_and_print($csv) 0 0 0
97  
98 0 my %col_for;
99 0 ▶ for(my $i = 0; $i < @$tokens; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$tokens) 0 0 0
100 0 $col_for{lc $tokens->[$i]} = $i;
101 }
102  
103 0 @headers = @$tokens;
104 0 ▶ $csv->combine(@$tokens) or $self->_push_csv_error_and_print($csv);
Branch: 1 branch
BranchTrueFalseTotal
$csv->combine(@$tokens) or $self->_push_csv_error_and_print($csv) 0 0 0
105 0 $header_line = $csv->string;
106 0 ▶ die "Could not parse the header" if $self->_push_csv_error_and_print($csv);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_push_csv_error_and_print($csv) 0 0 0
107 0 push @out_lines, $header_line;
108 0 \%col_for;
109 };
110  
111 0 my $key_col_idx = $col_for->{$key_col_name};
112 0 ▶ if (!defined($key_col_idx)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key_col_idx) 0 0 0
113 0 $self->_push_error_and_die(
114 "Couldn't find required field '$key_col_name' in CSV headers " .
115 quoted_list_str([(keys %$col_for)]));
116 }
117 0 my $text_col_idx = $col_for->{$text_col_name};
118 0 ▶ if (!defined($text_col_idx)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text_col_idx) 0 0 0
119 0 $self->_push_error_and_die(
120 "Couldn't find required field '$text_col_name' in CSV headers " .
121 quoted_list_str([(keys %$col_for)]));
122 }
123 0 ▶ my $context_col_idx = exists $col_for->{$context_col_name} ? $col_for->{$context_col_name} : -1;
Branch: 1 branch
BranchTrueFalseTotal
exists $col_for->{$context_col_name} ? : 0 0 0
124 0 ▶ my $comment_col_idx = exists $col_for->{$comment_col_name} ? $col_for->{$comment_col_name} : -1;
Branch: 1 branch
BranchTrueFalseTotal
exists $col_for->{$comment_col_name} ? : 0 0 0
125  
126 # When possible, we continue parsing on errors. We still die at the end,
127 # but this way we'll show the user all errors in the CSV file in one go,
128 # not just the first we encounter.
129 LINE:
130 0 ▶ while(my $tokens = $csv->getline($csv_fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tokens = $csv->getline($csv_fh)) 0 0 0
131 0 ▶ $has_errors++ && next LINE if $self->_push_csv_error_and_print($csv);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_push_csv_error_and_print($csv) 0 0 0
Condition0%0/5
$self->_push_csv_error_and_print($csv) && ($has_errors++ && (next LINE))
!ll&&!rl&&r
000
$has_errors++ and next LINE
!ll
00
MC/DC0%0/2
$self->_push_csv_error_and_print($csv) && ($has_errors++ && (next LINE))
$self->_push_csv_error_and_print($csv) $has_errors++
Truth table0%0/3
A && B
A= $self->_push_csv_error_and_print($csv)B= $has_errors++
ABresult
0X0
100
111
132  
133 0 ▶ if (@$tokens != @headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens != @headers) 0 0 0
134 0 my $line_num = $csv_fh->input_line_number;
135 0 my $num_tokens = @$tokens;
136 0 my $num_headers = @headers;
137 0 $self->_push_error_and_print(
138 "Can't parse line $line_num\n" .
139 "\tError: The file has $num_headers header fields, but we found $num_tokens tokens at line $line_num\n" .
140 "\tHeaders: " . join('|', @headers) . "\n" .
141 "\tContent: " . join('|', @$tokens));
142 0 $has_errors = 1;
143 0 next LINE;
144 }
145  
146 0 my ($key, $text) = @$tokens[$key_col_idx, $text_col_idx];
147 0 ▶ my $context = $context_col_idx >= 0 ? $tokens->[$context_col_idx] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$context_col_idx >= 0 ? : 0 0 0
148 0 ▶ my $comment = $comment_col_idx >= 0 ? $tokens->[$comment_col_idx] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$comment_col_idx >= 0 ? : 0 0 0
149  
150 0 ▶ if (!$lang || $self->{import_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lang or $self->{'import_mode'}) { } 0 0 0
Condition0%0/3
not $lang or $self->{"import_mode"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $lang or $self->{"import_mode"}
not $lang $self->{"import_mode"}
Truth table0%0/3
not(A) or B
A= not $langB= $self->{"import_mode"}
ABresult
1X1
011
000
151 0 &$callbackref($text, $context, $comment, undef, $lang, $key);
152 } else {
153 0 $tokens->[$text_col_idx] = &$callbackref($text, $context, $comment, undef, $lang, $key);
154 0 my $status = $csv->combine(@$tokens);
155 0 ▶ $has_errors++ && next LINE if $self->_push_csv_error_and_print($csv);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_push_csv_error_and_print($csv) 0 0 0
Condition0%0/5
$self->_push_csv_error_and_print($csv) && ($has_errors++ && (next LINE))
!ll&&!rl&&r
000
$has_errors++ and next LINE
!ll
00
MC/DC0%0/2
$self->_push_csv_error_and_print($csv) && ($has_errors++ && (next LINE))
$self->_push_csv_error_and_print($csv) $has_errors++
Truth table0%0/3
A && B
A= $self->_push_csv_error_and_print($csv)B= $has_errors++
ABresult
0X0
100
111
156 0 push @out_lines, $csv->string;
157 }
158 }
159  
160 0 ▶ if (!$csv->eof) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csv->eof) 0 0 0
161 0 ▶ $has_errors++ if $self->_push_csv_error_and_print($csv);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_push_csv_error_and_print($csv) 0 0 0
162 }
163  
164 0 ▶ if ($has_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_errors) 0 0 0
165 # The errors will be printed or mailed. This isn't the place to display them.
166 0 die "Encountered errors. Cannot continue";
167 }
168  
169 0 ▶ if (!$lang || $self->{import_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lang or $self->{'import_mode'}) { } 0 0 0
Condition0%0/3
not $lang or $self->{"import_mode"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $lang or $self->{"import_mode"}
not $lang $self->{"import_mode"}
Truth table0%0/3
not(A) or B
A= not $langB= $self->{"import_mode"}
ABresult
1X1
011
000
170 0 return $$textref;
171 } else {
172 # $csv->combine() adds end-of-line characters so don't join on
173 # newline here.
174 0 return join('', @out_lines);
175 }
176 }
177  
178 sub report_errors {
179 0 ▶ my ($self, $phase) = @_;
Subroutine report_errors: not called CC=7 SCAR=40.3
Pod undocumented
180  
181 0 my $email_from = $self->{data}->{email_from};
182 0 ▶ if (!$email_from) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email_from) 0 0 0
183 0 $self->{_csv_errors} = {};
184 0 return;
185 }
186  
187 0 my $email_to = $self->{data}->{email_to};
188 0 ▶ if (!$email_to) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email_to) 0 0 0
189 0 $self->{_csv_errors} = {};
190 0 return;
191 }
192  
193 my $email_subject = $self->{data}->{email_subject}
194 0 ▶ || "[$self->{parent}->{id}]: Serge CSV Parse Errors";
Condition0%0/2
$self->{'data'}{'email_subject'} || "[$self->{'parent'}{'id'}]: Serge CSV Parse Errors"
l!l
00
MC/DC0%0/1
$self->{'data'}{'email_subject'} || "[$self->{'parent'}{'id'}]: Serge CSV Parse Errors"
$self->{'data'}{'email_subject'}
Truth table0%0/2
A
A= $self->{'data'}{'email_subject'}
Aresult
11
00
195  
196 0 my $text;
197 0 foreach my $key (sort keys %{$self->{_csv_errors}}) {
198 0 $text .= "<hr />\n<p><b style='color: red'>$key</b>\n";
199 0 my $messages = $self->{_csv_errors}->{$key};
200 0 foreach my $message (@$messages) {
201 0 xml_escape_strref(\$message);
202 0 $text .= "<pre>$message</pre></p>\n";
203 }
204 }
205  
206 0 $self->{_csv_errors} = {};
207  
208 0 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 0 0 0
209 0 $text = qq|
210 <html>
211 <head>
212 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
213 </head>
214 <body style="font-family: sans-serif; font-size: 120%">
215  
216 <p>
217  
218 The following parsing errors were found when attempting to localize CSV files.
219 </p>
220  
221 $text
222  
223 </body>
224 </html>
225 |;
226  
227 0 Serge::Mail::send_html_message(
228 $email_from,
229 $email_to,
230 $email_subject,
231 $text
232 );
233 }
234  
235 0 return;
236 }
237  
238 sub _push_error {
239 0 ▶ my ($self, $message) = @_;
Subroutine _push_error: not called CC=1 SCAR=6.9
240 0 push @{$self->{_csv_errors}->{$self->_mail_key}}, $message;
241 0 return;
242 }
243  
244 sub _push_error_and_print {
245 0 ▶ my ($self, $message) = @_;
Subroutine _push_error_and_print: not called CC=1 SCAR=6.9
246 0 $self->_push_error($message);
247 0 print "Error: ${\$self->_mail_key}: $message\n";
248 0 return;
249 }
250  
251 sub _push_csv_error_and_print {
252 0 ▶ my ($self, $csv) = @_;
Subroutine _push_csv_error_and_print: not called CC=2 SCAR=17.9
253  
254 0 ▶ if (my $error = $csv->error_diag) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $csv->error_diag) 0 0 0
255 0 my (undef, undef, $bad_part) = $csv->error_diag;
256 0 my $formatted_error = "Error at line $INPUT_LINE_NUMBER:\n" .
257 "\tError: " . $csv->error_diag . "\n" .
258 "\tContent: " . $csv->error_input;
259 0 $self->_push_error_and_print($formatted_error);
260 0 return 1;
261 }
262  
263 0 return 0;
264 }
265  
266 sub _push_error_and_die {
267 0 ▶ my ($self, $message) = @_;
Subroutine _push_error_and_die: not called CC=1 SCAR=6.9
268 0 $self->_push_error($message);
269 0 die "Error: ${\$self->_mail_key}: $message";
270 }
271  
272 sub _mail_key {
273 0 ▶ my ($self) = @_;
Subroutine _mail_key: not called CC=1 SCAR=6.9
274 0 return $self->{parent}->{engine}->{current_file_rel};
275 }
276  
277 1 1;
« lib/Serge/Command/sync.pm ↑ Summary lib/Serge/DB/Cached.pm »