Add more supported formats to weblate
offlate/systems/weblate.py
1 | - | # Copyright (c) 2018 Julien Lepiller <julien@lepiller.eu> | |
1 | + | # Copyright (c) 2021 Julien Lepiller <julien@lepiller.eu> | |
2 | 2 | # | |
3 | 3 | # This program is free software: you can redistribute it and/or modify | |
4 | 4 | # it under the terms of the GNU Affero General Public License as | |
… | |||
13 | 13 | # You should have received a copy of the GNU Affero General Public License | |
14 | 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | |
15 | 15 | #### | |
16 | - | """ The transifex system connector. """ | |
16 | + | """ The weblate system connector. """ | |
17 | 17 | ||
18 | 18 | import json | |
19 | 19 | import os | |
20 | 20 | import requests | |
21 | 21 | from requests.auth import HTTPBasicAuth | |
22 | + | from pathlib import Path | |
23 | + | from zipfile import ZipFile | |
22 | 24 | ||
23 | 25 | from ..core.config import * | |
26 | + | from ..formats.androidstrings import AndroidStringsFormat | |
27 | + | from ..formats.appstore import AppstoreFormat | |
24 | 28 | from ..formats.gettext import GettextFormat | |
29 | + | from ..formats.ts import TSFormat | |
25 | 30 | from ..formats.exception import UnsupportedFormatException | |
26 | 31 | from .exception import ProjectNotFoundSystemException | |
27 | 32 | from .project import Project | |
… | |||
87 | 92 | 'version': self.conf['offlate_version'], | |
88 | 93 | 'fullname': self.conf['name'] + ' <' + self.conf['email'] + '>', | |
89 | 94 | 'lang': self.lang}) | |
95 | + | elif ff['file_format'] == 'ts': | |
96 | + | oldformat = TSFormat({'file': fname + '.old', 'lang': self.lang, | |
97 | + | 'template': sname + '.old'}) | |
98 | + | currentformat = TSFormat({'file': fname, 'lang': self.lang, | |
99 | + | 'template': sname}) | |
100 | + | elif ff['file_format'] == 'aresource': | |
101 | + | oldformat = AndroidStringsFormat({'file': fname + '.old', | |
102 | + | 'lang': self.lang, | |
103 | + | 'template': sname + '.old'}) | |
104 | + | currentformat = AndroidStringsFormat({'file': fname, | |
105 | + | 'lang': self.lang, | |
106 | + | 'template': sname}) | |
107 | + | elif ff['file_format'] == 'appstore': | |
108 | + | oldformat = AppstoreFormat({'file': fname + '.old', | |
109 | + | 'lang': self.lang, | |
110 | + | 'template': sname + '.old'}) | |
111 | + | currentformat = AppstoreFormat({'file': fname, | |
112 | + | 'lang': self.lang, | |
113 | + | 'template': sname}) | |
90 | 114 | else: | |
91 | 115 | raise UnsupportedFormatException(ff['file_format']) | |
92 | 116 | currentformat.merge(oldformat, askmerge) | |
… | |||
103 | 127 | break | |
104 | 128 | if f['file_format'] == 'po': | |
105 | 129 | ext = 'po' | |
130 | + | elif f['file_format'] == 'ts': | |
131 | + | ext = 'ts' | |
132 | + | elif f['file_format'] == 'aresource': | |
133 | + | ext = 'xml' | |
134 | + | elif f['file_format'] == 'appstore': | |
135 | + | ext = 'txt' | |
106 | 136 | else: | |
107 | 137 | raise UnsupportedFormatException(ff['file_format']) | |
108 | 138 | return self.basedir + '/' + slug + ('.source' if is_source else '') + '.' + ext | |
… | |||
119 | 149 | ans = requests.get(self.data['instance'] + '/api/translations/' + | |
120 | 150 | self.data['project'] + '/' + slug + '/' + self.lang + '/file/', | |
121 | 151 | auth=HTTPBasicAuth('Token', self.conf['token'])) | |
122 | - | encoding = ans.encoding if ans.encoding is not None else 'utf-8' | |
152 | + | mime = ans.headers['content-type'] | |
123 | 153 | if ans.status_code == 200: | |
124 | - | with open(self.filename(slug, False), 'wb') as f: | |
125 | - | f.write(ans.text.encode(encoding)) | |
154 | + | if mime == 'application/zip': | |
155 | + | directory = self.filename(slug, False) | |
156 | + | zipfile = directory + '.zip' | |
157 | + | with open(zipfile, 'wb') as f: | |
158 | + | f.write(ans.content) | |
159 | + | Path(directory).mkdir(parents=True, exist_ok=True) | |
160 | + | with ZipFile(zipfile, 'r') as myzip: | |
161 | + | myzip.extractall(path = directory) | |
162 | + | else: | |
163 | + | with open(self.filename(slug, False), 'wb') as f: | |
164 | + | f.write(ans.content) | |
126 | 165 | ||
127 | 166 | ans = requests.get(self.data['instance'] + '/api/translations/' + | |
128 | 167 | self.data['project'] + '/' + slug + '/' + source_lang + '/file/', | |
129 | 168 | auth=HTTPBasicAuth('Token', self.conf['token'])) | |
130 | - | encoding = ans.encoding if ans.encoding is not None else 'utf-8' | |
169 | + | mime = ans.headers['content-type'] | |
131 | 170 | if ans.status_code == 200: | |
132 | - | with open(self.filename(slug, True), 'wb') as f: | |
133 | - | f.write(ans.text.encode(encoding)) | |
171 | + | if mime == 'application/zip': | |
172 | + | directory = self.filename(slug, True) | |
173 | + | zipfile = directory + '.zip' | |
174 | + | with open(zipfile, 'wb') as f: | |
175 | + | f.write(ans.content) | |
176 | + | Path(directory).mkdir(parents=True, exist_ok=True) | |
177 | + | with ZipFile(zipfile, 'r') as myzip: | |
178 | + | myzip.extractall(path = directory) | |
179 | + | else: | |
180 | + | with open(self.filename(slug, True), 'wb') as f: | |
181 | + | f.write(ans.content) | |
134 | 182 | else: | |
135 | 183 | print(ans.text) | |
136 | 184 | ||
… | |||
166 | 214 | "Authorization": 'Token '+self.conf['token']}) | |
167 | 215 | ||
168 | 216 | if ans.status_code == 200: | |
217 | + | # If this file is a ZIP file, we need to repack it first | |
218 | + | zipfile = Path(filename + '.zip') | |
219 | + | if zipfile.exists(): | |
220 | + | zipfile.unlink() | |
221 | + | with ZipFile(zipfile, 'w') as myzip: | |
222 | + | myzip.write(filename, arcname="") | |
223 | + | filename = zipfile | |
224 | + | ||
169 | 225 | ans = requests.post( | |
170 | 226 | self.data['instance'] + '/api/translations/' + | |
171 | 227 | self.data['project'] + '/' + slug['slug'] + '/' + | |
… | |||
196 | 252 | 'version': self.conf['offlate_version'], | |
197 | 253 | 'fullname': self.conf['name'] + ' <' + self.conf['email'] + '>', | |
198 | 254 | 'lang': self.lang}) | |
255 | + | elif slug['file_format'] == 'ts': | |
256 | + | myslug = TSFormat({'file': fname, 'lang': self.lang, | |
257 | + | 'template': sname}) | |
258 | + | elif slug['file_format'] == 'aresource': | |
259 | + | myslug = AndroidStringsFormat({'file': fname, | |
260 | + | 'lang': self.lang, | |
261 | + | 'template': sname}) | |
262 | + | elif slug['file_format'] == 'appstore': | |
263 | + | myslug = AppstoreFormat({'file': fname, | |
264 | + | 'lang': self.lang, | |
265 | + | 'template': sname}) | |
199 | 266 | else: | |
200 | 267 | raise UnsupportedFormatException(ff['file_format']) | |
201 | 268 |