"""accu.chdir is same as os.chdir but with the 'with' syntax.
usage:
    >>> import os
    >>> import accu.chdir
    >>> with accu.chdir('/home'):
    >>>     print(os.getcwd())
    '/home'
"""
import contextlib
import os
import sys


@contextlib.contextmanager
def chdir(path):
    """Same as os.chdir but with the 'with' syntax.
    """
    cwd = os.getcwd()
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(cwd)


sys.modules['accu.chdir'] = chdir
