This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
85 lines (66 loc) · 2.34 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
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
import logging.handlers
import yaml
class InvalidConfigError(ValueError):
"""An invalid configuration was detected."""
def __init__(self) -> None:
"""Initialize the error message for invalid configuration."""
super().__init__('An invalid configuration was detected.')
class TimeError(ValueError):
"""Offsets are not valid times."""
def __init__(self, message: str) -> None:
"""Initialize the time error with a message."""
super().__init__(message)
class ShutdownDisabled(Warning):
"""Shutdown (switch_off) scripts are disabled in the configuration."""
def __init__(self) -> None:
"""Initialize the warning with a message."""
super().__init__('switch_off in the configuration is disabled.')
_LOGGER_NAME = 'Sunsetter'
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)
_MAX_H = 24
_MAX_M = 60
try:
with open('config.yaml', 'r') as f:
CONF = yaml.safe_load(f)
APPID = CONF['appid']
OFFSET = CONF['sunset']['offset']
OFFSET_H = OFFSET['hours']
OFFSET_M = OFFSET['minutes']
SHUTDOWN = CONF['shutdown']
SCRIPTS = CONF['scripts']
if OFFSET_H not in range(-_MAX_H + 1, _MAX_H):
raise TimeError('OFFSET_H is not within 24 hours')
if OFFSET_M not in range(-_MAX_M + 1, _MAX_M):
raise TimeError('OFFSET_M exceeds 60 minutes (use hours)')
except FileNotFoundError:
LOGGER.error(
'config.yaml was not found. Create it from config.yaml.example.')
LOGGER.warn('Exiting...')
raise InvalidConfigError
except (KeyError, TypeError, ValueError) as e:
LOGGER.error('config.yaml is malformed. More information:')
LOGGER.error(e)
LOGGER.warn('Exiting...')
raise InvalidConfigError
except TimeError as e:
LOGGER.error('Offset values are invalid. More information:')
LOGGER.error(e)
LOGGER.warn('Exiting...')
raise InvalidConfigError