Add update capability for transifex system

Julien LepillerFri Apr 20 11:57:56+0200 2018

c4b49e7

Add update capability for transifex system

offlate/systems/transifex.py

2020
            ans.extend(yaml_rec_load(path2, source[s], dest[s]))
2121
    return ans
2222
23+
def yaml_rec_update(callback, source, old, new):
24+
    ans = {}
25+
    for i in new:
26+
        o = ''
27+
        s = ''
28+
        n = new[i]
29+
        try:
30+
            s = source[i]
31+
        except Exception:
32+
            pass
33+
        try:
34+
            o = old[i]
35+
        except Exception:
36+
            pass
37+
        if isinstance(n, str):
38+
            if o == '':
39+
                ans[i] = n
40+
            elif n == '':
41+
                ans[i] = o
42+
            else:
43+
                ans[i] = callback(s, o, n)
44+
        else:
45+
            ans[i] = yaml_rec_update(callback, s, o, n)
46+
    return ans
47+
2348
class TransifexProject:
2449
    def __init__(self, conf, name, lang, data = {}):
2550
        self.uri = "https://www.transifex.com"

4873
4974
    def update(self, callback):
5075
        self.updateFileList()
76+
        for ff in self.files:
77+
            slug = ff['slug']
78+
            fname = self.filename(slug, False)
79+
            sname = self.filename(slug, True)
80+
            os.rename(fname, fname+'.old')
81+
            self.getFile(slug)
82+
            if ff['i18n_type'] == 'YML':
83+
                with open(fname+'.old') as oldf:
84+
                    with open(fname) as newf:
85+
                        with open(sname) as sourcef:
86+
                            old = yaml.safe_load(oldf)
87+
                            new = yaml.safe_load(newf)
88+
                            source = yaml.safe_load(sourcef)
89+
                            realnew = yaml_rec_update(callback, source, old, new)
90+
                            with open(fname, 'w') as f:
91+
                                yaml.dump(realnew, f, Dumper=yaml.RoundTripDumper,
92+
                                    allow_unicode=True)
5193
5294
    def updateFileList(self):
5395
        self.files = []

112154
                        json=sendcontent, auth=HTTPBasicAuth('api', self.conf['token']))
113155
                print(ans)
114156
                print(ans.text)
115-
        #for slug in self.slugs:
116-
        #    with open(self.filename(slug)) as f:
117-
        #        content = json.load(f)
118-
        #        sendcontent = {}
119-
        #        for s in content:
120-
        #            sendcontent[s['source_string']] = s['translation']
121-
        #        sendcontent = {'content': json.dumps({'content': sendcontent})}
122-
        #        ans = requests.put('https://www.transifex.com/api/2/project/'+
123-
        #                self.name+'/resource/'+slug+'/translation/'+self.lang+'/',
124-
        #                json=sendcontent, auth=HTTPBasicAuth('api', self.conf['token']))
125-
        #        print(ans)
126-
        #        print(ans.text)
127157
128158
    def save(self):
129159
        for slug in self.slugs: