Coverage for paikalta/api.py: 95.18%

51 statements  

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

1"""From the place (Finnish: paikalta) we derive the name - application programming interface.""" 

2 

3import argparse 

4import pathlib 

5import re 

6from typing import Union, no_type_check 

7 

8import msgspec 

9from paikalta import ( 

10 COMMA, 

11 ENCODING, 

12 FAIL, 

13 INVALID_ID, 

14 SUCC, 

15 VALID_NAME_PAT, 

16 parse_csl_as_is, 

17) 

18 

19 

20@no_type_check 

21def load(csaf_path): 

22 """Load the CSAF data from the path to the JSON file or fail miserably.""" 

23 with open(csaf_path, 'rt', encoding=ENCODING) as handle: 

24 return msgspec.json.decode(handle.read()) 

25 

26 

27@no_type_check 

28def dump(csaf_data, csaf_path): 

29 """Dump the CSAF data formatted to 2 space indent to the JSON file given by the path.""" 

30 with open(csaf_path, 'wt', encoding=ENCODING) as f: 

31 f.write(msgspec.json.format(msgspec.json.encode(csaf_data)).decode()) 

32 

33 

34@no_type_check 

35def compute_filename(csaf_data): 

36 """Derive the filename from document/tracking/id if exists else return the conventional invalid json name.""" 

37 document_tracking_id = INVALID_ID 

38 if (doc := csaf_data.get('document')) and (track := doc.get('tracking')) and (the_id := track.get('id')): # noqa 

39 document_tracking_id = the_id 

40 

41 return f'{re.sub(VALID_NAME_PAT, "_", document_tracking_id.lower())}.json' 

42 

43 

44@no_type_check 

45def derive(pointer: Union[str, dict[str, object], pathlib.Path]) -> str: 

46 """Derive the filename from data else return the conventional invalid json name.""" 

47 return compute_filename(load(pointer) if isinstance(pointer, (str, pathlib.Path)) else pointer) 

48 

49 

50@no_type_check 

51def filename_is_valid(path: Union[str, pathlib.Path], data: Union[None, dict[str, object]] = None) -> bool: 

52 """Verify the filename from data matches the given (True) else return False.""" 

53 given = pathlib.Path(path).name 

54 data = load(path) if data is None else data 

55 derived = compute_filename(data) 

56 return derived == given 

57 

58 

59@no_type_check 

60def process(options: argparse.Namespace): 

61 """Process the command line request.""" 

62 if options.labels: 

63 options.verbose = True 

64 succ, fail = SUCC, FAIL 

65 if options.labels and COMMA in options.labels: 

66 succ, fail = parse_csl_as_is(options.labels) 

67 

68 data = load(options.input_file) 

69 current_path = pathlib.Path(options.input_file) 

70 correct_path = current_path.parent / compute_filename(data) 

71 ok = current_path == correct_path 

72 

73 if options.echo: 

74 print(correct_path.name) 

75 if options.verbose: 

76 print(succ if ok else fail) 

77 

78 if ok: 

79 return 0 

80 

81 if options.add: 81 ↛ 82line 81 didn't jump to line 82, because the condition on line 81 was never true

82 dump(data, correct_path) 

83 elif options.update: 83 ↛ 84line 83 didn't jump to line 84, because the condition on line 83 was never true

84 _ = current_path.rename(correct_path) 

85 

86 return 1