-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
72 lines (54 loc) · 1.62 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
import logging
import logging.handlers
from pathlib import Path
from typing import Dict, Union
_LOGGER_NAME = 'sorter-gpm'
LOGGER = logging.getLogger(_LOGGER_NAME)
LOGGER.setLevel(logging.DEBUG)
_FH = logging.handlers.RotatingFileHandler(
f'{_LOGGER_NAME}.log',
maxBytes=40960,
backupCount=5,
)
_FH.setLevel(logging.DEBUG)
_CH = logging.StreamHandler()
_CH.setLevel(logging.WARNING)
_FORMATTER = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
_FH.setFormatter(_FORMATTER)
_CH.setFormatter(_FORMATTER)
LOGGER.addHandler(_FH)
LOGGER.addHandler(_CH)
_CONFIG_LOAD_ERRORS = (
FileNotFoundError,
KeyError,
TypeError,
ValueError,
json.decoder.JSONDecodeError,
)
_FIELDS = ('Artist', 'Album', 'Title')
CORR: Dict[str, Union[Dict[str, str], Dict]] = {}
def insert_missing_corrections() -> None:
"""Insert empty dictionaries into corrections if not present."""
for field in _FIELDS:
if field not in CORR:
CORR[field] = {}
try:
with open('config.json', 'r') as f:
CONFIG = json.load(f)
FMT = CONFIG['format']
_TAKEOUT = Path(CONFIG['takeout_dir']).expanduser()
TRACKS = _TAKEOUT / 'Google Play Music' / 'Tracks'
DEST = Path(CONFIG['dest_dir']).expanduser()
except _CONFIG_LOAD_ERRORS as e:
LOGGER.error("config.json doesn't exist or is malformed.")
LOGGER.error(f'More information: {e}')
raise e
try:
with open('corrections.json', 'r') as f:
CORR = json.load(f)
except _CONFIG_LOAD_ERRORS:
LOGGER.info("corrections.json doesn't exist. Skipping.")
insert_missing_corrections()