make script execution directory @path sensitive?

15 views
Skip to first unread message

Terry Brown

unread,
Mar 18, 2011, 10:38:48 AM3/18/11
to leo-e...@googlegroups.com
Should os.chdir() be called to set the current directory to match any
@path directives in effect at the current node when a script is run or
a button pushed? Seems this would be useful.

Cheers -Terry

Edward K. Ream

unread,
Mar 19, 2011, 10:14:15 AM3/19/11
to leo-e...@googlegroups.com

There are several instances of os.chdir and g.chdir in c.open,
c.save, the read, import, export logic and in several global
functions. Thus, Leo does not typically depend on the current
directory--it sets the current directory as needed. The **tentative**
conclusion is that setting os.chdir automatically in the
execute-script would be safe.

However, changing the execute-script logic could change the meaning of
existing scripts. I don't think it is wise.

It should be easy enough to set the path explicitly in a script as
needed using::

import os
d = c.scanAllDirectives(p) # use c.p if p is omitted.
path = d.get('path')
if path: os.chdir(path)

However, c.scanDirectives returns a default if there is no @path, so
maybe this isn't so convenient, but that doesn't change my conclusion.

Edward

Terry Brown

unread,
Mar 19, 2011, 10:36:26 AM3/19/11
to leo-e...@googlegroups.com
On Sat, 19 Mar 2011 09:14:15 -0500
"Edward K. Ream" <edre...@gmail.com> wrote:

> On Fri, Mar 18, 2011 at 9:38 AM, Terry Brown <terry_...@yahoo.com> wrote:
> > Should os.chdir() be called to set the current directory to match any
> > @path directives in effect at the current node when a script is run or
> > a button pushed?  Seems this would be useful.
>
> There are several instances of os.chdir and g.chdir in c.open,
> c.save, the read, import, export logic and in several global
> functions. Thus, Leo does not typically depend on the current
> directory--it sets the current directory as needed.

As it must, scripts can and do change cwd, it's not a value you can
rely on beyond initial startup code.

> The **tentative**
> conclusion is that setting os.chdir automatically in the
> execute-script would be safe.
>
> However, changing the execute-script logic could change the meaning of
> existing scripts. I don't think it is wise.

But it would change cwd from an "undefined" to a defined, and often
useful, value. Scripts can't have been relying on any particular value
before, seeing there wasn't one.

> It should be easy enough to set the path explicitly in a script as
> needed using::
>
> import os
> d = c.scanAllDirectives(p) # use c.p if p is omitted.
> path = d.get('path')
> if path: os.chdir(path)
>
> However, c.scanDirectives returns a default if there is no @path, so
> maybe this isn't so convenient, but that doesn't change my conclusion.

I would have thought that was cleaner these days, although I have
trouble keeping track of path calculating methods...

g.es(c.getNodePath(c.topPosition()))
g.es(c.getNodePath(p))
g.es(g.computeLeoDir())
g.es(g.scanAllAtPathDirectives(c,p))
g.es(g.computeLoadDir())
g.es(g.getBaseDirectory(c))
g.es(c.scanAllDirectives(p)['path'])

c.getNodePath(p) seems most friendly?

Cheers -Terry

> Edward
>

Ville M. Vainio

unread,
Mar 19, 2011, 12:43:10 PM3/19/11
to leo-e...@googlegroups.com, Terry Brown

I have wished this quite often. As it stands, current working
directory is unspecified which is not desirable.

Edward K. Ream

unread,
Mar 20, 2011, 6:40:19 AM3/20/11
to leo-e...@googlegroups.com

It may be unspecified, but your proposal would change it :-)

However, you and Terry have convinced me that it is worth doing on an
experimental basis. It will be easy to revert if there are unforeseen
consequences.

I'll probably do this today...

Edward

Reply all
Reply to author
Forward
0 new messages