Debian Codegraph @

arduino-core/src/processing/app/i18n/python/transifex.py

raw symbols SCIP
1 #!/usr/bin/env python2
2 #vim:set fileencoding=utf-8 sw=2 expandtab
3
4 import update
5 import requests
6 import json
7
8 class Transifex(object):
9 def __init__(self, user, passwd):
10 self.auth_ = (user, passwd)
11
12 r = requests.get(
13 'http://www.transifex.com/api/2/project/'
14 'arduino-ide-15/resource/ide-15/?details',
15 auth=self.auth_
16 )
17 r.raise_for_status()
18 d = r.json()
19 self.languages_ = set(lang['code'] for lang in d['available_languages'])
20
21 def canonical_lang(self, lang):
22 lang = lang.lower()
23
24 for l in self.languages_:
25 if l.lower() == lang:
26 return l
27
28 match = []
29 for l in self.languages_:
30 if l.split('_', 1)[0].lower() == lang:
31 match.append(l)
32 if len(match) > 1:
33 raise RuntimeError('Two or more candidates for %s: %s' % (lang, ' '.join(match)))
34 if len(match) == 0:
35 raise RuntimeError('No language code %s' % lang)
36 return match[0]
37
38 def translation(self, lang):
39 r = requests.get(
40 'https://www.transifex.com/api/2/project/arduino-ide-15/resource/ide-15/translation/%s/?file' % lang,
41 auth=self.auth_
42 )
43 r.raise_for_status()
44 r.encoding = 'utf-8' # workaround for a Transifex issue.
45 return r.text
46
47 def pull(self, lang, fname):
48 new = self.translation(lang).encode('utf-8')
49 new = map(lambda a: a + '\n', new.split('\n'))
50 new = update.read_po(new)
51
52 update.dump(new, fname)
53
54 def push(self, lang, data):
55 r = requests.put(
56 'http://www.transifex.com/api/2/project/'
57 'arduino-ide-15/resource/ide-15/translation/%s/' % lang,
58 data=json.dumps({ 'content': data }),
59 headers={ 'content-type': 'application/json' },
60 auth=self.auth_
61 )
62 r.raise_for_status()