Forget the decorator!

I'm not sure what you try to accomplish.
Your publicCommands tables in Leo already associate command_names with 
functions or methods, so you don't need  a decorator for that. As you told 
Terry (somewhere above) you need to retrieve the information if you need a 
'self' argument or not (in other words: if you need to call a method or a 
function). But for that you only need a simple test. Here is a solution:


# No-decorator solution

def pureFunction(event=None):
    """pureFunction docstring"""
    print("-- function:", event)

class MyClass:

    def instanceMethod(self, event=None):
        """instanceMethod docstring"""
        print("** method:", event)

# You already have created these tables manually in Leo.
# So you don't need a decorator to do that.
publicCommands = {
    'first-command': pureFunction,
    'second-command': MyClass.instanceMethod,
}

# And so you only need to test 
# if you need to call a method or a function.
# No need for a decorator either.
def tests():
    for command in publicCommands:
        func = publicCommands[command]
        print(command, func, str(func))
        if '.' in str(func):
            print('Command <%s> is a method.' % command)
            # Calling your method
            func('self', 'anEvent')
        else:
            print('Command <%s> is a function.' % command)
            # Calling your function
            func("anEvent")

tests()


Decorators are not meant to be called as functions. They must appear directly 
above function/method (or class) that is to be enhanced or modified. They are 
not meant to modify functions/methods whose names are retrieved from elsewhere 
(i.e. tables).


So if you wanted to apply decorators i.e. to your help-methods, 

you would put the decorator immediately before the function definition:


@cmd('help-for-help')
def helpForHelp(self, ...):
    return "Some Help"


This way, you don't have to manually construct (and maintain!) separate command 
tables. If you still need such a dispatch table it could be created 
automatically by the decorator. 


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.

Reply via email to