Coverage for src/artemis_sg/config/__init__.py: 44%

49 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-05 09:33 -0700

1import logging 

2import os 

3 

4from flatten_dict import flatten, unflatten 

5 

6import artemis_sg 

7 

8namespace = "artemis_sg.config" 

9 

10# Everyghing that can be configured is here. 

11CFG = { 

12 "asg": { 

13 "vendors": [ 

14 {"code": "sample", "name": "Sample Vendor", "isbn_key": "ISBN-13"}, 

15 {"code": "sample2", "name": "Another Vendor", "isbn_key": "ISBN"}, 

16 ], 

17 "spreadsheet": { 

18 "sheet_image": { 

19 "image_row_height": 105, 

20 "image_col_width": 18, 

21 "isbn_col_width": 13, 

22 "max_col_width": 50, 

23 "col_buffer": 1.23, 

24 }, 

25 "mkthumbs": { 

26 "width": 130, 

27 "height": 130, 

28 }, 

29 }, 

30 "scraper": { 

31 "headless": False, 

32 }, 

33 "data": { 

34 "file": { 

35 "scraped": os.path.join(artemis_sg.data_dir, "scraped_items.json"), 

36 }, 

37 "dir": { 

38 "images": os.path.join(artemis_sg.data_dir, "downloaded_images"), 

39 "upload_source": os.path.join(artemis_sg.data_dir, "downloaded_images"), 

40 }, 

41 }, 

42 "test": { 

43 "sheet": {"id": "GOOGLE_SHEET_ID_HERE", "tab": "GOOGLE_SHEET_TAB_HERE"} 

44 }, 

45 }, 

46 "google": { 

47 "cloud": { 

48 "bucket": "my_bucket", 

49 "bucket_prefix": "my_bucket_prefix", 

50 "key_file": os.path.join( 

51 artemis_sg.data_dir, "google_cloud_service_key.json" 

52 ), 

53 }, 

54 "docs": { 

55 "api_creds_file": os.path.join(artemis_sg.data_dir, "credentials.json"), 

56 "api_creds_token": os.path.join( 

57 artemis_sg.data_dir, "app_creds_token.json" 

58 ), 

59 }, 

60 }, 

61} 

62 

63try: 

64 import tomllib 

65except ModuleNotFoundError: 

66 import tomli as tomllib 

67 

68conf_file = "config.toml" 

69 

70conf_path = os.path.join(artemis_sg.conf_dir, conf_file) 

71 

72try: 

73 with open(conf_path, mode="rb") as fp: 

74 f_config = tomllib.load(fp) 

75except FileNotFoundError: 

76 import tomli_w 

77 

78 logging.warning(f"{namespace}: Config file not found at {conf_path}.") 

79 logging.warning(f"{namespace}: Creating new config file at {conf_path}.") 

80 logging.warning( 

81 f"{namespace}: IMPORTANT: Edit file to set proper values for google_cloud." 

82 ) 

83 

84 d = os.path.dirname(conf_path) 

85 if not os.path.exists(d): 

86 os.makedirs(d) 

87 with open(conf_path, mode="wb") as fp: 

88 tomli_w.dump(CFG, fp) 

89 with open(conf_path, mode="rb") as fp: 

90 f_config = tomllib.load(fp) 

91 

92# Update CFG with contents of f_config 

93flat_cfg = flatten(CFG) 

94flat_f_config = flatten(f_config) 

95flat_merged = flat_cfg | flat_f_config 

96CFG = unflatten(flat_merged) 

97 

98# Create all defined data_dir subdirectories 

99for key in CFG["asg"]["data"]["dir"]: 

100 d = CFG["asg"]["data"]["dir"][key] 

101 if not os.path.exists(d): 101 ↛ 102line 101 didn't jump to line 102, because the condition on line 101 was never true

102 logging.warning(f"{namespace}: Creating new directory at {d}.") 

103 os.makedirs(d) 

104 

105# Create all defined data_dir files 

106for key in CFG["asg"]["data"]["file"]: 

107 f = CFG["asg"]["data"]["file"][key] 

108 if not os.path.exists(f): 108 ↛ 109line 108 didn't jump to line 109, because the condition on line 108 was never true

109 d = os.path.dirname(f) 

110 if not os.path.exists(d): 

111 logging.warning(f"{namespace}: Creating new directory at {d}.") 

112 os.makedirs(d) 

113 logging.warning(f"{namespace}: Creating new file at {f}.") 

114 _root, ext = os.path.splitext(f) 

115 with open(f, "w") as fp: 

116 # Seed JSON files with valid empty JSON. 

117 if ext.lower() == ".json": 

118 fp.write("{ }") 

119 pass