-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
118 lines (95 loc) · 4.22 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import logging
import re
import os
import yaml
import sys
from errors import ConfigError
logger = logging.getLogger()
class Config(object):
def __init__(self, filepath):
"""
Args:
filepath (str): Path to config file
"""
if not os.path.isfile(filepath):
raise ConfigError(f"Config file '{filepath}' does not exist")
# Load in the config file at the given filepath
with open(filepath) as file_stream:
config = yaml.full_load(file_stream.read())
# Logging setup
formatter = logging.Formatter(
"%(asctime)s | %(name)s [%(levelname)s] %(message)s"
)
log_dict = config.get("logging", {})
log_level = log_dict.get("level", "INFO")
logger.setLevel(log_level)
file_logging = log_dict.get("file_logging", {})
file_logging_enabled = file_logging.get("enabled", False)
file_logging_filepath = file_logging.get("filepath", "bot.log")
if file_logging_enabled:
handler = logging.FileHandler(file_logging_filepath)
handler.setFormatter(formatter)
logger.addHandler(handler)
console_logging = log_dict.get("console_logging", {})
console_logging_enabled = console_logging.get("enabled", True)
if console_logging_enabled:
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)
# Database setup
database_dict = config.get("database", {})
self.database_filepath = database_dict.get("filepath")
# Whitelist setups
self.invite_whitelist = config.get("invite_whitelist")
if not type(self.invite_whitelist) == list:
raise ConfigError(
"Leave the list empty if the invite whitelist should be disabled."
)
elif not self.invite_whitelist:
self.invite_whitelist_enabled = False
else:
self.invite_whitelist_enabled = True
self.admin_whitelist = config.get("admin_whitelist")
if not type(self.admin_whitelist) == list:
raise ConfigError(
"Leave the list empty if the admin whitelist should be disabled."
)
elif not self.admin_whitelist:
self.admin_whitelist_enabled = False
else:
self.admin_whitelist_enabled = True
# Matrix bot account setup
matrix = config.get("matrix", {})
self.user_id = matrix.get("user_id")
if not self.user_id:
raise ConfigError("matrix.user_id is a required field")
elif not re.match("@.*:.*", self.user_id):
raise ConfigError("matrix.user_id must be in the form @name:domain")
self.access_token = matrix.get("access_token")
if not self.access_token:
raise ConfigError("matrix.access_token is a required field")
self.device_id = matrix.get("device_id", "cribbage bot")
self.homeserver_url = matrix.get("homeserver_url")
if not self.homeserver_url:
raise ConfigError("matrix.homeserver_url is a required field")
# ombi setup
ombi = config.get("ombi", {})
self.url = ombi.get("url")
if not self.url:
raise ConfigError("ombi.url is a required field")
self.ombi_apikey = ombi.get("apikey")
if not self.ombi_apikey:
raise ConfigError("ombi.apikey is a required field")
if not len(self.ombi_apikey) == 32:
raise ConfigError("Length of ombi.apikey is not correct")
# moviedb setup
moviedb = config.get("moviedb", {})
self.language = moviedb.get("language")
if not self.language:
raise ConfigError("moviedb.language is a required field")
self.moviedb_apikey = moviedb.get("apikey")
if not self.moviedb_apikey:
raise ConfigError("moviedb.apikey is a required field")
if not len(self.moviedb_apikey) == 32:
raise ConfigError("Length of moviedb.apikey is not correct")
self.command_prefix = config.get("command_prefix", "!c") + " "