Coverage for src/shephex/experiment/meta.py: 89%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-03-30 20:58 +0200

1import json 

2from pathlib import Path 

3from typing import Self 

4 

5 

6class Meta(dict): 

7 def __init__(self, name: str ='meta.json', **kwargs) -> None: 

8 super().__init__(**kwargs) 

9 self.name = name 

10 

11 def dump(self, directory: Path) -> None: 

12 with open(directory / self.name, 'w') as f: 

13 json.dump(self, f, indent=4) 

14 

15 def load(self, directory: Path) -> None: 

16 with open(directory / self.name, 'r') as f: 

17 data = json.load(f) 

18 for key, value in data.items(): 

19 self.update(key, value) 

20 

21 def update(self, key: str, value: str) -> None: 

22 self[key] = value 

23 

24 @classmethod 

25 def from_file(cls, directory: Path, retries: int = 0) -> Self: 

26 meta = cls() 

27 try: 

28 meta.load(directory) 

29 except: 

30 print(f"Directory: {directory} was unreadable, check the job. Exiting") 

31 exit() 

32 return meta 

33 

34 def get_dict(self) -> dict: 

35 return dict(self.copy())