blib/lib/XML/TMX/FromPO.pm

filter: Statementstmt 12.6% 21 / 166 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 35.2% 6 / 17 Podpod 100% 5 / 5 totaltotal 9.7% 32 / 330 CC 65 SCAR 47.7
CC
65
Cov
7%
__processa
272.0
rec_get_po
177.4
parse_dir
132.0
CRAP 118.5
↑ Summary blib/lib/XML/TMX/Reader.pm »
1 package XML::TMX::FromPO;
2 2 $XML::TMX::FromPO::VERSION = '0.39';
3 # ABSTRACT: Generates a TMX file from a group of PO files
4  
5 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use XML::TMX::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 2 our @ISA = 'Exporter';
13 2 our @EXPORT_OK = qw(&new &parse_dir &create_tmx &clean_tmx);
14  
15  
16 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
17 0 my $proto = shift;
18 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
19 0 my $self = {};
20  
21 0 $self->{LANG} = undef;
22 0 $self->{OUTPUT} = undef;
23 0 $self->{DEBUG} = 0;
24  
25 0 __common_conf($self, @_);
26  
27 0 ▶ unless(defined($self->{CONVER})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"CONVER"}) 0 0 0
28 0 ▶ if(system('recode >/dev/null 2>&1')) {
Branch: 1 branch
BranchTrueFalseTotal
if (system 'recode >/dev/null 2>&1') { } 0 0 0
29 0 $self->{CONVERT} = 'iconv -f %t -t utf8 < %f';
30 } else {
31 0 $self->{CONVERT} = 'recode %t..utf8 < %f';
32 }
33 }
34  
35 0 $self->{TMX} = {};
36 0 bless($self, $class);
37 0 return($self);
38 }
39  
40  
41  
42 0 ▶sub rec_get_po {
Subroutine rec_get_po: not called CC=14 SCAR=51.8
Pod documented
43 0 my $self = shift;
44 0 my $dir = shift;
45 0 my $lan1 = shift;
46 0 __common_conf($self, @_);
47  
48 # check if directory is readable
49 0 ▶ if(-f $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $dir) { } 0 0 0
50 0 my $file=$dir;
51 0 my $lang = lc($lan1);
52  
53 0 ▶ if(!defined($self->{LANG}) || __check_lang($self, $lang)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"LANG"} or __check_lang($self, $lang)) 0 0 0
Condition0%0/3
not defined $self->{"LANG"} or __check_lang($self, $lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"LANG"} or __check_lang($self, $lang)
not defined $self->{"LANG"} __check_lang($self, $lang)
Truth table0%0/3
not(A) or B
A= not defined $self->{"LANG"}B= __check_lang($self, $lang)
ABresult
1X1
011
000
54 0 __processa($self, $file, $lang);
55 }
56 }
57 else {
58 0 ▶ die("$dir is not a readable directory\n") unless(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
59 0 ▶ for my $file (<$dir/*>) {
Subroutine BEGIN: called CC=1 SCAR=0.2
60  
61 0 ▶ if($file =~ /(.*)\.(po|messages)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($file =~ /(.*)\.(po|messages)$/) { } 0 0 0
elsif ($file =~ /(.*)\.(\w+)\.(po|messages)$/) { } 0 0 0
elsif (-d $file) { } 0 0 0
62 0 my $lang = lc($lan1);
63  
64 0 ▶ if(!defined($self->{LANG}) || __check_lang($self, $lang)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"LANG"} or __check_lang($self, $lang)) 0 0 0
Condition0%0/3
not defined $self->{"LANG"} or __check_lang($self, $lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"LANG"} or __check_lang($self, $lang)
not defined $self->{"LANG"} __check_lang($self, $lang)
Truth table0%0/3
not(A) or B
A= not defined $self->{"LANG"}B= __check_lang($self, $lang)
ABresult
1X1
011
000
65 0 __processa($self, $file, $lang);
66 }
67 }
68 elsif($file =~ /(.*)\.(\w+)\.(po|messages)$/) {
69 0 my $lang = lc($lan1);
70 ## ??? my $lang = "\L$2";
71  
72 0 ▶ if(!defined($self->{LANG}) || __check_lang($self, $lang)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"LANG"} or __check_lang($self, $lang)) 0 0 0
Condition0%0/3
not defined $self->{"LANG"} or __check_lang($self, $lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"LANG"} or __check_lang($self, $lang)
not defined $self->{"LANG"} __check_lang($self, $lang)
Truth table0%0/3
not(A) or B
A= not defined $self->{"LANG"}B= __check_lang($self, $lang)
ABresult
1X1
011
000
73 0 __processa($self, $file, $lang);
74 }
75 }
76 elsif(-d $file) {
77 0 rec_get_po($self,$file,$lan1)
78 }
79 else {
80 ## warn ("$file ... não tem lingua\n") if($self->{DEBUG});
81 }
82 }
83 }
84  
85 0 ▶ __add_en($self) if(__check_lang($self, 'en'));
Branch: 1 branch
BranchTrueFalseTotal
if __check_lang($self, "en") 0 0 0
86 0 __limpa($self);
87 }
88  
89  
90  
91 0 ▶sub parse_dir {
Subroutine parse_dir: not called CC=11 SCAR=48.8
Pod documented
92 0 my $self = shift;
93 0 my $dir = shift;
94  
95 0 __common_conf($self, @_);
96  
97 # check if directory is readable
98 0 ▶ die("$dir is not a readable directory\n") unless(-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
99  
100 0 for my $file ((<$dir/*.po>),(<$dir/*.messages>)) {
101 0 ▶ if($file =~ /(\w+)\.(po|messages)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /(\w+)\.(po|messages)$/) { } 0 0 0
elsif ($file =~ /(.*)\.(\w+)\.(po|messages)$/) { } 0 0 0
102 0 my $lang = "\L$1";
103  
104 0 ▶ if(!defined($self->{LANG}) || __check_lang($self, $lang)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"LANG"} or __check_lang($self, $lang)) 0 0 0
Condition0%0/3
not defined $self->{"LANG"} or __check_lang($self, $lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"LANG"} or __check_lang($self, $lang)
not defined $self->{"LANG"} __check_lang($self, $lang)
Truth table0%0/3
not(A) or B
A= not defined $self->{"LANG"}B= __check_lang($self, $lang)
ABresult
1X1
011
000
105 0 __processa($self, $file, $lang);
106 }
107 } elsif($file =~ /(.*)\.(\w+)\.(po|messages)$/) {
108 0 my $lang = "\L$2";
109  
110 0 ▶ if(!defined($self->{LANG}) || __check_lang($self, $lang)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"LANG"} or __check_lang($self, $lang)) 0 0 0
Condition0%0/3
not defined $self->{"LANG"} or __check_lang($self, $lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"LANG"} or __check_lang($self, $lang)
not defined $self->{"LANG"} __check_lang($self, $lang)
Truth table0%0/3
not(A) or B
A= not defined $self->{"LANG"}B= __check_lang($self, $lang)
ABresult
1X1
011
000
111 0 __processa($self, $file, $lang);
112 }
113 } else {
114 0 ▶ warn ("$file ... não tem lingua\n") if($self->{DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 0 0
115 }
116 }
117  
118 0 ▶ __add_en($self) if(__check_lang($self, 'en'));
Branch: 1 branch
BranchTrueFalseTotal
if __check_lang($self, "en") 0 0 0
119 0 __limpa($self);
120 }
121  
122 # return value:
123 # * 0 -> lang does not exist
124 # * 1 -> lang exists
125 0 ▶sub __check_lang {
Subroutine __check_lang: not called CC=4 SCAR=30.0
126 0 my $self = shift;
127 0 my $lang = shift;
128 0 my @regex = @{$self->{LANG}};
129  
130 0 ▶ while(my $regex = shift(@regex)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = shift @regex) 0 0 0
131 0 ▶ last if($regex gt $lang);
Branch: 1 branch
BranchTrueFalseTotal
if $regex gt $lang 0 0 0
132 0 ▶ if($lang =~ /^$regex$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang =~ /^$regex$/i) 0 0 0
133 0 return(1);
134 }
135 }
136 0 return(0);
137 }
138  
139 0 ▶sub __add_en {
Subroutine __add_en: not called CC=2 SCAR=17.9
140 0 my $self = shift;
141  
142 0 for my $str (keys %{$self->{TMX}}) {
143 0 $self->{TMX}{$str}{'en'} = $str;
144 }
145 }
146  
147  
148 0 ▶sub create_tmx {
Subroutine create_tmx: not called CC=4 SCAR=30.0
Pod documented
149 0 my $self = shift;
150 0 my $tmx = new XML::TMX::Writer();
151  
152 0 __common_conf($self, @_);
153  
154 0 my $n_langs = @{$self->{LANG}};
155  
156 0 ▶ if(defined($self->{OUTPUT})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'OUTPUT'}) { } 0 0 0
157 0 $tmx->start_tmx(ID => 'XML::TMX::FromPO', OUTPUT => $self->{OUTPUT});
158 } else {
159 0 $tmx->start_tmx(ID => 'XML::TMX::FromPO');
160 }
161  
162 0 for my $chave (keys %{$self->{TMX}}) {
163 0 my $reg = __make_tu($self, $self->{TMX}{$chave});
164 # only write to file if all languages are defined
165 0 ▶ $tmx->add_tu(%{$reg}) if(keys(%{$reg}) >= $n_langs);
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$reg;} >= $n_langs 0 0 0
166 }
167 0 $tmx->end_tmx();
168 }
169  
170  
171 0 ▶sub clean_tmx {
Subroutine clean_tmx: not called CC=1 SCAR=6.9
Pod documented
172 0 my $self = shift;
173 0 $self->{TMX} = {};
174 }
175  
176 0 ▶sub __make_tu {
Subroutine __make_tu: not called CC=4 SCAR=30.0
177 0 my $self = shift;
178 0 my $block = shift;
179 0 my $reg = {};
180  
181 0 ▶ if(!defined($self->{LANG})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"LANG"}) 0 0 0
182 0 return($block);
183 }
184  
185 0 for my $lang (keys %$block) {
186 0 ▶ $reg->{$lang} = $block->{$lang} if(__check_lang($self, $lang));
Branch: 1 branch
BranchTrueFalseTotal
if __check_lang($self, $lang) 0 0 0
187 }
188 0 return($reg);
189 }
190  
191 0 ▶sub __processa {
Subroutine __processa: not called CC=16 SCAR=56.1
192 0 my $self = shift;
193 0 my $a = shift;
194 0 my $l = shift;
195  
196 0 local $/ = "\nmsgid";
197 #local $/ = "\nmsgid ";
198 0 ▶ print STDERR "$a\n" if($self->{DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 0 0
199  
200 0 my $codeline = `grep -i Content-Type $a | grep -i charset`;
201 0 my $code = "?";
202  
203 0 ▶ if($codeline =~ /charset=([\w-]+)/) { $code = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($codeline =~ /charset=([\w-]+)/) 0 0 0
204  
205 0 my $convert = $self->{CONVERT};
206  
207 0 $convert =~ s/\%t/$code/i;
208 0 $convert =~ s/\%f/$a/i;
209  
210 0 ▶ if($code eq "?" || $code =~ /utf-?8/i ) { open(F,$a) or die;}
Branch: 2 branches
BranchTrueFalseTotal
if ($code eq '?' or $code =~ /utf-?8/i) { } 0 0 0
open F, $a or die 0 0 0
Condition0%0/3
$code eq "?" or $code =~ /utf-?8/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$code eq "?" or $code =~ /utf-?8/i
$code eq "?" $code =~ /utf-?8/i
Truth table0%0/3
A or B
A= $code eq "?"B= $code =~ /utf-?8/i
ABresult
1X1
011
000
211 0 ▶ else { open(F,"$convert|") or die;}
Branch: 1 branch
BranchTrueFalseTotal
open F, "$convert|" or die 0 0 0
212  
213 0 my $mi = 0;
214  
215 0 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 0 0 0
216 0 chomp;
217 0 ▶ next if($mi == 0 && /^msgid\s+""/);
Branch: 1 branch
BranchTrueFalseTotal
if $mi == 0 and /^msgid\s+""/ 0 0 0
Condition0%0/3
$mi == 0 and /^msgid\s+""/
!ll&&!rl&&r
000
MC/DC0%0/2
$mi == 0 and /^msgid\s+""/
$mi == 0 /^msgid\s+""/
Truth table0%0/3
A and B
A= $mi == 0B= /^msgid\s+""/
ABresult
0X0
100
111
218 0 ▶ if(/"Content-Type:/ && /charset=([\w-]+)/) { $code = $1; next }
Branch: 1 branch
BranchTrueFalseTotal
if (/"Content-Type:/ and /charset=([\w-]+)/) 0 0 0
Condition0%0/3
/"Content-Type:/ and /charset=([\w-]+)/
!ll&&!rl&&r
000
MC/DC0%0/2
/"Content-Type:/ and /charset=([\w-]+)/
/"Content-Type:/ /charset=([\w-]+)/
Truth table0%0/3
A and B
A= /"Content-Type:/B= /charset=([\w-]+)/
ABresult
0X0
100
111
219 0 s/(^|\n)\s*#.*//g;
220  
221 # s/_//g unless $under;
222  
223 0 ▶ next unless(/\n\s*msgstr/);
Branch: 1 branch
BranchTrueFalseTotal
unless /\n\s*msgstr/ 0 0 0
224 0 my ($m1,$m2) = ($`,$');
225  
226 0 $m1 =~ s/(^\s*"|"\s*$)//g;
227 0 $m1 =~ s/("\s*\n\s*")/ /g;
228 0 $m2 =~ s/(^\s*"|"\s*$)//g;
229 0 $m2 =~ s/("\s*\n\s*")/ /g;
230  
231 0 ▶ unless($m1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m1) 0 0 0
232 0 warn "\n====M1 vazio... \n$m1\n=$m2\n";
233 0 next;
234 }
235  
236 0 ▶ if($m2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m2) 0 0 0
237 0 $self->{TMX}{$m1}{$l} = $m2;
238 } # || "????? $m1";
239  
240 #$self->{TMX}{$m1}{'en'} = $m1;
241 # print "\n====\n$m1\n=$m2\n";
242  
243 0 $mi++;
244 }
245 0 ▶ print STDERR "Charset: $code\n" if($self->{DEGUB});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEGUB"} 0 0 0
246 0 close F;
247 }
248  
249 0 ▶sub __limpa {
Subroutine __limpa: not called CC=7 SCAR=40.3
250 0 my $self = shift;
251  
252 # possíveis limpezas
253 # (1) eliminar traduções que sejam igual ao original
254 # (2) eliminar strings que não contenham pelo menos 2 letras
255 # consecutivas
256 # (3) eliminar frases que fiquem sem traduções
257 #
258 # um teste realizado com os po's do evolution mostrou uma redução do
259 # ficheiro final de 12M para 8,6M, uma análise com o diff aos dumps
260 # permitiu ver que grande parte do ''lixo'' eram de (1)
261  
262 0 for my $h1 (keys %{$self->{TMX}}) {
263 0 ▶ if($h1 =~ /[a-z][a-z]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h1 =~ /[a-z][a-z]/i) { } 0 0 0
264 0 for my $h2 (keys %{$self->{TMX}{$h1}}) {
265 # optimização (1)
266 0 ▶ delete($self->{TMX}{$h1}{$h2}) if($h2 !~ /^en/i && $h1 eq $self->{TMX}{$h1}{$h2});
Branch: 1 branch
BranchTrueFalseTotal
if not $h2 =~ /^en/i and $h1 eq $self->{"TMX"}{$h1}{$h2} 0 0 0
Condition0%0/3
not $h2 =~ /^en/i and $h1 eq $self->{"TMX"}{$h1}{$h2}
!ll&&!rl&&r
000
MC/DC0%0/2
not $h2 =~ /^en/i and $h1 eq $self->{"TMX"}{$h1}{$h2}
not $h2 =~ /^en/i $h1 eq $self->{"TMX"}{$h1}{$h2}
Truth table0%0/3
not(A) and B
A= not $h2 =~ /^en/iB= $h1 eq $self->{"TMX"}{$h1}{$h2}
ABresult
0X0
100
111
267 }
268 # optimização (3)
269 0 ▶ delete($self->{TMX}{$h1}) unless(keys %{$self->{TMX}{$h1}});
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"TMX"}{$h1};} 0 0 0
270 } else {
271 # optimização (2)
272 0 delete($self->{TMX}{$h1});
273 }
274 }
275 }
276  
277  
278  
279 0 ▶sub __common_conf {
Subroutine __common_conf: not called CC=8 SCAR=42.8
280 0 my $self = shift;
281 0 my %opt = @_;
282  
283 0 ▶ if(defined($opt{LANG})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"LANG"}) 0 0 0
284 0 my @list;
285 0 for my $l (sort(split(/\s+/, $opt{LANG}))) {
286 0 ▶ push(@list, $l) if($l =~ /^[a-z0-9_]+$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^[a-z0-9_]+$/i 0 0 0
287 }
288 0 ▶ $self->{LANG} = \@list if(@list);
Branch: 1 branch
BranchTrueFalseTotal
if @list 0 0 0
289 }
290  
291 0 ▶ $self->{CONVERT} = $opt{CONVERT} if defined($opt{CONVERT});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"CONVERT"} 0 0 0
292 0 ▶ $self->{OUTPUT} = $opt{OUTPUT} if defined($opt{OUTPUT});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"OUTPUT"} 0 0 0
293 0 ▶ $self->{DEBUG} = $opt{DEBUG} if defined($opt{DEBUG});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"DEBUG"} 0 0 0
294 }
295  
296  
297 2 1;
298  
299 __END__
↑ Summary blib/lib/XML/TMX/Reader.pm »