cliutils 0.1.1 released: Working directory decorator factory

Came up with a handy decorator factory: @indir.  Say you want a shell script to execute in a different working directory—for example, you want to do complex config file manipulation, or batch operations on a lot of files, or you just don't want to pass absolute paths all the time. Just decorate the function in question with @indir(workdir), where workdir is the directory to which you want to change:

>>> import tempfile
>>> realpath = os.path.realpath
>>> new, cur = map(realpath, (tempfile.mkdtemp(), os.curdir))
>>> @indir(new)
... def whereami():
...     return realpath(os.curdir)
>> whereami() == new
>>> realpath(os.curdir) == cur

Notice that it'll change back to the original directory upon the completion of the function.

Download from Google Code or pypi.  Upgrade or install anew with easy_install -U cliutils. API docs are here.


  • Anonymous  
    September 27, 2008 at 5:32 PM

    This is a useful set of utilities. However, I think that the log_decorator is misnamed. It has nothing to do with the logging module. It is really just temporarily redirecting stdout to some other file-like object, and logging.Loggers are not file-like objects. It should be called a redirect_decorator, and @redirect.

  • Ian McCracken  
    September 27, 2008 at 7:54 PM

    That's a good point. I was originally going to work on some abstraction of the logging module, or something, but you're right, I think redirect makes more sense. I'll rename it in the next micro.

Post a Comment