#!/usr/bin/env python from xml.etree.ElementTree import parse import sys def printBegin(): print "#!/usr/bin/env python" print "#-*- coding:utf-8 -*-\n" print "import sys\ntry:\n import pygtk\n pygtk.require('2.0')" print " import gtk\n import gtk.glade\nexcept:\n sys.exit(1)\n" def printEnd(): print "\n\ndef main():" print " app = appWindow()" print " gtk.main()" print "\n\nif __name__ == '__main__':" print " main()" def printAppWindow(wnode, gladefile): windowname = wnode.attrib['id'] print "class appWindow:\n def __init__(self):" print " self.wtree = gtk.glade.XML('%s')" % gladefile print "\n #Get the Main Window, and connect the 'destroy' event" print " self.window = self.wtree.get_widget('%s')" % windowname print " if (self.window):\n self.window.connect('destroy', gtk.main_quit)" print "\n #Widgets -------" printWidgets(wnode.getiterator('widget')) print " #Autoconnect Signals and Callbacks" print " self.wtree.signal_autoconnect(self)" print "\n self.window.show_all()" def printWidgets(nodes): for node in nodes: try: c = node.attrib['class'] id = node.attrib['id'] if c == 'GtkToggleButton' or c == 'GtkSpinButton' \ or c == 'GtkCheckButton' or c == 'GtkRadioButton' \ or c == 'GtkEntry' or c == 'GtkComboBox' \ or c == 'GtkComboBoxEntry' or c == 'GtkProgressBar' \ or c == 'GtkStatusBar': print " self.%s = self.wtree.get_widget('%s')" % (id, id) elif c == 'GtkTextView': print " #TextView" print " self.%s = self.wtree.get_widget('%s')" % (id, id) print " self.%sBuffer = gtk.TextBuffer()" % node.attrib['id'] print " self.%s.set_buffer(self.%sBuffer)\n" % (id, id) elif c == 'GtkTreeView': print "\n #TreeView" print " self.%s = self.wtree.get_widget('%s')" % (id, id) print " self.%sStore = gtk.ListStore(str)" % node.attrib['id'] print " self.%s.set_model(self.%sStore)" % (id, id) print " column = gtk.TreeViewColumn('Coluna', \\" print " gtk.CellRendererText(), text=0)" print " self.%s.append_column(column)\n" % id except: pass def printBody(root, gladefile=None): widgets = root.getiterator('widget') for widget in widgets: if widget.attrib['class'] == 'GtkWindow': printAppWindow(widget, gladefile) def printSignalsCallbacks(root): print "\n # Callbacks -------------------------------------------------" \ "--------------" signals = root.getiterator('signal') for signal in signals: print " def %s(self, widget):" % signal.attrib['handler'] print " print '%s'\n" % signal.attrib['handler'] if __name__ == '__main__': try: gladefile = sys.argv[1] except: print "Usage: %s yourfile.glade" % sys.argv[0] sys.exit(1) tree = parse(gladefile) root = tree.getroot() printBegin() printBody(root, gladefile) printSignalsCallbacks(root) print " # Auxiliary Methods -----------------------------------------" \ "--------------" printEnd()