from xml.sax import * import re, sys ### # Real name. This RE distinguishes 'filler' widgets from the # widgets we actually care about. ### rn = re.compile(r"^r_") def cppClass(gladeClass): gtk = re.compile(r"^Gtk") gnome = re.compile(r"^Gnome") if gtk.search(gladeClass): return "Gtk::" + gladeClass[3:] elif gnome.search(gladeClass): return "Gnome::" + gladeClass[5:] class widget: def __init__(self, clss, name): self.clss = cppClass(clss) self.name = name def getAccessor(self): function = "" function += self.clss + " * " + dialog +"::" + self.name function += "() const \n{\n return getWidget<" + self.clss function += ">(\"" + "r_" + self.name + "\");\n}\n" return function def getDeclaration(self): function = "/// generated by accessors.py\n" function += self.clss + " * " + self.name function += "() const;\n"; return function class GnomeFrontendHandler(ContentHandler): def __init__(self): self.elemstack = [] self.widget = 0 self.TODO = [] def startElement(self, name, attrs): self.elemstack.append(name) if name == "widget" and rn.search(attrs["id"]): self.TODO.append(widget(attrs["class"], re.sub("^r_", "", attrs["id"]))) def endElement(self, name): self.elemstack.pop() def characters(self, data): elem = self.elemstack[-1] def widgets(self): return self.TODO dialog = sys.argv[2] glade = open(sys.argv[1]) ## parse the document prs = make_parser() hndlr = GnomeFrontendHandler() prs.setContentHandler(hndlr) prs.parse(glade) ## write the definitions to .C_gen dotC = open(dialog + ".C_gen", "w+") for i in hndlr.widgets(): dotC.write( i.getAccessor()) dotC.close() ## write the declarations to .h_gen dotH = open(dialog + ".h_gen", "w+") for i in hndlr.widgets(): dotH.write( i.getDeclaration()) dotH.close()