Coverage for src/romfarmer/ir/identity.py: 59%

37 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-05 02:19 -0700

1"""Identity — content-addressing primitives for the ROM Farmer IR. 

2 

3An Identity may be *partial* (only some fields populated) everywhere except 

4inside the Executor, which is the ONLY component that requires 

5``is_complete`` and the ONLY writer of alias records to the database. 

6""" 

7 

8from __future__ import annotations 

9 

10from dataclasses import dataclass 

11 

12 

13class IdentityConflict(ValueError): 

14 """Raised by ``Identity.merged()`` when two populated fields disagree.""" 

15 

16 

17@dataclass(frozen=True, slots=True) 

18class ZipIdentity: 

19 """The in-archive identity of the dominant member of a ZIP. 

20 

21 "Dominant member" means the largest file, or the ``.cue`` sheet for 

22 multi-track disc images. 

23 """ 

24 

25 member_crc32: int # CRC32 of the dominant member 

26 member_size: int # uncompressed size of that member 

27 member_name: str # filename of that member 

28 

29 def serialized(self) -> str: 

30 """Canonical string form used as a cache-key alias: ``crc32:size:name``.""" 

31 return f"{self.member_crc32}:{self.member_size}:{self.member_name}" 

32 

33 

34@dataclass(frozen=True, slots=True) 

35class Identity: 

36 """Accumulated content-addressing knowledge for one file. 

37 

38 Fields are optional because knowledge accumulates incrementally: 

39 - Source scanning produces ``zip_identity`` (pre-extraction). 

40 - DAT matching adds ``md5`` (and sometimes ``size``). 

41 - The Executor, after computing or verifying, fills in ``sha256`` and 

42 writes all aliases atomically — fixing the MD5-vs-CRC32 divergence 

43 described in the system briefing §3.9. 

44 """ 

45 

46 size: int | None = None 

47 sha256: str | None = None # primary CAS key; REQUIRED by Executor 

48 md5: str | None = None # DAT-matching alias 

49 zip_identity: ZipIdentity | None = None # pre-extraction alias 

50 

51 @property 

52 def is_complete(self) -> bool: 

53 """True iff ``sha256`` is populated (Executor precondition).""" 

54 return self.sha256 is not None 

55 

56 def merged(self, other: Identity) -> Identity: 

57 """Return the union of knowledge from ``self`` and ``other``. 

58 

59 Raises: 

60 IdentityConflict: if any field is populated in *both* instances 

61 with different values. 

62 """ 

63 

64 def _merge(a: object, b: object, name: str) -> object: 

65 if a is None: 

66 return b 

67 if b is None: 

68 return a 

69 if a != b: 

70 raise IdentityConflict(f"Identity conflict on field '{name}': {a!r} != {b!r}") 

71 return a 

72 

73 return Identity( 

74 size=_merge(self.size, other.size, "size"), # type: ignore[arg-type] 

75 sha256=_merge(self.sha256, other.sha256, "sha256"), # type: ignore[arg-type] 

76 md5=_merge(self.md5, other.md5, "md5"), # type: ignore[arg-type] 

77 zip_identity=_merge( # type: ignore[arg-type] 

78 self.zip_identity, other.zip_identity, "zip_identity" 

79 ), 

80 ) 

81 

82 def best_key(self) -> tuple[str, str]: 

83 """Return the strongest available ``(kind, value)`` pair. 

84 

85 Precedence: ``sha256`` > ``md5`` > ``zip_identity`` (serialized as 

86 ``"crc32:size:name"``). 

87 

88 Raises: 

89 ValueError: if all hash fields are ``None``. 

90 """ 

91 if self.sha256 is not None: 

92 return ("sha256", self.sha256) 

93 if self.md5 is not None: 

94 return ("md5", self.md5) 

95 if self.zip_identity is not None: 

96 return ("zip_identity", self.zip_identity.serialized()) 

97 raise ValueError("Identity has no hash fields set — cannot determine best_key")