Attempt to make some sort of setup file

This commit is contained in:
Michał 2023-03-02 17:00:54 +00:00
parent e16792c37e
commit 9e87c74c96
5 changed files with 141 additions and 55 deletions

1
.github/README.md vendored
View file

@ -31,3 +31,4 @@ Currently only for reference
poetry install poetry install
poetry run python3 -m flask --app gallery --debug run --host 0.0.0.0 poetry run python3 -m flask --app gallery --debug run --host 0.0.0.0
poetry run python3 -m gunicorn -w 4 -b 0.0.0.0:5000 'gallery:create_app()'

View file

@ -8,41 +8,37 @@ print("""
Created by Fluffy Bean - Version 23.03.02 Created by Fluffy Bean - Version 23.03.02
""") """)
from flask import Flask, render_template from flask import Flask, render_template
from flask_compress import Compress from flask_compress import Compress
from dotenv import load_dotenv from dotenv import load_dotenv
import platformdirs import platformdirs
# Load logger
from gallery.logger import logger
logger.innit_logger()
import yaml import yaml
import os import os
# I could use something other than the config dir, but this works well enough
# Check if any of the required files are missing
if not os.path.exists(platformdirs.user_config_dir('onlylegs')):
from setup import setup
setup()
user_dir = platformdirs.user_config_dir('onlylegs') user_dir = platformdirs.user_config_dir('onlylegs')
if not os.path.exists(user_dir):
os.makedirs(user_dir)
print("Created user directory")
# Location of instance folder, where sqlite db is stored
instance_path = os.path.join(user_dir, 'instance') instance_path = os.path.join(user_dir, 'instance')
if not os.path.exists(instance_path):
os.makedirs(instance_path)
print("Created instance directory")
# Get environment variables # Get environment variables
if os.path.exists(os.path.join(user_dir, '.env')): if os.path.exists(os.path.join(user_dir, '.env')):
load_dotenv(os.path.join(user_dir, '.env')) load_dotenv(os.path.join(user_dir, '.env'))
print("Loaded environment variables") print("Loaded environment variables")
else: else:
conf = { print("No environment variables found!")
'FLASK_SECRETE': 'dev', exit(1)
}
# Create .env file with default values
with open(os.path.join(user_dir, '.env'), 'w') as f:
for key, value in conf.items():
f.write(f"{key}={value}\n")
print("Created default environment variables at:",
os.path.join(user_dir, '.env'),
"\nCHANGE THESE VALUES USING TEHE GALLERY!")
# Get config file # Get config file
if os.path.exists(os.path.join(user_dir, 'conf.yml')): if os.path.exists(os.path.join(user_dir, 'conf.yml')):
@ -50,44 +46,13 @@ if os.path.exists(os.path.join(user_dir, 'conf.yml')):
conf = yaml.load(f, Loader=yaml.FullLoader) conf = yaml.load(f, Loader=yaml.FullLoader)
print("Loaded gallery config") print("Loaded gallery config")
else: else:
conf = { print("No config file found!")
'admin': { exit(1)
'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'
}
}
# Create yaml config file with default values
with open(os.path.join(user_dir, 'conf.yml'), 'w') as f:
yaml.dump(conf, f, default_flow_style=False)
print("Created default gallery config at:",
os.path.join(user_dir, 'conf.yml'),
"\nCHANGE THESE VALUES USING TEHE GALLERY!")
# Load logger
from .logger import logger
logger.innit_logger()
def create_app(test_config=None): def create_app(test_config=None):
# create and configure the app # create and configure the app
app = Flask(__name__, instance_path=instance_path) app = Flask(__name__,instance_path=instance_path)
compress = Compress() compress = Compress()
# App configuration # App configuration
@ -95,8 +60,8 @@ def create_app(test_config=None):
SECRET_KEY=os.environ.get('FLASK_SECRET'), SECRET_KEY=os.environ.get('FLASK_SECRET'),
DATABASE=os.path.join(app.instance_path, 'gallery.sqlite'), DATABASE=os.path.join(app.instance_path, 'gallery.sqlite'),
UPLOAD_FOLDER=os.path.join(user_dir, 'uploads'), UPLOAD_FOLDER=os.path.join(user_dir, 'uploads'),
MAX_CONTENT_LENGTH=1024 * 1024 * conf['upload']['max-size'],
ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'], ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'],
MAX_CONTENT_LENGTH=1024 * 1024 * conf['upload']['max-size'],
WEBSITE=conf['website'], WEBSITE=conf['website'],
) )

81
gallery/setup.py Normal file
View file

@ -0,0 +1,81 @@
# Import dependencies
import platformdirs
import os
import yaml
class setup:
def __init__(self):
self.user_dir = platformdirs.user_config_dir('onlylegs')
print("Running setup...")
if not os.path.exists(self.user_dir):
self.make_dir()
if not os.path.exists(os.path.join(self.user_dir, '.env')):
self.make_env()
if not os.path.exists(os.path.join(self.user_dir, 'conf.yml')):
self.make_yaml()
def make_dir(self):
try:
os.makedirs(self.user_dir)
os.makedirs(os.path.join(self.user_dir, 'instance'))
print("Created user directory at:", self.user_dir)
except Exception as e:
print("Error creating user directory:", e)
exit(1) # exit with error code
def make_env(self):
# Create .env file with default values
env_conf = {
'FLASK_SECRETE': 'dev',
}
try:
with open(os.path.join(self.user_dir, '.env'), 'w') as f:
for key, value in env_conf.items():
f.write(f"{key}={value}\n")
print("Created environment variables")
except Exception as e:
print("Error creating environment variables:", e)
exit(1)
print("Generated default .env file. EDIT IT BEFORE RUNNING THE APP AGAIN!")
def make_yaml(self):
# Create yaml config file with default values
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:
with open(os.path.join(self.user_dir, 'conf.yml'), 'w') as f:
yaml.dump(yaml_conf, f, default_flow_style=False)
print("Created default gallery config")
except Exception as e:
print("Error creating default gallery config:", e)
exit(1)
print("Generated default YAML config. EDIT IT BEFORE RUNNING THE APP AGAIN!")

40
poetry.lock generated
View file

@ -172,6 +172,27 @@ files = [
brotli = "*" brotli = "*"
flask = "*" flask = "*"
[[package]]
name = "gunicorn"
version = "20.1.0"
description = "WSGI HTTP Server for UNIX"
category = "main"
optional = false
python-versions = ">=3.5"
files = [
{file = "gunicorn-20.1.0-py3-none-any.whl", hash = "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e"},
{file = "gunicorn-20.1.0.tar.gz", hash = "sha256:e0a968b5ba15f8a328fdfd7ab1fcb5af4470c28aaf7e55df02a99bc13138e6e8"},
]
[package.dependencies]
setuptools = ">=3.0"
[package.extras]
eventlet = ["eventlet (>=0.24.1)"]
gevent = ["gevent (>=1.4.0)"]
setproctitle = ["setproctitle"]
tornado = ["tornado (>=0.2)"]
[[package]] [[package]]
name = "itsdangerous" name = "itsdangerous"
version = "2.1.2" version = "2.1.2"
@ -449,6 +470,23 @@ files = [
{file = "PyYAML-6.0.tar.gz", hash = "sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2"}, {file = "PyYAML-6.0.tar.gz", hash = "sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2"},
] ]
[[package]]
name = "setuptools"
version = "67.4.0"
description = "Easily download, build, install, upgrade, and uninstall Python packages"
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "setuptools-67.4.0-py3-none-any.whl", hash = "sha256:f106dee1b506dee5102cc3f3e9e68137bbad6d47b616be7991714b0c62204251"},
{file = "setuptools-67.4.0.tar.gz", hash = "sha256:e5fd0a713141a4a105412233c63dc4e17ba0090c8e8334594ac790ec97792330"},
]
[package.extras]
docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-favicon", "sphinx-hoverxref (<2)", "sphinx-inline-tabs", "sphinx-lint", "sphinx-notfound-page (==0.8.3)", "sphinx-reredirects", "sphinxcontrib-towncrier"]
testing = ["build[virtualenv]", "filelock (>=3.4.0)", "flake8 (<5)", "flake8-2020", "ini2toml[lite] (>=0.9)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pip (>=19.1)", "pip-run (>=8.8)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)", "pytest-perf", "pytest-timeout", "pytest-xdist", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"]
testing-integration = ["build[virtualenv]", "filelock (>=3.4.0)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pytest", "pytest-enabler", "pytest-xdist", "tomli", "virtualenv (>=13.0.0)", "wheel"]
[[package]] [[package]]
name = "werkzeug" name = "werkzeug"
version = "2.2.3" version = "2.2.3"
@ -470,4 +508,4 @@ watchdog = ["watchdog"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "74da938044546c1a1b0250dcb63f316449fc2087fbf9f0e72b4066e3c626d025" content-hash = "850ee074cc1e4ad92a8650334d306e3c4269062f0324887a2d6f19149f4d5a30"

View file

@ -11,6 +11,7 @@ readme = ".github/README.md"
python = "^3.10" python = "^3.10"
Flask = "^2.2.2" Flask = "^2.2.2"
flask-compress = "^1.13" flask-compress = "^1.13"
gunicorn = "^20.1.0"
python-dotenv = "^0.21.0" python-dotenv = "^0.21.0"
pyyaml = "^6.0" pyyaml = "^6.0"
libsass = "^0.22.0" libsass = "^0.22.0"