mirror of
https://github.com/meeb/tubesync.git
synced 2025-06-24 05:56:37 +00:00
Merge pull request #821 from tcely/patch-9
Better environment variable support for settings
This commit is contained in:
commit
0104669c7c
@ -1,3 +1,4 @@
|
||||
import os
|
||||
import string
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlunsplit, urlencode, urlparse
|
||||
@ -6,6 +7,41 @@ from yt_dlp.utils import LazyList
|
||||
from .errors import DatabaseConnectionError
|
||||
|
||||
|
||||
def getenv(key, default=None, /, *, integer=False, string=True):
|
||||
'''
|
||||
Guarantees a returned type from calling `os.getenv`
|
||||
The caller can request the integer type,
|
||||
or use the default string type.
|
||||
'''
|
||||
|
||||
args = dict(key=key, default=default, integer=integer, string=string)
|
||||
supported_types = dict(zip(args.keys(), (
|
||||
(str,), # key
|
||||
(
|
||||
bool,
|
||||
float,
|
||||
int,
|
||||
str,
|
||||
None.__class__,
|
||||
), # default
|
||||
(bool,) * (len(args.keys()) - 2),
|
||||
)))
|
||||
unsupported_type_msg = 'Unsupported type for positional argument, "{}": {}'
|
||||
for k, t in supported_types.items():
|
||||
v = args[k]
|
||||
assert isinstance(v, t), unsupported_type_msg.format(k, type(v))
|
||||
|
||||
d = str(default) if default is not None else None
|
||||
|
||||
r = os.getenv(key, d)
|
||||
if r is None:
|
||||
if string: r = str()
|
||||
if integer: r = int()
|
||||
elif integer:
|
||||
r = int(float(r))
|
||||
return r
|
||||
|
||||
|
||||
def parse_database_connection_string(database_connection_string):
|
||||
'''
|
||||
Parses a connection string in a URL style format, such as:
|
||||
|
@ -1,40 +1,41 @@
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from urllib.parse import urljoin
|
||||
from common.utils import parse_database_connection_string
|
||||
from common.utils import getenv, parse_database_connection_string
|
||||
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
ROOT_DIR = Path('/')
|
||||
CONFIG_BASE_DIR = ROOT_DIR / 'config'
|
||||
DOWNLOADS_BASE_DIR = ROOT_DIR / 'downloads'
|
||||
DJANGO_URL_PREFIX = os.getenv('DJANGO_URL_PREFIX', None)
|
||||
STATIC_URL = str(os.getenv('DJANGO_STATIC_URL', '/static/'))
|
||||
DJANGO_URL_PREFIX = getenv('DJANGO_URL_PREFIX').strip()
|
||||
STATIC_URL = getenv('DJANGO_STATIC_URL', '/static/').strip()
|
||||
if DJANGO_URL_PREFIX and STATIC_URL:
|
||||
STATIC_URL = urljoin(DJANGO_URL_PREFIX, STATIC_URL[1:])
|
||||
|
||||
|
||||
# This is not ever meant to be a public web interface so this isn't too critical
|
||||
SECRET_KEY = str(os.getenv('DJANGO_SECRET_KEY', 'tubesync-django-secret'))
|
||||
SECRET_KEY = getenv('DJANGO_SECRET_KEY', 'tubesync-django-secret')
|
||||
|
||||
|
||||
ALLOWED_HOSTS_STR = str(os.getenv('TUBESYNC_HOSTS', '*'))
|
||||
ALLOWED_HOSTS_STR = getenv('TUBESYNC_HOSTS', '*')
|
||||
ALLOWED_HOSTS = ALLOWED_HOSTS_STR.split(',')
|
||||
DEBUG = True if os.getenv('TUBESYNC_DEBUG', False) else False
|
||||
FORCE_SCRIPT_NAME = os.getenv('DJANGO_FORCE_SCRIPT_NAME', DJANGO_URL_PREFIX)
|
||||
DEBUG_STR = getenv('TUBESYNC_DEBUG', False)
|
||||
DEBUG = True if 'true' == DEBUG_STR.strip().lower() else False
|
||||
FORCE_SCRIPT_NAME = getenv('DJANGO_FORCE_SCRIPT_NAME', DJANGO_URL_PREFIX)
|
||||
|
||||
|
||||
database_dict = {}
|
||||
database_connection_env = os.getenv('DATABASE_CONNECTION', '')
|
||||
database_connection_env = getenv('DATABASE_CONNECTION')
|
||||
if database_connection_env:
|
||||
database_dict = parse_database_connection_string(database_connection_env)
|
||||
|
||||
|
||||
if database_dict:
|
||||
print(f'Using database connection: {database_dict["ENGINE"]}://'
|
||||
print(f'Using database connection: {database_dict["DRIVER"]}://'
|
||||
f'{database_dict["USER"]}:[hidden]@{database_dict["HOST"]}:'
|
||||
f'{database_dict["PORT"]}/{database_dict["NAME"]}', file=sys.stdout)
|
||||
f'{database_dict["PORT"]}/{database_dict["NAME"]}',
|
||||
file=sys.stdout, flush=True)
|
||||
DATABASES = {
|
||||
'default': database_dict,
|
||||
}
|
||||
@ -60,7 +61,7 @@ else:
|
||||
|
||||
|
||||
DEFAULT_THREADS = 1
|
||||
BACKGROUND_TASK_ASYNC_THREADS = int(os.getenv('TUBESYNC_WORKERS', DEFAULT_THREADS))
|
||||
BACKGROUND_TASK_ASYNC_THREADS = getenv('TUBESYNC_WORKERS', DEFAULT_THREADS, integer=True)
|
||||
|
||||
|
||||
MEDIA_ROOT = CONFIG_BASE_DIR / 'media'
|
||||
@ -70,14 +71,14 @@ YOUTUBE_DL_TEMPDIR = DOWNLOAD_ROOT / 'cache'
|
||||
COOKIES_FILE = CONFIG_BASE_DIR / 'cookies.txt'
|
||||
|
||||
|
||||
HEALTHCHECK_FIREWALL_STR = str(os.getenv('TUBESYNC_HEALTHCHECK_FIREWALL', 'True')).strip().lower()
|
||||
HEALTHCHECK_FIREWALL = True if HEALTHCHECK_FIREWALL_STR == 'true' else False
|
||||
HEALTHCHECK_ALLOWED_IPS_STR = str(os.getenv('TUBESYNC_HEALTHCHECK_ALLOWED_IPS', '127.0.0.1'))
|
||||
HEALTHCHECK_FIREWALL_STR = getenv('TUBESYNC_HEALTHCHECK_FIREWALL', True)
|
||||
HEALTHCHECK_FIREWALL = ( 'true' == HEALTHCHECK_FIREWALL_STR.strip().lower() )
|
||||
HEALTHCHECK_ALLOWED_IPS_STR = getenv('TUBESYNC_HEALTHCHECK_ALLOWED_IPS', '127.0.0.1')
|
||||
HEALTHCHECK_ALLOWED_IPS = HEALTHCHECK_ALLOWED_IPS_STR.split(',')
|
||||
|
||||
|
||||
BASICAUTH_USERNAME = os.getenv('HTTP_USER', '').strip()
|
||||
BASICAUTH_PASSWORD = os.getenv('HTTP_PASS', '').strip()
|
||||
BASICAUTH_USERNAME = getenv('HTTP_USER').strip()
|
||||
BASICAUTH_PASSWORD = getenv('HTTP_PASS').strip()
|
||||
if BASICAUTH_USERNAME and BASICAUTH_PASSWORD:
|
||||
BASICAUTH_DISABLE = False
|
||||
BASICAUTH_USERS = {
|
||||
@ -88,25 +89,25 @@ else:
|
||||
BASICAUTH_USERS = {}
|
||||
|
||||
|
||||
SOURCE_DOWNLOAD_DIRECTORY_PREFIX_STR = os.getenv('TUBESYNC_DIRECTORY_PREFIX', 'True').strip().lower()
|
||||
SOURCE_DOWNLOAD_DIRECTORY_PREFIX = True if SOURCE_DOWNLOAD_DIRECTORY_PREFIX_STR == 'true' else False
|
||||
SOURCE_DOWNLOAD_DIRECTORY_PREFIX_STR = getenv('TUBESYNC_DIRECTORY_PREFIX', True)
|
||||
SOURCE_DOWNLOAD_DIRECTORY_PREFIX = ( 'true' == SOURCE_DOWNLOAD_DIRECTORY_PREFIX_STR.strip().lower() )
|
||||
|
||||
|
||||
SHRINK_NEW_MEDIA_METADATA_STR = os.getenv('TUBESYNC_SHRINK_NEW', 'false').strip().lower()
|
||||
SHRINK_NEW_MEDIA_METADATA = ( 'true' == SHRINK_NEW_MEDIA_METADATA_STR )
|
||||
SHRINK_OLD_MEDIA_METADATA_STR = os.getenv('TUBESYNC_SHRINK_OLD', 'false').strip().lower()
|
||||
SHRINK_OLD_MEDIA_METADATA = ( 'true' == SHRINK_OLD_MEDIA_METADATA_STR )
|
||||
SHRINK_NEW_MEDIA_METADATA_STR = getenv('TUBESYNC_SHRINK_NEW', False)
|
||||
SHRINK_NEW_MEDIA_METADATA = ( 'true' == SHRINK_NEW_MEDIA_METADATA_STR.strip().lower() )
|
||||
SHRINK_OLD_MEDIA_METADATA_STR = getenv('TUBESYNC_SHRINK_OLD', False)
|
||||
SHRINK_OLD_MEDIA_METADATA = ( 'true' == SHRINK_OLD_MEDIA_METADATA_STR.strip().lower() )
|
||||
|
||||
|
||||
# TUBESYNC_RENAME_ALL_SOURCES: True or False
|
||||
RENAME_ALL_SOURCES_STR = os.getenv('TUBESYNC_RENAME_ALL_SOURCES', 'False').strip().lower()
|
||||
RENAME_ALL_SOURCES = ( 'true' == RENAME_ALL_SOURCES_STR )
|
||||
RENAME_ALL_SOURCES_STR = getenv('TUBESYNC_RENAME_ALL_SOURCES', False)
|
||||
RENAME_ALL_SOURCES = ( 'true' == RENAME_ALL_SOURCES_STR.strip().lower() )
|
||||
# TUBESYNC_RENAME_SOURCES: A comma-separated list of Source directories
|
||||
RENAME_SOURCES_STR = os.getenv('TUBESYNC_RENAME_SOURCES', '')
|
||||
RENAME_SOURCES_STR = getenv('TUBESYNC_RENAME_SOURCES')
|
||||
RENAME_SOURCES = RENAME_SOURCES_STR.split(',') if RENAME_SOURCES_STR else None
|
||||
|
||||
|
||||
VIDEO_HEIGHT_CUTOFF = int(os.getenv("TUBESYNC_VIDEO_HEIGHT_CUTOFF", "240"))
|
||||
VIDEO_HEIGHT_CUTOFF = getenv("TUBESYNC_VIDEO_HEIGHT_CUTOFF", 240, integer=True)
|
||||
|
||||
|
||||
# ensure that the current directory exists
|
||||
@ -117,4 +118,11 @@ old_youtube_cache_dirs = list(YOUTUBE_DL_CACHEDIR.parent.glob('youtube-*'))
|
||||
old_youtube_cache_dirs.extend(list(YOUTUBE_DL_CACHEDIR.parent.glob('youtube/youtube-*')))
|
||||
for cache_dir in old_youtube_cache_dirs:
|
||||
cache_dir.rename(YOUTUBE_DL_CACHEDIR / cache_dir.name)
|
||||
# try to remove the old, hopefully empty, directory
|
||||
empty_old_youtube_dir = YOUTUBE_DL_CACHEDIR.parent / 'youtube'
|
||||
if empty_old_youtube_dir.is_dir():
|
||||
try:
|
||||
empty_old_youtube_dir.rmdir()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from common.utils import getenv
|
||||
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
@ -97,7 +97,7 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
TIME_ZONE = os.getenv('TZ', 'UTC')
|
||||
TIME_ZONE = getenv('TZ', 'UTC')
|
||||
USE_I18N = True
|
||||
USE_L10N = True
|
||||
USE_TZ = True
|
||||
|
Loading…
Reference in New Issue
Block a user