lib/Serge/Sync/Plugin/TranslationService/weblate.pm

filter: Statementstmt 80.1% 121 / 151 Branchbran 50.0% 21 / 42 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 84.0% 21 / 25 Podpod 0.0% 0 / 13 totaltotal 68.4% 165 / 241 SCAR 38.0
CC
28
Cov
72%
parse_result
12.0
validate_data
7.3
parse_json
7.1
CRAP 44.6
« blib/lib/Serge/Sync/Plugin/TranslationService/weblate.pm ↑ Summary
1 # ABSTRACT: Weblate (https://weblate.org/) synchronization plugin for Serge
2  
3 package Serge::Sync::Plugin::TranslationService::weblate;
4 2 ▶use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec::Functions qw(catfile abs2rel);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON -support_by_pp; # -support_by_pp is used to make Perl on Mac happy
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Serge::Util qw(subst_macros);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our $VERSION = qv('0.900.3');
19  
20 sub name {
21 5 ▶ return 'Weblate translation software (https://weblate.org/) synchronization plugin';
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
22 }
23  
24 sub init {
25 5 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
26  
27 5 $self->SUPER::init(@_);
28  
29 5 $self->{optimizations} = 1; # set to undef to disable optimizations
30  
31 5 $self->merge_schema({
32 root_directory => 'STRING',
33 project => 'STRING',
34 config_file => 'STRING',
35 languages => 'ARRAY'
36 });
37 }
38  
39 sub validate_data {
40 5 ▶ my ($self) = @_;
Subroutine validate_data: called CC=7 SCAR=19.9
Pod undocumented
41  
42 5 $self->SUPER::validate_data;
43  
44 5 $self->{data}->{root_directory} = subst_macros($self->{data}->{root_directory});
45 5 $self->{data}->{project} = subst_macros($self->{data}->{project});
46 5 $self->{data}->{config_file} = subst_macros($self->{data}->{config_file});
47 5 $self->{data}->{languages} = subst_macros($self->{data}->{languages});
48  
49 5 ▶ die "'root_directory', which is set to '$self->{data}->{root_directory}', does not point to a valid folder." unless -d $self->{data}->{root_directory};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"data"}{"root_directory"} 1 4 5
50 4 ▶ die "'config_file' not defined" unless defined $self->{data}->{config_file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"}{"config_file"} 1 3 4
51 3 ▶ die "'config_file', which is set to '$self->{data}->{config_file}', does not point to a valid file.\n" unless -f $self->{data}->{config_file};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"data"}{"config_file"} 0 3 3
52 3 ▶ die "'project' not defined" unless defined $self->{data}->{project};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"}{"project"} 1 2 3
53  
54 2 ▶ if (!exists $self->{data}->{languages} or scalar(@{$self->{data}->{languages}}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"data"}{"languages"} or scalar @{$self->{"data"}{"languages"};} == 0) 0 1 1
Condition33%1/3
not exists $self->{"data"}{"languages"} or scalar @{$self->{"data"}{"languages"};} == 0
l!l&&r!l&&!r
002
MC/DC0%0/2
not exists $self->{"data"}{"languages"} or scalar @{$self->{"data"}{"languages"};} == 0
not exists $self->{"data"}{"languages"} scalar @{$self->{"data"}{"languages"};} == 0
Truth table33%1/3
not(A) or B
A= not exists $self->{"data"}{"languages"}B= scalar @{$self->{"data"}{"languages"};} == 0
ABresult
1X1
011
000
55 0 die "the list of destination languages is empty";
56 }
57 }
58  
59 sub pull_ts {
60 1 ▶ my ($self, $langs) = @_;
Subroutine pull_ts: called CC=4 SCAR=14.4
Pod undocumented
61  
62 1 my $langs_to_push = $self->get_all_langs($langs);
63 1 my %files = $self->translation_files($langs_to_push);
64  
65 1 foreach my $key (sort keys %files) {
66 2 my $file = $files{$key};
67 2 my $full_path = catfile($self->{data}->{root_directory}, $file);
68  
69 2 my ($file_name,$folder_path,$file_suffix) = fileparse($full_path);
70  
71 2 ▶ if (!(-d $folder_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $folder_path) 0 2 2
72 0 make_path($folder_path);
73 }
74  
75 2 my $cli_return = $self->run_weblate_cli('download --output "'.$file.'" '.$key, 0);
76  
77 2 ▶ if ($cli_return != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli_return != 0) 0 2 2
78 0 return $cli_return;
79 }
80 }
81  
82 1 return 0;
83 }
84  
85 sub push_ts {
86 1 ▶ my ($self, $langs) = @_;
Subroutine push_ts: called CC=5 SCAR=16.7
Pod undocumented
87  
88 1 my $langs_to_push = $self->get_all_langs($langs);
89 1 my %files = $self->translation_files($langs_to_push);
90  
91 1 foreach my $key (sort keys %files) {
92 2 my $file = $files{$key};
93 2 my $full_path = catfile($self->{data}->{root_directory}, $file);
94  
95 2 ▶ if (-f $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $full_path) 2 0 2
96 2 my $command = 'upload --overwrite --input "' . $file . '"';
97 2 $command .= ' --method replace';
98 2 ▶ if ($self->{data}->{fuzzy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"fuzzy"}) 0 2 2
99 0 $command .= ' --fuzzy '.$self->{data}->{fuzzy};
100 }
101 2 $command .= ' '.$key;
102  
103 2 my $cli_return = $self->run_weblate_cli($command, 0);
104  
105 2 ▶ if ($cli_return != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli_return != 0) 0 2 2
106 0 return $cli_return;
107 }
108 }
109 }
110  
111 1 return 0;
112 }
113  
114 sub translation_files {
115 2 ▶ my ($self, $langs) = @_;
Subroutine translation_files: called CC=4 SCAR=13.9
Pod undocumented
116  
117 2 my $json_components = $self->run_weblate_cli('--format json list-components '.$self->{data}->{project}, 1);
118  
119 2 my $json_components_tree = $self->parse_json($json_components);
120 2 my $json_components_list = $self->parse_list($json_components_tree);
121 2 my @components = map { $_->{slug} } @$json_components_list;
122  
123 2 my %translations = ();
124  
125 2 my %langs_hash = map { $_ => 1 } @$langs;
126  
127 2 foreach my $component (@components) {
128 2 my $json_translations = $self->run_weblate_cli('--format json list-translations '.$self->{data}->{project}.'/'.$component, 1);
129  
130 2 my $json_translations_tree = $self->parse_json($json_translations);
131 2 my $json_translations_list = $self->parse_list($json_translations_tree);
132  
133 2 foreach my $translation (@$json_translations_list) {
134 12 my $language = $translation->{language}->{code};
135 12 my $filename = $translation->{filename};
136 12 my $language_code = $translation->{language_code};
137  
138 12 ▶ if (exists $langs_hash{$language_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $langs_hash{$language_code}) 4 8 12
139 4 $translations{$self->{data}->{project}.'/'.$component.'/'.$language} = $filename;
140 }
141 }
142 }
143  
144 2 return %translations;
145 }
146  
147 sub get_all_langs {
148 2 ▶ my ($self, $langs) = @_;
Subroutine get_all_langs: called CC=2 SCAR=7.0
Pod undocumented
149  
150 2 ▶ if (!$langs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($langs) 2 0 2
151 2 $langs = $self->{data}->{languages};
152 }
153  
154 2 my @all_langs = ();
155  
156 2 push @all_langs, @$langs;
157  
158 2 return \@all_langs;
159 }
160  
161 sub run_weblate_cli {
162 8 ▶ my ($self, $action, $capture) = @_;
Subroutine run_weblate_cli: called CC=3 SCAR=11.1
Pod undocumented
163  
164 8 my $cli_return = 0;
165  
166 8 my $command = 'wlc --config '.$self->{data}->{config_file}.' '.$action;
167  
168 8 print "Running $action ...\n";
169  
170 8 my $json_response = $self->run_in($self->{data}->{root_directory}, $command, 1);
171  
172 8 ▶ if ($capture) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capture) 4 4 8
173 4 return $json_response;
174 }
175  
176 4 ▶ if ($json_response == '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_response == "") 4 0 4
177 4 return 0;
178 }
179  
180 0 return $self->parse_result($json_response);
181 }
182  
183 sub parse_result {
184 0 ▶ my ($self, $json_response) = @_;
Subroutine parse_result: not called CC=3 SCAR=24.8
Pod undocumented
185  
186 0 my $json_tree = $self->parse_json($json_response);
187  
188 0 ▶ if (reftype($json_tree) == 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($json_tree) == "ARRAY") 0 0 0
189 0 return 0;
190 }
191  
192 0 my $result = $json_tree->{result};
193  
194 0 ▶ if ($result == 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result == "true") 0 0 0
195 0 return 0;
196 }
197  
198 0 return 1;
199 }
200  
201 sub parse_list {
202 4 ▶ my ($self, $json_tree) = @_;
Subroutine parse_list: called CC=2 SCAR=7.6
Pod undocumented
203  
204 4 ▶ if (reftype($json_tree) == 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($json_tree) == "ARRAY") 4 0 4
205 4 return $json_tree;
206 }
207  
208 0 return $json_tree->{results};
209 }
210  
211 sub find_lang_files {
212 0 ▶ my ($self, $directory) = @_;
Subroutine find_lang_files: not called CC=1 SCAR=6.9
Pod undocumented
213  
214 0 my @files = ();
215  
216 find(sub {
217 0 ▶ push @files, abs2rel($File::Find::name, $directory) if(-f $_);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ 0 0 0
218 0 }, $directory);
219  
220 0 return @files;
221 }
222  
223 sub parse_json {
224 4 ▶ my ($self, $json) = @_;
Subroutine parse_json: called CC=4 SCAR=19.6
Pod undocumented
225  
226 4 my $tree;
227 4 eval {
228 4 ($tree) = from_json($json, {relaxed => 1});
229 };
230 4 ▶ if ($@ || !$tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $tree) 0 4 4
Condition33%1/3
$@ or not $tree
l!l&&r!l&&!r
004
MC/DC0%0/2
$@ or not $tree
$@ not $tree
Truth table33%1/3
A or not(B)
A= $@B= not $tree
ABresult
1X1
011
000
231 0 my $error_text = $@;
232 0 ▶ if ($error_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_text) { } 0 0 0
233 0 $error_text =~ s/\t/ /g;
234 0 $error_text =~ s/^\s+//s;
235 } else {
236 0 $error_text = "from_json() returned empty data structure";
237 }
238  
239 0 die $error_text;
240 }
241  
242 4 return $tree;
243 }
244  
245 sub get_langs {
246 0 ▶ my ($self, $langs) = @_;
Subroutine get_langs: not called CC=2 SCAR=17.9
Pod undocumented
247  
248 0 ▶ if (!$langs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($langs) 0 0 0
249 0 $langs = $self->{data}->{languages};
250 }
251  
252 0 return $langs;
253 }
254  
255 2 1;
« blib/lib/Serge/Sync/Plugin/TranslationService/weblate.pm ↑ Summary