File: po4a-trim.py

package info (click to toggle)
debian-reference 2.128
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,588 kB
  • sloc: python: 586; makefile: 572; sh: 526; sed: 80
file content (72 lines) | stat: -rwxr-xr-x 2,069 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python3
# vim: set sts=4 expandtab:
"""
po4a-trim

Copyright (C) 2009 Osamu Aoki, GPL

Design: Trim po file depending on mode

Add blank line at the end of po
"""

import sys, os, re

VERSION = '1.0.1'

if __name__ == '__main__':

    po4a_mode = 'NORMAL'
    po4a_mode_old = 'NORMAL'
    # 'NORMAL' for header/comment which is dropped.
    # 'MSGID' for English original part
    # 'MSGSTR' for translated text part
    msgid = ''
    msgstr = ''

    for line in sys.stdin.readlines():
        line=line[0:-1]
        if re.match("^[ \t]*$", line):
            # blank line
            line = ""
            if po4a_mode != 'NORMAL':
                po4a_mode_old = po4a_mode
            po4a_mode = 'NORMAL'
        elif re.match("^#", line):
            # comment line
            line = ""
            if po4a_mode != 'NORMAL':
                po4a_mode_old = po4a_mode
            po4a_mode = 'NORMAL'
        elif re.match("^msgid", line):
            line = re.sub(r"^msgid +\"(.*)\" *$",r'\1',line)
            if po4a_mode != 'MSGID':
                po4a_mode_old = po4a_mode
            else:
                print("error @ " + line)
            po4a_mode = 'MSGID'
        elif re.match("^msgstr", line):
            line = re.sub(r"^msgstr +\"(.*)\" *$",r'\1',line)
            if po4a_mode != 'MSGSTR':
                po4a_mode_old = po4a_mode
            else:
                print("error @ " + line)
            po4a_mode = 'MSGSTR'
        elif re.match("^\"", line):
            line = re.sub(r"^\"(.*)\" *$",r'\1',line)
            # po4a_mode_old and po4a_mode are the same
        #
        if po4a_mode == 'MSGID':
            msgid = msgid + line
        if po4a_mode == 'MSGSTR':
            msgstr = msgstr + line
        if po4a_mode == 'NORMAL' and po4a_mode_old == 'MSGSTR':
            if msgid != "" and  msgstr != "":
                print("msgid \"" + msgid + "\"")
                print("msgstr \"" + msgstr + "\"\n")
            #
            msgid = ''
            msgstr = ''
            po4a_mode_old = 'NORMAL'