Changeset View
Changeset View
Standalone View
Standalone View
swh/journal/serializers.py
# Copyright (C) 2016-2017 The Software Heritage developers | # Copyright (C) 2016-2017 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 | ||||
from typing import Any, Dict, Union, overload | from typing import Any, Dict, Union, overload | ||||
import msgpack | import msgpack | ||||
from swh.core.api.serializers import msgpack_dumps, msgpack_loads | from swh.core.api.serializers import msgpack_dumps, msgpack_loads | ||||
from swh.model.hashutil import DEFAULT_ALGORITHMS | from swh.model.hashutil import DEFAULT_ALGORITHMS | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Content, | Content, | ||||
Directory, | Directory, | ||||
MetadataAuthority, | |||||
MetadataFetcher, | |||||
Origin, | Origin, | ||||
OriginVisit, | OriginVisit, | ||||
OriginVisitStatus, | OriginVisitStatus, | ||||
RawExtrinsicMetadata, | |||||
Release, | Release, | ||||
Revision, | Revision, | ||||
SkippedContent, | SkippedContent, | ||||
Snapshot, | Snapshot, | ||||
) | ) | ||||
ModelObject = Union[ | ModelObject = Union[ | ||||
Content, | Content, | ||||
Directory, | Directory, | ||||
MetadataAuthority, | |||||
MetadataFetcher, | |||||
Origin, | Origin, | ||||
OriginVisit, | OriginVisit, | ||||
OriginVisitStatus, | OriginVisitStatus, | ||||
RawExtrinsicMetadata, | |||||
Release, | Release, | ||||
Revision, | Revision, | ||||
SkippedContent, | SkippedContent, | ||||
Snapshot, | Snapshot, | ||||
] | ] | ||||
KeyType = Union[Dict[str, str], Dict[str, bytes], bytes] | KeyType = Union[Dict[str, str], Dict[str, bytes], bytes] | ||||
Show All 11 Lines | |||||
def object_key( | def object_key( | ||||
object_type: str, object_: Union[Origin, SkippedContent] | object_type: str, object_: Union[Origin, SkippedContent] | ||||
) -> Dict[str, bytes]: | ) -> Dict[str, bytes]: | ||||
... | ... | ||||
@overload | @overload | ||||
def object_key( | def object_key( | ||||
object_type: str, object_: Union[OriginVisit, OriginVisitStatus] | object_type: str, | ||||
object_: Union[ | |||||
MetadataAuthority, | |||||
MetadataFetcher, | |||||
OriginVisit, | |||||
OriginVisitStatus, | |||||
RawExtrinsicMetadata, | |||||
], | |||||
) -> Dict[str, str]: | ) -> Dict[str, str]: | ||||
... | ... | ||||
def object_key(object_type: str, object_) -> KeyType: | def object_key(object_type: str, object_) -> KeyType: | ||||
ardumont: why don't we use the object_.object_type here?
(I assume `object_` is a BaseModel object). | |||||
vlorentzAuthorUnsubmitted Done Inline Actionsbecause it didn't exist when this code was written ^^ vlorentz: because it didn't exist when this code was written ^^ | |||||
if object_type in ("revision", "release", "directory", "snapshot"): | if object_type in ("revision", "release", "directory", "snapshot"): | ||||
return object_.id | return object_.id | ||||
elif object_type == "content": | elif object_type == "content": | ||||
return object_.sha1 # TODO: use a dict of hashes | return object_.sha1 # TODO: use a dict of hashes | ||||
elif object_type == "skipped_content": | elif object_type == "skipped_content": | ||||
return {hash: getattr(object_, hash) for hash in DEFAULT_ALGORITHMS} | return {hash: getattr(object_, hash) for hash in DEFAULT_ALGORITHMS} | ||||
elif object_type == "origin": | elif object_type == "origin": | ||||
return {"url": object_.url} | return {"url": object_.url} | ||||
elif object_type == "origin_visit": | elif object_type == "origin_visit": | ||||
return { | return { | ||||
"origin": object_.origin, | "origin": object_.origin, | ||||
"date": str(object_.date), | "date": str(object_.date), | ||||
} | } | ||||
elif object_type == "origin_visit_status": | elif object_type == "origin_visit_status": | ||||
return { | return { | ||||
"origin": object_.origin, | "origin": object_.origin, | ||||
"visit": str(object_.visit), | "visit": str(object_.visit), | ||||
"date": str(object_.date), | "date": str(object_.date), | ||||
} | } | ||||
elif object_type == "metadata_authority": | |||||
return { | |||||
"type": object_.type.value, | |||||
"url": object_.url, | |||||
} | |||||
elif object_type == "metadata_fetcher": | |||||
return { | |||||
"name": object_.name, | |||||
"version": object_.version, | |||||
} | |||||
elif object_type == "raw_extrinsic_metadata": | |||||
return { | |||||
"type": object_.type.value, | |||||
"id": str(object_.id), | |||||
"authority_type": object_.authority.type.value, | |||||
"authority_url": object_.authority.url, | |||||
"discovery_date": str(object_.discovery_date), | |||||
"fetcher_name": object_.fetcher.name, | |||||
"fetcher_version": object_.fetcher.version, | |||||
} | |||||
else: | else: | ||||
raise ValueError("Unknown object type: %s." % object_type) | raise ValueError("Unknown object type: %s." % object_type) | ||||
def stringify_key_item(k: str, v: Union[str, bytes]) -> str: | def stringify_key_item(k: str, v: Union[str, bytes]) -> str: | ||||
"""Turn the item of a dict key into a string""" | """Turn the item of a dict key into a string""" | ||||
if isinstance(v, str): | if isinstance(v, str): | ||||
return v | return v | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
why don't we use the object_.object_type here?
(I assume object_ is a BaseModel object).
Yeah, out of the scope of this new objects adding though.
Just checking if i'm on track with what we wanted to do with that fields in model.