File: gettext.py

package info (click to toggle)
moin 1.5.3-1.2etch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 20,692 kB
  • ctags: 26,801
  • sloc: python: 32,907; java: 10,704; perl: 1,424; php: 642; makefile: 172; xml: 162; sh: 121; sed: 5
file content (75 lines) | stat: -rw-r--r-- 2,325 bytes parent folder | download | duplicates (3)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: iso-8859-15 -*-
"""
    MoinMoin - GetText .po parser for moin 1.3.x

    Usage:
    ======

    Either begin your page like this:
    #format gettext
    (followed by .po file content only)

    Or attach the .po file and inline it:
    ... (wiki markup) ...
    inline:de.po
    ... (wiki markup) ...

    Of course, if you just attach the file, you won't be able to edit it in
    the wiki. So the #format method is better for online editing.
    
    Requirements:
    =============
    
    * requires Python 2.4 and installed "gettext" package (msgfmt)
    * requires wiki page content to be in config.charset, so do not put non-
      utf-8 content into a utf-8 wiki or it will crash.

    @copyright: 2005 by MoinMoin:AlexanderSchremmer
    @license: GNU GPL, see COPYING for details.
"""

Dependencies = []

import subprocess
from MoinMoin import config

class Parser:
    extensions = ['.po']
    def __init__ (self, raw, request, **kw):
        self.raw = raw
        self.request = request

    def format(self, formatter):
        PIPE = subprocess.PIPE
        STDOUT = subprocess.STDOUT
        p = subprocess.Popen(("msgfmt", "-c", "--statistics", "-",), shell=False,
                             stdin=PIPE, stdout=PIPE, stderr=STDOUT)
        
        charset = config.charset
        textin = self.raw.encode(charset)
        out = p.communicate(input=textin)[0]
        if out is None:
            out = ''
        out = out.decode(charset).replace('<stdin>:', 'input data line ')

        # show po file data with line numbers as msgfmt refers to them
        text, lineno = [], 0
        for l in self.raw.splitlines():
            lineno += 1
            text.append("%04d: %s" % (lineno, l))

        textout = [formatter.heading(1, 3),
                   'Gettext status messages:',
                   formatter.heading(0, 3),
                   formatter.preformatted(1),
                   formatter.text(out),
                   formatter.preformatted(0),
                   formatter.heading(1, 3),
                   'Input:',
                   formatter.heading(0, 3),
                   formatter.preformatted(1),
                   formatter.text("\n".join(text)),
                   formatter.preformatted(0),
                  ]

        self.request.write(''.join(textout))