lib/Serge/Engine/Plugin/serialize_csv.pm

filter: Statementstmt 31.4% 17 / 54 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 45.4% 5 / 11 Podpod 0.0% 0 / 6 totaltotal 24.7% 22 / 89 SCAR 41.7
CC
11
Cov
24%
deserialize
30.0
make_csv
20.0
unescape_csv_value
12.0
CRAP 64.9
« lib/Serge/Sync/Plugin/TranslationService/command.pm ↑ Summary lib/Serge/Engine/Plugin/run_command.pm »
1 package Serge::Engine::Plugin::serialize_csv;
2 1 ▶use parent Serge::Engine::Plugin::Base::Serializer;
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 Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Serge;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Serge::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my @field_names = qw(key context hint source target fuzzy comment);
12  
13 sub name {
14 0 ▶ return '.CSV Serializer';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
15 }
16  
17 sub serialize {
18 0 ▶ my ($self, $units, $file, $lang) = @_;
Subroutine serialize: not called CC=1 SCAR=6.9
Pod undocumented
19 0 unshift @$units, ({
20 key => 'ID',
21 context => 'Context',
22 hint => 'Hint',
23 source => 'Source',
24 target => 'Translation',
25 fuzzy => 'Needs Work',
26 comment => "Translator's Comment",
27 });
28 0 return make_csv($units, \@field_names);
29 }
30  
31 sub deserialize {
32 0 ▶ my ($self, $textref) = @_;
Subroutine deserialize: not called CC=5 SCAR=34.0
Pod undocumented
33 0 my @units;
34  
35 0 $$textref =~ s/\r\n/\n/sg; # normalize line-feeds
36  
37 0 my @row;
38 0 ▶ while ($$textref =~ /([^,"]*?|"(?:[^"]|"")*?")([,\n])/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$textref =~ /([^,"]*?|"(?:[^"]|"")*?")([,\n])/gs) 0 0 0
39 0 push @row, unescape_csv_value($1);
40 0 ▶ if ($2 eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "\n") 0 0 0
41 0 ▶ if ($row[0] ne 'ID') {
Branch: 1 branch
BranchTrueFalseTotal
if ($row[0] ne "ID") 0 0 0
42 0 my %h;
43 0 @h{@field_names} = @row;
44 0 ▶ $h{fuzzy} = $h{fuzzy} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$h{'fuzzy'} ? : 0 0 0
45 0 push @units, \%h;
46 }
47 0 @row = ();
48 }
49 }
50 0 return \@units;
51 }
52  
53 sub make_csv {
54 0 ▶ my ($units, $field_names) = @_;
Subroutine make_csv: not called CC=4 SCAR=30.0
Pod undocumented
55 0 my $out = '';
56 0 foreach my $unit (@$units) {
57 0 ▶ $unit->{fuzzy} = $unit->{fuzzy} ? 1 : undef if ($unit->{key} ne 'ID'); # normalize the value except for the header row
Branch: 2 branches
BranchTrueFalseTotal
if $unit->{"key"} ne "ID" 0 0 0
$unit->{'fuzzy'} ? : 0 0 0
58 0 $out .= join(',', map { escape_csv_value($unit->{$_}) } @$field_names)."\n";
59 }
60 0 return $out;
61 }
62  
63 sub escape_csv_value {
64 0 ▶ my $s = shift;
Subroutine escape_csv_value: not called CC=2 SCAR=17.9
Pod undocumented
65 0 ▶ if ($s =~ m/(^\s|[",\n]|\s$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /(^\s|[",\n]|\s$)/) 0 0 0
66 0 $s =~ s/"/""/g;
67 0 return qq{"$s"};
68 }
69 0 return $s;
70 }
71  
72 sub unescape_csv_value {
73 0 ▶ my $s = shift;
Subroutine unescape_csv_value: not called CC=3 SCAR=24.8
Pod undocumented
74 0 ▶ if ($s =~ m/^".*"$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^".*"$/s) 0 0 0
75 0 $s =~ s/^"(.*)"$/$1/se;
76 0 $s =~ s/""/"/g;
77 }
78 0 ▶ $s = undef if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 0 0
79 0 return $s;
80 }
81  
82 1 1;
« lib/Serge/Sync/Plugin/TranslationService/command.pm ↑ Summary lib/Serge/Engine/Plugin/run_command.pm »