Add more supported formats to weblate

Julien LepillerWed Dec 08 03:05:10+0100 2021

9af4313

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>
22
#
33
#   This program is free software: you can redistribute it and/or modify
44
#   it under the terms of the GNU Affero General Public License as

1313
#   You should have received a copy of the GNU Affero General Public License
1414
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.
1515
####
16-
""" The transifex system connector. """
16+
""" The weblate system connector. """
1717
1818
import json
1919
import os
2020
import requests
2121
from requests.auth import HTTPBasicAuth
22+
from pathlib import Path
23+
from zipfile import ZipFile
2224
2325
from ..core.config import *
26+
from ..formats.androidstrings import AndroidStringsFormat
27+
from ..formats.appstore import AppstoreFormat
2428
from ..formats.gettext import GettextFormat
29+
from ..formats.ts import TSFormat
2530
from ..formats.exception import UnsupportedFormatException
2631
from .exception import ProjectNotFoundSystemException
2732
from .project import Project

8792
                    'version': self.conf['offlate_version'],
8893
                    'fullname': self.conf['name'] + ' <' + self.conf['email'] + '>',
8994
                    '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})
90114
            else:
91115
                raise UnsupportedFormatException(ff['file_format'])
92116
            currentformat.merge(oldformat, askmerge)

103127
                break
104128
        if f['file_format'] == 'po':
105129
            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'
106136
        else:
107137
            raise UnsupportedFormatException(ff['file_format'])
108138
        return self.basedir + '/' + slug + ('.source' if is_source else '') + '.' + ext

119149
        ans = requests.get(self.data['instance'] + '/api/translations/' +
120150
                self.data['project'] + '/' + slug + '/' + self.lang + '/file/',
121151
                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']
123153
        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)
126165
127166
        ans = requests.get(self.data['instance'] + '/api/translations/' +
128167
                self.data['project'] + '/' + slug + '/' + source_lang + '/file/',
129168
                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']
131170
        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)
134182
        else:
135183
            print(ans.text)
136184

166214
                        "Authorization": 'Token '+self.conf['token']})
167215
            
168216
            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+
169225
                ans = requests.post(
170226
                    self.data['instance'] + '/api/translations/' +
171227
                        self.data['project'] + '/' + slug['slug'] + '/' +

196252
                    'version': self.conf['offlate_version'],
197253
                    'fullname': self.conf['name'] + ' <' + self.conf['email'] + '>',
198254
                    '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})
199266
            else:
200267
                raise UnsupportedFormatException(ff['file_format'])
201268