Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/metadata_dictionary/base.py
# Copyright (C) 2017-2022 The Software Heritage developers | # Copyright (C) 2017-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import json | import json | ||||
import logging | import logging | ||||
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar | from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar | ||||
from typing_extensions import TypedDict | from typing_extensions import TypedDict | ||||
import yaml | import yaml | ||||
from swh.indexer.codemeta import SCHEMA_URI, compact, merge_values | from swh.indexer.codemeta import compact, merge_values | ||||
from swh.indexer.namespaces import SCHEMA | |||||
from swh.indexer.storage.interface import Sha1 | from swh.indexer.storage.interface import Sha1 | ||||
class DirectoryLsEntry(TypedDict): | class DirectoryLsEntry(TypedDict): | ||||
target: Sha1 | target: Sha1 | ||||
sha1: Sha1 | sha1: Sha1 | ||||
name: bytes | name: bytes | ||||
type: str | type: str | ||||
TTranslateCallable = TypeVar( | TTranslateCallable = TypeVar( | ||||
"TTranslateCallable", bound=Callable[[Any, Dict[str, Any], Any], None] | "TTranslateCallable", bound=Callable[[Any, Dict[str, Any], Any], None] | ||||
) | ) | ||||
def produce_terms( | def produce_terms(*uris: str) -> Callable[[TTranslateCallable], TTranslateCallable]: | ||||
namespace: str, terms: List[str] | |||||
) -> Callable[[TTranslateCallable], TTranslateCallable]: | |||||
"""Returns a decorator that marks the decorated function as adding | """Returns a decorator that marks the decorated function as adding | ||||
the given terms to the ``translated_metadata`` dict""" | the given terms to the ``translated_metadata`` dict""" | ||||
def decorator(f: TTranslateCallable) -> TTranslateCallable: | def decorator(f: TTranslateCallable) -> TTranslateCallable: | ||||
if not hasattr(f, "produced_terms"): | if not hasattr(f, "produced_terms"): | ||||
f.produced_terms = [] # type: ignore | f.produced_terms = [] # type: ignore | ||||
f.produced_terms.extend(namespace + term for term in terms) # type: ignore | f.produced_terms.extend(uris) # type: ignore | ||||
return f | return f | ||||
return decorator | return decorator | ||||
class BaseMapping: | class BaseMapping: | ||||
"""Base class for :class:`BaseExtrinsicMapping` and :class:`BaseIntrinsicMapping`, | """Base class for :class:`BaseExtrinsicMapping` and :class:`BaseIntrinsicMapping`, | ||||
not to be inherited directly.""" | not to be inherited directly.""" | ||||
▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | ) -> Dict[str, str]: | ||||
Args: | Args: | ||||
content_dict (dict): content dict to translate | content_dict (dict): content dict to translate | ||||
Returns: | Returns: | ||||
dict: translated metadata in json-friendly form needed for | dict: translated metadata in json-friendly form needed for | ||||
the indexer | the indexer | ||||
""" | """ | ||||
translated_metadata = {"@type": SCHEMA_URI + "SoftwareSourceCode"} | translated_metadata = {"@type": SCHEMA.SoftwareSourceCode} | ||||
for k, v in content_dict.items(): | for k, v in content_dict.items(): | ||||
# First, check if there is a specific translation | # First, check if there is a specific translation | ||||
# method for this key | # method for this key | ||||
translation_method = getattr( | translation_method = getattr( | ||||
self, "translate_" + self._normalize_method_name(k), None | self, "translate_" + self._normalize_method_name(k), None | ||||
) | ) | ||||
if translation_method: | if translation_method: | ||||
translation_method(translated_metadata, v) | translation_method(translated_metadata, v) | ||||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |