Esempio n. 1
0
    def __init__(self, name, category, ttl=DAY, description='', **kwargs):
        self.ttl = ttl
        self.category = category

        self._name, self.extension = os.path.splitext(name)
        self._name = substitute_config(self._name)
        self.extension = self.extension.replace('.', '')
        self._content = None
        self.description = description
def get_evidence_dependency(path, locker, fetcher=None):
    """
    Provide evidence to fetchers that depend on that other evidence to fetch.

    Use when a fetcher needs evidence fetched by another fetcher in order to
    complete its fetch process.  The premise is that if the evidence is in the
    evidence cache then return that because it was placed there by another
    fetcher.  If not then get the evidence directly from the locker without
    putting that evidence into the evidence cache.  When an evidence dependency
    is not found the fetcher is queued up as a dependency rerun for subsequent
    processing.

    :param path: relative path to the evidence within the Locker. For example,
      ``raw/source1/evidence.json``.
    :param locker: evidence Locker object.
    :param fetcher: optional Python notation path to fetcher method.  If
      provided, this defines the fetcher that is added to the re-run list.
      Otherwise the execution stack is traversed to find the fetcher caller.

    :returns: the evidence object.
    """
    path = substitute_config(path)
    evidence = get_config().get_evidence(path)
    if evidence is None or evidence.content is None:
        try:
            evidence = locker.get_evidence(path)
        except (StaleEvidenceError, EvidenceNotFoundError):
            rerun = None
            if fetcher:
                module, clss, method = fetcher.rsplit('.', 2)
                rerun = {'module': module, 'class': clss, 'method': method}
            else:
                for frame_info in inspect.stack()[1:]:
                    if frame_info.function.startswith(FETCH_PREFIX):
                        frame = frame_info.frame
                        rerun = {
                            'module': inspect.getmodule(frame).__name__,
                            'class': frame.f_locals['self'].__class__.__name__,
                            'method': frame.f_code.co_name
                        }
                        break
            if not rerun:
                raise DependencyFetcherNotFoundError(
                    f'Cannot re-run, no fetcher found for evidence {path}.')
            locker.dependency_rerun.append(rerun)
            raise DependencyUnavailableError(
                f'evidence dependency {path} is currently unavailable.')
    return evidence
def get_evidence_by_path(path, locker=None):
    """
    Provide an evidence object specified by the given path.

    The following strategy is used:

      * Return evidence if it is present in the evidence cache populating
        content if necessary and possible.
      * If evidence is not in the evidence cache but a locker is provided, then
        the evidence will be retrieved from the locker.
      * Otherwise, an evidence object is built with the default parameters.

    :param path: relative path to the evidence within the Locker. For example,
      ``raw/source1/evidence.json``.

    :returns: the evidence object.
    """
    path = substitute_config(path)
    evidence = get_config().get_evidence(path)
    if evidence:
        if locker and evidence.content is None:
            evidence = locker.load_content(evidence)
        return evidence

    if locker:
        try:
            evidence = locker.get_evidence(path)
            get_config().add_evidences([evidence])
            return evidence
        except ValueError:
            pass

    try:
        rootdir, category, name = path.strip('/').split('/')
    except ValueError:
        raise ValueError(f'Invalid evidence path format "{path}"')

    if rootdir not in __init_map:
        raise ValueError(f'Unable to create evidence from root dir {rootdir}')
    return __init_map[rootdir](name, category)
 def name(self):
     return substitute_config(self._name)
 def from_locker(cls, path, locker):
     path = substitute_config(path)
     return cls(locker.get_evidence(path))