File size: 2,745 Bytes
ee0883c
 
 
f01cd90
 
 
ee0883c
1e74ff3
adf5f6e
 
ee0883c
c937d6c
ee0883c
d418201
ee0883c
17d9988
4fdda60
 
ee0883c
1e74ff3
ee0883c
 
 
3c17c01
ee0883c
 
17d9988
ee0883c
 
 
bf4870b
f07b408
ee0883c
bf4870b
17d9988
bf4870b
17d9988
ee0883c
 
6611b2f
ee0883c
 
17d9988
 
 
 
 
ee0883c
b9e0a3b
 
11d8599
cf2f8df
b9e0a3b
11d8599
cf2f8df
b9e0a3b
7d6bf0e
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