forked from oivas000/lichess-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
220 lines (183 loc) · 12.5 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import yaml
import os
import os.path
import logging
import math
from abc import ABCMeta
from enum import Enum
from typing import Dict, Any
CONFIG_DICT_TYPE = Dict[str, Any]
logger = logging.getLogger(__name__)
class DelayType(str, Enum):
NONE = "none"
COARSE = "coarse"
FINE = "fine"
class Configuration:
def __init__(self, parameters: CONFIG_DICT_TYPE) -> None:
self.config = parameters
def __getattr__(self, name: str) -> Any:
return self.lookup(name)
def lookup(self, name: str) -> Any:
data = self.config.get(name)
return Configuration(data) if isinstance(data, dict) else data
def items(self) -> Any:
return self.config.items()
def __bool__(self) -> bool:
return bool(self.config)
def __getstate__(self) -> CONFIG_DICT_TYPE:
return self.config
def __setstate__(self, d: CONFIG_DICT_TYPE) -> None:
self.config = d
def config_assert(assertion: bool, error_message: str) -> None:
if not assertion:
raise Exception(error_message)
def check_config_section(config: CONFIG_DICT_TYPE, data_name: str, data_type: ABCMeta, subsection: str = "") -> None:
config_part = config[subsection] if subsection else config
sub = f"`{subsection}` sub" if subsection else ""
data_location = f"`{data_name}` subsection in `{subsection}`" if subsection else f"Section `{data_name}`"
type_error_message = {str: f"{data_location} must be a string wrapped in quotes.",
dict: f"{data_location} must be a dictionary with indented keys followed by colons."}
config_assert(data_name in config_part, f"Your config.yml does not have required {sub}section `{data_name}`.")
config_assert(isinstance(config_part[data_name], data_type), type_error_message[data_type])
def set_config_default(config: CONFIG_DICT_TYPE, *sections: str, key: str, default: Any,
force_empty_values: bool = False) -> CONFIG_DICT_TYPE:
subconfig = config
for section in sections:
subconfig = subconfig.setdefault(section, {})
if not isinstance(subconfig, dict):
raise Exception(f'The {section} section in {sections} should hold a set of key-value pairs, not a value.')
if force_empty_values:
if subconfig.get(key) in [None, ""]:
subconfig[key] = default
else:
subconfig.setdefault(key, default)
return subconfig
def change_value_to_list(config: CONFIG_DICT_TYPE, *sections: str, key: str) -> None:
subconfig = set_config_default(config, *sections, key=key, default=[])
if subconfig[key] is None:
subconfig[key] = []
if not isinstance(subconfig[key], list):
subconfig[key] = [subconfig[key]]
def insert_default_values(CONFIG: CONFIG_DICT_TYPE) -> None:
set_config_default(CONFIG, key="abort_time", default=20)
set_config_default(CONFIG, key="move_overhead", default=1000)
set_config_default(CONFIG, key="rate_limiting_delay", default=0)
set_config_default(CONFIG, "engine", key="working_dir", default=os.getcwd(), force_empty_values=True)
set_config_default(CONFIG, "engine", key="silence_stderr", default=False)
set_config_default(CONFIG, "engine", "draw_or_resign", key="offer_draw_enabled", default=False)
set_config_default(CONFIG, "engine", "draw_or_resign", key="offer_draw_for_egtb_zero", default=True)
set_config_default(CONFIG, "engine", "draw_or_resign", key="resign_enabled", default=False)
set_config_default(CONFIG, "engine", "draw_or_resign", key="resign_for_egtb_minus_two", default=True)
set_config_default(CONFIG, "engine", "draw_or_resign", key="resign_moves", default=3)
set_config_default(CONFIG, "engine", "draw_or_resign", key="resign_score", default=-1000)
set_config_default(CONFIG, "engine", "draw_or_resign", key="offer_draw_moves", default=5)
set_config_default(CONFIG, "engine", "draw_or_resign", key="offer_draw_score", default=0)
set_config_default(CONFIG, "engine", "draw_or_resign", key="offer_draw_pieces", default=10)
set_config_default(CONFIG, "engine", "online_moves", key="max_out_of_book_moves", default=10)
set_config_default(CONFIG, "engine", "online_moves", key="max_retries", default=2, force_empty_values=True)
set_config_default(CONFIG, "engine", "online_moves", "online_egtb", key="enabled", default=False)
set_config_default(CONFIG, "engine", "online_moves", "online_egtb", key="source", default="lichess")
set_config_default(CONFIG, "engine", "online_moves", "online_egtb", key="min_time", default=20)
set_config_default(CONFIG, "engine", "online_moves", "online_egtb", key="max_pieces", default=7)
set_config_default(CONFIG, "engine", "online_moves", "online_egtb", key="move_quality", default="best")
set_config_default(CONFIG, "engine", "online_moves", "chessdb_book", key="enabled", default=False)
set_config_default(CONFIG, "engine", "online_moves", "chessdb_book", key="min_time", default=20)
set_config_default(CONFIG, "engine", "online_moves", "chessdb_book", key="move_quality", default="good")
set_config_default(CONFIG, "engine", "online_moves", "chessdb_book", key="min_depth", default=20)
set_config_default(CONFIG, "engine", "online_moves", "chessdb_book", key="contribute", default=True)
set_config_default(CONFIG, "engine", "online_moves", "lichess_cloud_analysis", key="enabled", default=False)
set_config_default(CONFIG, "engine", "online_moves", "lichess_cloud_analysis", key="min_time", default=20)
set_config_default(CONFIG, "engine", "online_moves", "lichess_cloud_analysis", key="move_quality", default="best")
set_config_default(CONFIG, "engine", "online_moves", "lichess_cloud_analysis", key="min_depth", default=20)
set_config_default(CONFIG, "engine", "online_moves", "lichess_cloud_analysis", key="min_knodes", default=0)
set_config_default(CONFIG, "engine", "online_moves", "lichess_cloud_analysis", key="max_score_difference", default=50)
set_config_default(CONFIG, "engine", "lichess_bot_tbs", "syzygy", key="enabled", default=False)
set_config_default(CONFIG, "engine", "lichess_bot_tbs", "syzygy", key="max_pieces", default=7)
set_config_default(CONFIG, "engine", "lichess_bot_tbs", "syzygy", key="move_quality", default="best")
set_config_default(CONFIG, "engine", "lichess_bot_tbs", "gaviota", key="enabled", default=False)
set_config_default(CONFIG, "engine", "lichess_bot_tbs", "gaviota", key="max_pieces", default=5)
set_config_default(CONFIG, "engine", "lichess_bot_tbs", "gaviota", key="move_quality", default="best")
set_config_default(CONFIG, "engine", "lichess_bot_tbs", "gaviota", key="min_dtm_to_consider_as_wdl_1", default=120)
set_config_default(CONFIG, "engine", "polyglot", key="enabled", default=False)
set_config_default(CONFIG, "engine", "polyglot", key="max_depth", default=8)
set_config_default(CONFIG, "engine", "polyglot", key="selection", default="weighted_random")
set_config_default(CONFIG, "engine", "polyglot", key="min_weight", default=1)
set_config_default(CONFIG, "challenge", key="concurrency", default=1)
set_config_default(CONFIG, "challenge", key="sort_by", default="best")
set_config_default(CONFIG, "challenge", key="accept_bot", default=False)
set_config_default(CONFIG, "challenge", key="only_bot", default=False)
set_config_default(CONFIG, "challenge", key="max_increment", default=180)
set_config_default(CONFIG, "challenge", key="min_increment", default=0)
set_config_default(CONFIG, "challenge", key="max_base", default=math.inf)
set_config_default(CONFIG, "challenge", key="min_base", default=0)
set_config_default(CONFIG, "challenge", key="max_days", default=math.inf)
set_config_default(CONFIG, "challenge", key="min_days", default=1)
set_config_default(CONFIG, "challenge", key="block_list", default=[], force_empty_values=True)
set_config_default(CONFIG, "correspondence", key="checkin_period", default=600)
set_config_default(CONFIG, "correspondence", key="move_time", default=60, force_empty_values=True)
set_config_default(CONFIG, "correspondence", key="disconnect_time", default=300)
set_config_default(CONFIG, "matchmaking", key="challenge_timeout", default=30, force_empty_values=True)
CONFIG["matchmaking"]["challenge_timeout"] = max(CONFIG["matchmaking"]["challenge_timeout"], 1)
set_config_default(CONFIG, "matchmaking", key="block_list", default=[], force_empty_values=True)
set_config_default(CONFIG, "matchmaking", key="delay_after_decline", default=DelayType.NONE, force_empty_values=True)
set_config_default(CONFIG, "matchmaking", key="allow_matchmaking", default=False)
set_config_default(CONFIG, "matchmaking", key="challenge_initial_time", default=[60], force_empty_values=True)
change_value_to_list(CONFIG, "matchmaking", key="challenge_initial_time")
set_config_default(CONFIG, "matchmaking", key="challenge_increment", default=[2], force_empty_values=True)
change_value_to_list(CONFIG, "matchmaking", key="challenge_increment")
set_config_default(CONFIG, "matchmaking", key="challenge_days", default=[None], force_empty_values=True)
change_value_to_list(CONFIG, "matchmaking", key="challenge_days")
set_config_default(CONFIG, "matchmaking", key="opponent_min_rating", default=600, force_empty_values=True)
set_config_default(CONFIG, "matchmaking", key="opponent_max_rating", default=4000, force_empty_values=True)
set_config_default(CONFIG, "matchmaking", key="opponent_allow_tos_violation", default=True)
set_config_default(CONFIG, "matchmaking", key="challenge_variant", default="random")
set_config_default(CONFIG, "matchmaking", key="challenge_mode", default="random")
for section in ["engine", "correspondence"]:
for ponder in ["ponder", "uci_ponder"]:
set_config_default(CONFIG, section, key=ponder, default=False)
for type in ["hello", "goodbye"]:
for target in ["", "_spectators"]:
set_config_default(CONFIG, "greeting", key=type + target, default="", force_empty_values=True)
def log_config(CONFIG: CONFIG_DICT_TYPE) -> None:
logger_config = CONFIG.copy()
logger_config["token"] = "logger"
logger.debug(f"Config:\n{yaml.dump(logger_config, sort_keys=False)}")
logger.debug("====================")
def load_config(config_file: str) -> Configuration:
with open(config_file) as stream:
try:
CONFIG = yaml.safe_load(stream)
except Exception:
logger.exception("There appears to be a syntax problem with your config.yml")
raise
log_config(CONFIG)
if "LICHESS_BOT_TOKEN" in os.environ:
CONFIG["token"] = os.environ["LICHESS_BOT_TOKEN"]
check_config_section(CONFIG, "token", str)
check_config_section(CONFIG, "url", str)
check_config_section(CONFIG, "engine", dict)
check_config_section(CONFIG, "challenge", dict)
check_config_section(CONFIG, "dir", str, "engine")
check_config_section(CONFIG, "name", str, "engine")
config_assert(CONFIG["token"] != "xxxxxxxxxxxxxxxx",
"Your config.yml has the default Lichess API token. This is probably wrong.")
config_assert(os.path.isdir(CONFIG["engine"]["dir"]),
f'Your engine directory `{CONFIG["engine"]["dir"]}` is not a directory.')
working_dir = CONFIG["engine"].get("working_dir")
config_assert(not working_dir or os.path.isdir(working_dir),
f"Your engine's working directory `{working_dir}` is not a directory.")
engine = os.path.join(CONFIG["engine"]["dir"], CONFIG["engine"]["name"])
config_assert(os.path.isfile(engine) or CONFIG["engine"]["protocol"] == "homemade",
f"The engine {engine} file does not exist.")
config_assert(os.access(engine, os.X_OK) or CONFIG["engine"]["protocol"] == "homemade",
f"The engine {engine} doesn't have execute (x) permission. Try: chmod +x {engine}")
if CONFIG["engine"]["protocol"] == "xboard":
for section, subsection in (("online_moves", "online_egtb"),
("lichess_bot_tbs", "syzygy"),
("lichess_bot_tbs", "gaviota")):
online_section = (CONFIG["engine"].get(section) or {}).get(subsection) or {}
config_assert(online_section.get("move_quality") != "suggest" or not online_section.get("enabled"),
f"XBoard engines can't be used with `move_quality` set to `suggest` in {subsection}.")
insert_default_values(CONFIG)
log_config(CONFIG)
return Configuration(CONFIG)