Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/utils.py
# Copyright (C) 2018 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 iso8601 | |||||
from types import GeneratorType | from types import GeneratorType | ||||
from swh.model.identifiers import normalize_timestamp | |||||
def merge(*dicts): | def merge(*dicts): | ||||
"""Given an iterator of dicts, merge them losing no information. | """Given an iterator of dicts, merge them losing no information. | ||||
Args: | Args: | ||||
*dicts: arguments are all supposed to be dict to merge into one | *dicts: arguments are all supposed to be dict to merge into one | ||||
Returns: | Returns: | ||||
Show All 32 Lines | for data in dicts: | ||||
if isinstance(value, dict): | if isinstance(value, dict): | ||||
new_val = merge(existing_val, value) | new_val = merge(existing_val, value) | ||||
else: | else: | ||||
new_val = _extend([existing_val], value) | new_val = _extend([existing_val], value) | ||||
else: | else: | ||||
new_val = _extend([existing_val], value) | new_val = _extend([existing_val], value) | ||||
d[key] = new_val | d[key] = new_val | ||||
return d | return d | ||||
def normalize_date(date): | |||||
"""Normalize date fields as expected by swh workers. | |||||
If date is a list, elect arbitrarily the first element of that | |||||
list | |||||
If date is (then) a string, parse it through | |||||
dateutil.parser.parse to extract a datetime. | |||||
Then normalize it through | |||||
swh.model.identifiers.normalize_timestamp. | |||||
Returns | |||||
The swh date object | |||||
""" | |||||
if isinstance(date, list): | |||||
date = date[0] | |||||
if isinstance(date, str): | |||||
date = iso8601.parse_date(date) | |||||
return normalize_timestamp(date) |