switch the TP and Gettext to a new system/format architecture

Julien LepillerTue Oct 09 22:48:21+0200 2018

cea8609

switch the TP and Gettext to a new system/format architecture

offlate/formats/entry.py unknown status 1

1+
class Entry:
2+
    def __init__(self, msgids, msgstrs, fuzzy, obsolete):
3+
        self.msgids = msgids
4+
        self.msgstrs = msgstrs
5+
        self.fuzzy = fuzzy
6+
        self.obsolete = obsolete
7+
8+
    def isTranslated(self):
9+
        for msgstr in self.msgstrs:
10+
            if msgstr == '':
11+
                return False
12+
        return True
13+
14+
    def isFuzzy(self):
15+
        return self.fuzzy
16+
17+
    def isObsolete(self):
18+
        return self.obsolete
19+
20+
    def update(self, index, content):
21+
        self.msgstrs[index] = content
22+
23+
    def get(self, index):
24+
        return self.msgstrs[index]
25+
26+
class POEntry(Entry):
27+
    def __init__(self, entry):
28+
        msgids = [entry.msgid]
29+
        msgstrs = [entry.msgstr]
30+
        if 0 in entry.msgstr_plural:
31+
            msgstrs = []
32+
            for msgstr in entry.msgstr_plural:
33+
                msgstrs.append(entry.msgstr_plural[msgstr])
34+
            msgids = [entry.msgid, entry.msgid_plural]
35+
        Entry.__init__(self, msgids, msgstrs, "fuzzy" in entry.flags, entry.obsolete)
36+
        self.entry = entry
37+
38+
    def update(self, index, content):
39+
        Entry.update(self, index, content)
40+
        self.fuzzy = False
41+
        self.entry.flags = [x for x in self.entry.flags if x != 'fuzzy']
42+
        if 0 in self.entry.msgstr_plural:
43+
            self.entry.msgstr_plural[index] = content
44+
        else:
45+
            self.entry.msgstr = content
46+
47+
class JSONEntry(Entry):
48+
    def __init__(self, entry):
49+
        Entry.__init__(self, [entry['source_string']], [entry['translation']], False, False)
50+
        self.entry = entry
51+
52+
    def update(self, index, content):
53+
        Entry.update(self, index, content)
54+
        self.entry['translation'] = content
55+
56+
class YAMLEntry(Entry):
57+
    def __init__(self, entry):
58+
        self.key = list(entry.keys())[0]
59+
        Entry.__init__(self, self.key, entry[self.key], False, False)
60+
        self.entry = entry
61+
62+
    def update(self, index, content):
63+
        Entry.update(self, index, content)
64+
        self.entry[self.key] = content

offlate/formats/gettext.py unknown status 1

1+
""" The gettext format. """
2+
3+
import polib
4+
import datetime
5+
from dateutil.tz import tzlocal
6+
from .entry import POEntry
7+
8+
class GettextFormat:
9+
    def __init__(self, files, conf):
10+
        self.po = polib.pofile(files["file"])
11+
        self.conf = conf
12+
13+
    def content(self):
14+
        po = [POEntry(x) for x in self.po]
15+
        return po
16+
17+
    def save(self):
18+
        self.po.metadata['PO-Revision-Date'] = str(datetime.datetime.now(tzlocal()).__format__("%Y-%m-%d %H:%M%z"))
19+
        self.po.metadata['Last-Translator'] = self.conf['fullname']
20+
        self.po.metadata['Language'] = self.conf['lang']
21+
        self.po.metadata['X-Generator'] = 'Offlate ' + self.conf['version']
22+
        self.po.save()

offlate/systems/entry.py unknown status 2

1-
class Entry:
2-
    def __init__(self, msgids, msgstrs, fuzzy, obsolete):
3-
        self.msgids = msgids
4-
        self.msgstrs = msgstrs
5-
        self.fuzzy = fuzzy
6-
        self.obsolete = obsolete
7-
8-
    def isTranslated(self):
9-
        for msgstr in self.msgstrs:
10-
            if msgstr == '':
11-
                return False
12-
        return True
13-
14-
    def isFuzzy(self):
15-
        return self.fuzzy
16-
17-
    def isObsolete(self):
18-
        return self.obsolete
19-
20-
    def update(self, index, content):
21-
        self.msgstrs[index] = content
22-
23-
    def get(self, index):
24-
        return self.msgstrs[index]
25-
26-
class POEntry(Entry):
27-
    def __init__(self, entry):
28-
        msgids = [entry.msgid]
29-
        msgstrs = [entry.msgstr]
30-
        if 0 in entry.msgstr_plural:
31-
            msgstrs = []
32-
            for msgstr in entry.msgstr_plural:
33-
                msgstrs.append(entry.msgstr_plural[msgstr])
34-
            msgids = [entry.msgid, entry.msgid_plural]
35-
        Entry.__init__(self, msgids, msgstrs, "fuzzy" in entry.flags, entry.obsolete)
36-
        self.entry = entry
37-
38-
    def update(self, index, content):
39-
        Entry.update(self, index, content)
40-
        self.fuzzy = False
41-
        self.entry.flags = [x for x in self.entry.flags if x != 'fuzzy']
42-
        if 0 in self.entry.msgstr_plural:
43-
            self.entry.msgstr_plural[index] = content
44-
        else:
45-
            self.entry.msgstr = content
46-
47-
class JSONEntry(Entry):
48-
    def __init__(self, entry):
49-
        Entry.__init__(self, [entry['source_string']], [entry['translation']], False, False)
50-
        self.entry = entry
51-
52-
    def update(self, index, content):
53-
        Entry.update(self, index, content)
54-
        self.entry['translation'] = content
55-
56-
class YAMLEntry(Entry):
57-
    def __init__(self, entry):
58-
        self.key = list(entry.keys())[0]
59-
        Entry.__init__(self, self.key, entry[self.key], False, False)
60-
        self.entry = entry
61-
62-
    def update(self, index, content):
63-
        Entry.update(self, index, content)
64-
        self.entry[self.key] = content

offlate/systems/tp.py

1414
import shutil
1515
from pathlib import Path
1616
17-
from .entry import POEntry
17+
from ..formats.entry import POEntry
18+
from ..formats.gettext import GettextFormat
1819
1920
class TPProject:
2021
    def __init__(self, conf, name, lang, data = {}):

127128
            s.send_message(msg)
128129
129130
    def save(self):
130-
        self.po.metadata['PO-Revision-Date'] = str(datetime.datetime.now(tzlocal()).__format__("%Y-%m-%d %H:%M%z"))
131-
        self.po.metadata['Last-Translator'] = self.conf['fullname']
132-
        self.po.metadata['Language'] = self.lang
133-
        self.po.metadata['X-Generator'] = 'Offlate 0.1'
134131
        self.po.save()
135132
136133
    def content(self):
137-
        self.po = polib.pofile(self.popath)
138-
        po = [POEntry(x) for x in self.po]
139-
        return {'default': po}
134+
        self.po = GettextFormat({'file': self.popath},
135+
                {'version': '0.1',
136+
                 'fullname': self.conf['fullname'],
137+
                 'lang': self.lang})
138+
        return {'default': self.po.content()}

offlate/systems/transifex.py

77
88
from pathlib import Path
99
10-
from .entry import JSONEntry
10+
from ..formats.entry import JSONEntry
1111
1212
def yaml_rec_load(path, source, dest):
1313
    ans = []