Coverage for stativ/delta_store.py: 66.20%

45 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 22:28:28 +00:00

1import json 

2import pathlib 

3from typing import no_type_check 

4 

5ENCODING = 'utf-8' 

6 

7 

8@no_type_check 

9def is_delta_store(delta_store_root: pathlib.Path, deep_inspection: bool = False) -> bool: 

10 """Belts and braces.""" 

11 if not delta_store_root.is_dir(): 

12 return False 

13 

14 store_path = pathlib.Path(delta_store_root, 'store') 

15 if not store_path.is_dir(): 

16 return False 

17 

18 proxy_path = pathlib.Path(store_path, 'proxy.json') 

19 if not proxy_path.is_file(): 19 ↛ 20line 19 didn't jump to line 20, because the condition on line 19 was never true

20 return False 

21 

22 if deep_inspection: 22 ↛ 23line 22 didn't jump to line 23, because the condition on line 22 was never true

23 try: 

24 with open(proxy_path, 'rt', encoding=ENCODING) as handle: 

25 _ = json.load(handle) 

26 except json.JSONDecodeError: 

27 return False 

28 

29 return True 

30 

31 

32@no_type_check 

33def _dump(aspect_store: dict, path: pathlib.Path, indent: bool = False) -> bool: 

34 """DRY.""" 

35 options = {'indent': 2} if indent else {} 

36 try: 

37 with open(path, 'wt', encoding=ENCODING) as handle: 

38 json.dump(aspect_store, handle, **options) 

39 except Exception: # pylint: disable=broad-except 

40 return False 

41 return True 

42 

43 

44@no_type_check 

45def dump_gone(aspect_store: dict, indent: bool = False) -> bool: 

46 """Not too dry ...""" 

47 return _dump(aspect_store, pathlib.Path('gone.json'), indent) 

48 

49 

50@no_type_check 

51def dump_change(aspect_store: dict, indent: bool = False) -> bool: 

52 """Not too dry ...""" 

53 return _dump(aspect_store, pathlib.Path('change.json'), indent) 

54 

55 

56@no_type_check 

57def dump_enter(aspect_store: dict, indent: bool = False) -> bool: 

58 """Not too dry ...""" 

59 return _dump(aspect_store, pathlib.Path('enter.json'), indent) 

60 

61 

62@no_type_check 

63def dump_keep(aspect_store: dict, indent: bool = False) -> bool: 

64 """Not too dry ...""" 

65 return _dump(aspect_store, pathlib.Path('keep.json'), indent) 

66 

67 

68@no_type_check 

69def dump_remain(aspect_store: dict, indent: bool = False) -> bool: 

70 """Not too dry ...""" 

71 return _dump(aspect_store, pathlib.Path('remain.json'), indent)