S
Steve Holden
Wouldn't this be better written as a context manager?Phlip said:One thing I really like about ... my hacked version of path.py ... is
path.cd( lambda: ... ). It works great inside fabfile.py to
temporarily switch to a different folder:
sample_project = path('sample_project').abspath()
def run():
sample_project.cd( lambda:
_sh('python manage.py runserver 0.0.0.0:8000 --
settings=test_settings') )
After the lambda runs, we exception-safely return to the home folder.
(BTW I'm aware that a fabfile.py command with only one statement will
return to its shell and remain in the correct folder. It's just ...
the thought!)
This be .cd():
class path:
def cd(self, block=None):
previous = path(os.path.curdir).abspath()
self.chdir()
if block:
try: block()
finally: previous.chdir()
That's based on Jason Orendoff's work at http://www.jorendorff.com/articles/python/path
Be aware also that this won't work well in a multi-threaded environment
(assuming os.path.chdir is ultimately used to change directories)
because it effects the process's (globaL current directory.
regards
Steve