~dholbach/help-app/1426304

« back to all changes in this revision

Viewing changes to edit-here/translations.py

  • Committer: Daniel Holbach
  • Date: 2015-03-04 13:58:25 UTC
  • Revision ID: daniel.holbach@canonical.com-20150304135825-qcozqh1evmni71br
instead of doing internal bookkeeping, assume that the first line is the title_line - also error out, if we find a title line that doesn't have linenumber 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import codecs
2
2
import copy
3
3
import glob
4
 
import json
5
4
import os
6
5
import re
7
6
import shutil
108
107
        results = {}
109
108
        for entry in self.find_in_msgid('Title: '):
110
109
            if entry.msgid.startswith('Title: '):
 
110
                if entry.linenum != 1:
 
111
                    print('Title line "%s" found, but at linenum "%s", '
 
112
                          'not 1.' % (entry.msgid, entry.linenum))
 
113
                    return False
111
114
                entry.msgid = entry.msgid.replace('Title: ', '')
112
115
                fn = entry.occurrences[0][0]
113
116
                results[fn] = entry.msgid
114
117
                if self.po_fn.endswith('.po'):
115
118
                    entry.msgstr = ''
116
119
        self.save()
117
 
        return results
 
120
        return True
118
121
 
119
122
    def rewrite_links(self, documents, bcp47):
120
123
        link_regex = r'\[.+?\]\(\{filename\}(.+?)\)'
173
176
    def generate_pot_file(self, documents):
174
177
        if not self.po4a.gettextize(documents.docs, self.pot_fn):
175
178
            return False
176
 
        results = self.pot_file_ob.replace_title_lines()
177
 
        documents.add_title_lines(results)
 
179
        if not self.pot_file_ob.replace_title_lines():
 
180
            return False
178
181
        for po_fn in self.langs:
179
182
            self.load_pofile(po_fn)
180
183
            self.langs[po_fn]['pofile'].merge(self.pot_file_ob.pofile)
181
 
            self.langs[po_fn]['pofile'].replace_title_lines()
 
184
            if not self.langs[po_fn]['pofile'].replace_title_lines():
 
185
                return False
182
186
        return True
183
187
 
184
188
    # we generate a fake translation for en-US which is going to be
201
205
 
202
206
class Documents(object):
203
207
    def __init__(self):
204
 
        self.docs = {}
 
208
        self.docs = []
205
209
        for dirpath, dirnames, fns in os.walk(PATH):
206
210
            for fn in fns:
207
211
                fn = os.path.relpath(os.path.join(dirpath, fn),
208
212
                                     os.path.join(PATH, '..'))
209
 
                self.docs[fn] = {
210
 
                    'title': None,
211
 
                }
212
 
        self.title_line_fn = os.path.abspath(
213
 
            os.path.join(PATH, '../../.title_lines'))
214
 
        self._read_saved_title_lines()
215
 
 
216
 
    def __del__(self):
217
 
        self._save_title_lines()
 
213
                self.docs += [fn]
218
214
 
219
215
    def translated_doc_fn(self, fn, bcp47_code):
220
216
        match = [doc for doc in self.docs
232
228
        for po_fn in po.langs:
233
229
            for doc_fn in self.docs:
234
230
                output = self._call_po4a_translate(doc_fn, po_fn, po4a)
235
 
                output = '\n'.join([line for line in output.split('\n')
236
 
                                    if not line.startswith('Title: ')])
237
 
                title_line = po.find_translated_title_line(
238
 
                    self.docs[doc_fn]['title'], po_fn)
 
231
                title_line = output.split('\n')[0].split('Title: ')[1]
 
232
                translated_title_line = po.find_translated_title_line(
 
233
                    title_line, po_fn)
 
234
                output = '\n'.join([line for line in output.split('\n')][1:])
239
235
                new_path = self.translated_doc_fn(doc_fn,
240
236
                                                  po.langs[po_fn]['bcp47'])
241
 
                text = "Title: %s\nDate:\n\n" % (title_line)
 
237
                text = "Title: %s\nDate:\n\n" % (translated_title_line)
 
238
                print((doc_fn, po_fn, text))
242
239
                text += output
243
240
                if os.path.exists(new_path):
244
241
                    os.remove(new_path)
247
244
                with open(new_path, 'w', encoding='utf-8') as f:
248
245
                    f.write(text)
249
246
 
250
 
    def add_title_lines(self, results):
251
 
        for doc_fn in results:
252
 
            self.docs[doc_fn]['title'] = results[doc_fn]
253
 
 
254
 
    def _save_title_lines(self):
255
 
        title_lines = []
256
 
        for doc_fn in self.docs:
257
 
            title_lines += [(doc_fn, self.docs[doc_fn]['title'])]
258
 
        if os.path.exists(self.title_line_fn):
259
 
            os.remove(self.title_line_fn)
260
 
        with open(self.title_line_fn, 'w') as f:
261
 
            f.write(json.dumps(title_lines))
262
 
 
263
 
    def _read_saved_title_lines(self):
264
 
        if os.path.exists(self.title_line_fn):
265
 
            title_lines = json.loads(open(self.title_line_fn).read())
266
 
            for entry in title_lines:
267
 
                self.docs[entry[0]]['title'] = entry[1]
268
 
 
269
247
 
270
248
class Translations(object):
271
249
    def __init__(self):