update .gitattributes
[enigma2-plugins.git] / scripts / meta2po.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,file):
17                 self.isPointsElement, self.isReboundsElement = 0, 0
18                 self.currentFile = file
19                 self.attrlist = attrlist
20                 self.last_comment = None
21                 self.elements = []
22                 self.data = ""
23                 self.attributes = {}
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
31                 self.elements.append(name)
32                 if name in ( "shortdescription", "description" ):
33                         self.data = ""
34
35         def endElement(self, name):
36                 #print "endElement", name
37                 #print "self.elements:", self.elements
38                 self.elements.pop()
39
40                 if len(self.attributes) == 2:
41                         attrlist.add((self.attributes["shortdescription"], self.last_comment,self.currentFile))
42                         attrlist.add((self.attributes["description"], self.last_comment,self.currentFile))
43                         self.attributes = {}
44
45         def characters(self, data):
46                 if self.elements[-1] == "shortdescription":
47                         self.attributes["shortdescription"] = str(data)
48                 if self.elements[-1] == "description":
49                         self.data += data.strip()
50                         self.attributes["description"] = str(self.data)
51
52
53 parser = make_parser()
54
55 attrlist = set()
56
57 if not no_comments:
58         parser.setProperty(property_lexical_handler, contentHandler)
59
60 for arg in sys.argv[1:]:
61         if os.path.isdir(arg):
62                 for file in os.listdir(arg):
63                         if (file.endswith(".xml")):
64                                 contentHandler = parseXML(attrlist,file)
65                                 parser.setContentHandler(contentHandler)
66                                 parser.parse(os.path.join(arg, file))
67         elif (arg.endswith(".xml")):
68                 contentHandler = parseXML(attrlist,arg)
69                 parser.setContentHandler(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                 print
77                 print '#: ' + arg + f
78                 string.replace(k, "\\n", "\"\n\"")
79                 if c:
80                         for l in c.split('\n'):
81                                 print "#. ", l
82                 if str(k).strip() != "":
83                         print 'msgid "' + str(k) + '"'
84                         print 'msgstr ""'
85
86         attrlist = set()