forked from lichess-bot-devs/lichess-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
48 lines (38 loc) · 2.35 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
import yaml
import os
import os.path
def load_config():
with open("./config.yml", 'r') as stream:
try:
CONFIG = yaml.load(stream)
except Exception as e:
print("There appears to be a syntax problem with your config.yml")
raise e
#[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.."],
["max_concurrent_games", int, "Section `max_concurrent_games` must be an integer number without quotes."],
["supported_tc", list, "Section `supported_tc` must be a list with indented entries starting with dashes.."],
["supported_modes", list, "Section `supported_modes` must be a list with indented entries starting with dashes.."]]
for section in sections:
if section[0] not in CONFIG:
raise Exception("Your config.yml does not have required section `{}`.".format(section[0]))
elif not isinstance(CONFIG[section[0]], section[1]):
raise Exception(section[2])
engine_sections = ["dir", "name"]
for subsection in engine_sections:
if subsection not in CONFIG["engine"]:
raise Exception("Your config.yml does not have required `engine` subsection `{}`.".format(subsection))
if not isinstance(CONFIG["engine"][subsection], str):
raise Exception("Engine subsection `{}` must be a string wrapped in quotes.".format(subsection))
if CONFIG["token"] == "xxxxxxxxxxxxxxxx":
raise Exception("Your config.yml has the default Lichess API token. This is probably wrong.")
if not os.path.isdir(CONFIG["engine"]["dir"]):
raise Exception("Your engine directory `{}` is not a directory.")
engine = os.path.join(CONFIG["engine"]["dir"], CONFIG["engine"]["name"])
if not os.path.isfile(engine):
raise Exception("The engine %s file does not exist." % engine)
if not os.access(engine, os.X_OK):
raise Exception("The engine %s doesn't have execute (x) permission. Try: chmod +x %s" % (engine, engine))
return CONFIG