This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
84 lines (66 loc) · 3.01 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
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")
# 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")
self.command_prefix = config.get("command_prefix", "!c") + " "
show = config.get("show", {})
self.title = show.get("title", {})
self.all_episode_text = show.get("all_episode_text")
self.episodes = show.get("episodes", {})
self.episode_titles_to_link = {}
for _, season in self.episodes.items():
for _, episode in season.items():
self.episode_titles_to_link[episode["name"]] = episode["link"]
self.episode_titles_to_code = {}
for s, season in self.episodes.items():
for e, episode in season.items():
self.episode_titles_to_code[episode["name"]] = "S%sE%s" % (str(s).zfill(2), str(e).zfill(2))