# Path: datapackage_pipelines/specs/resolver.py
# def resolve_generator(module_name):
#     if module_name in resolved_generators:
#         return resolved_generators[module_name]
#     resolved_generators[module_name] = None
#     module = load_module(module_name)
#     if module is None:
#         return None
#     try:
#         generator_class = module.Generator
#     except AttributeError:
#         logging.warning("Can't find 'Generator' identifier in %s", module_name)
#         return None
#     generator = generator_class()
#     resolved_generators[module_name] = generator
#     return generator
# 
# Path: datapackage_pipelines/specs/errors.py
# class SpecError(NamedTuple):
#     short_msg: str
#     long_msg: str
# 
#     def __str__(self):
#         return '{}: {}'.format(self.short_msg, self.long_msg)
# 
# Path: datapackage_pipelines/specs/parsers/base_parser.py
# class BaseParser(object):
# 
#     class InvalidFileException(Exception):
#         def __init__(self, short_msg, long_msg):
#             self.short_msg = short_msg
#             self.long_msg = long_msg
# 
#     @classmethod
#     def check_filename(cls, filename):
#         raise NotImplementedError()
# 
#     @classmethod
#     def to_pipeline(cls, spec, fullpath):
#         raise NotImplementedError()
# 
#     @staticmethod
#     def replace_root_dir(path, root_dir):
#         if root_dir.endswith('/'):
#             root_dir = root_dir[:-1]
#         if path.startswith(root_dir):
#             path = '.' + path[len(root_dir):]
#         return path
# 
# class PipelineSpec(object):
#     def __init__(self,
#                  path=None,
#                  pipeline_id=None,
#                  pipeline_details=None,
#                  source_details=None,
#                  validation_errors=None,
#                  dependencies=None,
#                  cache_hash='',
#                  schedule=None,
#                  environment=None):
#         self.path = path
#         self.pipeline_id = pipeline_id
#         self.pipeline_details = pipeline_details
#         self.source_details = source_details
#         self.validation_errors = [] if validation_errors is None else validation_errors
#         self.dependencies = [] if dependencies is None else dependencies
#         self.cache_hash = cache_hash
#         self.schedule = schedule
#         self.environment = environment
# 
#     def __str__(self):
#         return 'PipelineSpec({}, validation_errors={}, ' \
#                'dependencies={}, cache_hash={})'\
#             .format(self.pipeline_id, self.validation_errors,
#                     self.dependencies, self.cache_hash)
# 
#     def __repr__(self):
#         return str(self)


import os
from ..resolver import resolve_generator
from ..errors import SpecError
from .base_parser import BaseParser, PipelineSpec



class SourceSpecPipelineParser(BaseParser):

    SOURCE_FILENAME_SUFFIX = '.source-spec.yaml'

    @classmethod
    def check_filename(cls, filename):
        return filename.endswith(cls.SOURCE_FILENAME_SUFFIX)

    @classmethod
    def fix_dependency(cls, dep, dirpath, root_dir):
        if dep.startswith('./'):
            dep = dep[2:]
        return os.path.join(cls.replace_root_dir(dirpath, root_dir), dep)

    @classmethod
    def to_pipeline(cls, source_spec, fullpath, root_dir='.'):
        filename = os.path.basename(fullpath)
        dirpath = os.path.dirname(fullpath)

        module_name = filename[:-len(cls.SOURCE_FILENAME_SUFFIX)]
        pipeline_id = os.path.join(dirpath, module_name)

# Path: datapackage_pipelines/specs/resolver.py
# def resolve_generator(module_name):
#     if module_name in resolved_generators:
#         return resolved_generators[module_name]
#     resolved_generators[module_name] = None
#     module = load_module(module_name)
#     if module is None:
#         return None
#     try:
#         generator_class = module.Generator
#     except AttributeError:
#         logging.warning("Can't find 'Generator' identifier in %s", module_name)
#         return None
#     generator = generator_class()
#     resolved_generators[module_name] = generator
#     return generator
# 
# Path: datapackage_pipelines/specs/errors.py
# class SpecError(NamedTuple):
#     short_msg: str
#     long_msg: str
# 
#     def __str__(self):
#         return '{}: {}'.format(self.short_msg, self.long_msg)
# 
# Path: datapackage_pipelines/specs/parsers/base_parser.py
# class BaseParser(object):
# 
#     class InvalidFileException(Exception):
#         def __init__(self, short_msg, long_msg):
#             self.short_msg = short_msg
#             self.long_msg = long_msg
# 
#     @classmethod
#     def check_filename(cls, filename):
#         raise NotImplementedError()
# 
#     @classmethod
#     def to_pipeline(cls, spec, fullpath):
#         raise NotImplementedError()
# 
#     @staticmethod
#     def replace_root_dir(path, root_dir):
#         if root_dir.endswith('/'):
#             root_dir = root_dir[:-1]
#         if path.startswith(root_dir):
#             path = '.' + path[len(root_dir):]
#         return path
# 
# class PipelineSpec(object):
#     def __init__(self,
#                  path=None,
#                  pipeline_id=None,
#                  pipeline_details=None,
#                  source_details=None,
#                  validation_errors=None,
#                  dependencies=None,
#                  cache_hash='',
#                  schedule=None,
#                  environment=None):
#         self.path = path
#         self.pipeline_id = pipeline_id
#         self.pipeline_details = pipeline_details
#         self.source_details = source_details
#         self.validation_errors = [] if validation_errors is None else validation_errors
#         self.dependencies = [] if dependencies is None else dependencies
#         self.cache_hash = cache_hash
#         self.schedule = schedule
#         self.environment = environment
# 
#     def __str__(self):
#         return 'PipelineSpec({}, validation_errors={}, ' \
#                'dependencies={}, cache_hash={})'\
#             .format(self.pipeline_id, self.validation_errors,
#                     self.dependencies, self.cache_hash)
# 
#     def __repr__(self):
#         return str(self)


# Path: datapackage_pipelines/specs/parsers/source_spec_pipeline.py
import os
from ..resolver import resolve_generator
from ..errors import SpecError
from .base_parser import BaseParser, PipelineSpec




class SourceSpecPipelineParser(BaseParser):

    SOURCE_FILENAME_SUFFIX = '.source-spec.yaml'

    @classmethod
    def check_filename(cls, filename):
        return filename.endswith(cls.SOURCE_FILENAME_SUFFIX)

    @classmethod
    def fix_dependency(cls, dep, dirpath, root_dir):
        if dep.startswith('./'):
            dep = dep[2:]
        return os.path.join(cls.replace_root_dir(dirpath, root_dir), dep)

    @classmethod
    def to_pipeline(cls, source_spec, fullpath, root_dir='.'):
        filename = os.path.basename(fullpath)
        dirpath = os.path.dirname(fullpath)

        module_name = filename[:-len(cls.SOURCE_FILENAME_SUFFIX)]
        pipeline_id = os.path.join(dirpath, module_name)

        generator = resolve_generator(module_name)