import contextlib
import os
import glob as globlib

__version__ = '0.0.7'


class ObjectDict(dict):
    """Makes a dictionary behave like an object, with attribute-style access.
    """

    def __getattr__(self, name):
        try:
            return self[name]
        except KeyError:
            raise AttributeError(name)

    def __setattr__(self, name, value):
        self[name] = value


@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)


def glob(*pathname):
    """Return an iterator which yields the paths matching pathname patterns. Example:

    for fname in iglob('**/*.py', '**/*.go'):
        ...
    """
    for pname in pathname:
        if not set(pname).intersection(set('*?![]')):
            yield pname
        else:
            recursive = True if '**' in pname else False
            for fname in globlib.iglob(pname, recursive=recursive):
                yield fname
