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

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