Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 # This file is part of the Adblock Plus web scripts, | |
2 # Copyright (C) 2006-present eyeo GmbH | |
3 # | |
4 # Adblock Plus is free software: you can redistribute it and/or modify | |
5 # it under the terms of the GNU General Public License version 3 as | |
6 # published by the Free Software Foundation. | |
7 # | |
8 # Adblock Plus is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
13 # You should have received a copy of the GNU General Public License | |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
15 import sys | |
16 import logging | |
17 import ConfigParser | |
18 import zipfile | |
19 from io import BytesIO | |
20 | |
21 import cms.bin.xtm_translations.constants as cnts | |
Vasily Kuznetsov
2018/09/24 16:32:11
What do you think about using "cnst" instead of "c
Tudor Avram
2018/09/25 12:26:24
Used `const`. And replaced it in all other files
D
| |
22 from cms.bin.xtm_translations.xtm_api import XTMCloudAPI, XTMCloudException | |
23 from cms.sources import FileSource | |
24 from cms.bin.xtm_translations import utils | |
25 | |
26 __all__ = [ | |
27 'create_project', 'upload_files', 'download_files', 'main', | |
28 ] | |
29 | |
30 input = cnts.input_fn | |
Vasily Kuznetsov
2018/09/24 16:32:11
Since this is not used this much, we could just ca
Tudor Avram
2018/09/25 12:26:25
Oh, actually it isn't anymore. It was used initial
| |
31 | |
32 | |
33 def create_project(args, api, source): | |
34 """Create a project. | |
35 | |
36 Parameters | |
37 ---------- | |
38 args: argparse.Namespace | |
39 The arguments parsed by the main script. | |
40 api: XTMCloudAPI | |
41 Used for interacting with the project. | |
42 source: FileSource | |
43 Representing the website the project is created for. | |
44 | |
45 """ | |
46 config = source.read_config() | |
47 | |
48 try: | |
49 project_id = config.get(cnts.CONFIG['XTM_section'], | |
50 cnts.CONFIG['project_option']) | |
51 sys.exit(cnts.ErrorMessages.EXISTENT_PROJECT.format(project_id)) | |
52 except ConfigParser.NoOptionError, ConfigParser.NoSectionError: | |
53 pass | |
54 | |
55 target_langs = utils.map_locales(source) | |
56 | |
57 page_strings = utils.extract_strings(source) | |
58 files = utils.get_local_files(page_strings) | |
59 files_to_upload = utils.get_files_to_upload(files, page_strings) | |
60 | |
61 try: | |
62 name = utils.resolve_naming_conflicts(args.name) | |
63 logging.info(cnts.InfoMessages.PROJECT_NAME_CREATING.format(name)) | |
64 logging.info(cnts.InfoMessages.UPLOADING_FILES.format(len(files))) | |
65 | |
66 project_id, resulting_jobs = utils.run_and_wait( | |
67 api.create_project, | |
68 XTMCloudException, | |
69 cnts.UNDER_ANALYSIS_MESSAGE, | |
70 cnts.InfoMessages.WAITING_FOR_PROJECT, | |
71 name=name, | |
72 description=args.desc, | |
73 reference_id=args.ref_id, | |
74 target_languages=target_langs, | |
75 customer_id=args.client_id, | |
76 workflow_id=args.workflow_id, | |
77 source_language=args.source_lang, | |
78 files=files_to_upload, | |
79 ) | |
80 except XTMCloudException as err: | |
81 sys.exit(err) | |
82 | |
83 logging.info(cnts.InfoMessages.PROJECT_CREATED.format(project_id)) | |
84 utils.log_resulting_jobs(resulting_jobs) | |
85 | |
86 if args.save_id: | |
87 source.write_to_config( | |
88 cnts.CONFIG['XTM_section'], | |
89 cnts.CONFIG['project_option'], | |
90 str(project_id), | |
91 ) | |
92 logging.info(cnts.InfoMessages.SAVED_PROJECT_ID) | |
93 | |
94 | |
95 def upload_files(args, api, source): | |
96 """Upload files to project. | |
97 | |
98 Parameters | |
99 ---------- | |
100 args: argparse.Namespace | |
101 The arguments parsed by the main script. | |
102 api: XTMCloudAPI | |
103 Used for interacting with the project. | |
104 source: cms.sources.FileSource | |
105 Representing the website the project is created for. | |
106 | |
107 """ | |
108 config = source.read_config() | |
109 try: | |
110 project_id = config.get(cnts.CONFIG['XTM_section'], | |
111 cnts.CONFIG['project_option']) | |
112 except ConfigParser.NoOptionError, ConfigParser.NoSectionError: | |
113 sys.exit(cnts.ErrorMessages.NO_PROJECT.format(source.get_path(''))) | |
114 | |
115 utils.resolve_locales(api, source) | |
116 | |
117 page_strings = utils.extract_strings(source) | |
118 files = utils.get_local_files(page_strings) | |
119 files_to_upload = utils.get_files_to_upload(files, page_strings) | |
120 logging.info(cnts.InfoMessages.UPLOADING_FILES.format(len(files))) | |
121 | |
122 new_jobs = utils.run_and_wait( | |
123 api.upload_files, | |
124 XTMCloudException, | |
125 cnts.UNDER_ANALYSIS_MESSAGE, | |
126 cnts.InfoMessages.WAITING_FOR_PROJECT, | |
127 files=files_to_upload, | |
128 project_id=project_id, | |
129 overwrite=not args.no_overwrite, | |
130 ) | |
131 | |
132 logging.info(cnts.InfoMessages.FILES_UPLOADED) | |
133 utils.log_resulting_jobs(new_jobs) | |
134 | |
135 | |
136 def download_files(args, api, source): | |
137 """Download the translation files and save them as locales. | |
138 | |
139 Parameters | |
140 ---------- | |
141 args: argparse.Namespace | |
142 The arguments provided when running the script | |
143 api: XTMCloudAPI | |
144 Used for interacting with the project. | |
145 source: FileSource | |
146 Used to write the downloaded translation files locally. | |
147 | |
148 """ | |
149 try: | |
150 raw_bytes = api.download_files( | |
151 source.read_config().get( | |
152 cnts.CONFIG['XTM_section'], cnts.CONFIG['project_option'], | |
153 ), | |
154 ) | |
155 except ConfigParser.NoOptionError, ConfigParser.NoSectionError: | |
156 sys.exit(cnts.ErrorMessages.NO_PROJECT.format(source.get_path(''))) | |
157 except XTMCloudException as err: | |
158 sys.exit(err) | |
159 except Exception as err: | |
160 sys.exit(err) | |
161 | |
162 try: | |
163 with zipfile.ZipFile(BytesIO(raw_bytes)) as zf: | |
164 zip_contents = zf.namelist() | |
165 if len(zip_contents) == 0: | |
166 sys.exit( | |
167 cnts.InfoMessages.NO_FILES_FOUND.format(args.project_id), | |
168 ) | |
169 | |
170 logging.info(cnts.InfoMessages.FILES_DOWNLOADED) | |
171 locales_path = source.get_path('locales') | |
172 default_locale = source.read_config().get( | |
173 cnts.CONFIG['main_section'], | |
174 cnts.CONFIG['default_locale_option'], | |
175 ) | |
176 valid_locales = utils.get_locales(locales_path, default_locale) | |
177 utils.clear_files(locales_path, valid_locales) | |
178 | |
179 for name in zip_contents: | |
180 path = utils.resolve_remote_filename(name, locales_path, | |
181 valid_locales) | |
182 utils.write_to_file(zf.read(name), path) | |
183 logging.info(cnts.InfoMessages.FILE_SAVED.format( | |
184 path, | |
185 zf.getinfo(name).file_size, | |
186 )) | |
187 logging.info(cnts.InfoMessages.GREETINGS) | |
188 except zipfile.BadZipfile: | |
189 sys.exit(cnts.ErrorMessages.NO_TARGET_FILES_FOUND) | |
190 except IOError: | |
191 sys.exit(cnts.ErrorMessages.COULD_NOT_SAVE_FILES) | |
192 | |
193 | |
194 def main(args): | |
195 try: | |
196 api = XTMCloudAPI(utils.read_token()) | |
197 except Exception as err: | |
198 sys.exit(err) | |
199 with FileSource(args.source_dir) as fs: | |
200 args.projects_func(args, api, fs) | |
OLD | NEW |