Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/metadata_dictionary/codemeta.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2019 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 | ||||
from typing import Any, Dict, List, Optional | from typing import Any, Dict, List, Optional, Union | ||||
from swh.indexer.codemeta import CODEMETA_TERMS, expand | from swh.indexer.codemeta import CODEMETA_TERMS, expand | ||||
from .base import SingleFileMapping | from .base import SingleFileMapping | ||||
class CodemetaMapping(SingleFileMapping): | class CodemetaMapping(SingleFileMapping): | ||||
""" | """ | ||||
dedicated class for CodeMeta (codemeta.json) mapping and translation | dedicated class for CodeMeta (codemeta.json) mapping and translation | ||||
""" | """ | ||||
name = "codemeta" | name = "codemeta" | ||||
filename = b"codemeta.json" | filename = b"codemeta.json" | ||||
string_fields = None | string_fields = None | ||||
@classmethod | @classmethod | ||||
def supported_terms(cls) -> List[str]: | def supported_terms(cls) -> List[str]: | ||||
return [term for term in CODEMETA_TERMS if not term.startswith("@")] | return [term for term in CODEMETA_TERMS if not term.startswith("@")] | ||||
def translate(self, content: bytes) -> Optional[Dict[str, Any]]: | def translate( | ||||
self, content: bytes | |||||
) -> Optional[ | |||||
Dict[str, Union[str, List[Union[str, Dict[str, Any]]], Dict[str, Any]]] | |||||
]: | |||||
try: | try: | ||||
return self.normalize_translation(expand(json.loads(content.decode()))) | return self.normalize_translation(expand(json.loads(content.decode()))) | ||||
except Exception: | except Exception: | ||||
return None | return None |