⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xrc_codegen.py

📁 用python写的ide开发环境,巨强大,不过需要wxpython的支持
💻 PY
📖 第 1 页 / 共 2 页
字号:
# xrc_codegen.py: wxWidgets resources XRC code generator# $Id: xrc_codegen.py,v 1.21 2007/03/27 07:02:06 agriggio Exp $## Copyright (c) 2002-2007 Alberto Griggio <agriggio@users.sourceforge.net># License: MIT (see license.txt)# THIS PROGRAM COMES WITH NO WARRANTY"""\Generates the xml code for the app in XRC format.Calls the appropriate ``writers'' of the various objects. These functionsreturn an instance of XrcObject"""import common, configimport sysimport cStringIOfrom xml_parse import XmlParsingErrorfrom xml.sax.saxutils import escape, quoteattr, unescapelanguage = "XRC"writer = sys.modules[__name__]# default extensions for generated files: a list of file extensionsdefault_extensions = ['xrc']# output string buffer for the code output_file = None# name of the output fileoutput_file_name = None# dictionary of ``writers'' for the various objectsobj_builders = {}# current indentation levelcurr_tab = 0def tabs(number): return '    ' * number# encoding of the applicationapp_encoding = 'ISO-8859-1' # default, if nothing else foundclass XrcObject:    """\    Class to produce the XRC code for a given widget. This is a base class    which does nothing    """    def __init__(self):        self.properties = {}        self.children = [] # sub-objects    def write_child_prologue(self, child, out_file, ntabs): pass    def write_child_epilogue(self, child, out_file, ntabs): pass    def write_property(self, name, val, outfile, ntabs): pass    def write(self, out_file, ntabs): pass# end of class XrcObject"""\dictionary of active XrcObject instances: during the code generation it storesall the non-sizer objects that have children (i.e. frames, dialogs, panels,notebooks, etc.), while at the end of the code generation, before finalizeis called, it contains only the true toplevel objects (frames and dialogs), andis used to write their XML code (see finalize). The other objects are deletedwhen add_object is called with their corresponding code_object as argument (seeadd_object)"""xrc_objects = None class SizerItemXrcObject(XrcObject):    """\    XrcObject to handle sizer items    """    def __init__(self, obj, option, flag, border):        XrcObject.__init__(self)        self.obj = obj # the XrcObject representing the widget        self.option = option        self.flag = flag        self.border = border            def write(self, out_file, ntabs):        write = out_file.write        write(tabs(ntabs) + '<object class="sizeritem">\n')        if self.option != '0':            write(tabs(ntabs+1) + '<option>%s</option>\n' % self.option)        if self.flag and self.flag != '0':            write(tabs(ntabs+1) + '<flag>%s</flag>\n' % self.flag)        if self.border != '0':            write(tabs(ntabs+1) + '<border>%s</border>\n' % self.border)        # write the widget        self.obj.write(out_file, ntabs+1)        write(tabs(ntabs) + '</object>\n')        # end of class SizerItemXrcObjectclass SpacerXrcObject(XrcObject):    """\    XrcObject to handle widgets    """    def __init__(self, size_str, option, flag, border):        self.size_str = size_str        self.option = option        self.flag = flag        self.border = border    def write(self, out_file, ntabs):        write = out_file.write        write(tabs(ntabs) + '<object class="spacer">\n')        write(tabs(ntabs+1) + '<size>%s</size>\n' % self.size_str.strip())        if self.option != '0':            write(tabs(ntabs+1) + '<option>%s</option>\n' % self.option)        if self.flag and self.flag != '0':            write(tabs(ntabs+1) + '<flag>%s</flag>\n' % self.flag)        if self.border != '0':            write(tabs(ntabs+1) + '<border>%s</border>\n' % self.border)        write(tabs(ntabs) + '</object>\n')# end of class SpacerXrcObjectclass DefaultXrcObject(XrcObject):    """\    Standard XrcObject for every widget, used if no specific XrcObject is    available    """    def __init__(self, code_obj):        XrcObject.__init__(self)        self.properties = code_obj.properties        self.code_obj = code_obj        self.name = code_obj.name        self.klass = code_obj.base # custom classes aren't allowed in XRC        self.subclass = code_obj.klass    def write_property(self, name, val, outfile, ntabs):        if val:            name = escape(name)            outfile.write(tabs(ntabs) + '<%s>%s</%s>\n' % \                          (name, escape(val), name))    def write(self, out_file, ntabs):        write = out_file.write        if self.code_obj.in_sizers:            write(tabs(ntabs) + '<object class=%s>\n' % quoteattr(self.klass))        else:            if self.subclass and self.subclass != self.klass:                write(tabs(ntabs) +                      '<object class=%s name=%s subclass=%s>\n' % \                      (quoteattr(self.klass), quoteattr(self.name),                       quoteattr(self.subclass)))            else:                write(tabs(ntabs) + '<object class=%s name=%s>\n' % \                      (quoteattr(self.klass), quoteattr(self.name)))        tab_str = tabs(ntabs+1)        # write the properties        if self.properties.has_key('foreground'):            if self.properties['foreground'].startswith('#'):                # XRC does not support colors from system settings                self.properties['fg'] = self.properties['foreground']            del self.properties['foreground']        if self.properties.has_key('background'):            if self.properties['background'].startswith('#'):                # XRC does not support colors from system settings                self.properties['bg'] = self.properties['background']            del self.properties['background']        if self.properties.has_key('font'):            font = self.properties['font']            del self.properties['font']        else: font = None        style = str(self.properties.get('style', ''))        if style and style == '0':            del self.properties['style']        if 'id' in self.properties:            del self.properties['id'] # id has no meaning for XRC        # ALB 2004-12-05        if 'events' in self.properties:            #del self.properties['events'] # no event handling in XRC            for handler, event in self.properties['events'].iteritems():                write(tab_str + '<handler event=%s>%s</handler>\n' % \                      (quoteattr(handler), escape(event)))            del self.properties['events']        # 'disabled' property is actually 'enabled' for XRC        if 'disabled' in self.properties:            try: val = int(self.properties['disabled'])            except: val = False            if val:                self.properties['enabled'] = '0'            del self.properties['disabled']        # ALB 2007-08-31 extracode property        if 'extracode' in self.properties:            write(self.properties['extracode'].replace('\\n', '\n'))            del self.properties['extracode']        # custom base classes are ignored for XRC...        if 'custom_base' in self.properties:            del self.properties['custom_base']                    if 'extraproperties' in self.properties:            prop = self.properties['extraproperties']            del self.properties['extraproperties']            self.properties.update(prop)                    for name, val in self.properties.iteritems():            self.write_property(str(name), val, out_file, ntabs+1)        # write the font, if present        if font:            write(tab_str + '<font>\n')            tab_str = tabs(ntabs+2)            for key, val in font.iteritems():                if val:                    write(tab_str + '<%s>%s</%s>\n' % \                          (escape(key), escape(val), escape(key)))            write(tabs(ntabs+1) + '</font>\n')        # write the children        for c in self.children:            self.write_child_prologue(c, out_file, ntabs+1)            c.write(out_file, ntabs+1)            self.write_child_epilogue(c, out_file, ntabs+1)        write(tabs(ntabs) + '</object>\n')        # end of class DefaultXrcObjectclass NotImplementedXrcObject(XrcObject):    """\    XrcObject used when no code for the widget can be generated (for example,    because XRC does not currently handle such widget)    """    def __init__(self, code_obj):        XrcObject.__init__(self)        self.code_obj = code_obj            def write(self, outfile, ntabs):        m = 'code generator for %s objects not available' % self.code_obj.base        print >> sys.stderr, 'WARNING: %s' % m         outfile.write(tabs(ntabs) + '<!-- %s -->\n' % m)# end of class NotImplementedXrcObject

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -