rashid 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rashid/__init__.py ADDED
@@ -0,0 +1,32 @@
1
+ """rashid — validator and linter for Portolan catalogs.
2
+
3
+ Usage:
4
+
5
+ from rashid import validate
6
+ report = validate("path/to/catalog")
7
+ if not report.passed:
8
+ for finding in report.errors:
9
+ print(finding.message)
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from rashid.config import RulesConfig
15
+ from rashid.data import validate_data
16
+ from rashid.live import validate_live
17
+ from rashid.model import Finding, Report, Severity
18
+ from rashid.runner import validate
19
+ from rashid.schema import validate_schema
20
+ from rashid.structural import validate_structural
21
+
22
+ __all__ = [
23
+ "Finding",
24
+ "Report",
25
+ "RulesConfig",
26
+ "Severity",
27
+ "validate",
28
+ "validate_data",
29
+ "validate_live",
30
+ "validate_schema",
31
+ "validate_structural",
32
+ ]
rashid/_multihash.py ADDED
@@ -0,0 +1,58 @@
1
+ """Multihash well-formedness checking.
2
+
3
+ A multihash is ``varint(code) + varint(digest length) + digest``, hex-encoded
4
+ (https://github.com/multiformats/multihash). This module only checks that a
5
+ string decodes as one; verifying the digest against file bytes is a data-pass
6
+ job, so no hashing happens here and no dependency is needed.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+
12
+ def _read_varint(data: bytes, offset: int) -> tuple[int, int] | None:
13
+ """Decode an unsigned varint at ``offset``; return (value, next_offset)."""
14
+ value = 0
15
+ shift = 0
16
+ while offset < len(data):
17
+ byte = data[offset]
18
+ value |= (byte & 0x7F) << shift
19
+ offset += 1
20
+ if not byte & 0x80:
21
+ return value, offset
22
+ shift += 7
23
+ if shift > 63: # implausibly large varint; corrupt input
24
+ return None
25
+ return None # ran out of bytes mid-varint
26
+
27
+
28
+ def decode_multihash(value: object) -> tuple[int, bytes] | None:
29
+ """Decode a hex multihash into ``(hash-function code, digest bytes)``.
30
+
31
+ Returns None when ``value`` is not a hex string that decodes as a complete
32
+ multihash. The code names the hash function (e.g. ``0x12`` = sha2-256); the
33
+ data pass maps it to a hashlib algorithm to verify the digest against the
34
+ asset's bytes.
35
+ """
36
+ if not isinstance(value, str) or not value:
37
+ return None
38
+ try:
39
+ raw = bytes.fromhex(value)
40
+ except ValueError:
41
+ return None
42
+ decoded = _read_varint(raw, 0)
43
+ if decoded is None:
44
+ return None
45
+ code, offset = decoded
46
+ decoded = _read_varint(raw, offset)
47
+ if decoded is None:
48
+ return None
49
+ length, offset = decoded
50
+ digest = raw[offset:]
51
+ if length <= 0 or len(digest) != length:
52
+ return None
53
+ return code, digest
54
+
55
+
56
+ def is_well_formed_multihash(value: object) -> bool:
57
+ """True when ``value`` is a hex string decoding as a complete multihash."""
58
+ return decode_multihash(value) is not None