Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/cassandra/converters.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-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 | ||||
import json | import json | ||||
import attr | import attr | ||||
from copy import deepcopy | from copy import deepcopy | ||||
from typing import Any, Dict, Tuple | from typing import Any, Dict, Tuple | ||||
from cassandra.cluster import ResultSet | from cassandra.cluster import ResultSet | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
ObjectType, | ObjectType, | ||||
OriginVisit, | |||||
OriginVisitStatus, | OriginVisitStatus, | ||||
Revision, | Revision, | ||||
RevisionType, | RevisionType, | ||||
Release, | Release, | ||||
Sha1Git, | Sha1Git, | ||||
) | ) | ||||
from swh.model.hashutil import DEFAULT_ALGORITHMS | from swh.model.hashutil import DEFAULT_ALGORITHMS | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | def row_to_content_hashes(row: Row) -> Dict[str, bytes]: | ||||
"""Convert cassandra row to a content hashes | """Convert cassandra row to a content hashes | ||||
""" | """ | ||||
hashes = {} | hashes = {} | ||||
for algo in DEFAULT_ALGORITHMS: | for algo in DEFAULT_ALGORITHMS: | ||||
hashes[algo] = getattr(row, algo) | hashes[algo] = getattr(row, algo) | ||||
return hashes | return hashes | ||||
vlorentz: ResultSet is an iterable of Rows. | |||||
def row_to_visit(row: ResultSet) -> OriginVisit: | |||||
"""Format a row representing an origin_visit to an actual OriginVisit. | |||||
""" | |||||
return OriginVisit( | |||||
origin=row.origin, | |||||
visit=row.visit, | |||||
date=row.date.replace(tzinfo=datetime.timezone.utc), | |||||
type=row.type, | |||||
) | |||||
def row_to_visit_status(row: ResultSet) -> OriginVisitStatus: | def row_to_visit_status(row: ResultSet) -> OriginVisitStatus: | ||||
"""Format a row representing a visit_status to an actual dict representing an | """Format a row representing a visit_status to an actual OriginVisitStatus. | ||||
Done Inline Actionsso this annotation is wrong too vlorentz: so this annotation is wrong too | |||||
Done Inline ActionsSo it's plain Row i want, got it. (why don't mypy say something?) ardumont: So it's plain Row i want, got it.
Thanks.
(why don't mypy say something?) | |||||
OriginVisitStatus. | |||||
""" | """ | ||||
return OriginVisitStatus.from_dict( | return OriginVisitStatus.from_dict( | ||||
{ | { | ||||
**row._asdict(), | **row._asdict(), | ||||
"origin": row.origin, | "origin": row.origin, | ||||
"date": row.date.replace(tzinfo=datetime.timezone.utc), | "date": row.date.replace(tzinfo=datetime.timezone.utc), | ||||
"metadata": (json.loads(row.metadata) if row.metadata else None), | "metadata": (json.loads(row.metadata) if row.metadata else None), | ||||
Done Inline Actionsdamn, the type ignore is useless... ardumont: damn, the type ignore is useless...
| |||||
} | } | ||||
) | ) |
ResultSet is an iterable of Rows.