-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
92 lines (77 loc) · 2.4 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
86
87
88
89
90
91
92
import logging
import os
class Config:
LOG_LEVELS = ["DEBUG", "INFO", "WARNING", "ERROR"]
__config = {
"calibredb": os.environ.get("CALIBRE_REST_PATH", "/opt/calibre/calibredb"),
"library": os.environ.get("CALIBRE_REST_LIBRARY", "/library"),
"bind_addr": os.environ.get("CALIBRE_REST_ADDR", "localhost:5000"),
"username": os.environ.get("CALIBRE_REST_USERNAME", ""),
"password": os.environ.get("CALIBRE_REST_PASSWORD", ""),
"log_level": os.environ.get("CALIBRE_REST_LOG_LEVEL", "INFO"),
"debug": False,
"testing": False,
}
__setters = [
"calibredb",
"library",
"bind_addr",
"username",
"password",
"log_level",
"debug",
"testing",
]
@staticmethod
def config():
return Config.__config
@staticmethod
def get(key):
return Config.__config[key]
@staticmethod
def set(key, value):
if key in Config.__setters:
if key == "log_level":
if value not in Config.LOG_LEVELS:
logging.warning(
f'Log level "{value}" not supported. Setting log level to "INFO"'
)
Config.__config[key] = "INFO"
return
Config.__config[key] = value
else:
raise NameError(f'Key "{key}" not accepted in Config')
class DevConfig(Config):
def __init__(
self,
calibredb=None,
library=None,
bind_addr=None,
username=None,
password=None,
log_level=None,
):
for k, v in locals().items():
if v is not None and k != "self":
Config.set(k, v)
Config.set("log_level", os.environ.get("CALIBRE_REST_LOG_LEVEL", "DEBUG"))
Config.set("debug", True)
class TestConfig(Config):
def __init__(self, calibredb=None, library=None, bind_addr=None):
for k, v in locals().items():
if v is not None and k != "self":
Config.set(k, v)
Config.set("testing", True)
class ProdConfig(Config):
def __init__(
self,
calibredb=None,
library=None,
bind_addr=None,
username=None,
password=None,
log_level=None,
):
for k, v in locals().items():
if v is not None and k != "self":
Config.set(k, v)