update .gitattributes
[enigma2-plugins.git] / scripts / xml2po.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 import sys
4 import os
5 import string
6
7 from xml.sax import make_parser, saxutils
8 from xml.sax.handler import ContentHandler, property_lexical_handler
9 try:
10         from _xmlplus.sax.saxlib import LexicalHandler
11         no_comments = False
12 except ImportError:
13         class LexicalHandler:
14                 pass
15         no_comments = True
16
17 class parseXML(ContentHandler, LexicalHandler):
18         def __init__(self, attrlist, file):
19                 self.isPointsElement, self.isReboundsElement = 0, 0
20                 self.currentFile = file
21                 self.attrlist = attrlist
22                 self.last_comment = None
23                 self.data = ""
24
25         def comment(self, comment):
26                 if comment.find("TRANSLATORS:") != -1:
27                         self.last_comment = comment
28
29         def startElement(self, name, attrs):
30                 #print "startElement", name, attrs
31                 self.last_comment = None
32                 self.data = ""
33                 for x in ["text", "title", "value", "caption", "description"]:
34                         try:
35                                 attrlist.add((attrs[x], self.last_comment,self.currentFile))
36                                 self.last_comment = None
37                         except KeyError:
38                                 pass
39
40         def endElement(self, name):
41                 #print "endElement", name
42                 if name in ("shortdescription", "description"):
43                         attrlist.add((self.data.strip().decode("utf-8"), self.last_comment,self.currentFile))
44                 self.data = ""
45
46         def characters(self, data):
47                 self.data += data.strip()
48
49
50 parser = make_parser()
51
52 attrlist = set()
53
54 for arg in sys.argv[1:]:
55         #print "processing:",arg
56         parse_path = ""
57         if os.path.isdir(arg):
58                 for file in os.listdir(arg):
59                         if (file.endswith(".xml")):
60                                 contentHandler = parseXML(attrlist,file)
61                                 parser.setContentHandler(contentHandler)
62                                 if not no_comments:
63                                         parser.setProperty(property_lexical_handler, contentHandler)
64                                 parser.parse(os.path.join(arg, file))
65         else:
66                 contentHandler = parseXML(attrlist,arg)
67                 parser.setContentHandler(contentHandler)
68                 if not no_comments:
69                         parser.setProperty(property_lexical_handler, contentHandler)
70                 parser.parse(arg)
71
72         attrlist = list(attrlist)
73         attrlist.sort(key=lambda a: a[2])
74
75         for (k,c,f) in attrlist:
76                 if c:
77                         for l in c.split("\n"):
78                                 print "#. ", l
79                 if arg == f:
80                         print "#: %s" % (arg)
81                 else:
82                         print "#: %s" % (arg + f)
83                 msgid = saxutils.escape(k, {'"': '"'}).encode("utf-8")
84                 #print type(msgid), repr(msgid)
85                 string.replace(msgid, "\\n", "\"\n\"")
86                 msgstr = ""
87                 if msgid.strip() != "":
88                         if msgid.find("\"") != -1:
89                                 print "msgid \"%s\"" % (msgid.replace('\"', '\\"'))
90                         else:
91                                 print "msgid \"%s\"" % (msgid)
92                         print "msgstr \"%s\"\n" % (msgstr)
93
94         attrlist = set()