Fix some bugs in weblate system

Julien LepillerSat Apr 10 15:06:13+0200 2021

79ac155

Fix some bugs in weblate system

offlate/systems/weblate.py

6161
            l = json.loads(ans.text)['results']
6262
            self.files = l
6363
        else:
64+
            print(ans.text)
6465
            raise ProjectNotFoundSystemException(self.name)
6566
6667
    def update(self, askmerge, callback=None):
6768
        self.updateFileList()
6869
        i = 0
6970
        for ff in self.files:
70-
            callback.reportProgress(100.0 * i / len(self.files))
71+
            if callback is not None:
72+
                callback.reportProgress(100.0 * i / len(self.files))
7173
            slug = ff['slug']
7274
            fname = self.filename(slug, False)
7375
            sname = self.filename(slug, True)

8991
                raise UnsupportedFormatException(ff['file_format'])
9092
            currentformat.merge(oldformat, askmerge)
9193
            i += 1
92-
        callback.reportProgress(100)
94+
        if callback is not None:
95+
            callback.reportProgress(100)
9396
9497
    def filename(self, slug, is_source):
9598
        ext = ''

116119
        ans = requests.get(self.data['instance'] + '/api/translations/' +
117120
                self.data['project'] + '/' + slug + '/' + self.lang + '/file',
118121
                auth=HTTPBasicAuth('Token', self.conf['token']))
122+
        encoding = ans.encoding if ans.encoding is not None else 'utf-8'
119123
        if ans.status_code == 200:
120124
            with open(self.filename(slug, False), 'wb') as f:
121-
                f.write(ans.text.encode(ans.encoding))
125+
                f.write(ans.text.encode(encoding))
122126
123127
        ans = requests.get(self.data['instance'] + '/api/translations/' +
124128
                self.data['project'] + '/' + slug + '/' + source_lang + '/file',
125129
                auth=HTTPBasicAuth('Token', self.conf['token']))
130+
        encoding = ans.encoding if ans.encoding is not None else 'utf-8'
126131
        if ans.status_code == 200:
127132
            with open(self.filename(slug, True), 'wb') as f:
128-
                f.write(ans.text.encode(ans.encoding))
133+
                f.write(ans.text.encode(encoding))
129134
        else:
130135
            print(ans.text)
131136

136141
            callback.reportProgress(100.0 * i / len(self.files))
137142
            print('{} => {}'.format(slug['slug'], slug['file_format']))
138143
            filename = self.filename(slug['slug'], False)
139-
            with open(filename, 'rb') as f:
140-
                ans = requests.get(self.data['instance'] + '/api/translations/' +
141-
                        self.data['project'] + '/' + slug['slug'] + '/' +
142-
                        self.lang + '/file',
143-
                        files={'file': f},
144-
                        auth=HTTPBasicAuth('Token', self.conf['token']))
145-
                print(ans)
146-
                print(ans.text)
144+
            ans = requests.post(
145+
                self.data['instance'] + '/api/translations/' +
146+
                    self.data['project'] + '/' + slug['slug'] + '/' +
147+
                    self.lang + '/file/',
148+
                    files={'file': (os.path.basename(filename), open(filename, 'rb'))},
149+
                    #data={'conflicts': 'replace-translated',
150+
                    #    'method': 'translate'},
151+
                    auth=HTTPBasicAuth('Token', self.conf['token']))
152+
            print(ans)
153+
            print(ans.text)
147154
            i += 1
148155
        callback.reportProgress(100)
149156