Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/postgresql/converters.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2020 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 datetime | import datetime | ||||
from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
import warnings | import warnings | ||||
from swh.core.utils import encode_with_unescape | from swh.core.utils import encode_with_unescape | ||||
from swh.model.identifiers import CoreSWHID, ExtendedSWHID, origin_identifier | from swh.model.identifiers import CoreSWHID, ExtendedSWHID | ||||
from swh.model.identifiers import ObjectType as SwhidObjectType | |||||
from swh.model.identifiers import origin_identifier | |||||
from swh.model.model import ( | from swh.model.model import ( | ||||
ExtID, | |||||
MetadataAuthority, | MetadataAuthority, | ||||
MetadataAuthorityType, | MetadataAuthorityType, | ||||
MetadataFetcher, | MetadataFetcher, | ||||
ObjectType, | ObjectType, | ||||
Person, | Person, | ||||
RawExtrinsicMetadata, | RawExtrinsicMetadata, | ||||
Release, | Release, | ||||
Revision, | Revision, | ||||
Show All 10 Lines | DEFAULT_AUTHOR = { | ||||
"email": None, | "email": None, | ||||
} | } | ||||
DEFAULT_DATE = { | DEFAULT_DATE = { | ||||
"timestamp": None, | "timestamp": None, | ||||
"offset": 0, | "offset": 0, | ||||
"neg_utc_offset": None, | "neg_utc_offset": None, | ||||
} | } | ||||
def author_to_db(author: Optional[Person]) -> Dict[str, Any]: | def author_to_db(author: Optional[Person]) -> Dict[str, Any]: | ||||
"""Convert a swh-model author to its DB representation. | """Convert a swh-model author to its DB representation. | ||||
Args: | Args: | ||||
author: a :mod:`swh.model` compatible author | author: a :mod:`swh.model` compatible author | ||||
vlorentz: dupe | |||||
Done Inline Actionsso what's the "proper" way of doing this mapping? douardda: so what's the "proper" way of doing this mapping? | |||||
Not Done Inline ActionsOBJECT_TYPE_MAP_SHORT[key] -> ObjectType[key.upper()] vlorentz: `OBJECT_TYPE_MAP_SHORT[key]` -> `ObjectType[key.upper()]` | |||||
Returns: | Returns: | ||||
dict: a dictionary with three keys: author, fullname and email | dict: a dictionary with three keys: author, fullname and email | ||||
""" | """ | ||||
if author is None: | if author is None: | ||||
return DEFAULT_AUTHOR | return DEFAULT_AUTHOR | ||||
▲ Show 20 Lines • Show All 263 Lines • ▼ Show 20 Lines | return RawExtrinsicMetadata( | ||||
origin=row["origin"], | origin=row["origin"], | ||||
visit=row["visit"], | visit=row["visit"], | ||||
snapshot=map_optional(CoreSWHID.from_string, row["snapshot"]), | snapshot=map_optional(CoreSWHID.from_string, row["snapshot"]), | ||||
release=map_optional(CoreSWHID.from_string, row["release"]), | release=map_optional(CoreSWHID.from_string, row["release"]), | ||||
revision=map_optional(CoreSWHID.from_string, row["revision"]), | revision=map_optional(CoreSWHID.from_string, row["revision"]), | ||||
path=row["path"], | path=row["path"], | ||||
directory=map_optional(CoreSWHID.from_string, row["directory"]), | directory=map_optional(CoreSWHID.from_string, row["directory"]), | ||||
) | ) | ||||
def db_to_extid(row) -> ExtID: | |||||
return ExtID( | |||||
extid=row["extid"], | |||||
extid_type=row["extid_type"], | |||||
target=CoreSWHID( | |||||
object_id=row["target"], | |||||
object_type=SwhidObjectType[row["target_type"].upper()], | |||||
), | |||||
) |
dupe