-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
101 lines (83 loc) · 3.15 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.path
import enum
import apiRequests.bancho.querries as bancho_api
import apiRequests.challonge.querries as challonge_api
config = {}
@enum.unique
class VerificationType(enum.Enum):
"""Range from 0 to 2."""
NONE = 0
BANCHO = 1
CHALLONGE_KEY = 2
def create_config_file(config_dictionary):
with open("config.cfg", "w", encoding="utf-8") as file:
for key, value in config_dictionary.items():
file.write(f"{key} = {value}\n")
def get_config_value(key):
if config:
return config[key]
else:
read_config()
if config:
return config[key]
logging.error("Didn't find config file. Creating one.")
class ConfigItem(object):
def __init__(self, key_name: str, input_label: str, verify_data: VerificationType):
self.keyName = key_name
self.inputLabel = input_label
self.verifyData = verify_data
def create_config():
# Note to myself: osu key has length of 40
# Inspired by inquirer's solution.
temp_config = [ConfigItem("bancho_api_key", "Please paste your bancho API key here: ",
VerificationType.BANCHO),
ConfigItem("challonge_api_key", "Please paste your challonge API key here (can be left empty): ",
VerificationType.CHALLONGE_KEY)]
config_data = get_and_verify_config_data(temp_config)
create_config_file(config_data)
def get_and_verify_config_data(temp_config):
"""Verifies data using the APIs."""
config_data = {}
for item in temp_config:
while True:
temp = input(item.inputLabel)
if item.verifyData == VerificationType.BANCHO:
if len(temp) < 40:
print("API key is too short.")
continue
bancho_api.API_KEY = temp
check_result = bancho_api.check_if_user_id_exists("2")
if not check_result:
print("Bancho api key is invalid.")
continue
break
if item.verifyData == VerificationType.CHALLONGE_KEY:
if temp == "":
break
challonge_api.APIKEY = temp
check = challonge_api.check_challonge_key(temp)
if not check:
print("Challonge api key is invalid.")
continue
break
config_data[item.keyName] = temp
return config_data
# Another fancy csv reader function, redundancy approved
# TODO: Remove redundant code in reading files.
def read_config():
if not os.path.exists("config.cfg"):
create_config()
with open("config.cfg", "r", encoding="utf-8") as file:
line_count = 0
for line_count, line in enumerate(file):
if line[0] == "#":
continue
temp = line.replace(" ", "").replace("\n", "")
key, value = temp.split("=")
config[key] = value
logging.info(f"Read {line_count} lines from config.cfg.")
return config
# Used for config testing.
if __name__ == "__main__":
print(get_config_value("challonge_api_key"))