python-gallery/setup/configuration.py

157 lines
4.5 KiB
Python
Raw Normal View History

2023-03-04 13:45:26 +00:00
"""
OnlyLegs - Setup
Runs when the app detects that there is no user directory
"""
import os
import logging
import re
2023-03-20 17:19:42 +00:00
import platformdirs
import yaml
2023-04-07 12:35:30 +00:00
USER_DIR = platformdirs.user_config_dir("onlylegs")
class Configuration:
2023-03-04 13:45:26 +00:00
"""
Setup the application on first run
"""
2023-04-07 12:35:30 +00:00
def __init__(self):
2023-03-04 13:45:26 +00:00
"""
Main setup function
"""
print("Running startup checks...")
2023-03-20 17:19:42 +00:00
# Check if the user directory exists
2023-03-04 13:45:26 +00:00
if not os.path.exists(USER_DIR):
self.make_dir()
2023-03-20 17:19:42 +00:00
# Check if the .env file exists
2023-04-07 12:35:30 +00:00
if not os.path.exists(os.path.join(USER_DIR, ".env")):
self.make_env()
2023-03-20 17:19:42 +00:00
# Check if the conf.yml file exists
2023-04-07 12:35:30 +00:00
if not os.path.exists(os.path.join(USER_DIR, "conf.yml")):
self.make_yaml()
2023-03-20 17:19:42 +00:00
# Load the config files
self.logging_config()
2023-03-04 13:45:26 +00:00
@staticmethod
def make_dir():
2023-03-04 13:45:26 +00:00
"""
Create the user directory
"""
2023-04-02 16:50:52 +00:00
os.makedirs(USER_DIR)
2023-04-07 12:35:30 +00:00
os.makedirs(os.path.join(USER_DIR, "instance"))
os.makedirs(os.path.join(USER_DIR, "media"))
os.makedirs(os.path.join(USER_DIR, "media", "uploads"))
os.makedirs(os.path.join(USER_DIR, "media", "cache"))
os.makedirs(os.path.join(USER_DIR, "media", "pfp"))
2023-03-20 17:19:42 +00:00
print("Created user directory at:", USER_DIR)
2023-03-04 13:45:26 +00:00
@staticmethod
def make_env():
2023-03-04 13:45:26 +00:00
"""
Create the .env file with default values
"""
env_conf = {
2023-04-07 12:35:30 +00:00
"FLASK_SECRET": os.urandom(32).hex(),
}
2023-03-20 17:19:42 +00:00
2023-04-07 12:35:30 +00:00
with open(os.path.join(USER_DIR, ".env"), encoding="utf-8", mode="w+") as file:
2023-04-02 16:50:52 +00:00
for key, value in env_conf.items():
file.write(f"{key}={value}\n")
2023-03-20 17:19:42 +00:00
2023-04-07 12:35:30 +00:00
print(
"""
####################################################
2023-03-12 18:53:57 +00:00
# A NEW KEY WAS GENERATED FOR YOU! PLEASE NOTE #
# DOWN THE FLASK_SECRET KEY LOCATED IN YOUR #
# .config/onlylegs/.env FOLDER! LOOSING THIS KEY #
# WILL RESULT IN YOU BEING UNABLE TO LOG IN! #
####################################################
2023-04-07 12:35:30 +00:00
"""
)
2023-03-04 13:45:26 +00:00
@staticmethod
def make_yaml():
2023-03-04 13:45:26 +00:00
"""
Create the YAML config file with default values
"""
is_correct = False
2023-04-07 12:35:30 +00:00
email_regex = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b")
username_regex = re.compile(r"\b[A-Za-z0-9._%+-]+\b")
2023-03-20 17:19:42 +00:00
2023-03-12 18:53:57 +00:00
print("\nNo config file found, please enter the following information:")
while not is_correct:
username = input("Admin username: ")
name = input("Admin name: ")
email = input("Admin email: ")
# Check if the values are valid
if not username or not username_regex.match(username):
print("Username is invalid!")
continue
2023-03-20 17:19:42 +00:00
if not name:
print("Name is invalid!")
continue
if not email or not email_regex.match(email):
print("Email is invalid!")
continue
2023-03-20 17:19:42 +00:00
# Check if user is happy with the values
2023-04-07 12:35:30 +00:00
if input("Is this correct? (y/n): ").lower() == "y":
is_correct = True
2023-03-20 17:19:42 +00:00
yaml_conf = {
2023-04-07 12:35:30 +00:00
"admin": {
"name": name,
"username": username,
"email": email,
},
2023-04-07 12:35:30 +00:00
"upload": {
"allowed-extensions": {
"jpg": "jpeg",
"jpeg": "jpeg",
"png": "png",
"webp": "webp",
},
2023-04-07 12:35:30 +00:00
"max-size": 69,
"max-load": 50,
"rename": "GWA_{{username}}_{{time}}",
},
"website": {
"name": "OnlyLegs",
"motto": "A gallery built for fast and simple image management!",
"language": "en",
},
}
2023-03-20 17:19:42 +00:00
2023-04-07 12:35:30 +00:00
with open(
os.path.join(USER_DIR, "conf.yml"), encoding="utf-8", mode="w+"
) as file:
2023-04-02 16:50:52 +00:00
yaml.dump(yaml_conf, file, default_flow_style=False)
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
print(
"Generated config file, you can change these values in the settings of the app"
)
@staticmethod
def logging_config():
2023-03-20 17:19:42 +00:00
"""
Set the logging config
"""
2023-04-07 12:35:30 +00:00
logging.getLogger("werkzeug").disabled = True
logging.basicConfig(
2023-04-07 12:35:30 +00:00
filename=os.path.join(USER_DIR, "only.log"),
level=logging.INFO,
2023-04-07 12:35:30 +00:00
datefmt="%Y-%m-%d %H:%M:%S",
format="%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s",
encoding="utf-8",
)