py_codegen.py
来自「用python写的ide开发环境,巨强大,不过需要wxpython的支持」· Python 代码 · 共 1,410 行 · 第 1/4 页
PY
1,410 行
if not i: write(indentation + '%s.__init__(self, *args, **kwds)\n' % b) else: write(indentation + '%s.__init__(self)\n' % b) else: write(indentation + '%s.__init__(self, *args, **kwds)\n' % \ mycn(code_obj.base)) tab = indentation init_lines = classes[code_obj.klass].init # --- patch 2002-08-26 --------------------------------------------------- parents_init = classes[code_obj.klass].parents_init parents_init.reverse() for l in parents_init: write(tab+l) # ------------------------------------------------------------------------ for l in init_lines: write(tab + l) # now check if there are extra lines to add to the init method if hasattr(builder, 'get_init_code'): for l in builder.get_init_code(code_obj): write(tab + l) write('\n' + tab + 'self.__set_properties()\n') write(tab + 'self.__do_layout()\n') # ALB 2004-12-05 now let's write the "event table"... event_handlers = classes[code_obj.klass].event_handlers if hasattr(builder, 'get_events'): for id, event, handler in builder.get_events(code_obj): event_handlers.append((id, mycn(event), handler)) if event_handlers: write('\n') if for_version < (2, 5) or not use_new_namespace: for win_id, event, handler in event_handlers: if win_id.startswith('#'): win_id = win_id[1:] + '.GetId()' write(tab + '%s(self, %s, self.%s)\n' % \ (event, win_id, handler)) else: for win_id, event, handler in event_handlers: if win_id.startswith('#'): write(tab + 'self.Bind(%s, self.%s, %s)\n' % \ (event, handler, win_id[1:])) else: write(tab + 'self.Bind(%s, self.%s, id=%s)\n' % \ (event, handler, win_id)) # end tag write(tab + '# end wxGlade\n') if prev_src is not None and not is_new: # replace the lines inside the __init__ wxGlade block with the new ones tag = '<%swxGlade replace %s %s>' % (nonce, code_obj.klass, '__init__') if prev_src.content.find(tag) < 0: # no __init__ tag found, issue a warning and do nothing common.message("WARNING", "wxGlade __init__ block not found for %s," \ " __init__ code NOT generated" % code_obj.name) else: prev_src.content = prev_src.content.replace(tag, "".join(buffer)) buffer = [] write = buffer.append # __set_properties obj_p = getattr(builder, 'get_properties_code', generate_common_properties)(code_obj) obj_p.extend(classes[code_obj.klass].props) write_body = len(obj_p) if is_new: write('\n%sdef __set_properties(self):\n' % tabs(1)) # begin tag write(tab + '# begin wxGlade: %s.__set_properties\n' % \ without_package(code_obj.klass)) if not write_body: write(tab + 'pass\n') else: for l in obj_p: write(tab + l) # end tag write(tab + '# end wxGlade\n') if prev_src is not None and not is_new: # replace the lines inside the __set_properties wxGlade block # with the new ones tag = '<%swxGlade replace %s %s>' % (nonce, code_obj.klass, '__set_properties') if prev_src.content.find(tag) < 0: # no __set_properties tag found, issue a warning and do nothing common.message("WARNING", "wxGlade __set_properties block " \ "not found for %s, " "__set_properties code NOT generated" % code_obj.name) else: prev_src.content = prev_src.content.replace(tag, "".join(buffer)) buffer = [] write = buffer.append # __do_layout if is_new: write('\n' + tabs(1) + 'def __do_layout(self):\n') layout_lines = classes[code_obj.klass].layout sizers_init_lines = classes[code_obj.klass].sizers_init # check if there are extra layout lines to add if hasattr(builder, 'get_layout_code'): extra_layout_lines = builder.get_layout_code(code_obj) else: extra_layout_lines = [] # begin tag write(tab + '# begin wxGlade: %s.__do_layout\n' % \ without_package(code_obj.klass)) if layout_lines or sizers_init_lines or extra_layout_lines: sizers_init_lines.reverse() for l in sizers_init_lines: write(tab + l) for l in layout_lines: write(tab + l) #write(tab + 'self.Layout()\n') for l in extra_layout_lines: write(tab + l) else: write(tab + 'pass\n') # end tag write(tab + '# end wxGlade\n') if prev_src is not None and not is_new: # replace the lines inside the __do_layout wxGlade block # with the new ones tag = '<%swxGlade replace %s %s>' % (nonce, code_obj.klass, '__do_layout') if prev_src.content.find(tag) < 0: # no __do_layout tag found, issue a warning and do nothing common.message("WARNING", "wxGlade __do_layout block " \ "not found for %s, __do_layout code NOT generated" % code_obj.name) else: prev_src.content = prev_src.content.replace(tag, "".join(buffer)) # ALB 2004-12-05 now let's generate the event handler stubs... if prev_src is not None and not is_new: already_there = prev_src.event_handlers.get(code_obj.klass, {}) buf = [] for name, event, handler in event_handlers: if handler not in already_there: buf.append(tabs(1) + 'def %s(self, event): ' '# wxGlade: %s.<event_handler>\n' % (handler, without_package(code_obj.klass))) buf.append( tab + 'print "Event handler `%s\' not implemented"\n' % handler) buf.append(tab + 'event.Skip()\n\n') already_there[handler] = 1 tag = '<%swxGlade event_handlers %s>' % (nonce, code_obj.klass) if prev_src.content.find(tag) < 0: # no event_handlers tag found, issue a warning and do nothing common.message("WARNING", "wxGlade event_handlers block " \ "not found for %s," " event_handlers code NOT generated" % code_obj.name) else: prev_src.content = prev_src.content.replace(tag, "".join(buf)) del buf else: already_there = {} for name, event, handler in event_handlers: if handler not in already_there: write('\n' + tabs(1) + 'def %s(self, event): ' '# wxGlade: %s.<event_handler>\n' % (handler, without_package(code_obj.klass))) write(tab + 'print "Event handler `%s\' not implemented!"\n' % handler) write(tab + 'event.Skip()\n') already_there[handler] = 1 # the code has been generated classes[code_obj.klass].done = True write('\n# end of class %s\n\n\n' % without_package(code_obj.klass)) if not multiple_files and prev_src is not None: # if this is a new class, add its code to the new_classes list of the # SourceFileContent instance if is_new: prev_src.new_classes.append("".join(buffer)) return if multiple_files: if prev_src is not None: tag = '<%swxGlade insert new_classes>' % nonce prev_src.content = prev_src.content.replace(tag, "") #code) # insert the extra modules tag = '<%swxGlade extra_modules>\n' % nonce code = "".join(_current_extra_modules.keys()) prev_src.content = prev_src.content.replace(tag, code) # insert the module dependencies of this class extra_modules = classes[code_obj.klass].dependencies.keys() deps = ['# begin wxGlade: dependencies\n'] + extra_modules + \ ['# end wxGlade\n'] tag = '<%swxGlade replace dependencies>' % nonce prev_src.content = prev_src.content.replace(tag, "".join(deps)) # insert the extra code of this class extra_code = "".join(classes[code_obj.klass].extra_code[::-1]) # if there's extra code but we are not overwriting existing # sources, warn the user if extra_code: common.message('WARNING', '%s (or one of its chilren) has ' 'extra code classes, but you are ' 'not overwriting existing sources: please check ' 'that the resulting code is correct!' % \ code_obj.name) extra_code = '# begin wxGlade: extracode\n%s\n# end wxGlade\n' % \ extra_code## "".join(classes[code_obj.klass].extra_code[::-1]) tag = '<%swxGlade replace extracode>' % nonce prev_src.content = prev_src.content.replace(tag, extra_code) try: # store the new file contents to disk common.save_file(filename, prev_src.content, 'codegen') except: raise IOError("py_codegen.add_class: %s, %s, %s" % \ (out_dir, prev_src.name, code_obj.klass)) return # create the new source file filename = os.path.join(out_dir, code_obj.klass.replace('.', os.sep) + '.py') out = cStringIO.StringIO() write = out.write # write the common lines for line in header_lines: write(line) # write the module dependecies for this class write('\n# begin wxGlade: dependencies\n') for module in classes[code_obj.klass].dependencies: write(module) write('# end wxGlade\n') write('\n') # insert the extra code of this class extra_code = "".join(classes[code_obj.klass].extra_code[::-1]) extra_code = '# begin wxGlade: extracode\n%s\n# end wxGlade\n' % \ extra_code write(extra_code) write('\n') # write the class body for line in buffer: write(line) try: # store the contents to filename common.save_file(filename, out.getvalue(), 'codegen') except: import traceback; traceback.print_exc() out.close() else: # not multiple_files # write the class body onto the single source file for dep in classes[code_obj.klass].dependencies: _current_extra_modules[dep] = 1 if classes[code_obj.klass].extra_code: _current_extra_code.extend(classes[code_obj.klass].extra_code[::-1]) write = output_file.write for line in buffer: write(line) _app_added = Falsedef add_app(app_attrs, top_win_class): """\ Generates the code for a wxApp instance. If the file to write into already exists, this function does nothing. """ global _app_added _app_added = True name = app_attrs.get('name') if not name: name = 'app' if not multiple_files: prev_src = previous_source else: filename = os.path.join(out_dir, name + '.py') if not os.path.exists(filename): prev_src = None elif _overwrite: prev_src = None else: # prev_src doesn't need to be a SourceFileContent instance in this # case, as we do nothing if it is not None prev_src = 1 if prev_src is not None: return # do nothing if the file existed klass = app_attrs.get('class') top_win = app_attrs.get('top_window') if not top_win: return # do nothing if there is no top window lines = [] append = lines.append if klass: tab = tabs(2) append('class %s(%s):\n' % (klass, cn('wxApp'))) append(tabs(1) + 'def OnInit(self):\n') else: tab = tabs(1) append('if __name__ == "__main__":\n') if _use_gettext: append(tab + 'import gettext\n') append(tab + 'gettext.install("%s") # replace with the appropriate' ' catalog name\n\n' % name) append(tab + '%s = %s(0)\n' % (name, cn('wxPySimpleApp'))) append(tab + cn('wxInitAllImageHandlers') + '()\n') # to avoid troubles append(tab + '%s = %s(None, -1, "")\n' % (top_win, top_win_class)) if klass: append(tab + 'self.SetTopWindow(%s)\n' % top_win) append(tab + '%s.Show()\n' % top_win) append(tab + 'return 1\n\n') append('# end of class %s\n\n' % klass) append('if __name__ == "__main__":\n') tab = tabs(1) if _use_gettext: append(tab + 'import gettext\n') append(tab + 'gettext.install("%s") # replace with the appropriate' ' catalog name\n\n' % name) append(tab + '%s = %s(0)\n' % (name, klass)) else: append(tab + '%s.SetTopWindow(%s)\n' % (name, top_win)) append(tab + '%s.Show()\n' % top_win) append(tab + '%s.MainLoop()\n' % name) if multiple_files: filename = os.path.join(out_dir, name + '.py') out = cStringIO.StringIO() write = out.write write('#!/usr/bin/env python\n') # write the common lines for line in header_lines: write(line) # import the top window module write('from %s import %s\n\n' % (top_win_class, top_win_class)) # write the wxApp code for line in lines: write(line) try: common.save_file(filename, out.getvalue(), 'codegen') except: import traceback; traceback.print_exc() # make the file executable try: os.chmod(filename, 0755) except OSError: pass # this is not a bad error out.close() else: write = output_file.write for line in lines: write(line)def _get_code_name(obj): if obj.is_toplevel: return 'self' else: if test_attribute(obj): return 'self.%s' % obj.name else: return obj.namedef generate_code_size(obj): """\ returns the code fragment that sets the size of the given object. """ name = _get_code_name(obj)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?