# Path: distgen/err.py
# def fatal(msg):
#     logging.fatal(msg)
#     sys.exit(1)
# 
# Path: distgen/pathmanager.py
# class PathManager(object):
#     envvar = None
# 
#     def __init__(self, path, envvar=None, file_suffix=None):
#         self.path = path
#         self.envvar = envvar
#         self.suffix = file_suffix
# 
#     def get_file(self, filename, prefered_path=None, fail=False,
#                  file_desc="file"):
# 
#         if filename.startswith('/'):
#             if os.path.isfile(filename):
#                 return filename
#             else:
#                 return None
# 
#         path = self.get_path()
#         if prefered_path:
#             path = prefered_path + path
# 
#         for i in path:
#             config_files = [os.path.join(i, filename)]
#             if self.suffix:
#                 config_files.append(config_files[0] + self.suffix)
#             for cf in config_files:
#                 if os.path.isfile(cf):
#                     return cf
# 
#         if fail:
#             fatal("can't find {0} '{1}'".format(file_desc, filename))
# 
#         return None
# 
#     def open_file(self, relative, prefered_path=None,
#                   fail=False, file_desc="file"):
# 
#         filename = self.get_file(relative, prefered_path, fail=fail,
#                                  file_desc=file_desc)
#         if not filename:
#             return None
# 
#         try:
#             fd = open(filename)
#         except IOError:
#             if fail:
#                 fatal("can't open file {0}".format(relative))
#             return None
# 
#         return fd
# 
#     def get_path(self):
#         path = self.path
#         if self.envvar and self.envvar in os.environ:
#             env_path = os.environ[self.envvar].split(':')
#             path = env_path + path
# 
#         return [os.getcwd()] + path


import six
import copy
    import yaml
from distgen.err import fatal
from distgen.pathmanager import PathManager



def _merge_yaml(origin, override):
    """
    Merge simple yaml node recursively.  If the node is non-dict, return
    itself, otherwise recurse down for each item.
    """
    if isinstance(origin, dict) and isinstance(override, dict):
        for k, v in six.iteritems(override):
            if k in origin:
                origin[k] = _merge_yaml(origin[k], override[k])
            else:
                origin[k] = copy.deepcopy(override[k])
        return origin

    return copy.deepcopy(override)


def merge_yaml(origin, override):
    old = copy.deepcopy(origin)
    new = copy.deepcopy(override)
    return _merge_yaml(old, new)


def __recursive_load(pm, stack, filename):
    if filename in stack:

# Path: distgen/err.py
# def fatal(msg):
#     logging.fatal(msg)
#     sys.exit(1)
# 
# Path: distgen/pathmanager.py
# class PathManager(object):
#     envvar = None
# 
#     def __init__(self, path, envvar=None, file_suffix=None):
#         self.path = path
#         self.envvar = envvar
#         self.suffix = file_suffix
# 
#     def get_file(self, filename, prefered_path=None, fail=False,
#                  file_desc="file"):
# 
#         if filename.startswith('/'):
#             if os.path.isfile(filename):
#                 return filename
#             else:
#                 return None
# 
#         path = self.get_path()
#         if prefered_path:
#             path = prefered_path + path
# 
#         for i in path:
#             config_files = [os.path.join(i, filename)]
#             if self.suffix:
#                 config_files.append(config_files[0] + self.suffix)
#             for cf in config_files:
#                 if os.path.isfile(cf):
#                     return cf
# 
#         if fail:
#             fatal("can't find {0} '{1}'".format(file_desc, filename))
# 
#         return None
# 
#     def open_file(self, relative, prefered_path=None,
#                   fail=False, file_desc="file"):
# 
#         filename = self.get_file(relative, prefered_path, fail=fail,
#                                  file_desc=file_desc)
#         if not filename:
#             return None
# 
#         try:
#             fd = open(filename)
#         except IOError:
#             if fail:
#                 fatal("can't open file {0}".format(relative))
#             return None
# 
#         return fd
# 
#     def get_path(self):
#         path = self.path
#         if self.envvar and self.envvar in os.environ:
#             env_path = os.environ[self.envvar].split(':')
#             path = env_path + path
# 
#         return [os.getcwd()] + path


# Path: distgen/config.py
import six
import copy
    import yaml
from distgen.err import fatal
from distgen.pathmanager import PathManager




def _merge_yaml(origin, override):
    """
    Merge simple yaml node recursively.  If the node is non-dict, return
    itself, otherwise recurse down for each item.
    """
    if isinstance(origin, dict) and isinstance(override, dict):
        for k, v in six.iteritems(override):
            if k in origin:
                origin[k] = _merge_yaml(origin[k], override[k])
            else:
                origin[k] = copy.deepcopy(override[k])
        return origin

    return copy.deepcopy(override)


def merge_yaml(origin, override):
    old = copy.deepcopy(origin)
    new = copy.deepcopy(override)
    return _merge_yaml(old, new)


def __recursive_load(pm, stack, filename):
    if filename in stack:

        fatal("already parsed " + filename)