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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import copy
import os
import shutil
import subprocess

from translations.utils import (
    require,
    use_top_level_dir,
)

# This defines how complete we expect translations to be before we
# generate HTML from them. Needs to be string.
TRANSLATION_COMPLETION_PERCENTAGE = '0'


class PO4A(object):
    pwd = None

    def __init__(self):
        self.default_args = [
            '-f', 'text',
            '-o', 'markdown',
            '-M', 'utf-8',
            ]
        if not shutil.which('po4a'):
            require('po4a')

    def run(self, po4a_command, additional_args, with_output=False,
            top_level_dir=False):
        if top_level_dir:
            self.pwd = use_top_level_dir()
        args = copy.copy(self.default_args)
        args += additional_args
        if with_output:
            ret = subprocess.Popen([po4a_command]+args, stdout=subprocess.PIPE)
        else:
            ret = subprocess.call([po4a_command]+args)
        if top_level_dir:
            os.chdir(self.pwd)
            self.pwd = None
        return ret

    def gettextize(self, document_fns, pot_file):
        args = copy.copy(self.default_args)
        for document_fn in document_fns:
            args += ['-m', document_fn]
        args += [
            '-p', pot_file,
            '-L', 'utf-8',
            ]
        ret = self.run('po4a-gettextize', args, top_level_dir=True)
        return (not ret)

    def translate(self, doc, po_fn):
        args = [
            '-k', TRANSLATION_COMPLETION_PERCENTAGE,
            '-m', doc,
            '-p', po_fn,
            '-L', 'utf-8',
            ]
        return self.run('po4a-translate', args, with_output=True,
                        top_level_dir=True)