Merge lp:~dholbach/help-app/break-out-po4a-operations into lp:~ubuntu-touch-coreapps-drivers/help-app/trunk

Proposed by Daniel Holbach on 2015-02-24
Status: Merged
Approved by: David Planella on 2015-02-24
Approved revision: 74
Merged at revision: 52
Proposed branch: lp:~dholbach/help-app/break-out-po4a-operations
Merge into: lp:~ubuntu-touch-coreapps-drivers/help-app/trunk
Diff against target: 152 lines (+66/-42)
1 file modified
edit-here/translations.py (+66/-42)
To merge this branch: bzr merge lp:~dholbach/help-app/break-out-po4a-operations
Reviewer Review Type Date Requested Status
David Planella 2015-02-24 Needs Information on 2015-02-24
Review via email: mp+250731@code.launchpad.net

Commit Message

Move all functionality related to po4a into its own class. This will avoid further duplication of the code and make the code in general easier to read.

To post a comment you must log in.
David Planella (dpm) wrote :

Looks good to me and it works. Happy to approve, but just so I make sure I understand - could you explain the rationale for the change?

review: Needs Information
Daniel Holbach (dholbach) wrote :

Simple refactoring to avoid duplication of code.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'edit-here/translations.py'
2--- edit-here/translations.py 2015-02-24 09:17:31 +0000
3+++ edit-here/translations.py 2015-02-24 11:03:11 +0000
4@@ -1,4 +1,5 @@
5 import codecs
6+import copy
7 import glob
8 import os
9 import shutil
10@@ -13,15 +14,66 @@
11 'date',
12 ]
13
14-# This defines how complete we expect translations to be before we generate
15-# HTML from them. Needs to be string.
16+# This defines how complete we expect translations to be before we
17+# generate HTML from them. Needs to be string.
18 TRANSLATION_COMPLETION_PERCENTAGE = '0'
19
20-PO4A_ARGS = [
21- '-f', 'text',
22- '-o', 'markdown',
23- '-M', 'utf-8',
24- ]
25+
26+class PO4A(object):
27+ def __init__(self, translations_dir, temp_dir):
28+ self.default_args = [
29+ '-f', 'text',
30+ '-o', 'markdown',
31+ '-M', 'utf-8',
32+ ]
33+ self.translations_dir = translations_dir
34+ self.temp_dir = temp_dir
35+
36+ def run(self, po4a_command, additional_args, with_output=False,
37+ working_dir=None):
38+ if not working_dir:
39+ working_dir = self.temp_dir
40+ pwd = os.getcwd()
41+ args = copy.copy(self.default_args)
42+ args += additional_args
43+ os.chdir(working_dir)
44+ if with_output:
45+ ret = subprocess.Popen([po4a_command]+args, stdout=subprocess.PIPE)
46+ else:
47+ ret = subprocess.call([po4a_command]+args)
48+ os.chdir(pwd)
49+ return ret
50+
51+ def gettextize(self, documents):
52+ args = []
53+ for document in documents:
54+ args += ['-m', document]
55+ args += [
56+ '-p', os.path.join(self.translations_dir, 'help.pot'),
57+ '-L', 'utf-8',
58+ ]
59+ return self.run('po4a-gettextize', args)
60+
61+ def updatepo(self, langs, documents):
62+ for lang in langs:
63+ args = []
64+ for document in documents:
65+ args += ['-m', document]
66+ args += ['-p', os.path.join(self.translations_dir, lang+'.po')]
67+ ret = self.run('po4a-updatepo', args)
68+ if ret:
69+ return False
70+ return True
71+
72+ def translate(self, doc, lang):
73+ args = [
74+ '-k', TRANSLATION_COMPLETION_PERCENTAGE,
75+ '-m', doc,
76+ '-p', 'po/%s.po' % lang,
77+ '-L', 'utf-8',
78+ ]
79+ return self.run('po4a-translate', args, with_output=True,
80+ working_dir=os.path.join(PATH, '..'))
81
82
83 class Translations(object):
84@@ -32,10 +84,11 @@
85 [os.path.basename(a).split('.po')[0] for a
86 in glob.glob(self.translations_dir+'/*.po')]
87 self.documents = self._find_documents()
88- self.tempdir = tempfile.mkdtemp()
89+ self.temp_dir = tempfile.mkdtemp()
90+ self.po4a = PO4A(self.translations_dir, self.temp_dir)
91
92 def __del__(self):
93- shutil.rmtree(self.tempdir)
94+ shutil.rmtree(self.temp_dir)
95
96 def _find_documents(self):
97 documents = []
98@@ -57,7 +110,7 @@
99 os.remove(f)
100
101 def _remove_metadata(self, filename):
102- new_dir = os.path.join(self.tempdir, os.path.dirname(filename))
103+ new_dir = os.path.join(self.temp_dir, os.path.dirname(filename))
104 os.makedirs(new_dir, exist_ok=True)
105 new_filename = os.path.join(new_dir, os.path.basename(filename))
106 shutil.copy(filename, new_filename)
107@@ -88,42 +141,13 @@
108 return True
109
110 def generate_pot_file(self):
111- args = PO4A_ARGS.copy()
112- for document in self.documents:
113- args += ['-m', document]
114- args += [
115- '-p', os.path.join(self.translations_dir, 'help.pot'),
116- '-L', 'utf-8',
117- ]
118- pwd = os.getcwd()
119- os.chdir(self.tempdir)
120- subprocess.call(['po4a-gettextize']+args)
121- os.chdir(pwd)
122+ return self.po4a.gettextize(self.documents)
123
124 def update_po_files(self):
125- pwd = os.getcwd()
126- os.chdir(self.tempdir)
127- for lang in self.available_languages:
128- args = PO4A_ARGS.copy()
129- for document in self.documents:
130- args += ['-m', document]
131- args += ['-p', os.path.join(self.translations_dir, lang+'.po')]
132- ret = subprocess.call(['po4a-updatepo']+args)
133- if ret:
134- return False
135- os.chdir(pwd)
136- return True
137+ return self.po4a.updatepo(self.available_languages, self.documents)
138
139 def _call_po4a_translate(self, doc, lang):
140- args = PO4A_ARGS.copy()
141- args += [
142- '-k', TRANSLATION_COMPLETION_PERCENTAGE,
143- '-m', doc,
144- '-p', 'po/%s.po' % lang,
145- '-L', 'utf-8',
146- ]
147- res = subprocess.Popen(['po4a-translate']+args,
148- stdout=subprocess.PIPE)
149+ res = self.po4a.translate(doc, lang)
150 output = codecs.decode(res.communicate()[0])
151 broken_title_line = [line for line in output.split('\n')
152 if line.lower().startswith('title:')][0]

Subscribers

People subscribed via source and target branches