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

Proposed by Daniel Holbach
Status: Merged
Approved by: David Planella
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 Needs Information
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.
Revision history for this message
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
Revision history for this message
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
=== modified file 'edit-here/translations.py'
--- edit-here/translations.py 2015-02-24 09:17:31 +0000
+++ edit-here/translations.py 2015-02-24 11:03:11 +0000
@@ -1,4 +1,5 @@
1import codecs1import codecs
2import copy
2import glob3import glob
3import os4import os
4import shutil5import shutil
@@ -13,15 +14,66 @@
13 'date',14 'date',
14 ]15 ]
1516
16# This defines how complete we expect translations to be before we generate17# This defines how complete we expect translations to be before we
17# HTML from them. Needs to be string.18# generate HTML from them. Needs to be string.
18TRANSLATION_COMPLETION_PERCENTAGE = '0'19TRANSLATION_COMPLETION_PERCENTAGE = '0'
1920
20PO4A_ARGS = [21
21 '-f', 'text',22class PO4A(object):
22 '-o', 'markdown',23 def __init__(self, translations_dir, temp_dir):
23 '-M', 'utf-8',24 self.default_args = [
24 ]25 '-f', 'text',
26 '-o', 'markdown',
27 '-M', 'utf-8',
28 ]
29 self.translations_dir = translations_dir
30 self.temp_dir = temp_dir
31
32 def run(self, po4a_command, additional_args, with_output=False,
33 working_dir=None):
34 if not working_dir:
35 working_dir = self.temp_dir
36 pwd = os.getcwd()
37 args = copy.copy(self.default_args)
38 args += additional_args
39 os.chdir(working_dir)
40 if with_output:
41 ret = subprocess.Popen([po4a_command]+args, stdout=subprocess.PIPE)
42 else:
43 ret = subprocess.call([po4a_command]+args)
44 os.chdir(pwd)
45 return ret
46
47 def gettextize(self, documents):
48 args = []
49 for document in documents:
50 args += ['-m', document]
51 args += [
52 '-p', os.path.join(self.translations_dir, 'help.pot'),
53 '-L', 'utf-8',
54 ]
55 return self.run('po4a-gettextize', args)
56
57 def updatepo(self, langs, documents):
58 for lang in langs:
59 args = []
60 for document in documents:
61 args += ['-m', document]
62 args += ['-p', os.path.join(self.translations_dir, lang+'.po')]
63 ret = self.run('po4a-updatepo', args)
64 if ret:
65 return False
66 return True
67
68 def translate(self, doc, lang):
69 args = [
70 '-k', TRANSLATION_COMPLETION_PERCENTAGE,
71 '-m', doc,
72 '-p', 'po/%s.po' % lang,
73 '-L', 'utf-8',
74 ]
75 return self.run('po4a-translate', args, with_output=True,
76 working_dir=os.path.join(PATH, '..'))
2577
2678
27class Translations(object):79class Translations(object):
@@ -32,10 +84,11 @@
32 [os.path.basename(a).split('.po')[0] for a84 [os.path.basename(a).split('.po')[0] for a
33 in glob.glob(self.translations_dir+'/*.po')]85 in glob.glob(self.translations_dir+'/*.po')]
34 self.documents = self._find_documents()86 self.documents = self._find_documents()
35 self.tempdir = tempfile.mkdtemp()87 self.temp_dir = tempfile.mkdtemp()
88 self.po4a = PO4A(self.translations_dir, self.temp_dir)
3689
37 def __del__(self):90 def __del__(self):
38 shutil.rmtree(self.tempdir)91 shutil.rmtree(self.temp_dir)
3992
40 def _find_documents(self):93 def _find_documents(self):
41 documents = []94 documents = []
@@ -57,7 +110,7 @@
57 os.remove(f)110 os.remove(f)
58111
59 def _remove_metadata(self, filename):112 def _remove_metadata(self, filename):
60 new_dir = os.path.join(self.tempdir, os.path.dirname(filename))113 new_dir = os.path.join(self.temp_dir, os.path.dirname(filename))
61 os.makedirs(new_dir, exist_ok=True)114 os.makedirs(new_dir, exist_ok=True)
62 new_filename = os.path.join(new_dir, os.path.basename(filename))115 new_filename = os.path.join(new_dir, os.path.basename(filename))
63 shutil.copy(filename, new_filename)116 shutil.copy(filename, new_filename)
@@ -88,42 +141,13 @@
88 return True141 return True
89142
90 def generate_pot_file(self):143 def generate_pot_file(self):
91 args = PO4A_ARGS.copy()144 return self.po4a.gettextize(self.documents)
92 for document in self.documents:
93 args += ['-m', document]
94 args += [
95 '-p', os.path.join(self.translations_dir, 'help.pot'),
96 '-L', 'utf-8',
97 ]
98 pwd = os.getcwd()
99 os.chdir(self.tempdir)
100 subprocess.call(['po4a-gettextize']+args)
101 os.chdir(pwd)
102145
103 def update_po_files(self):146 def update_po_files(self):
104 pwd = os.getcwd()147 return self.po4a.updatepo(self.available_languages, self.documents)
105 os.chdir(self.tempdir)
106 for lang in self.available_languages:
107 args = PO4A_ARGS.copy()
108 for document in self.documents:
109 args += ['-m', document]
110 args += ['-p', os.path.join(self.translations_dir, lang+'.po')]
111 ret = subprocess.call(['po4a-updatepo']+args)
112 if ret:
113 return False
114 os.chdir(pwd)
115 return True
116148
117 def _call_po4a_translate(self, doc, lang):149 def _call_po4a_translate(self, doc, lang):
118 args = PO4A_ARGS.copy()150 res = self.po4a.translate(doc, lang)
119 args += [
120 '-k', TRANSLATION_COMPLETION_PERCENTAGE,
121 '-m', doc,
122 '-p', 'po/%s.po' % lang,
123 '-L', 'utf-8',
124 ]
125 res = subprocess.Popen(['po4a-translate']+args,
126 stdout=subprocess.PIPE)
127 output = codecs.decode(res.communicate()[0])151 output = codecs.decode(res.communicate()[0])
128 broken_title_line = [line for line in output.split('\n')152 broken_title_line = [line for line in output.split('\n')
129 if line.lower().startswith('title:')][0]153 if line.lower().startswith('title:')][0]

Subscribers

People subscribed via source and target branches