2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
OnlyLegs - Setup
|
|
|
|
Runs when the app detects that there is no user directory
|
|
|
|
"""
|
2023-03-02 17:00:54 +00:00
|
|
|
import os
|
2023-03-04 13:45:26 +00:00
|
|
|
import sys
|
|
|
|
import platformdirs
|
2023-03-02 17:00:54 +00:00
|
|
|
import yaml
|
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
USER_DIR = platformdirs.user_config_dir('onlylegs')
|
2023-03-02 17:00:54 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
class SetupApp:
|
|
|
|
"""
|
|
|
|
Setup the application on first run
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Main setup function
|
|
|
|
"""
|
2023-03-02 17:00:54 +00:00
|
|
|
print("Running setup...")
|
2023-03-04 13:45:26 +00:00
|
|
|
|
|
|
|
if not os.path.exists(USER_DIR):
|
2023-03-02 17:00:54 +00:00
|
|
|
self.make_dir()
|
2023-03-04 13:45:26 +00:00
|
|
|
if not os.path.exists(os.path.join(USER_DIR, '.env')):
|
2023-03-02 17:00:54 +00:00
|
|
|
self.make_env()
|
2023-03-04 13:45:26 +00:00
|
|
|
if not os.path.exists(os.path.join(USER_DIR, 'conf.yml')):
|
2023-03-02 17:00:54 +00:00
|
|
|
self.make_yaml()
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
def make_dir(self):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create the user directory
|
|
|
|
"""
|
2023-03-02 17:00:54 +00:00
|
|
|
try:
|
2023-03-04 13:45:26 +00:00
|
|
|
os.makedirs(USER_DIR)
|
|
|
|
os.makedirs(os.path.join(USER_DIR, 'instance'))
|
|
|
|
|
|
|
|
print("Created user directory at:", USER_DIR)
|
|
|
|
except Exception as err:
|
|
|
|
print("Error creating user directory:", err)
|
|
|
|
sys.exit(1) # exit with error code
|
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
def make_env(self):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create the .env file with default values
|
|
|
|
"""
|
2023-03-02 17:00:54 +00:00
|
|
|
env_conf = {
|
|
|
|
'FLASK_SECRETE': 'dev',
|
|
|
|
}
|
|
|
|
try:
|
2023-03-04 13:45:26 +00:00
|
|
|
with open(os.path.join(USER_DIR, '.env'), encoding='utf-8') as file:
|
2023-03-02 17:00:54 +00:00
|
|
|
for key, value in env_conf.items():
|
2023-03-04 13:45:26 +00:00
|
|
|
file.write(f"{key}={value}\n")
|
2023-03-02 17:00:54 +00:00
|
|
|
print("Created environment variables")
|
2023-03-04 13:45:26 +00:00
|
|
|
except Exception as err:
|
|
|
|
print("Error creating environment variables:", err)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
print("Generated default .env file. EDIT IT BEFORE RUNNING THE APP AGAIN!")
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
def make_yaml(self):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create the YAML config file with default values
|
|
|
|
"""
|
2023-03-02 17:00:54 +00:00
|
|
|
yaml_conf = {
|
|
|
|
'admin': {
|
|
|
|
'name': 'Real Person',
|
|
|
|
'username': 'User',
|
|
|
|
'email': 'real-email@some.place'
|
|
|
|
},
|
|
|
|
'upload': {
|
|
|
|
'allowed-extensions': {
|
|
|
|
'jpg': 'jpeg',
|
|
|
|
'jpeg': 'jpeg',
|
|
|
|
'png': 'png',
|
|
|
|
'webp': 'webp'
|
|
|
|
},
|
|
|
|
'max-size': 69,
|
|
|
|
'rename': 'GWA_\{\{username\}\}_\{\{time\}\}'
|
|
|
|
},
|
|
|
|
'website': {
|
|
|
|
'name': 'OnlyLegs',
|
|
|
|
'motto': 'Gwa Gwa',
|
|
|
|
'language': 'english'
|
|
|
|
},
|
|
|
|
'server': {
|
|
|
|
'host': '0.0.0.0',
|
|
|
|
'port': 5000
|
|
|
|
},
|
|
|
|
}
|
|
|
|
try:
|
2023-03-04 13:45:26 +00:00
|
|
|
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8') as file:
|
|
|
|
yaml.dump(yaml_conf, file, default_flow_style=False)
|
2023-03-02 17:00:54 +00:00
|
|
|
print("Created default gallery config")
|
2023-03-04 13:45:26 +00:00
|
|
|
except Exception as err:
|
|
|
|
print("Error creating default gallery config:", err)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
print("Generated default YAML config. EDIT IT BEFORE RUNNING THE APP AGAIN!")
|