1.0.1: Fix installation-bug (added postinst + prerm)
[enigma2-plugins.git] / xml2po.py
1 #!/usr/bin/python
2 import sys
3 import os
4 import string
5 from xml.sax import make_parser
6 from xml.sax.handler import ContentHandler, property_lexical_handler
7 try:
8         from _xmlplus.sax.saxlib import LexicalHandler
9         no_comments = False
10 except ImportError:
11         class LexicalHandler:
12                 pass
13         no_comments = True
14
15 class parseXML(ContentHandler, LexicalHandler):
16         def __init__(self, attrlist):
17                 self.isPointsElement, self.isReboundsElement = 0, 0
18                 self.attrlist = attrlist
19                 self.last_comment = None
20
21         def comment(self, comment):
22                 if comment.find("TRANSLATORS:") != -1:
23                         self.last_comment = comment
24
25         def startElement(self, name, attrs):
26                 for x in ["text", "title", "value", "caption"]:
27                         try:
28                                 attrlist.add((attrs[x], self.last_comment))
29                                 self.last_comment = None
30                         except KeyError:
31                                 pass
32
33 parser = make_parser()
34
35 attrlist = set()
36
37 contentHandler = parseXML(attrlist)
38 parser.setContentHandler(contentHandler)
39 if not no_comments:
40         parser.setProperty(property_lexical_handler, contentHandler)
41
42 for arg in sys.argv[1:]:
43         if os.path.isdir(arg):
44                 for file in os.listdir(arg):
45                         if (file.endswith(".xml")):
46                                 parser.parse(os.path.join(arg, file))
47         else:
48                 parser.parse(arg)
49
50         attrlist = list(attrlist)
51         attrlist.sort(key=lambda a: a[0])
52
53         for (k,c) in attrlist:
54                 print
55                 print '#: ' + arg
56                 string.replace(k, "\\n", "\"\n\"")
57                 if c:
58                         for l in c.split('\n'):
59                                 print "#. ", l
60                 if str(k).strip() != "":
61                         print 'msgid "' + str(k) + '"'
62                         print 'msgstr ""'
63
64         attrlist = set()