Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/utils.py
# Copyright (C) 2018-2020 The Software Heritage developers | # Copyright (C) 2018-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 logging | import logging | ||||
from types import GeneratorType | from types import GeneratorType | ||||
from typing import Any, Dict, Optional, Union | from typing import Any, Dict, Optional, Union | ||||
import iso8601 | import iso8601 | ||||
import xmltodict | import xmltodict | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.identifiers import ( | from swh.model.model import TimestampWithTimezone | ||||
ExtendedSWHID, | from swh.model.swhids import ExtendedSWHID, ObjectType, QualifiedSWHID | ||||
ObjectType, | |||||
QualifiedSWHID, | |||||
normalize_timestamp, | |||||
) | |||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
def parse_xml(stream, encoding="utf-8"): | def parse_xml(stream, encoding="utf-8"): | ||||
namespaces = { | namespaces = { | ||||
"http://www.w3.org/2005/Atom": "atom", | "http://www.w3.org/2005/Atom": "atom", | ||||
"http://www.w3.org/2007/app": "app", | "http://www.w3.org/2007/app": "app", | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | def normalize_date(date): | ||||
If date is a list, elect arbitrarily the first element of that | If date is a list, elect arbitrarily the first element of that | ||||
list | list | ||||
If date is (then) a string, parse it through | If date is (then) a string, parse it through | ||||
dateutil.parser.parse to extract a datetime. | dateutil.parser.parse to extract a datetime. | ||||
Then normalize it through | Then normalize it through | ||||
swh.model.identifiers.normalize_timestamp. | :class:`swh.model.model.TimestampWithTimezone` | ||||
Returns | Returns | ||||
The swh date object | The swh date object | ||||
""" | """ | ||||
if isinstance(date, list): | if isinstance(date, list): | ||||
date = date[0] | date = date[0] | ||||
if isinstance(date, str): | if isinstance(date, str): | ||||
date = iso8601.parse_date(date) | date = iso8601.parse_date(date) | ||||
return normalize_timestamp(date) | return TimestampWithTimezone.from_dict(date).to_dict() | ||||
def compute_metadata_context(swhid_reference: QualifiedSWHID) -> Dict[str, Any]: | def compute_metadata_context(swhid_reference: QualifiedSWHID) -> Dict[str, Any]: | ||||
"""Given a SWHID object, determine the context as a dict. | """Given a SWHID object, determine the context as a dict. | ||||
""" | """ | ||||
metadata_context: Dict[str, Any] = {"origin": None} | metadata_context: Dict[str, Any] = {"origin": None} | ||||
if swhid_reference.qualifiers(): | if swhid_reference.qualifiers(): | ||||
▲ Show 20 Lines • Show All 129 Lines • Show Last 20 Lines |