I think I have found the solution. I don't think it is possible to solve 
the problem just using decorators. The command_method decorator needs some 
help.
Here is a decorator:
def command_method(name):
    """A decorator that creates a Leo command from a method."""

    def _wrapper(f):
        """This wrapper is always called with one argument: the wrapped 
function."""
        clsname = f.__qualname__.partition('.')[0]
        @functools.wraps(f)
        def inner_wrapper(self, event=None):
            """Inner wrapper docstring."""
            getinst = globals().get(f'{clsname}_in_c')
            c = self.c
            instance = getinst(c)
            return f(instance, event)

        global_commands_dict[name] = inner_wrapper
        return inner_wrapper

    return _wrapper


It relies on g having a function 'Undoer_in_c' for the commands in leoUndo. 
For every class that uses this decorator, after the class definition it is 
necesssary to add the following line:
g.Undoer_in_c = lambda c:c.undoer
Other classes should add similar functions with their own names.
The inner_wrapper uses this function to get the instance for executing 
command.

I have pushed this code in commands2 branch and all unittests pass.

HTH Vitalije

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/b29e8574-8096-430c-a32f-871b4f6d1d32%40googlegroups.com.

Reply via email to