-
Notifications
You must be signed in to change notification settings - Fork 178
/
config.py
101 lines (77 loc) · 2.95 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
import logging
import os
import sys
from logging.handlers import RotatingFileHandler
from typing import Union
from environs import Env
import constants
env = Env()
env.read_env(path="env", recurse=False)
class CustomFormatter(logging.Formatter):
def format(self, record):
record.module = "[{}]".format(record.module)
record.levelname = "[{}]".format(record.levelname)
return "{} {}".format(record.levelname.ljust(10), record.msg)
class CustomRotatingFileHandler(RotatingFileHandler):
def __init__(self, file_name, **kwargs):
self.base_dir = "logs"
if not os.path.exists(self.base_dir):
os.mkdir(self.base_dir)
super().__init__("{}/{}".format(self.base_dir, file_name), **kwargs)
def do_rollover(self, new_file_name):
self.baseFilename = "{}/{}".format(self.base_dir, new_file_name)
self.doRollover()
def init_logging(level, log_to_file):
websockets_logger = logging.getLogger("websockets")
websockets_logger.setLevel(logging.INFO)
requests_logger = logging.getLogger("urllib3")
requests_logger.setLevel(logging.INFO)
# Gets the root logger to set handlers/formatters
logger = logging.getLogger()
logger.setLevel(level)
if log_to_file:
log_handler = CustomRotatingFileHandler("init.log")
else:
log_handler = logging.StreamHandler(sys.stdout)
ShowdownConfig.log_handler = log_handler
log_handler.setFormatter(CustomFormatter())
logger.addHandler(log_handler)
class _ShowdownConfig:
battle_bot_module: str
websocket_uri: str
username: str
password: str
bot_mode: str
pokemon_mode: str
run_count: int
team: str
user_to_challenge: str
save_replay: bool
room_name: str
damage_calc_type: str
log_level: str
log_to_file: bool
log_handler: Union[CustomRotatingFileHandler, logging.StreamHandler]
def configure(self):
self.battle_bot_module = env("BATTLE_BOT")
self.websocket_uri = env("WEBSOCKET_URI")
self.username = env("PS_USERNAME")
self.password = env("PS_PASSWORD")
self.bot_mode = env("BOT_MODE")
self.pokemon_mode = env("POKEMON_MODE")
self.run_count = env.int("RUN_COUNT", 1)
self.team = env("TEAM_NAME", None)
self.user_to_challenge = env("USER_TO_CHALLENGE", None)
self.save_replay = env.bool("SAVE_REPLAY", False)
self.room_name = env("ROOM_NAME", None)
self.damage_calc_type = env("DAMAGE_CALC_TYPE", "average")
self.log_level = env("LOG_LEVEL", "DEBUG")
self.log_to_file = env.bool("LOG_TO_FILE", False)
self.validate_config()
def validate_config(self):
assert self.bot_mode in constants.BOT_MODES
if self.bot_mode == constants.CHALLENGE_USER:
assert self.user_to_challenge is not None, (
"If bot_mode is `CHALLENGE_USER, you must declare USER_TO_CHALLENGE"
)
ShowdownConfig = _ShowdownConfig()