Add update and send methods to transifex system

Julien LepillerFri Oct 12 22:27:42+0200 2018

7526809

Add update and send methods to transifex system

offlate/formats/yaml.py

7474
            f.write(yaml.dump(data, allow_unicode=True, Dumper=yaml.RoundTripDumper).encode('utf8'))
7575
7676
    def merge(self, older, callback):
77-
        pass
77+
        with open(older.dest, 'rb') as oldf:
78+
            with open(self.dest, 'rb') as newf:
79+
                with open(self.source, 'rb') as sourcef:
80+
                    old = yaml.safe_load(oldf)
81+
                    new = yaml.safe_load(newf)
82+
                    source = yaml.safe_load(sourcef)
83+
                    merged = yaml_rec_update(callback, source, old, new)
84+
                    with open(self.dest, 'wb') as f:
85+
                        f.write(yaml.dump(merged, allow_unicode=True, Dumper=yaml.RoundTripDumper).encode('utf8'))

offlate/systems/transifex.py

5555
                currentformat = YamlFormat({'dest': fname, 'source': sname})
5656
            else:
5757
                raise Exception("Unsupported format: " + ff['i18n_type'])
58-
            currentformat.merge(oldformat)
58+
            currentformat.merge(oldformat, callback)
5959
6060
    def filename(self, slug, is_source):
6161
        ext = ''

8787
            print(ans.text)
8888
8989
    def send(self, interface):
90-
        pass
90+
        self.save()
91+
        for slug in self.files:
92+
            print('{} => {}'.format(slug['slug'], slug['i18n_type']))
93+
            with open(self.filename(slug['slug'], False), 'rb') as f:
94+
                content = f.read()
95+
                sendcontent = {"content": content.decode('utf8')}
96+
                ans = requests.put('https://www.transifex.com/api/2/project/'+
97+
                        self.name+'/resource/'+slug['slug']+'/translation/'+self.lang+'/',
98+
                        json=sendcontent, auth=HTTPBasicAuth('api', self.conf['token']))
99+
                print(ans)
100+
                print(ans.text)
91101
92102
    def save(self):
93-
        for slug in self.content:
103+
        for slug in self.slugs:
94104
            slug.save()
95105
96106
    def content(self):
97107
        content = {}
98-
        self.content = []
108+
        self.slugs = []
99109
        for slug in self.files:
100110
            if slug['i18n_type'] == 'YML':
101111
                myslug = YamlFormat(

103113
                         'source': self.filename(slug['slug'], True)})
104114
            else:
105115
                raise Exception("Unsupported format: " + slug['i18n_type'])
106-
            self.content.append(myslug)
116+
            self.slugs.append(myslug)
107117
            content[slug['slug']] = myslug.content()
108118
        return content