📄 lisp_codegen.py
字号:
# __init__ # write(indentation + '%s.__init__(obj, *args, **kwds)\n' % \ # mycn(code_obj.base)) 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) # 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(obj %s obj.%s)\n' % \ (event, win_id, handler)) else: for win_id, event, handler in event_handlers: if win_id.startswith('#'): write(tab + "(wxEvtHandler_Connect (slot-top-window obj) %s (exp%s)" "\n\t\t(wxClosure_Create #'%s obj))\n" % (win_id[1:],event, handler, )) else: write(tab + "(wxEvtHandler_Connect (slot-top-window obj) %s (exp%s)" "\n\t\t(wxClosure_Create #'%s obj))\n" % (win_id,event, handler, )) # end tag write(tab + ')\n') 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 print >> sys.stderr, "WARNING: wxGlade __init__ block not found," \ " __init__ code NOT generated" else: prev_src.content = prev_src.content.replace(tag, "".join(buffer)) buffer = [] write = buffer.append # __set_properties## props_builder = obj_properties.get(code_obj.base)## write_body = len(classes[code_obj.klass].props)## if props_builder:## obj_p = obj_properties[code_obj.base](code_obj)## if not write_body: write_body = len(obj_p)## else: obj_p = [] 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(defmethod set-properties ((obj %s))\n' % klass) # begin tag write(tab + ';;;begin wxGlade: %s.__set_properties\n' % code_obj.klass) if not write_body: write(tab + 'pass\n') else: for l in obj_p: write(tab + l) # end tag write(tab + ')\n;;;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 print >> sys.stderr, "WARNING: wxGlade __set_properties block " \ "not found, __set_properties code NOT generated" else: prev_src.content = prev_src.content.replace(tag, "".join(buffer)) buffer = [] write = buffer.append # __do_layout if is_new: write('\n' + '(defmethod do-layout ((obj %s))\n' % klass) 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' % 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 + ')\n;;;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 print >> sys.stderr, "WARNING: wxGlade __do_layout block " \ "not found, __do_layout code NOT generated" 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('(defun %s (function data event) ' ';;;wxGlade: %s.<event_handler>\n' % (handler, code_obj.klass)) buf.append(tab + '(print "Event handler `%s\' not implemented")\n' % handler) buf.append(tab + '(when event\n') buf.append(tab + '(wxEvent:wxEvent_Skip event)))\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 print >> sys.stderr, "WARNING: wxGlade event_handlers block " \ "not found, event_handlers code NOT generated" 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' + '(defun %s (function data event) ' ';;;wxGlade: %s.<event_handler>\n' % (handler, code_obj.klass)) write(tab + '(print "Event handler `%s\' not implemented!")\n' % handler) write(tab + '(when event\n') write(tab + tab + '(wxEvent:wxEvent_Skip event)))\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' % 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)) 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 + '.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') # 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 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 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('(defun init-func (fun data evt)\n') else: tab = tabs(1) append('(defun init-func (fun data evt)\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'))) top_win = top_win.replace('_','-')# top_win_class = top_win_class.replace('_','-') append(tab + '(let ((%s (make-%s)))\n' % (top_win, top_win_class)) if klass: append(tab + '(ELJApp_SetTopWindow (slot-top-window %s))\n' % top_win) append(tab + '(wxWindow_Show (slot-top-window %s))))\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 + '(ELJApp_SetTopWindow (slot-top-window %s))\n' % top_win) append(tab + '(wxWindow_Show (slot-top-window %s))))\n' % top_win) append("\n(unwind-protect\n\t(Eljapp_initializeC (wxclosure_Create #'init-func nil) 0 nil)") append("\n (ffi:close-foreign-library \"../miscellaneous/wxc-msw2.6.2.dll\"))\n") if multiple_files: filename = os.path.join(out_dir, name + '.py') out = cStringIO.StringIO() write = out.write write('#!/usr/bin/env lisp\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 '(slot-top-window obj)' else: if test_attribute(obj): return '(slot-%s obj)' % 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) size = obj.properties.get('size', '').strip() use_dialog_units = (size[-1] == 'd') if for_version < (2, 5) or obj.parent is None: method = 'wxWindow_SetSize' else: method = 'SetMinSize' if use_dialog_units: return "("+ method + ' '+name +'(' + cn('wxDLG_SZE') + \ '(%s (%s)))\n' % (name, size[:-1]) else: return name + '.' + method + '((%s))\n' % size
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -