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

27 statements  

1from dataclasses import dataclass 

2from datetime import datetime 

3from pathlib import Path 

4from typing import Optional, Union 

5 

6from yenta.utils.files import file_hash 

7 

8 

9@dataclass 

10class Artifact: 

11 

12 location: Union[str, Path] 

13 date_created: str = None 

14 hash: Optional[str] = None 

15 meta: dict = None 

16 

17 def artifact_hash(self): 

18 raise NotImplementedError 

19 

20 def __post_init__(self): 

21 

22 if not self.date_created: 

23 self.date_created = str(datetime.now()) 

24 

25 def __eq__(self, other): 

26 

27 return self.location == other.location and self.hash == other.hash 

28 

29 

30@dataclass 

31class FileArtifact(Artifact): 

32 

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() 

38 

39 def artifact_hash(self): 

40 return file_hash(self._path).hexdigest()