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