-
Notifications
You must be signed in to change notification settings - Fork 34
/
config.py
57 lines (44 loc) · 2.68 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
import yaml
import os
import os.path
import logging
logger = logging.getLogger(__name__)
def load_config(config_file):
with open(config_file) as stream:
try:
CONFIG = yaml.safe_load(stream)
except Exception as e:
logger.error("There appears to be a syntax problem with your config.yml")
raise e
if "LISHOGI_BOT_TOKEN" in os.environ:
CONFIG["token"] = os.environ["LISHOGI_BOT_TOKEN"]
# [section, type, error message]
sections = [["token", str, "Section `token` must be a string wrapped in quotes."],
["url", str, "Section `url` must be a string wrapped in quotes."],
["engine", dict, "Section `engine` must be a dictionary with indented keys followed by colons.."],
["challenge", dict, "Section `challenge` must be a dictionary with indented keys followed by colons.."]]
for section in sections:
if section[0] not in CONFIG:
raise Exception(f"Your config.yml does not have required section `{section[0]}`.")
elif not isinstance(CONFIG[section[0]], section[1]):
raise Exception(section[2])
engine_sections = [["dir", str, "´dir´ must be a string wrapped in quotes."],
["name", str, "´name´ must be a string wrapped in quotes."]]
for subsection in engine_sections:
if subsection[0] not in CONFIG["engine"]:
raise Exception(f"Your config.yml does not have required `engine` subsection `{subsection}`.")
if not isinstance(CONFIG["engine"][subsection[0]], subsection[1]):
raise Exception(f"´engine´ subsection {subsection[2]}")
if CONFIG["token"] == "xxxxxxxxxxxxxxxx":
raise Exception("Your config.yml has the default Lishogi API token. This is probably wrong.")
if not os.path.isdir(CONFIG["engine"]["dir"]):
raise Exception(f'Your engine directory `{CONFIG["engine"]["dir"]}` is not a directory.')
working_dir = CONFIG["engine"].get("working_dir")
if working_dir and not os.path.isdir(working_dir):
raise Exception(f"Your engine's working directory `{working_dir}` is not a directory.")
engine = os.path.join(CONFIG["engine"]["dir"], CONFIG["engine"]["name"])
if not os.path.isfile(engine) and CONFIG["engine"]["protocol"] != "homemade":
raise Exception(f"The engine {engine} file does not exist.")
if not os.access(engine, os.X_OK) and CONFIG["engine"]["protocol"] != "homemade":
raise Exception(f"The engine {engine} doesn't have execute (x) permission. Try: chmod +x {engine}")
return CONFIG