forhavu commited on
Commit
ee0883c
·
1 Parent(s): 35f3596

move load_config to config.py

Browse files
Files changed (2) hide show
  1. config.py +43 -0
  2. main.py +2 -43
config.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yaml
2
+ import os
3
+ import os.path
4
+
5
+ def load_config():
6
+ with open("./config.yml", 'r') as stream:
7
+ try:
8
+ CONFIG = yaml.load(stream)
9
+ except Exception as e:
10
+ print("There appears to be a syntax problem with your config.yml")
11
+ raise e
12
+
13
+ #[section, type, error message]
14
+ sections = [["token", str, "Section `token` must be a string wrapped in quotes."],
15
+ ["url", str, "Section `url` must be a string wrapped in quotes."],
16
+ ["engine", dict, "Section `engine` must be a dictionary with indented keys followed by colons.."],
17
+ ["max_concurrent_games", int, "Section `max_concurrent_games` must be an integer number without quotes."],
18
+ ["max_queued_challenges", int, "Section `max_queued_challenges` must be an integer number without quotes."],
19
+ ["supported_tc", list, "Section `supported_tc` must be a list with indented entries starting with dashes.."],
20
+ ["supported_modes", list, "Section `supported_modes` must be a list with indented entries starting with dashes.."]]
21
+ for section in sections:
22
+ if section[0] not in CONFIG:
23
+ raise Exception("Your config.yml does not have required section `{}`.".format(section[0]))
24
+ elif not isinstance(CONFIG[section[0]], section[1]):
25
+ raise Exception(section[2])
26
+
27
+
28
+ engine_sections = ["dir", "name"]
29
+ for subsection in engine_sections:
30
+ if subsection not in CONFIG["engine"]:
31
+ raise Exception("Your config.yml does not have required `engine` subsection `{}`.".format(subsection))
32
+ if not isinstance(CONFIG["engine"][subsection], str):
33
+ raise Exception("Engine subsection `{}` must be a string wrapped in quotes.".format(subsection))
34
+
35
+ if CONFIG["token"] == "xxxxxxxxxxxxxxxx":
36
+ raise Exception("Your config.yml has the default Lichess API token. This is probably wrong.")
37
+
38
+ if not os.path.isdir(CONFIG["engine"]["dir"]):
39
+ raise Exception("Your engine directory `{}` is not a directory.")
40
+
41
+ if not os.path.exists(CONFIG["engine"]["dir"] + CONFIG["engine"]["name"]):
42
+ raise Exception("The engine specified does not exist.")
43
+ return CONFIG
main.py CHANGED
@@ -10,8 +10,8 @@ import queue
10
  import os
11
  import os.path
12
  import traceback
13
- import yaml
14
  import logging_pool
 
15
  from conversation import Conversation, ChatLine
16
 
17
  CONFIG = {}
@@ -175,54 +175,13 @@ def update_board(board, move):
175
  board.push(uci_move)
176
  return board
177
 
178
-
179
- def load_config():
180
- global CONFIG
181
- with open("./config.yml", 'r') as stream:
182
- try:
183
- CONFIG = yaml.load(stream)
184
- except Exception as e:
185
- print("There appears to be a syntax problem with your config.yml")
186
- raise e
187
-
188
- #[section, type, error message]
189
- sections = [["token", str, "Section `token` must be a string wrapped in quotes."],
190
- ["url", str, "Section `url` must be a string wrapped in quotes."],
191
- ["engine", dict, "Section `engine` must be a dictionary with indented keys followed by colons.."],
192
- ["max_concurrent_games", int, "Section `max_concurrent_games` must be an integer number without quotes."],
193
- ["max_queued_challenges", int, "Section `max_queued_challenges` must be an integer number without quotes."],
194
- ["supported_tc", list, "Section `supported_tc` must be a list with indented entries starting with dashes.."],
195
- ["supported_modes", list, "Section `supported_modes` must be a list with indented entries starting with dashes.."]]
196
- for section in sections:
197
- if section[0] not in CONFIG:
198
- raise Exception("Your config.yml does not have required section `{}`.".format(section[0]))
199
- elif not isinstance(CONFIG[section[0]], section[1]):
200
- raise Exception(section[2])
201
-
202
-
203
- engine_sections = ["dir", "name"]
204
- for subsection in engine_sections:
205
- if subsection not in CONFIG["engine"]:
206
- raise Exception("Your config.yml does not have required `engine` subsection `{}`.".format(subsection))
207
- if not isinstance(CONFIG["engine"][subsection], str):
208
- raise Exception("Engine subsection `{}` must be a string wrapped in quotes.".format(subsection))
209
-
210
- if CONFIG["token"] == "xxxxxxxxxxxxxxxx":
211
- raise Exception("Your config.yml has the default Lichess API token. This is probably wrong.")
212
-
213
- if not os.path.isdir(CONFIG["engine"]["dir"]):
214
- raise Exception("Your engine directory `{}` is not a directory.")
215
-
216
- if not os.path.exists(CONFIG["engine"]["dir"] + CONFIG["engine"]["name"]):
217
- raise Exception("The engine specified does not exist.")
218
-
219
  if __name__ == "__main__":
220
  logger = logging.basicConfig(level=logging.INFO)
221
  parser = argparse.ArgumentParser(description='Play on Lichess with a bot')
222
  parser.add_argument('-u', action='store_true', help='Add this flag to upgrade your account to a bot account.')
223
  args = parser.parse_args()
224
 
225
- load_config()
226
  li = lichess.Lichess(CONFIG["token"], CONFIG["url"])
227
 
228
  user_profile = li.get_profile()
 
10
  import os
11
  import os.path
12
  import traceback
 
13
  import logging_pool
14
+ from config import load_config
15
  from conversation import Conversation, ChatLine
16
 
17
  CONFIG = {}
 
175
  board.push(uci_move)
176
  return board
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  if __name__ == "__main__":
179
  logger = logging.basicConfig(level=logging.INFO)
180
  parser = argparse.ArgumentParser(description='Play on Lichess with a bot')
181
  parser.add_argument('-u', action='store_true', help='Add this flag to upgrade your account to a bot account.')
182
  args = parser.parse_args()
183
 
184
+ CONFIG = load_config()
185
  li = lichess.Lichess(CONFIG["token"], CONFIG["url"])
186
 
187
  user_profile = li.get_profile()