from dataclasses import dataclass from uuid import uuid4 from config import ENVIRONMENT from .timestamp import Timestamp __all__ = ["Key", "ParseKeyError"] class ParseKeyError(Exception): pass @dataclass(frozen=True) class Key: environment: str = ENVIRONMENT timestamp: Timestamp = Timestamp.from_sf() uuid: str = str(uuid4()) @classmethod def from_string(cls, key: str) -> "Key": try: environment, timestamp, uuid = key.rsplit("_", 2) except ValueError as e: raise ParseKeyError(f"'{key}' is invalid key") from e return cls(environment, Timestamp.from_string(timestamp), uuid) def __str__(self) -> str: return f"{self.environment}_{self.timestamp}_{self.uuid}"