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

📄 py_codegen.py

📁 用python写的ide开发环境,巨强大,不过需要wxpython的支持
💻 PY
📖 第 1 页 / 共 4 页
字号:
    # ALB 2004-01-18    global use_new_namespace    try: use_new_namespace = int(app_attrs['use_new_namespace'])    except (KeyError, ValueError): pass # use the default value    # ALB 2004-12-05    global for_version    try:        for_version = tuple([int(t) for t in                             app_attrs['for_version'].split('.')[:2]])    except (KeyError, ValueError):        if common.app_tree is not None:            for_version = common.app_tree.app.for_version        else:            for_version = (2, 4) # default...    # add coding (PEP 263)    try: _encoding = app_attrs['encoding']    except (KeyError, ValueError): _encoding = None      classes = {}    _current_extra_modules = {}    header_lines = ['# generated by wxGlade %s on %s%s\n\n' % \                    (common.version, time.asctime(), common.generated_from()),                    use_new_namespace and 'import wx\n' or                    'from wxPython.wx import *\n']    if not config.preferences.write_timestamp:        header_lines[0] = '# generated by wxGlade %s%s\n\n' % \                          (common.version, common.generated_from())    # extra lines to generate (see the 'extracode' property of top-level    # widgets)    _current_extra_code = []    # add coding (PEP 263)    if _encoding:        header_lines.insert(0, "# -*- coding: %s -*-\n" % _encoding.lower())    multiple_files = multi_files    if not multiple_files:        global output_file, output_file_name        if not _overwrite and os.path.isfile(out_path):            # the file exists, we must keep all the lines not inside a wxGlade            # block. NOTE: this may cause troubles if out_path is not a valid            # python file, so be careful!            previous_source = SourceFileContent(out_path)        else:            # if the file doesn't exist, create it and write the ``intro''            previous_source = None            output_file = cStringIO.StringIO()            output_file_name = out_path            output_file.write('#!/usr/bin/env python\n')            for line in header_lines:                output_file.write(line)            output_file.write('<%swxGlade extra_modules>\n' % nonce)            output_file.write('\n<%swxGlade replace extracode>\n\n' % nonce)            output_file.write('\n')    else:        previous_source = None        global out_dir        if not os.path.isdir(out_path):            raise XmlParsingError("'path' must be a directory when generating"\                                  " multiple output files")        out_dir = out_pathdef finalize():    """\    Writer ``finalization'' function: flushes buffers, closes open files, ...    """    if previous_source is not None:        # insert all the new custom classes inside the old file        tag = '<%swxGlade insert new_classes>' % nonce        if previous_source.new_classes:            code = "".join(previous_source.new_classes)        else:            code = ""        previous_source.content = previous_source.content.replace(tag, code)        tag = '<%swxGlade extra_modules>\n' % nonce        code = "".join(_current_extra_modules.keys())        previous_source.content = previous_source.content.replace(tag, code)        # extra code (see the 'extracode' property of top-level widgets)        tag = '<%swxGlade replace extracode>' % nonce        code = "\n".join(['# begin wxGlade: extracode'] + _current_extra_code +                         ['# end wxGlade\n'])        previous_source.content = previous_source.content.replace(tag, code)                # now remove all the remaining <123415wxGlade ...> tags from the        # source: this may happen if we're not generating multiple files,        # and one of the container class names is changed        tags = re.findall('(<%swxGlade replace ([a-zA-Z_]\w*) +[.\w]+>)' % \                          nonce,                          previous_source.content)        for tag in tags:            indent = previous_source.spaces.get(tag[1], tabs(2))            comment = '%s# content of this block not found: ' \                      'did you rename this class?\n%spass\n' % (indent, indent)            previous_source.content = previous_source.content.replace(tag[0],                                                                      comment)        # ALB 2004-12-05        tags = re.findall('<%swxGlade event_handlers \w+>' % nonce,                          previous_source.content)        for tag in tags:            previous_source.content = previous_source.content.replace(tag, "")        # write the new file contents to disk        common.save_file(previous_source.name, previous_source.content,                         'codegen')            elif not multiple_files:        global output_file        em = "".join(_current_extra_modules.keys())        content = output_file.getvalue().replace(            '<%swxGlade extra_modules>\n' % nonce, em)        # extra code (see the 'extracode' property of top-level widgets)        tag = '<%swxGlade replace extracode>' % nonce        code = "\n".join(['# begin wxGlade: extracode'] + _current_extra_code +                         ['# end wxGlade\n'])        content = content.replace(tag, code)                output_file.close()        try:            common.save_file(output_file_name, content, 'codegen')            # make the file executable            if _app_added:                os.chmod(output_file_name, 0755)        except IOError, e:            raise XmlParsingError(str(e))        except OSError: pass # this isn't necessary a bad error        del output_filedef test_attribute(obj):    """\    Returns True if 'obj' should be added as an attribute of its parent's    class, False if it should be created as a local variable of __do_layout.    To do so, tests for the presence of the special property 'attribute'    """    try: return int(obj.properties['attribute'])    except (KeyError, ValueError): return True # this is the defaultdef add_object(top_obj, sub_obj):    """\    adds the code to build 'sub_obj' to the class body of 'top_obj'.    """    try: klass = classes[top_obj.klass]    except KeyError: klass = classes[top_obj.klass] = ClassLines()    try: builder = obj_builders[sub_obj.base]    except KeyError:        # no code generator found: write a comment about it        klass.init.extend(['\n', '# code for %s (type %s) not generated: '                           'no suitable writer found' % (sub_obj.name,                                                         sub_obj.klass),'\n'])        common.message('WARNING', 'code for %s (type %s) not generated: '                       'no suitable writer found',                       sub_obj.name, sub_obj.klass)    else:        try:            init, props, layout = builder.get_code(sub_obj)        except:            print sub_obj            raise # this shouldn't happen        if sub_obj.in_windows: # the object is a wxWindow instance            # --- patch 2002-08-26 ------------------------------------------            if sub_obj.is_container and not sub_obj.is_toplevel:                init.reverse()                klass.parents_init.extend(init)            else: klass.init.extend(init)            # ---------------------------------------------------------------            # ALB 2004-12-05            mycn = getattr(builder, 'cn', cn)            if hasattr(builder, 'get_events'):                evts = builder.get_events(sub_obj)                for id, event, handler in evts:                    klass.event_handlers.append((id, mycn(event), handler))            elif 'events' in sub_obj.properties:                id_name, id = generate_code_id(sub_obj)                #if id == '-1': id = 'self.%s.GetId()' % sub_obj.name                if id == '-1': id = '#self.%s' % sub_obj.name                for event, handler in sub_obj.properties['events'].iteritems():                    klass.event_handlers.append((id, mycn(event), handler))            # try to see if there's some extra code to add to this class            if not sub_obj.preview:                extra_code = getattr(builder, 'extracode',                                     sub_obj.properties.get('extracode', ""))                if extra_code:                    extra_code = re.sub(r'\\n', '\n', extra_code)                    klass.extra_code.append(extra_code)                    # if we are not overwriting existing source, warn the user                    # about the presence of extra code                    if multiple_files:                        warn = False                    else:                        warn = previous_source is not None                    if warn:                        common.message(                            'WARNING',                            '%s has extra code, but you are '                            'not overwriting existing sources: please check '                            'that the resulting code is correct!' % \                            sub_obj.name)        else: # the object is a sizer            # ALB 2004-09-17: workaround (hack) for static box sizers...            if sub_obj.base == 'wxStaticBoxSizer':                klass.parents_init.insert(1, init.pop(0))            klass.sizers_init.extend(init)        klass.props.extend(props)        klass.layout.extend(layout)        if multiple_files and \               (sub_obj.is_toplevel and sub_obj.base != sub_obj.klass):            key = 'from %s import %s\n' % (sub_obj.klass,                                           without_package(sub_obj.klass))            klass.dependencies[key] = 1##         for dep in _widget_extra_modules.get(sub_obj.base, []):        for dep in getattr(obj_builders.get(sub_obj.base),                           'import_modules', []):            klass.dependencies[dep] = 1def add_sizeritem(toplevel, sizer, obj, option, flag, border):    """\    writes the code to add the object 'obj' to the sizer 'sizer'    in the 'toplevel' object.    """    # an ugly hack to allow the addition of spacers: if obj_name can be parsed    # as a couple of integers, it is the size of the spacer to add    obj_name = obj.name    try: w, h = [ int(s) for s in obj_name.split(',') ]    except ValueError:        if obj.in_windows:            # attribute is a special property, which tells us if the object            # is a local variable or an attribute of its parent            if test_attribute(obj): obj_name = 'self.' + obj_name        if obj.base == 'wxNotebook' and for_version < (2, 5):            obj_name = cn('wxNotebookSizer') + '(%s)' % obj_name    else:        obj_name = '(%d, %d)' % (w, h) # it was the dimension of a spacer    try: klass = classes[toplevel.klass]    except KeyError: klass = classes[toplevel.klass] = ClassLines()    buffer = '%s.Add(%s, %s, %s, %s)\n' % \             (sizer.name, obj_name, option, cn_f(flag), cn_f(border))    klass.layout.append(buffer)def without_package(class_name):    """\    Removes the package name from the given class name    """    return class_name.split('.')[-1]def add_class(code_obj):    """\    Generates the code for a custom class.    """    global _current_extra_modules    if not multiple_files:        # in this case, previous_source is the SourceFileContent instance        # that keeps info about the single file to generate        prev_src = previous_source    else:        # let's see if the file to generate exists, and in this case        # create a SourceFileContent instance        filename = os.path.join(out_dir,                                code_obj.klass.replace('.', os.sep) + '.py')        if _overwrite or not os.path.exists(filename): prev_src = None        else: prev_src = SourceFileContent(filename)        _current_extra_modules = {}        if classes.has_key(code_obj.klass) and classes[code_obj.klass].done:        return # the code has already been generated    try:        builder = obj_builders[code_obj.base]        mycn = getattr(builder, 'cn', cn)        mycn_f = getattr(builder, 'cn_f', cn_f)    except KeyError:        raise # this is an error, let the exception be raised    if prev_src is not None and prev_src.classes.has_key(code_obj.klass):        is_new = False        indentation = prev_src.spaces[code_obj.klass]    else:        # this class wasn't in the previous version of the source (if any)        is_new = True        indentation = tabs(2)        mods = getattr(builder, 'extra_modules', [])        if mods:            for m in mods: _current_extra_modules[m] = 1    buffer = []    write = buffer.append    if not classes.has_key(code_obj.klass):        # if the class body was empty, create an empty ClassLines        classes[code_obj.klass] = ClassLines()    # try to see if there's some extra code to add to this class    if not code_obj.preview:        extra_code = getattr(builder, 'extracode',                             code_obj.properties.get('extracode', ""))        if extra_code:            extra_code = re.sub(r'\\n', '\n', extra_code)            classes[code_obj.klass].extra_code.append(extra_code)            if not is_new:                common.message('WARNING', '%s has extra code, but you are '                               'not overwriting existing sources: please check '                               'that the resulting code is correct!' % \                               code_obj.name)        if not multiple_files and extra_code:            _current_extra_code.append("".join(                classes[code_obj.klass].extra_code[::-1]))    # ALB 2007-08-31 custom base classes support    custom_base = getattr(code_obj, 'custom_base',                          code_obj.properties.get('custom_base', None))    if code_obj.preview or (custom_base and not custom_base.strip()):        custom_base = None            if is_new:        base = mycn(code_obj.base)        if custom_base is not None:            base = ", ".join([b.strip() for b in custom_base.split(',')])        if code_obj.preview and code_obj.klass == base:            import random            klass = code_obj.klass + ('_%d' % random.randrange(10**8, 10**9))        else:            klass = code_obj.klass        write('class %s(%s):\n' % (without_package(klass), base))        write(tabs(1) + 'def __init__(self, *args, **kwds):\n')    elif custom_base is not None:        # custom base classes set, but "overwrite existing sources" not        # set. Issue a warning about this        common.message('WARNING', '%s has custom base classes, but you are '                       'not overwriting existing sources: please check that '                       'the resulting code is correct!' % code_obj.name)                           # __init__ begin tag    write(indentation + '# begin wxGlade: %s.__init__\n' % \          without_package(code_obj.klass))    prop = code_obj.properties    style = prop.get("style", None)    if style: write(indentation + 'kwds["style"] = %s\n' % mycn_f(style))    # __init__    if custom_base is not None:        bases = [b.strip() for b in custom_base.split(',')]        for i, b in enumerate(bases):

⌨️ 快捷键说明

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