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

📄 xrc_codegen.py

📁 用python写的ide开发环境,巨强大,不过需要wxpython的支持
💻 PY
📖 第 1 页 / 共 2 页
字号:
def initialize(app_attrs): #out_path, multi_files):    """\    Code generator initialization function.    """    out_path = app_attrs['path']    multi_files = app_attrs['option']        global output_file, curr_tab, xrc_objects, output_file_name, app_encoding    # first, set the app encoding    if 'encoding' in app_attrs:        app_encoding = app_attrs['encoding']        # wx doesn't like latin-1        if app_encoding == 'latin-1': app_encoding = 'ISO-8859-1'    if multi_files:        # for now we handle only single-file code generation        raise IOError("XRC code cannot be split into multiple files")    output_file_name = out_path    output_file = cStringIO.StringIO() #open(out_path, 'w')    from time import asctime    header_lines = ['<?xml version="1.0" encoding="%s"?>' % app_encoding,                     '<!-- generated by wxGlade %s on %s%s -->' % \                    (common.version, asctime(), common.generated_from())]    if not config.preferences.write_timestamp:        header_lines[1] = '<!-- generated by wxGlade %s%s -->' % \                          (common.version, common.generated_from())    for line in header_lines:        output_file.write(line + '\n')    output_file.write('\n<resource version="2.3.0.1">\n')    curr_tab = 1    xrc_objects = {}def finalize():    """\    Code generator finalization function.    """    # write the code for every toplevel object    for obj in xrc_objects.itervalues():        obj.write(output_file, 1)    output_file.write('</resource>\n')    #output_file.close()    # store the contents to file    common.save_file(output_file_name, output_file.getvalue(), 'codegen')def add_object(unused, sub_obj):    """\    Adds the object sub_obj to the XRC tree. The first argument is unused.    """    # what we need in XRC is not top_obj, but sub_obj's true parent    top_obj = sub_obj.parent    builder = obj_builders.get(sub_obj.base, DefaultXrcObject)    try:        # check whether we already created the xrc_obj        xrc_obj = sub_obj.xrc    except AttributeError:        xrc_obj = builder(sub_obj) # builder functions must return a subclass                                   # of XrcObject        sub_obj.xrc = xrc_obj    else:        # if we found it, remove it from the xrc_objects dictionary (if it was        # there, i.e. the object is not a sizer), because this isn't a true        # toplevel object        if sub_obj in xrc_objects:            del xrc_objects[sub_obj]    # let's see if sub_obj's parent already has an XrcObject: if so, it is    # temporairly stored in the xrc_objects dict...    try: top_xrc = xrc_objects[top_obj]    except KeyError:        # ...otherwise, create it and store it in the xrc_objects dict        top_xrc = obj_builders.get(top_obj.base, DefaultXrcObject)(top_obj)        top_obj.xrc = top_xrc        xrc_objects[top_obj] = top_xrc    top_obj.xrc.children.append(xrc_obj)def add_sizeritem(unused, sizer, obj, option, flag, border):    """\    Adds a sizeritem to the XRC tree. The first argument is unused.    """    # what we need in XRC is not toplevel, but sub_obj's true parent    toplevel = obj.parent    top_xrc = toplevel.xrc    obj_xrc = obj.xrc    try: sizer_xrc = sizer.xrc    except AttributeError:        # if the sizer has not an XrcObject yet, create it now        sizer_xrc = obj_builders.get(sizer.base, DefaultXrcObject)(sizer)        sizer.xrc = sizer_xrc    # we now have to move the children from 'toplevel' to 'sizer'     index = top_xrc.children.index(obj_xrc)    if obj.klass == 'spacer':        w = obj.properties.get('width', '0')        h = obj.properties.get('height', '0')        obj_xrc = SpacerXrcObject('%s, %s' % (w, h), str(option), str(flag),                                  str(border))        sizer.xrc.children.append(obj_xrc)    else:        sizeritem_xrc = SizerItemXrcObject(obj_xrc, str(option), str(flag),                                           str(border))        sizer.xrc.children.append(sizeritem_xrc)    del top_xrc.children[index]def add_class(code_obj):    """\    Add class behaves very differently for XRC output than for other lanaguages    (i.e. pyhton): since custom classes are not supported in XRC, this has    effect only for true toplevel widgets, i.e. frames and dialogs. For other    kinds of widgets, this is equivalent to add_object    """    if not xrc_objects.has_key(code_obj):        builder = obj_builders.get(code_obj.base, DefaultXrcObject)        xrc_obj = builder(code_obj)        code_obj.xrc = xrc_obj        # add the xrc_obj to the dict of the toplevel ones        xrc_objects[code_obj] = xrc_objdef add_app(app_attrs, top_win_class):    # in the case of XRC output, there's no wxApp code to generate    passclass FontPropertyHandler:    def __init__(self):        self.props = {'size': '', 'family': '', 'style': '', 'weight': '',                      'underlined': '', 'face': ''}        self.current = None    def start_elem(self, name, attrs):        self.current = name    def end_elem(self, name, code_obj):        if name == 'font':            code_obj.properties['font'] = self.props            return True # to remove this handler    def char_data(self, data):        self.props[self.current] = str(data.strip())# end of class FontHandlerclass EventsPropertyHandler(object):    def __init__(self):        self.handlers = {}        self.event_name = None        self.curr_handler = []            def start_elem(self, name, attrs):        if name == 'handler':            self.event_name = attrs['event']    def end_elem(self, name, code_obj):        if name == 'handler':            if self.event_name and self.curr_handler:                self.handlers[self.event_name] = ''.join(self.curr_handler)            self.event_name = None            self.curr_handler = []        elif name == 'events':            code_obj.properties['events'] = self.handlers            return True    def char_data(self, data):        data = data.strip()        if data:            self.curr_handler.append(data)# end of class EventsPropertyHandlerclass DummyPropertyHandler:    """Empty handler for properties that do not need code"""    def start_elem(self, name, attrs): pass    def end_elem(self, name, code_obj): return True    def char_data(self, data): pass# end of class DummyPropertyHandlerclass ExtraPropertiesPropertyHandler(object):    def __init__(self):        self.props = {}        self.prop_name = None        self.curr_prop = []            def start_elem(self, name, attrs):        if name == 'property':            self.prop_name = attrs['name']    def end_elem(self, name, code_obj):        if name == 'property':            if self.prop_name and self.curr_prop:                self.props[self.prop_name] = ''.join(self.curr_prop)            self.prop_name = None            self.curr_prop = []        elif name == 'extraproperties':            code_obj.properties['extraproperties'] = self.props            return True # to remove this handler    def char_data(self, data):        data = data.strip()        if data:            self.curr_prop.append(data)# end of class ExtraPropertiesPropertyHandler# dictionary whose items are custom handlers for widget properties_global_property_writers = { 'font': FontPropertyHandler,                             'events': EventsPropertyHandler,                             'extraproperties': ExtraPropertiesPropertyHandler,                             }# dictionary of dictionaries of property handlers specific for a widget# the keys are the class names of the widgets# Ex: _property_writers['wxRadioBox'] = {'choices', choices_handler}_property_writers = {}def get_property_handler(property_name, widget_name):    try: cls = _property_writers[widget_name][property_name]    except KeyError: cls = _global_property_writers.get(property_name, None)    if cls: return cls()    return Nonedef add_property_handler(property_name, handler, widget_name=None):    """\    sets a function to parse a portion of XML to get the value of the property    property_name. If widget_name is not None, the function is called only if    the property in inside a widget whose class is widget_name    """    if widget_name is None: _global_property_writers[property_name] = handler    else:        try: _property_writers[widget_name][property_name] = handler        except KeyError:            _property_writers[widget_name] = { property_name: handler }def add_widget_handler(widget_name, handler, *args, **kwds):    obj_builders[widget_name] = handler

⌨️ 快捷键说明

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