*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')
On Friday, April 30, 2021 at 8:45:55 AM UTC-4 Edward K. Ream wrote:
> This branch contains work on #1914,
> <https://github.com/leo-editor/leo-editor/issues/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
>
--
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/b52a8b3f-aec6-4d97-b883-81b67606e9d6n%40googlegroups.com.