Coverage for src/artemis_sg/config/__init__.py: 44%
49 statements
« prev ^ index » next coverage.py v7.3.1, created at 2024-03-06 08:01 -0800
« prev ^ index » next coverage.py v7.3.1, created at 2024-03-06 08:01 -0800
1import logging
2import os
4from flatten_dict import flatten, unflatten
6import artemis_sg
8namespace = "artemis_sg.config"
10# Everyghing that can be configured is here.
11CFG = {
12 "asg": {
13 "vendors": [
14 {"code": "sample",
15 "name": "Sample Vendor",
16 "isbn_key": "ISBN-13",
17 "failover_scraper": ""},
18 {"code": "sample2",
19 "name": "Another Vendor",
20 "isbn_key": "ISBN",
21 "failover_scraper": "AmznUkScraper"},
22 ],
23 "item": {
24 "sort_order": [
25 "TITLE",
26 "SUBTITLE",
27 "AUTHOR",
28 "PUBLISHER",
29 "PUB DATE",
30 "PUBLISHERDATE",
31 "FORMAT",
32 ],
33 },
34 "spreadsheet": {
35 "sheet_image": {
36 "col_order": [
37 "ISBN",
38 "IMAGE",
39 "ORDER",
40 ],
41 "image_row_height": 105,
42 "image_col_width": 18,
43 "isbn_col_width": 13,
44 "max_col_width": 50,
45 "col_buffer": 1.23,
46 },
47 "mkthumbs": {
48 "width": 130,
49 "height": 130,
50 },
51 },
52 "scraper": {
53 "headless": False,
54 "login_timeout": 90,
55 "gjscraper": {
56 "sentinel_publisher": "Abbeville",
57 },
58 },
59 "data": {
60 "file": {
61 "scraped": os.path.join(artemis_sg.data_dir, "scraped_items.json"),
62 },
63 "dir": {
64 "images": os.path.join(artemis_sg.data_dir, "downloaded_images"),
65 "upload_source": os.path.join(artemis_sg.data_dir, "downloaded_images"),
66 },
67 },
68 "slide_generator": {
69 "title_default": "New Arrivals",
70 "line_spacing": 1,
71 "text_width": 80,
72 "max_fontsize": 18,
73 "slide_max_batch": 25,
74 "slide_ppi": 96,
75 "slide_w": 10.0,
76 "slide_h": 5.625,
77 "gutter": 0.375,
78 "text_box_resize_img_threshold": 2,
79 "logo_h": 1,
80 "logo_w": 1,
81 "addl_img_h": 1.5,
82 "addl_img_w": 3,
83 "logo_url": "https://images.squarespace-cdn.com/content/v1/6110970ca45ca157a1e98b76/e4ea0607-01c0-40e0-a7c0-b56563b67bef/artemis.png?format=1500w",
84 "blacklist_keys": (
85 "IMAGE",
86 "ON HAND",
87 "ORDER",
88 "ORDER QTY",
89 "GJB SUGGESTED",
90 "DATE RECEIVED",
91 "SUBJECT",
92 "QTYINSTOCK",
93 "QTY",
94 "SALESPRICE",
95 "AVAILABLE START DATE",
96 "CATEGORY",
97 "LINK",
98 ),
99 "gj_binding_map": {
100 "P": "Paperback",
101 "H": "Hardcover",
102 "C": "Hardcover",
103 "C NDJ": "Cloth, no dust jacket",
104 "CD": "CD",
105 },
106 "gj_type_map": {
107 "R": "Remainder",
108 "H": "Return"
109 },
110 "bg_color": "black",
111 "text_color": "white",
112 "tiny_isbn_x_inset": 1.0,
113 "tiny_isbn_fontsize": 6,
114 "text_box_max_lines": 36,
115 "text_box_resized_max_lines": 28,
116 "text_map": {
117 "AUTHOR": "by {t}",
118 "PUB LIST": "List Price: {t}",
119 "LISTPRICE": "List Price: {t}",
120 "USD COST": "USD Cost: ${t}",
121 "RRP": "List price: £{t}",
122 "BARGAIN": "Bargain: £{t}",
123 "NET COST": "Your Net Price: {t}",
124 "YOUR NET PRICE": "Your Net Price: {t}",
125 "PUB DATE": "Pub Date: {t}",
126 "PUBLISHERDATE": "Pub Date: {t}",
127 "BINDING": "Format: {t}",
128 "FORMAT": "Format: {t}",
129 "TYPE": "Type: {t}",
130 "PAGES": "Pages: {t} pp.",
131 "SIZE": "Size: {t}",
132 "ITEM#": "Item #: {t}",
133 "TBCODE": "Item #: {t}",
134 },
135 },
136 "test": {
137 "sheet": {"id": "GOOGLE_SHEET_ID_HERE", "tab": "GOOGLE_SHEET_TAB_HERE"}
138 },
139 },
140 "google": {
141 "cloud": {
142 "new_threshold_secs": 3600,
143 "bucket": "my_bucket",
144 "bucket_prefix": "my_bucket_prefix",
145 "key_file": os.path.join(
146 artemis_sg.data_dir, "google_cloud_service_key.json"
147 ),
148 },
149 "docs": {
150 "api_creds_file": os.path.join(artemis_sg.data_dir, "credentials.json"),
151 "api_creds_token": os.path.join(
152 artemis_sg.data_dir, "app_creds_token.json"
153 ),
154 },
155 },
156}
158try:
159 import tomllib
160except ModuleNotFoundError:
161 import tomli as tomllib
163conf_file = "config.toml"
165conf_path = os.path.join(artemis_sg.conf_dir, conf_file)
167try:
168 with open(conf_path, mode="rb") as fp:
169 f_config = tomllib.load(fp)
170except FileNotFoundError:
171 import tomli_w
173 logging.warning(f"{namespace}: Config file not found at {conf_path}.")
174 logging.warning(f"{namespace}: Creating new config file at {conf_path}.")
175 logging.warning(
176 f"{namespace}: IMPORTANT: Edit file to set proper values for google_cloud."
177 )
179 d = os.path.dirname(conf_path)
180 if not os.path.exists(d):
181 os.makedirs(d)
182 with open(conf_path, mode="wb") as fp:
183 tomli_w.dump(CFG, fp)
184 with open(conf_path, mode="rb") as fp:
185 f_config = tomllib.load(fp)
187# Update CFG with contents of f_config
188flat_cfg = flatten(CFG)
189flat_f_config = flatten(f_config)
190flat_merged = flat_cfg | flat_f_config
191CFG = unflatten(flat_merged)
193# Create all defined data_dir subdirectories
194for key in CFG["asg"]["data"]["dir"]:
195 d = CFG["asg"]["data"]["dir"][key]
196 if not os.path.exists(d): 196 ↛ 197line 196 didn't jump to line 197, because the condition on line 196 was never true
197 logging.warning(f"{namespace}: Creating new directory at {d}.")
198 os.makedirs(d)
200# Create all defined data_dir files
201for key in CFG["asg"]["data"]["file"]:
202 f = CFG["asg"]["data"]["file"][key]
203 if not os.path.exists(f): 203 ↛ 204line 203 didn't jump to line 204, because the condition on line 203 was never true
204 d = os.path.dirname(f)
205 if not os.path.exists(d):
206 logging.warning(f"{namespace}: Creating new directory at {d}.")
207 os.makedirs(d)
208 logging.warning(f"{namespace}: Creating new file at {f}.")
209 _root, ext = os.path.splitext(f)
210 with open(f, "w") as fp:
211 # Seed JSON files with valid empty JSON.
212 if ext.lower() == ".json":
213 fp.write("{ }")
214 pass