Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/query.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2022 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 Affero General Public License version 3, or any later version | # License: GNU Affero 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 re | import re | ||||
from uuid import UUID | |||||
from swh.model.hashutil import ALGORITHMS, hash_to_bytes | from swh.model.hashutil import ALGORITHMS, hash_to_bytes | ||||
from swh.web.common.exc import BadInputExc | from swh.web.common.exc import BadInputExc | ||||
SHA256_RE = re.compile(r"^[0-9a-f]{64}$", re.IGNORECASE) | SHA256_RE = re.compile(r"^[0-9a-f]{64}$", re.IGNORECASE) | ||||
SHA1_RE = re.compile(r"^[0-9a-f]{40}$", re.IGNORECASE) | SHA1_RE = re.compile(r"^[0-9a-f]{40}$", re.IGNORECASE) | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | def parse_hash_with_algorithms_or_throws(q, accepted_algo, error_msg): | ||||
""" | """ | ||||
algo, hash = parse_hash(q) | algo, hash = parse_hash(q) | ||||
if algo not in accepted_algo: | if algo not in accepted_algo: | ||||
raise BadInputExc(error_msg) | raise BadInputExc(error_msg) | ||||
return (algo, hash) | return (algo, hash) | ||||
def parse_uuid4(uuid): | |||||
"""Parse an uuid 4 from a string. | |||||
Args: | |||||
uuid: String representing an uuid. | |||||
Returns: | |||||
The uuid as is if everything is ok. | |||||
Raises: | |||||
BadInputExc: if the uuid is invalid. | |||||
""" | |||||
try: | |||||
UUID(uuid, version=4) | |||||
except ValueError as e: | |||||
# not a valid hex code for a UUID | |||||
raise BadInputExc(str(e)) | |||||
return uuid |