Unfortunately, there is a fly in the ointment. I have an external file at a location that uses "..", like this:
- @path d:/Tom/devel/leo/plugins
-@file ../plugins/viewrendered3.py
Leo wants this @file path to contain the ../plugins path step. With the expanduser() line, my file doesn't save. I tried adding an os.path.abspath() operation, but then a lot of other files don't load and Leo operation are quite messed up. Apparently certain calls expect relative paths to be returned. So it's a mess, so far as creating correct directory @paths that contain "~" is concerned.
And without adding the
os.path.expanduser() call, Leo doesn't make the correct directories when the @path subtree starts with "~". So this is not such an easy thing to fix properly.
I have come up with completely different code for opening a directory that properly expands "~". Of course, it won't cause the correct directories to be made, but it does return the correct directory path, including "~", and does not rely on ".". It won't work right if an @path directory contains ".." but it does work right if the @file headline contains ".." as in my example above.
"""Return the full path on disk to an external file or directory.
Honor all @path directives. Expand "~" in path to be the user's
home directory. If get_file is False, return only the directory's
path (less any filename).
"""
import os
steps = [d.get('path') for d in g.get_directives_dict_list(p) if 'path' in d]
steps.reverse()
# Comment in these four lines if you also want to include the file itself in the path.
#pth = g.fullPath(c, p)
#fn = os.path.basename(pth)
#file name if there is a file here.
#if fn:
#steps.append(fn)
if len(steps) > 1:
pth = os.path.join(steps[0], *steps[1:])
elif len(steps) == 1:
pth = steps[0]
else:
pth = os.getcwd()
pth = os.path.expanduser(pth)
if g.isWindows:
pth = pth.replace('/', '\\')
else:
pth = pth.replace('\\', '/')