import contextlib
import functools
import glob as globlib
import os
import subprocess


class ObjectDict(dict):
    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):
    cwd = os.getcwd()
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(cwd)


def glob(*pathname):
    for pname in pathname:
        if isinstance(pname, list):
            yield from glob(*pname)
        elif 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


call = functools.partial(subprocess.call, shell=True)
