~nskaggs/help-app/functional-test-template

« back to all changes in this revision

Viewing changes to internals/translations/po4a.py

  • Committer: Daniel Holbach
  • Date: 2015-03-19 17:54:55 UTC
  • mfrom: (111.2.14 1429896)
  • Revision ID: daniel.holbach@canonical.com-20150319175455-sjwdzzy3aqaaiu9u
mergedĀ lp:~dholbach/help-app/1429896

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import copy
 
2
import os
 
3
import shutil
 
4
import subprocess
 
5
 
 
6
from translations.utils import (
 
7
    require,
 
8
    use_top_level_dir,
 
9
)
 
10
 
 
11
# This defines how complete we expect translations to be before we
 
12
# generate HTML from them. Needs to be string.
 
13
TRANSLATION_COMPLETION_PERCENTAGE = '0'
 
14
 
 
15
 
 
16
class PO4A(object):
 
17
    pwd = None
 
18
 
 
19
    def __init__(self):
 
20
        self.default_args = [
 
21
            '-f', 'text',
 
22
            '-o', 'markdown',
 
23
            '-M', 'utf-8',
 
24
            ]
 
25
        if not shutil.which('po4a'):
 
26
            require('po4a')
 
27
 
 
28
    def run(self, po4a_command, additional_args, with_output=False,
 
29
            top_level_dir=False):
 
30
        if top_level_dir:
 
31
            self.pwd = use_top_level_dir()
 
32
        args = copy.copy(self.default_args)
 
33
        args += additional_args
 
34
        if with_output:
 
35
            ret = subprocess.Popen([po4a_command]+args, stdout=subprocess.PIPE)
 
36
        else:
 
37
            ret = subprocess.call([po4a_command]+args)
 
38
        if top_level_dir:
 
39
            os.chdir(self.pwd)
 
40
            self.pwd = None
 
41
        return ret
 
42
 
 
43
    def gettextize(self, document_fns, pot_file):
 
44
        args = copy.copy(self.default_args)
 
45
        for document_fn in document_fns:
 
46
            args += ['-m', document_fn]
 
47
        args += [
 
48
            '-p', pot_file,
 
49
            '-L', 'utf-8',
 
50
            ]
 
51
        ret = self.run('po4a-gettextize', args, top_level_dir=True)
 
52
        return (not ret)
 
53
 
 
54
    def translate(self, doc, po_fn):
 
55
        args = [
 
56
            '-k', TRANSLATION_COMPLETION_PERCENTAGE,
 
57
            '-m', doc,
 
58
            '-p', po_fn,
 
59
            '-L', 'utf-8',
 
60
            ]
 
61
        return self.run('po4a-translate', args, with_output=True,
 
62
                        top_level_dir=True)