File: update-manual.py

package info (click to toggle)
expeyes 5.3.3%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,480 kB
  • sloc: python: 36,039; ansic: 9,754; xml: 1,010; makefile: 784; sh: 581; asm: 202; javascript: 46; php: 1
file content (107 lines) | stat: -rw-r--r-- 3,095 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-

from subprocess import Popen, PIPE, call
import re,os.path,sys

class poDic:
    """
    a class which behaves like a dictionary,
    whose instances are intialized by PO files
    """
    def __init__(self, pofile):
        """
        The constructor
        @param pofile a PO file (gettext format)
        """
        self.dict={}
        self.mode=None
        mi=re.compile('^msgid "(.*)"$')
        ms=re.compile('^msgstr "(.*)"$')
        m=re.compile('^"(.*)"$')
        lines=open(pofile,"r").readlines()
        msgid=""
        msgstr=""
        for l in lines:
            l=l.strip()
            i=mi.match(l)
            s=ms.match(l)
            o=m.match(l)
            if i:
                if self.mode=="msgstr":
                    self.dict[msgid]=msgstr
                self.mode="msgid"
                msgid=i.group(1)
            elif s:
                self.mode="msgstr"
                msgstr=s.group(1)
            elif o:
                if self.mode=="msgid":
                    msgid+=o.group(1)
                elif self.mode=="msgstr":
                    msgstr+=o.group(1)
                else:
                    print ("error, '%s' not usable" %o.group(1))
        # record the last one
        if self.mode=="msgstr":
            self.dict[msgid]=msgstr

    def __str__(self):
        """
        Make a vivible representation of the instance
        """
        return "poDic:\n%s" %self.dict

    def __getitem__(self,key):
        """
        reads in the directory
        @param key a string
        @return the content of the dictionary when search with key
        """
        if self.dict.has_key(key):
            return self.dict[key]
        else:
            return None

def localizeManual(pofile, lyxfile, prefix="new-"):
    """
    localizes a manual file in LyX format
    @param pofile a PO file
    @param lyxfile a LyX file
    @param prefix a prefix to make the name of the new file
    """
    menuitem=re.compile(r"^menuitem\{(.*)\}$")
    buttonlabel=re.compile(r"^buttonlabel\{(.*)\}$")
    patterns=(menuitem,buttonlabel)
    manFileName=os.path.abspath(lyxfile)
    newFileName=os.path.join(os.path.dirname(manFileName),prefix+os.path.basename(manFileName))
    podic= poDic(pofile)
    infile=open(lyxfile,"r")
    outfile=open(newFileName,"w")
    while True:
        l=infile.readline()
        if not l:
            break
        for pat in patterns:
            it=pat.match(l)
            if it:
                key=it.group(1)
                if podic[key]:
                    l=l.replace(key, podic[key])
                else:
                    print ("ERROR: menuitem not found in dictionary '%s'" %key)
        outfile.write(l)
    outfile.close()
    infile.close()
    print ("localized %s => %s (using %s)" %(lyxfile,newFileName, pofile))


if __name__=="__main__":
    usage="""\
Usage: python update-manual.py <Po file> <LyX file>"""
    try:
        pofile =sys.argv[1]
        lyxfile=sys.argv[2]
        localizeManual(pofile, lyxfile, "new-")
    except:
        print (usage)