-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache_io.py
More file actions
66 lines (45 loc) · 1.77 KB
/
cache_io.py
File metadata and controls
66 lines (45 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2026 Den Rozhnovskiy
from __future__ import annotations
import hashlib
import hmac
from collections.abc import Mapping
from pathlib import Path
from ._json_io import (
json_text as _json_text,
)
from ._json_io import (
read_json_document as _read_json_document,
)
from ._json_io import (
write_json_document_atomically as _write_json_document_atomically,
)
def as_str_or_none(value: object) -> str | None:
return value if isinstance(value, str) else None
def as_int_or_none(value: object) -> int | None:
return value if isinstance(value, int) else None
def as_object_list(value: object) -> list[object] | None:
return value if isinstance(value, list) else None
def as_str_dict(value: object) -> dict[str, object] | None:
if not isinstance(value, dict):
return None
if not all(isinstance(key, str) for key in value):
return None
return value
def canonical_json(data: object) -> str:
return _json_text(data, sort_keys=True)
def sign_cache_payload(data: Mapping[str, object]) -> str:
canonical = canonical_json(data)
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
def verify_cache_payload_signature(
payload: Mapping[str, object],
signature: str,
) -> bool:
return hmac.compare_digest(signature, sign_cache_payload(payload))
def read_json_document(path: Path) -> object:
return _read_json_document(path)
def write_json_document_atomically(path: Path, document: object) -> None:
_write_json_document_atomically(path, document, sort_keys=True)