Add yaml format initial support

Julien LepillerFri Oct 12 00:01:38+0200 2018

3d5e795

Add yaml format initial support

offlate/formats/entry.py

5555
5656
class YAMLEntry(Entry):
5757
    def __init__(self, entry):
58-
        self.key = list(entry.keys())[0]
59-
        Entry.__init__(self, self.key, entry[self.key], False, False)
6058
        self.entry = entry
59+
        Entry.__init__(self, [entry['source_string']],
60+
                [entry['translation']], False, False)
6161
6262
    def update(self, index, content):
6363
        Entry.update(self, index, content)
64-
        self.entry[self.key] = content
64+
        self.entry['translation'] = content

offlate/formats/yaml.py unknown status 1

1+
""" The Yaml format. """
2+
3+
from ruamel import yaml
4+
from .entry import YAMLEntry
5+
6+
def yaml_rec_load(path, source, dest):
7+
    ans = []
8+
    for s in source:
9+
        path2 = list(path)
10+
        path2.append(s)
11+
        if isinstance(source[s], str):
12+
            ans.append({'path': path, 'id': s,
13+
                'source_string': str(source[s]),
14+
                'translation': str(dest[s])})
15+
        else:
16+
            ans.extend(yaml_rec_load(path2, source[s], dest[s]))
17+
    return ans
18+
19+
def yaml_rec_update(callback, source, old, new):
20+
    ans = {}
21+
    for i in new:
22+
        o = ''
23+
        s = ''
24+
        n = new[i]
25+
        try:
26+
            s = source[i]
27+
        except Exception:
28+
            pass
29+
        try:
30+
            o = old[i]
31+
        except Exception:
32+
            pass
33+
        if isinstance(n, str):
34+
            if o == '':
35+
                ans[i] = n
36+
            elif n == '':
37+
                ans[i] = o
38+
            else:
39+
                ans[i] = callback(s, o, n)
40+
        else:
41+
            ans[i] = yaml_rec_update(callback, s, o, n)
42+
    return ans
43+
44+
class YamlFormat:
45+
    def __init__(self, conf):
46+
        self.conf = conf
47+
        self.source = conf['source']
48+
        self.dest = conf['dest']
49+
        with open(self.source, 'rb') as sf:
50+
            with open(self.dest, 'rb') as df:
51+
                source = yaml.safe_load(sf)
52+
                dest = yaml.safe_load(df)
53+
                # TODO: check that Yaml files always are rooted with the language name
54+
                lang1 = list(source.keys())[0]
55+
                lang2 = list(dest.keys())[0]
56+
                self.contents = yaml_rec_load([lang2], source[lang1], dest[lang2])
57+
58+
    def content(self):
59+
        return [YAMLEntry(x) for x in self.contents]
60+
61+
    def save(self):
62+
        data = {}
63+
        for d in self.contents:
64+
            path = d['path']
65+
            curr = data
66+
            for p in path:
67+
                if p in curr:
68+
                    curr = curr[p]
69+
                else:
70+
                    curr[p] = {}
71+
                    curr = curr[p]
72+
            curr[d['id']] = str(d['translation'])
73+
        with open(self.dest, 'wb') as f:
74+
            f.write(yaml.dump(data, allow_unicode=True, Dumper=yaml.RoundTripDumper).encode('utf8'))
75+
76+
    def merge(self, older, callback):
77+
        pass

offlate/systems/transifex.py

44
import os
55
import requests
66
from requests.auth import HTTPBasicAuth
7+
from ..formats.yaml import YamlFormat
78
89
class TransifexProject:
910
    def __init__(self, conf, name, lang, data={}):