There is no need to 'compute' the class, because the class is stored in the
decorator, wenn the command is created.
I post my code from the other thread here again.
> When Leo sees a command, it looks up the wrapper in the dict and simply
> calls the wrapper with an event arg. The *wrapper *must compute "self"
> if and only if the wrapper represents a wrapper of a method. Yes, this is
> complicated. The following is my best illustration of the problem that
> can't be solved.
>
> The code just knows the class. It is as easy as it can get:
commands_dict = {}
# Use as function/method decorator
def cmd(*command_names):
class Decorator:
def __init__(self, func):
self.func = func
try: # Python3
self.isMethod = '.' in func.__qualname__
except AttributeError: # Python2
self.isMethod = 'instance' in str(func)
for command_name in command_names:
commands_dict[command_name] = self.__call__
def __call__(self, *args, **kwargs):
if self.isMethod:
return self.func(args[0], *args, **kwargs)
else:
return self.func(*args, **kwargs)
return Decorator
@cmd('command-one', 'first-command')
def pureFunction(event=None):
"""pureFunction docstring"""
print("-- function:", event)
class MyClass:
@cmd('command-two', 'second-command', 'another-second-command')
def instanceMethod(self, event=None):
"""instanceMethod docstring"""
print("** method:", event)
def tests():
"""Calling functions/methods from the command_dict"""
for command in commands_dict:
commands_dict[command]("I'm command <%s>." % command)
tests()
Reinhard
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.