from dataclasses import dataclass, field, asdict from typing import Dict, Any, List, Optional @dataclass class EpochInfo: major: int minor: int = 0 stability: str = "stable" def to_dict(self) -> Dict[str, Any]: return asdict(self) @classmethod def from_dict(cls, data: Dict[str, Any]) -> "EpochInfo": return cls(**data) @dataclass class ContributorInfo: name: str registered_at: Optional[int] = None contribution_count: int = 0 def to_dict(self) -> Dict[str, Any]: return asdict(self) @classmethod def from_dict(cls, data: Dict[str, Any]) -> "ContributorInfo": return cls(**data) @dataclass class SegmentLineage: segment_id: str start_line: int end_line: int start_byte: int end_byte: int def to_dict(self) -> Dict[str, Any]: return asdict(self) @classmethod def from_dict(cls, data: Dict[str, Any]) -> "SegmentLineage": return cls(**data) @dataclass class CodexEntry: source_file: str source_type: str version: str origin: str epoch: EpochInfo contributor: ContributorInfo segments: List[SegmentLineage] = field(default_factory=list) timestamp: Optional[str] = None def to_dict(self) -> Dict[str, Any]: return { "source_file": self.source_file, "source_type": self.source_type, "version": self.version, "origin": self.origin, "epoch": self.epoch.to_dict(), "contributor": self.contributor.to_dict(), "segments": [s.to_dict() for s in self.segments], "timestamp": self.timestamp } @classmethod def from_dict(cls, data: Dict[str, Any]) -> "CodexEntry": return cls( source_file=data.get("source_file", "unknown"), source_type=data.get("source_type", ".py"), version=data.get("version", "1.0.0"), origin=data.get("origin", "unknown"), epoch=EpochInfo.from_dict(data.get("epoch", {"major": 1})), contributor=ContributorInfo.from_dict(data.get("contributor", {"name": "unknown"})), segments=[SegmentLineage.from_dict(s) for s in data.get("segments", [])], timestamp=data.get("timestamp") )