# Path: databuild/loader.py
# def load_classpath(classpath):
#     modules, function = classpath.rsplit('.', 1)
#     module = import_module(modules)
#     fn = getattr(module, function)
#     return fn
# 
# Path: databuild/buildfile.py
# class BuildFile(object):
#     def __init__(self, path):
#         self.path = os.path.abspath(path)
#         super(BuildFile, self).__init__()
# 
#     @property
#     def parent_dir(self):
#         return os.path.dirname(self.path)
# 
#     @cached_property
#     def operations(self):
#         with _open(self.path, 'r', encoding='utf-8') as fh:
#             if self.path.endswith('.json'):
#                 operations = json.load(fh)
#             elif self.path.endswith('.yaml') or self.path.endswith('.yml'):
#                 operations = yaml.safe_load(fh)
#         return operations
# 
# Path: databuild/utils.py
# def multiglob(*patterns):
#     files_grabbed = []
#     [files_grabbed.extend(glob(pattern)) for pattern in patterns]
#     return files_grabbed


import os
from importlib import import_module
from databuild.loader import load_classpath
from databuild.buildfile import BuildFile
from databuild.utils import multiglob



class Builder(object):
    def __init__(self, settings='databuild.settings'):
        self.settings = import_module(settings)
        AdapterClass = load_classpath(self.settings.ADAPTER) 
        self.book = AdapterClass(settings=self.settings)
        super(Builder, self).__init__()
        
    def build(self, build_file_or_dir, echo=False):
        self.book.echo = echo

        if os.path.isfile(build_file_or_dir):
            build_files = [BuildFile(build_file_or_dir)]
        else:
            globs = (
                os.path.join(build_file_or_dir, "*.json"),
                os.path.join(build_file_or_dir, "*.yaml"),
                os.path.join(build_file_or_dir, "*.yml"),
            )

# Path: databuild/loader.py
# def load_classpath(classpath):
#     modules, function = classpath.rsplit('.', 1)
#     module = import_module(modules)
#     fn = getattr(module, function)
#     return fn
# 
# Path: databuild/buildfile.py
# class BuildFile(object):
#     def __init__(self, path):
#         self.path = os.path.abspath(path)
#         super(BuildFile, self).__init__()
# 
#     @property
#     def parent_dir(self):
#         return os.path.dirname(self.path)
# 
#     @cached_property
#     def operations(self):
#         with _open(self.path, 'r', encoding='utf-8') as fh:
#             if self.path.endswith('.json'):
#                 operations = json.load(fh)
#             elif self.path.endswith('.yaml') or self.path.endswith('.yml'):
#                 operations = yaml.safe_load(fh)
#         return operations
# 
# Path: databuild/utils.py
# def multiglob(*patterns):
#     files_grabbed = []
#     [files_grabbed.extend(glob(pattern)) for pattern in patterns]
#     return files_grabbed


# Path: databuild/builder.py
import os
from importlib import import_module
from databuild.loader import load_classpath
from databuild.buildfile import BuildFile
from databuild.utils import multiglob




class Builder(object):
    def __init__(self, settings='databuild.settings'):
        self.settings = import_module(settings)
        AdapterClass = load_classpath(self.settings.ADAPTER) 
        self.book = AdapterClass(settings=self.settings)
        super(Builder, self).__init__()
        
    def build(self, build_file_or_dir, echo=False):
        self.book.echo = echo

        if os.path.isfile(build_file_or_dir):
            build_files = [BuildFile(build_file_or_dir)]
        else:
            globs = (
                os.path.join(build_file_or_dir, "*.json"),
                os.path.join(build_file_or_dir, "*.yaml"),
                os.path.join(build_file_or_dir, "*.yml"),
            )

            build_files = map(BuildFile, sorted(multiglob(*globs)))