X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp;a=blobdiff_plain;f=po%2Fxml2po.py;h=cfbeaf4ba6e094200022660fd93e315d05b48300;hp=ec19acf1ada6cf6d4b3072d7425db9af24429d00;hb=32c942bb429ba650aeeed2887f6caed7deed0e39;hpb=0f986f99ef13e1e4fb0e76f0dc4074650b655f32 diff --git a/po/xml2po.py b/po/xml2po.py index ec19acf..cfbeaf4 100755 --- a/po/xml2po.py +++ b/po/xml2po.py @@ -1,23 +1,64 @@ #!/usr/bin/python import sys +import os +import string from xml.sax import make_parser -from xml.sax.handler import ContentHandler +from xml.sax.handler import ContentHandler, property_lexical_handler +try: + from _xmlplus.sax.saxlib import LexicalHandler + no_comments = False +except ImportError: + class LexicalHandler: + pass + no_comments = True -class parseXML(ContentHandler): - def __init__(self): +class parseXML(ContentHandler, LexicalHandler): + def __init__(self, attrlist): self.isPointsElement, self.isReboundsElement = 0, 0 + self.attrlist = attrlist + self.last_comment = None - def startElement(self, name, attrs): - if (attrs.has_key('text')): - print - print '#: ' + sys.argv[1] - print 'msgid "' + str(attrs.get('text', "")) + '"' - print 'msgstr ""' + def comment(self, comment): + if comment.find("TRANSLATORS:") != -1: + self.last_comment = comment -sys.argv[1] + def startElement(self, name, attrs): + for x in ["text", "title", "value", "caption"]: + try: + attrlist.add((attrs[x], self.last_comment)) + self.last_comment = None + except KeyError: + pass parser = make_parser() - -contentHandler = parseXML() + +attrlist = set() + +contentHandler = parseXML(attrlist) parser.setContentHandler(contentHandler) -parser.parse(sys.argv[1]) +if not no_comments: + parser.setProperty(property_lexical_handler, contentHandler) + +for arg in sys.argv[1:]: + if os.path.isdir(arg): + for file in os.listdir(arg): + if (file.endswith(".xml")): + parser.parse(os.path.join(arg, file)) + else: + parser.parse(arg) + + attrlist = list(attrlist) + attrlist.sort(key=lambda a: a[0]) + + for (k,c) in attrlist: + print + print '#: ' + arg + string.replace(k, "\\n", "\"\n\"") + if c: + for l in c.split('\n'): + print "#. ", l + if str(k).strip() != "": + print 'msgid "' + str(k) + '"' + print 'msgstr ""' + + attrlist = set()