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('\\', '/')


tbp1...@gmail.com

On Friday, April 9, 2021 at 10:11:49 PM UTC-4 tbp1...@gmail.com 
<https://groups.google.com/> wrote:
It looks like there is a simple fix.  This hasn't had hardly any testing, 
but on the surface it's doing the right thing.  The little example that 
failed in an earlier post created the right directories.

In *leoCommands.py*, in the method *expand_path_expression(self, s)*, add 
the line marked below

    val = ''.join(aList)
    *val = os.path.expanduser(val)  # <===== Add this line*
    if g.isWindows:
        val = val.replace('\\', '/')
    return val

-- 
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 leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/edad3311-dad2-4317-877c-f90d71339da1n%40googlegroups.com.

Reply via email to