Coverage for /Users/jerry/Development/yenta/yenta/artifacts/Artifact.py: 96%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from dataclasses import dataclass
2from datetime import datetime
3from pathlib import Path
4from typing import Optional, Union
6from yenta.utils.files import file_hash
9@dataclass
10class Artifact:
12 location: Union[str, Path]
13 date_created: str = None
14 hash: Optional[str] = None
15 meta: dict = None
17 def artifact_hash(self):
18 raise NotImplementedError
20 def __post_init__(self):
22 if not self.date_created:
23 self.date_created = str(datetime.now())
25 def __eq__(self, other):
27 return self.location == other.location and self.hash == other.hash
30@dataclass
31class FileArtifact(Artifact):
33 def __init__(self, *args, **kwargs):
34 super().__init__(*args, **kwargs)
35 self._path: Path = Path(self.location)
36 if self._path.exists() and not self._path.is_dir():
37 self.hash = self.artifact_hash()
39 def artifact_hash(self):
40 return file_hash(self._path).hexdigest()