python-gallery/setup/configuration.py

146 lines
4.3 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-03-04 13:45:26 +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
"""
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-03-04 13:45:26 +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-03-04 13:45:26 +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)
os.makedirs(os.path.join(USER_DIR, 'instance'))
os.makedirs(os.path.join(USER_DIR, 'uploads'))
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 = {
'FLASK_SECRET': os.urandom(32).hex(),
}
2023-03-20 17:19:42 +00:00
2023-04-02 16:50:52 +00:00
with open(os.path.join(USER_DIR, '.env'), encoding='utf-8', mode='w+') as file:
for key, value in env_conf.items():
file.write(f"{key}={value}\n")
2023-03-20 17:19:42 +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-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
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-03-12 15:08:49 +00:00
if input("Is this correct? (y/n): ").lower() == 'y':
is_correct = True
2023-03-20 17:19:42 +00:00
yaml_conf = {
'admin': {
2023-03-12 15:08:49 +00:00
'name': name,
'username': username,
'email': email,
},
'upload': {
'allowed-extensions': {
'jpg': 'jpeg',
'jpeg': 'jpeg',
'png': 'png',
'webp': 'webp',
},
'max-size': 69,
2023-04-07 09:18:03 +00:00
'max-load': 50,
'rename': 'GWA_{{username}}_{{time}}',
},
'website': {
'name': 'OnlyLegs',
2023-03-12 18:53:57 +00:00
'motto': 'A gallery built for fast and simple image management!',
'language': 'en',
2023-03-12 00:04:58 +00:00
}
}
2023-03-20 17:19:42 +00:00
2023-04-02 16:50:52 +00:00
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8', mode='w+') as file:
yaml.dump(yaml_conf, file, default_flow_style=False)
2023-03-04 13:45:26 +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
"""
logging.getLogger('werkzeug').disabled = True
logging.basicConfig(
filename=os.path.join(USER_DIR, 'only.log'),
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s',
encoding='utf-8')