通过python自动化生成GTK的布局代码(仿Qt)

时间:2024-10-02 12:21:07
  • import os,sys
  • import re
  • from string import Template
  • from import ElementTree,Element
  • ###############################################################################
  • # @Function: description
  • ###############################################################################
  • def readFile(fname):
  • with open(fname, 'rb') as f:
  • return ()
  • def writeFile(fname, data):
  • with open(fname, 'wb') as fo:
  • (data)
  • ###############################################################################
  • # @Function: read/write xml.
  • ###############################################################################
  • def read_xml(in_path):
  • tree = ElementTree()
  • (in_path)
  • return tree
  • def write_xml(tree, out_path):
  • (out_path, encoding="utf-8",xml_declaration=True)
  • ###############################################################################
  • # @Function: basic utils.
  • ###############################################################################
  • def if_match(node, kv_map):
  • for key in kv_map:
  • if (key) != kv_map.get(key):
  • return False
  • return True
  • ###############################################################################
  • # @Function: various search actions.
  • ###############################################################################
  • def getElementsByTag(tree, path):
  • return (path)
  • def getElementsByKey(nodelist, kv_map):
  • result_nodes = []
  • for node in nodelist:
  • if if_match(node, kv_map):
  • result_nodes.append(node)
  • return result_nodes
  • ###############################################################################
  • # @Function: various modify actions.
  • ###############################################################################
  • def removeAttribute(nodelist, kv_map):
  • for node in nodelist:
  • for key in kv_map:
  • if key in :
  • del [key]
  • def addAttribute(nodelist, kv_map):
  • for node in nodelist:
  • for key in kv_map:
  • node.set(key, kv_map.get(key))
  • def setAttribute(nodelist, text):
  • for node in nodelist:
  • = text
  • def createElement(tag, property_map, content):
  • element = Element(tag, property_map)
  • = content
  • return element
  • def add_child_node(nodelist, element):
  • for node in nodelist:
  • (element)
  • def removeNodeByTagAndKey(nodelist, tag, kv_map):
  • for parent_node in nodelist:
  • children = parent_node.getchildren()
  • for child in children:
  • if == tag and if_match(child, kv_map):
  • parent_node.remove(child)
  • class Ui(object):
  • def __init__(self,fname,platform):
  • self._fname = fname
  • self._classname = ""
  • self._widgetmap = {}
  • self._raw = ""
  • self._platform = platform
  • self._root = ()
  • self._uiname = fname
  • self._tree = None
  • ()
  • def getWidget(self):
  • if self._platform == "html":
  • from widget_html import Widget_Html
  • return Widget_Html()
  • if self._platform == "qt":
  • from widget_qt import Widget_Qt
  • return Widget_Qt()
  • if self._platform == "android":
  • from widget_android import Widget_Android
  • return Widget_Android()
  • if self._platform == "cocos2d":
  • from widget_cocos2d import Widget_Cocos2d
  • return Widget_Cocos2d()
  • if self._platform == "mfc":
  • from widget_mfc import Widget_Mfc
  • return Widget_Mfc()
  • if self._platform == "x11":
  • from widget_x11 import Widget_X11
  • return Widget_X11()
  • if self._platform == "gtk":
  • from widget_gtk import Widget_Gtk
  • return Widget_Gtk()
  • if self._platform == "tk":
  • from widget_tk import Widget_Tk
  • return Widget_Tk()
  • if self._platform == "canvas":
  • from widget_canvas import Widget_Canvas
  • return Widget_Canvas()
  • def load(self):
  • self._tree = read_xml(self._uiname)
  • root = self._tree.getroot()
  • def parse(self):
  • #[1]
  • class_nodes = getElementsByTag(self._tree, "class")
  • if len(class_nodes) >= 1:
  • self._classname = class_nodes[0].text
  • else:
  • print "==> No Class Node!!"
  • #[2]
  • widget_nodes = getElementsByTag(self._tree, "widget")
  • self._root = self.parse_widget(widget_nodes[0],None)
  • def parse_widget(self,node,pp):
  • w = ()
  • w._parent = pp
  • w._fname = self._fname
  • w._class = ["class"]
  • w._name = ["name"]
  • #[property geometry]
  • rnode = getElementsByKey(getElementsByTag(node, "property"),{"name":"geometry"})
  • if len(rnode) >= 1:
  • w._rect._x = getElementsByTag(rnode[0],"rect/x")[0].text
  • w._rect._y = getElementsByTag(rnode[0],"rect/y")[0].text
  • w._rect._width = getElementsByTag(rnode[0],"rect/width")[0].text
  • w._rect._height = getElementsByTag(rnode[0],"rect/height")[0].text
  • #[property text]
  • text_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"text"})
  • if len(text_node) >= 1:
  • w._vtext = getElementsByTag(text_node[0],"string")[0].text
  • #[property stylesheet]
  • stylesheet_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"styleSheet"})
  • if len(stylesheet_node) >= 1:
  • w._stylesheet = getElementsByTag(stylesheet_node[0],"string")[0].text
  • #[property tooltip]
  • tooltip_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"toolTip"})
  • if len(tooltip_node) >= 1:
  • w._tooltip = getElementsByTag(tooltip_node[0],"string")[0].text
  • #[property title]
  • title_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"title"})
  • if len(title_node) >= 1:
  • w._title = getElementsByTag(title_node[0],"string")[0].text
  • #[property orient]
  • orient_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"orientation"})
  • if len(orient_node) >= 1:
  • w._orient = getElementsByTag(orient_node[0],"enum")[0].("::")[1]
  • #[property statustip]
  • statustip_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"statusTip"})
  • if len(statustip_node) >= 1:
  • w._statustip = getElementsByTag(statustip_node[0],"string")[0].text
  • #[property whatthis]
  • whatthis_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"whatsThis"})
  • if len(whatthis_node) >= 1:
  • w._whatthis = getElementsByTag(whatthis_node[0],"string")[0].text
  • #[property accessiblename]
  • accessiblename_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"accessibleName"})
  • if len(accessiblename_node) >= 1:
  • w._accessiblename = getElementsByTag(accessiblename_node[0],"string")[0].text
  • #[property autorepeatdelay]
  • autorepeatdelay_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"autoRepeatDelay"})
  • if len(autorepeatdelay_node) >= 1:
  • w._autorepeatdelay = getElementsByTag(autorepeatdelay_node[0],"number")[0].text
  • #[property autorepeatinterval]
  • autorepeatinterval_node = getElementsByKey(getElementsByTag(node, "property"),{"name":"autoRepeatInterval"})
  • if len(autorepeatinterval_node) >= 1:
  • w._autorepeatinterval = getElementsByTag(autorepeatinterval_node[0],"number")[0].text
  • #[children]
  • rchilds = getElementsByTag(node, "widget")
  • if len(rchilds) <= 0:
  • return w
  • for child in rchilds:
  • w._children.append(self.parse_widget(child,w))
  • return w
  • def generate(self):
  • self._root.print_msg()
  • ###############################################################################
  • # @Function: Main Function
  • ###############################################################################
  • if len() < 2:
  • print " *.ui [html/qt/cocos2d/android/mfc/x11/gtk]"
  • (-1)
  • pname = "html"
  • if len() > 2:
  • pname = [2]
  • ui = Ui([1],pname)
  • ()
  • ()