py_codegen.py

来自「用python写的ide开发环境,巨强大,不过需要wxpython的支持」· Python 代码 · 共 1,410 行 · 第 1/4 页

PY
1,410
字号
    size = obj.properties.get('size', '').strip()    use_dialog_units = (size[-1] == 'd')    if for_version < (2, 5) or obj.parent is None:        method = 'SetSize'    else:        method = 'SetMinSize'    if use_dialog_units:        return name + '.' + method + '(' + cn('wxDLG_SZE') + \               '(%s, (%s)))\n' % (name, size[:-1])    else:        return name + '.' + method + '((%s))\n' % sizedef _string_to_colour(s):    return '%d, %d, %d' % (int(s[1:3], 16), int(s[3:5], 16), int(s[5:], 16))def generate_code_foreground(obj):     """\    returns the code fragment that sets the foreground colour of    the given object.    """    self = _get_code_name(obj)    try:        color = cn('wxColour') + '(%s)' % \                _string_to_colour(obj.properties['foreground'])    except (IndexError, ValueError): # the color is from system settings        color = cn('wxSystemSettings_GetColour') + '(%s)' % \                cn(obj.properties['foreground'])    return self + '.SetForegroundColour(%s)\n' % colordef generate_code_background(obj):    """\    returns the code fragment that sets the background colour of    the given object.    """    self = _get_code_name(obj)    try:        color = cn('wxColour') + '(%s)' % \                _string_to_colour(obj.properties['background'])    except (IndexError, ValueError): # the color is from system settings        color = cn('wxSystemSettings_GetColour') + '(%s)' % \                cn(obj.properties['background'])    return self + '.SetBackgroundColour(%s)\n' % colordef generate_code_font(obj):    """\    returns the code fragment that sets the font of the given object.    """    font = obj.properties['font']     size = font['size']    family = cn(font['family'])    underlined = font['underlined']    style = cn(font['style'])    weight = cn(font['weight'])    face = '"%s"' % font['face'].replace('"', r'\"')    self = _get_code_name(obj)    return self + '.SetFont(' + cn('wxFont') + '(%s, %s, %s, %s, %s, %s))\n' %\            (size, family, style, weight, underlined, face)def generate_code_id(obj, id=None):    """\    returns a 2-tuple of strings representing the LOC that sets the id of the    given object: the first line is the declaration of the variable, and is    empty if the object's id is a constant, and the second line is the value    of the id    """    if obj and obj.preview:        return '', '-1' # never generate ids for preview code    if id is None:        id = obj.properties.get('id')    if id is None: return '', '-1'    tokens = id.split('=')    if len(tokens) > 1: name, val = tokens[:2]    else: return '', tokens[0] # we assume name is declared elsewhere    if not name: return '', val    if val.strip() == '?':        val = cn('wxNewId()')    # check to see if we have to make the var global or not...    name = name.strip()    val = val.strip()    if '.' in name: return ('%s = %s\n' % (name, val), name)    return ('global %s; %s = %s\n' % (name, name, val), name)def generate_code_tooltip(obj):    """\    returns the code fragment that sets the tooltip of the given object.    """    self = _get_code_name(obj)    return self + '.SetToolTipString(%s)\n' % \           quote_str(obj.properties['tooltip'])def generate_code_disabled(obj):    self = _get_code_name(obj)    try: disabled = int(obj.properties['disabled'])    except: disabled = False    if disabled:        return self + '.Enable(False)\n'def generate_code_focused(obj):    self = _get_code_name(obj)    try: focused = int(obj.properties['focused'])    except: focused = False    if focused:        return self + '.SetFocus()\n'def generate_code_hidden(obj):    self = _get_code_name(obj)    try: hidden = int(obj.properties['hidden'])    except: hidden = False    if hidden:        return self + '.Hide()\n'def generate_code_extraproperties(obj):    self = _get_code_name(obj)    prop = obj.properties['extraproperties']    ret = []    for name in sorted(prop):        ret.append(self + '.Set%s(%s)\n' % (name, prop[name]))    return retdef generate_common_properties(widget):    """\    generates the code for various properties common to all widgets (background    and foreground colors, font, ...)    Returns a list of strings containing the generated code    """    prop = widget.properties    out = []    if prop.get('size', '').strip(): out.append(generate_code_size(widget))    if prop.get('background'): out.append(generate_code_background(widget))    if prop.get('foreground'): out.append(generate_code_foreground(widget))    if prop.get('font'): out.append(generate_code_font(widget))    # tooltip    if prop.get('tooltip'): out.append(generate_code_tooltip(widget))    # trivial boolean properties    if prop.get('disabled'): out.append(generate_code_disabled(widget))    if prop.get('focused'): out.append(generate_code_focused(widget))    if prop.get('hidden'): out.append(generate_code_hidden(widget))    # ALB 2007-09-01 extra properties    if prop.get('extraproperties') and not widget.preview:        out.extend(generate_code_extraproperties(widget))    return out# custom property handlersclass FontPropertyHandler:    """Handler for font properties"""    font_families = { 'default': 'wxDEFAULT',                      'decorative': 'wxDECORATIVE',                      'roman': 'wxROMAN', 'swiss': 'wxSWISS',                      'script': 'wxSCRIPT', 'modern': 'wxMODERN',                      'teletype': 'wxTELETYPE' }    font_styles = { 'normal': 'wxNORMAL', 'slant': 'wxSLANT',                    'italic': 'wxITALIC' }    font_weights = { 'normal': 'wxNORMAL', 'light': 'wxLIGHT',                     'bold': 'wxBOLD' }    def __init__(self):        self.dicts = { 'family': self.font_families, 'style': self.font_styles,                       'weight': self.font_weights }        self.attrs = { 'size': '0', 'style': '0', 'weight': '0', 'family': '0',                       'underlined': '0', 'face': '' }        self.current = None         self.curr_data = []            def start_elem(self, name, attrs):        self.curr_data = []        if name != 'font' and name in self.attrs:            self.current = name        else: self.current = None                def end_elem(self, name, code_obj):        if name == 'font':            code_obj.properties['font'] = self.attrs            return True        elif self.current is not None:            decode = self.dicts.get(self.current)            if decode: val = decode.get("".join(self.curr_data), '0')            else: val = "".join(self.curr_data)            self.attrs[self.current] = val            def char_data(self, data):        self.curr_data.append(data)# end of class FontPropertyHandlerclass 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 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 ExtraPropertiesPropertyHandler(object):    def __init__(self):        self.props = {}        self.prop_name = None        self.curr_prop = []            def start_elem(self, name, attrs):        if name == 'property':            name = attrs['name']            if name and name[0].islower():                name = name[0].upper() + name[1:]            self.prop_name = 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 = {}# map of widget class names to a list of extra modules needed for the# widget. Example: 'wxGrid': 'from wxPython.grid import *\n'_widget_extra_modules = {}# set of lines of extra modules to add to the current file_current_extra_modules = {} 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 }class WidgetHandler:    """\    Interface the various code generators for the widgets must implement    """        """list of modules to import (eg. ['from wxPython.grid import *\n'])"""    import_modules = []    def get_code(self, obj):        """\        Handler for normal widgets (non-toplevel): returns 3 lists of strings,        init, properties and layout, that contain the code for the        corresponding methods of the class to generate        """        return [], [], []    def get_properties_code(self, obj):        """\        Handler for the code of the set_properties method of toplevel objects.        Returns a list of strings containing the code to generate        """        return []    def get_init_code(self, obj):        """\        Handler for the code of the constructor of toplevel objects.  Returns a        list of strings containing the code to generate.  Usually the default        implementation is ok (i.e. there are no extra lines to add). The        generated lines are appended at the end of the constructor        """        return []    def get_layout_code(self, obj):        """\        Handler for the code of the do_layout method of toplevel objects.        Returns a list of strings containing the code to generate.        Usually the default implementation is ok (i.e. there are no        extra lines to add)        """        return []# end of class WidgetHandlerdef add_widget_handler(widget_name, handler):    obj_builders[widget_name] = handler

⌨️ 快捷键说明

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