caselawclient.models.utilities

 1import re
 2from typing import TypedDict
 3
 4from requests_toolbelt.multipart.decoder import BodyPart
 5
 6from caselawclient.types import DocumentURIString, MarkLogicDocumentURIString
 7
 8VERSION_REGEX = r"xml_versions/(\d{1,10})-"
 9# Here we limit the number of digits in the version to 10 on purpose, see
10# https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS for an explanation of why.
11
12
13class VersionsDict(TypedDict):
14    uri: DocumentURIString
15    version: int
16
17
18def render_versions(decoded_versions: list[BodyPart]) -> list[VersionsDict]:
19    versions: list[VersionsDict] = [
20        {
21            "uri": MarkLogicDocumentURIString(part.text).as_document_uri(),
22            "version": extract_version(part.text),
23        }
24        for part in decoded_versions
25    ]
26    sorted_versions = sorted(versions, key=lambda d: -d["version"])
27    return sorted_versions
28
29
30def extract_version(version_string: str) -> int:
31    result = re.search(VERSION_REGEX, version_string)
32    return int(result.group(1)) if result else 0
VERSION_REGEX = 'xml_versions/(\\d{1,10})-'
class VersionsDict(typing.TypedDict):
14class VersionsDict(TypedDict):
15    uri: DocumentURIString
16    version: int
version: int
def render_versions( decoded_versions: list[requests_toolbelt.multipart.decoder.BodyPart]) -> list[VersionsDict]:
19def render_versions(decoded_versions: list[BodyPart]) -> list[VersionsDict]:
20    versions: list[VersionsDict] = [
21        {
22            "uri": MarkLogicDocumentURIString(part.text).as_document_uri(),
23            "version": extract_version(part.text),
24        }
25        for part in decoded_versions
26    ]
27    sorted_versions = sorted(versions, key=lambda d: -d["version"])
28    return sorted_versions
def extract_version(version_string: str) -> int:
31def extract_version(version_string: str) -> int:
32    result = re.search(VERSION_REGEX, version_string)
33    return int(result.group(1)) if result else 0