ENB: How to eat the elephant: doing #325 in nibbles

19 views
Skip to first unread message

Edward K. Ream

unread,
May 2, 2020, 11:28:44 AM5/2/20
to leo-editor
#325 recommends replacing per-file @cmd decorators with the global @g.command decorator.

There are over 300 commands to be munged, so it will pay to create helper scripts. This will save my body and avoid typos.

The first script clones all @file nodes containing a definition of an @cmd decorator.

The second script prints, for one particular @file node, what the proposed new code would be like. Eventually, it will actually change p.b. For example, given this node in leoApp.py:

@cmd('exit-leo')
@cmd('quit-leo')
def onQuit(self, event=None):
   
"""Exit Leo, prompting to save unsaved outlines first."""
   
### Rest of onQuit.

The printed output is:

----- app.onQuit
@g.command (exit-leo)
def exit_leo(event):
   
self.onQuit(event)

def onQuit(self, event=None):
   
"""Exit Leo, prompting to save unsaved outlines first."""
   
if 'shutdown' in g.app.debug:
        g
.trace()
   
### Rest of onQuit.

This kind of script is much more flexible, and more reliable, than Leo abbreviations or emacs-like macros could be.

As I write this, I see that more work is needed:

1. exit_leo does not follow the proper pattern (and "self" is undefined):

@g.command('command-name')
def a_Command(event):
    c
= event.get('c')
   
if not c:
       
return
   
### get the instance of the proper class, usually from c.

2. The script might copy docstrings to the newly-created functions.

3. Oops, single quotes should surround the command name.

These problems mirror possible manual editing mistakes. Much easier to correct the script than to fix by-hand typos.

Edward
Reply all
Reply to author
Forward
0 new messages