Update config
This commit is contained in:
parent
a1e56b8116
commit
1c58d5e215
100
config.py
100
config.py
@ -27,6 +27,7 @@ LOG.addHandler(stream)
|
|||||||
TOKEN = None
|
TOKEN = None
|
||||||
BOT_COLOR = None
|
BOT_COLOR = None
|
||||||
|
|
||||||
|
SQLITE_NAME = "disarchive"
|
||||||
DB_NAME = None
|
DB_NAME = None
|
||||||
DB_ENGINE = None
|
DB_ENGINE = None
|
||||||
DB_HOST = None
|
DB_HOST = None
|
||||||
@ -46,11 +47,34 @@ schema = {
|
|||||||
},
|
},
|
||||||
"required": ["token"],
|
"required": ["token"],
|
||||||
},
|
},
|
||||||
"database": {
|
"sqlite": { # TODO - turn SQLITE path and name into global variables
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {"type": "string", "default": "disarchive"},
|
||||||
|
},
|
||||||
|
"required": ["name"],
|
||||||
|
},
|
||||||
|
"mysql": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {"type": "string", "default": "disarchive"},
|
||||||
|
"host": {"type": "string"},
|
||||||
|
"port": {"type": "integer"},
|
||||||
|
"user": {"type": "string"},
|
||||||
|
"password": {"type": "string"},
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"name",
|
||||||
|
"host",
|
||||||
|
"port",
|
||||||
|
"user",
|
||||||
|
"password",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"postgresql": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"name": {"type": "string", "default": "disarchive"},
|
"name": {"type": "string", "default": "disarchive"},
|
||||||
"engine": {"type": "string"},
|
|
||||||
"host": {"type": "string"},
|
"host": {"type": "string"},
|
||||||
"port": {"type": "integer"},
|
"port": {"type": "integer"},
|
||||||
"user": {"type": "string"},
|
"user": {"type": "string"},
|
||||||
@ -58,7 +82,6 @@ schema = {
|
|||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
"name",
|
"name",
|
||||||
"engine",
|
|
||||||
"host",
|
"host",
|
||||||
"port",
|
"port",
|
||||||
"user",
|
"user",
|
||||||
@ -66,7 +89,7 @@ schema = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"required": ["bot_info", "database"],
|
"required": ["bot_info"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -83,35 +106,17 @@ def load_config():
|
|||||||
validate_config(file_contents)
|
validate_config(file_contents)
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
# Create a new config.yaml file with the template
|
|
||||||
with open(file_path, "w") as f:
|
|
||||||
f.write(
|
|
||||||
"""
|
|
||||||
bot_info:
|
|
||||||
token:
|
|
||||||
bot_color:
|
|
||||||
|
|
||||||
database:
|
|
||||||
name:
|
|
||||||
engine:
|
|
||||||
host:
|
|
||||||
port:
|
|
||||||
user:
|
|
||||||
password: """
|
|
||||||
)
|
|
||||||
|
|
||||||
sys.exit(
|
sys.exit(
|
||||||
LOG.critical(
|
LOG.critical(
|
||||||
"Configuration file `config.yaml` has been generated. Please"
|
"config.yaml file not found. Please use the template to"
|
||||||
" fill out all of the necessary information. Refer to the docs"
|
" configure the bot."
|
||||||
" for information on what a specific configuration option is."
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Thouroughly validate all of the options in the config.yaml file
|
# Thouroughly validate all of the options in the config.yaml file
|
||||||
def validate_config(file_contents):
|
def validate_config(file_contents):
|
||||||
global TOKEN, BOT_COLOR, DB_NAME, DB_ENGINE, DB_HOST, DB_PORT, DB_USER, DB_PASSWORD
|
global TOKEN, BOT_COLOR, SQLITE_NAME, DB_NAME, DB_ENGINE, DB_HOST, DB_PORT, DB_USER, DB_PASSWORD
|
||||||
config = yaml.safe_load(file_contents)
|
config = yaml.safe_load(file_contents)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -123,37 +128,46 @@ def validate_config(file_contents):
|
|||||||
hex_pattern_one = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
|
hex_pattern_one = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
|
||||||
hex_pattern_two = "^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
|
hex_pattern_two = "^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
|
||||||
|
|
||||||
|
# Check if the bot_color is a valid hex color
|
||||||
if "bot_color" in config["bot_info"]:
|
if "bot_color" in config["bot_info"]:
|
||||||
if not bool(
|
if not bool(
|
||||||
re.match(hex_pattern_one, config["bot_info"]["bot_color"])
|
re.match(hex_pattern_one, config["bot_info"]["bot_color"])
|
||||||
) and not bool(
|
) and not bool(
|
||||||
re.match(hex_pattern_two, config["bot_info"]["bot_color"])
|
re.match(hex_pattern_two, config["bot_info"]["bot_color"])
|
||||||
):
|
):
|
||||||
LOG.critical(
|
LOG.warn(
|
||||||
"Error in config.yaml file: bot_color is not a valid hex color"
|
"bot_color is not a valid hex color... defaulting to #26dfc9"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
BOT_COLOR = discord.Color(
|
BOT_COLOR = discord.Color(
|
||||||
int((config["bot_info"]["bot_color"]).replace("#", ""), 16)
|
int((config["bot_info"]["bot_color"]).replace("#", ""), 16)
|
||||||
)
|
)
|
||||||
|
|
||||||
if config["database"]["engine"] not in [
|
# Assign database variables
|
||||||
"sqlite",
|
if "sqlite" in config:
|
||||||
"mysql",
|
DB_ENGINE = "sqlite"
|
||||||
"postgresql",
|
if "name" in config["sqlite"]:
|
||||||
]:
|
SQLITE_NAME = config["sqlite"]["name"]
|
||||||
LOG.error(
|
|
||||||
"database_engine must be either 'sqlite', 'mysql', or 'postgresql'"
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
DB_ENGINE = config["database"]["engine"]
|
|
||||||
|
|
||||||
DB_NAME = config["database"]["name"]
|
elif "mysql" in config:
|
||||||
DB_HOST = config["database"]["host"]
|
DB_ENGINE = "mysql"
|
||||||
DB_PORT = config["database"]["port"]
|
DB_NAME = config["mysql"]["name"]
|
||||||
DB_USER = config["database"]["user"]
|
DB_HOST = config["mysql"]["host"]
|
||||||
DB_PASSWORD = config["database"]["password"]
|
DB_PORT = config["mysql"]["port"]
|
||||||
|
DB_USER = config["mysql"]["user"]
|
||||||
|
DB_PASSWORD = config["mysql"]["password"]
|
||||||
|
|
||||||
|
elif "postgresql" in config:
|
||||||
|
DB_ENGINE = "postgresql"
|
||||||
|
DB_NAME = config["postgresql"]["name"]
|
||||||
|
DB_HOST = config["postgresql"]["host"]
|
||||||
|
DB_PORT = config["postgresql"]["port"]
|
||||||
|
DB_USER = config["postgresql"]["user"]
|
||||||
|
DB_PASSWORD = config["postgresql"]["password"]
|
||||||
|
|
||||||
|
else:
|
||||||
|
LOG.warn("No database engine specified. Defaulting to SQLite.")
|
||||||
|
DB_ENGINE = "sqlite"
|
||||||
|
|
||||||
TOKEN = config["bot_info"]["token"]
|
TOKEN = config["bot_info"]["token"]
|
||||||
|
|
||||||
|
21
config.yaml.example
Normal file
21
config.yaml.example
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
bot_info:
|
||||||
|
token: "BOT TOKEN"
|
||||||
|
bot_color: 26dfc9 #optional - default is 26dfc9
|
||||||
|
|
||||||
|
# Comment out if not using SQLite, and uncomment preferred database
|
||||||
|
sqlite:
|
||||||
|
name: disarchive
|
||||||
|
|
||||||
|
# mysql:
|
||||||
|
# name: disarchive
|
||||||
|
# host: 127.0.0.1
|
||||||
|
# port: 3306
|
||||||
|
# user: username
|
||||||
|
# password: pass123
|
||||||
|
|
||||||
|
# postgresql:
|
||||||
|
# name: disarchive
|
||||||
|
# host: 127.0.0.1
|
||||||
|
# port: 5432
|
||||||
|
# user: username
|
||||||
|
# password: pass123
|
@ -14,7 +14,7 @@ elif config.DB_ENGINE == "postgresql":
|
|||||||
else:
|
else:
|
||||||
if not os.path.exists("data"):
|
if not os.path.exists("data"):
|
||||||
os.makedirs("data")
|
os.makedirs("data")
|
||||||
database_url = "sqlite:///data/data.db"
|
database_url = f"sqlite:///data/{config.SQLITE_NAME}.db"
|
||||||
|
|
||||||
engine = create_engine(database_url)
|
engine = create_engine(database_url)
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user