def cb(self): try: return self._cb except AttributeError: self._cb = import_function(self) return self._cb
def proto_init(proto_name): return import_function(protocols[proto_name].path)
def menu(parent, obj, menu=None, cls=None, search_bases=True, filter=lambda func: True): 'Builds or adds to an existing menu with actions for the specified object.' from gui.uberwidgets.umenu import UMenu actions = forclass(cls if cls is not None else obj, search_bases) menu = menu if menu is not None else UMenu(parent) # For each action defined in the YAML names = set() for action in actions: # # ---- means separator # if isinstance(action, basestring) and action.startswith('--'): menu.AddSep() continue # # method: a function to call # elif 'method' in action: introspect.import_function(action['method'])(menu, obj) continue name = action['call'] if name in names: continue else: names.add(name) # the actual function func = getattr(obj, name, None) if func is None: continue gui_name = action['name'] try: # read precondition lambda and "needs" from the @action annotations precondition, needslist = getattr(obj, '_actions')[name] except KeyError, _e: # ...if there is no @action annotating the method, just call it precondition, needslist = lambda v: True, [] if needslist: import gui.userform as userform callback = partial(userform.getinput, obj, parent, needslist, func, title=gui_name.replace('&', '')) elif 'gui' in action: gui = introspect.import_function(action['gui']) if not hasattr(gui, 'Prompt'): callback = lambda gui=gui, func=func: gui(obj, func) else: def callback(gui=gui, func=func): gui(None, obj).Prompt(func) else: callback = func # Preconditions # - None: Don't show menu item # - False: Show disabled menu item # - True: Show menu item result = precondition(obj) if precondition is not None else True if filter(func) and (precondition is None or result is not None): name = action_name(obj, gui_name) menu.AddItem(name, callback=callback).Enable(bool(result))
def menu(parent, obj, menu = None, cls = None, search_bases = True, filter = lambda func: True): 'Builds or adds to an existing menu with actions for the specified object.' from gui.uberwidgets.umenu import UMenu actions = forclass(cls if cls is not None else obj, search_bases) menu = menu if menu is not None else UMenu(parent) # For each action defined in the YAML names = set() for action in actions: # # ---- means separator # if isinstance(action, basestring) and action.startswith('--'): menu.AddSep() continue # # method: a function to call # elif 'method' in action: introspect.import_function(action['method'])(menu, obj) continue name = action['call'] if name in names: continue else: names.add(name) # the actual function func = getattr(obj, name, None) if func is None: continue gui_name = action['name'] try: # read precondition lambda and "needs" from the @action annotations precondition, needslist = getattr(obj, '_actions')[name] except KeyError, _e: # ...if there is no @action annotating the method, just call it precondition, needslist = lambda v: True, [] if needslist: import gui.userform as userform callback = partial(userform.getinput, obj, parent, needslist, func, title = gui_name.replace('&','')) elif 'gui' in action: gui = introspect.import_function(action['gui']) if not hasattr(gui, 'Prompt'): callback = lambda gui=gui, func=func: gui(obj, func) else: def callback(gui=gui, func=func): gui(None, obj).Prompt(func) else: callback = func # Preconditions # - None: Don't show menu item # - False: Show disabled menu item # - True: Show menu item result = precondition( obj ) if precondition is not None else True if filter(func) and (precondition is None or result is not None): name = action_name(obj, gui_name) menu.AddItem(name, callback = callback).Enable(bool(result))