Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/checks.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-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 | ||||
"""Functional Metadata checks: | """Functional Metadata checks: | ||||
Mandatory fields: | Mandatory alternate fields: | ||||
- 'author' | - 'author' or 'codemeta:author' | ||||
- 'name' or 'title' | - 'name' or 'codemeta:name' or 'title' or 'codemeta:title' | ||||
""" | """ | ||||
from typing import Dict, Optional, Tuple | from typing import Dict, Optional, Tuple | ||||
MANDATORY_FIELDS_MISSING = "Mandatory fields are missing" | |||||
moranegg: why did this distinction change?
because all fields are now alternate? | |||||
ardumontAuthorUnsubmitted Done Inline Actionsthey were all mandatory field, including the ones below. ardumont: they were all mandatory field, including the ones below.
But that field `author` did not have… | |||||
ALTERNATE_FIELDS_MISSING = "Mandatory alternate fields are missing" | ALTERNATE_FIELDS_MISSING = "Mandatory alternate fields are missing" | ||||
def check_metadata(metadata: Dict) -> Tuple[bool, Optional[Dict]]: | def check_metadata(metadata: Dict) -> Tuple[bool, Optional[Dict]]: | ||||
"""Check metadata for mandatory field presence. | """Check metadata for mandatory field presence. | ||||
Args: | Args: | ||||
metadata: Metadata dictionary to check for mandatory fields | metadata: Metadata dictionary to check for mandatory fields | ||||
Returns: | Returns: | ||||
tuple (status, error_detail): True, None if metadata are | tuple (status, error_detail): True, None if metadata are | ||||
ok (False, <detailed-error>) otherwise. | ok (False, <detailed-error>) otherwise. | ||||
""" | """ | ||||
# following fields are mandatory | |||||
required_fields = { | |||||
"author": False, | |||||
} | |||||
# at least one value per couple below is mandatory | # at least one value per couple below is mandatory | ||||
alternate_fields = { | mandatory_alternate_fields = { | ||||
("name", "title"): False, | ("codemeta:author", "author"): False, | ||||
("name", "title", "codemeta:name", "codemeta:title"): False, | |||||
Done Inline Actions@moranegg see, "everything comes to *them* who wait" ;) I don't remember when you said to me, "it'd be nice to rename those" or some such ardumont: @moranegg see, "everything comes to *them* who wait" ;)
I don't remember when you said to me… | |||||
Not Done Inline Actions:-) moranegg: :-) | |||||
Not Done Inline Actionscodemeta:title doesn't exists I'm a bit on the fence here, because we used these properties in a more generic way without the namespace even thought the algorithm identified name in codemeta:name. Also, the overall idea in the future will be not to keep codemeta in xml format. moranegg: `codemeta:title` doesn't exists
it was `title` for AtomPub and `name` for CodeMeta.
I'm a bit… | |||||
Done Inline Actionsoh ok, so so codemeta:title can be removed from the alternative fields since it makes no sense. ardumont: oh ok, so so `codemeta:title` can be removed from the alternative fields since it makes no… | |||||
} | } | ||||
for field, value in metadata.items(): | for field, value in metadata.items(): | ||||
for name in required_fields: | for possible_names in mandatory_alternate_fields: | ||||
if name in field: | |||||
required_fields[name] = True | |||||
for possible_names in alternate_fields: | |||||
for possible_name in possible_names: | for possible_name in possible_names: | ||||
if possible_name in field: | if possible_name in field: | ||||
alternate_fields[possible_names] = True | mandatory_alternate_fields[possible_names] = True | ||||
continue | continue | ||||
mandatory_result = [k for k, v in required_fields.items() if not v] | alternate_fields_result_check = [ | ||||
optional_result = [" or ".join(k) for k, v in alternate_fields.items() if not v] | " or ".join(k) for k, v in mandatory_alternate_fields.items() if not v | ||||
] | |||||
if mandatory_result == [] and optional_result == []: | if alternate_fields_result_check == []: | ||||
return True, None | return True, None | ||||
detail = [] | detail = [ | ||||
if mandatory_result != []: | {"summary": ALTERNATE_FIELDS_MISSING, "fields": alternate_fields_result_check,} | ||||
detail.append({"summary": MANDATORY_FIELDS_MISSING, "fields": mandatory_result}) | ] | ||||
if optional_result != []: | |||||
detail.append( | |||||
{"summary": ALTERNATE_FIELDS_MISSING, "fields": optional_result,} | |||||
) | |||||
return False, {"metadata": detail} | return False, {"metadata": detail} |
why did this distinction change?
because all fields are now alternate?