Please test the ekr-fullPath branch

49 views
Skip to first unread message

Edward K. Ream

unread,
Apr 30, 2021, 8:45:55 AM4/30/21
to leo-editor
This branch contains work on #1914, a simplification of Leo's path-related logic based on g.fullPath.

All tests pass, but this is tricky code. Please report any problems immediately.

Edward

tbp1...@gmail.com

unread,
Apr 30, 2021, 4:25:58 PM4/30/21
to leo-editor
g.fullPath is a really good method. I could wish that it would return a directory instead of None when the path is to a directory instead of a file, but I suppose that would break some things that use it.  It could have a new returnDir=False argument, so that nothing downstream get broken.

I wrote a more complete script than I had used before to open a directory manager window on a node's directory, and g.fullPath did half the work for me:

@language python
"""Open directory for a node, respecting any @path directives and "~"."""

import os
from subprocess import run

pth = g.fullPath(c, p)
if pth:
    direc, _ = os.path.split(pth)
else:
    # not a file, so construct directory's path from @path directives
    steps = [d.get('path') for d in g.get_directives_dict_list(p) if 'path' in d]
    if steps:
        steps.reverse()
        if len(steps) > 1:
            direc = os.path.join(steps[0], *steps[1:])
        else:
            direc = steps[0]

        # Expand Leo-specific "{}" expressions in path
        direc = c.expand_path_expression(direc)

        # Expand "~" to user's home directory, even on Windows
        direc = os.path.expanduser(direc)

        if not os.path.isabs(direc):
            # Path is relative to the outline's path
            outline_dir, _ = g.os_path_split(c.fileName())
            direc = g.os_path_join(outline_dir, direc)

    else:
        # No @path directives, directory is the outline's directory
        direc, _ = g.os_path_split(c.fileName())

direc = os.path.normpath(direc)

if g.os_path_exists(direc):
    term = 'explorer.exe' if g.isWindows else 'xdg-open'
    run([term, direc])
else:
    g.es(f'Path {direc} doesn't exist')

Edward K. Ream

unread,
Apr 30, 2021, 6:54:17 PM4/30/21
to leo-editor
On Fri, Apr 30, 2021 at 3:26 PM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
g.fullPath is a really good method.

Indeed. The latest rev (4726839) eliminates several faux-helper methods and several kwargs. I am going to continue eliminating kwargs in leoAtFile.py.

I could wish that it would return a directory instead of None when the path is to a directory instead of a file, but I suppose that would break some things that use it.  It could have a new returnDir=False argument, so that nothing downstream get broken.

The test for p.isAnyAtFileNode is an integral part of g.fullPath. Changing the meaning of g.fullPath with a kwarg would not be wise.

If you don't care whether there is an @<file node> in p or p's ancestors, you could do something like this:

aList = g.get_directives_dict_list(p)
path = c.scanAtPathDirectives(aList)

c.scanAtPathDirective handles many details like expanding user expressions and "~". This might be simpler than your script.

Edward

tbp1...@gmail.com

unread,
Apr 30, 2021, 11:30:31 PM4/30/21
to leo-editor
On Friday, April 30, 2021 at 6:54:17 PM UTC-4 Edward K. Ream wrote:
On Fri, Apr 30, 2021 at 3:26 PM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
g.fullPath is a really good method.

If you don't care whether there is an @<file node> in p or p's ancestors, you could do something like this:

aList = g.get_directives_dict_list(p)
path = c.scanAtPathDirectives(aList)
 
c.scanAtPathDirective handles many details like expanding user expressions and "~". This might be simpler than your script.

Actually, g.scanAllAtPathDirectives(c, p) might be the best of all the variations for this usage.

When I replaced that part of my code with direc = g.scanAllAtPathDirectives(c, p)
I got the right behavior for all my test cases.  They don't include and {..} expressions, but those get expanded by the method, so they ought to be OK.

None of this used your new ekr-fullPath branch, though. 
Reply all
Reply to author
Forward
0 new messages