Add update capability for transifex system
offlate/systems/transifex.py
20 | 20 | ans.extend(yaml_rec_load(path2, source[s], dest[s])) | |
21 | 21 | return ans | |
22 | 22 | ||
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 | + | ||
23 | 48 | class TransifexProject: | |
24 | 49 | def __init__(self, conf, name, lang, data = {}): | |
25 | 50 | self.uri = "https://www.transifex.com" | |
… | |||
48 | 73 | ||
49 | 74 | def update(self, callback): | |
50 | 75 | 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) | |
51 | 93 | ||
52 | 94 | def updateFileList(self): | |
53 | 95 | self.files = [] | |
… | |||
112 | 154 | json=sendcontent, auth=HTTPBasicAuth('api', self.conf['token'])) | |
113 | 155 | print(ans) | |
114 | 156 | 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) | |
127 | 157 | ||
128 | 158 | def save(self): | |
129 | 159 | for slug in self.slugs: |