made all folder paths configurable
This commit is contained in:
parent
e0f669c62c
commit
ce8f5f0084
1 changed files with 31 additions and 17 deletions
|
@ -5,12 +5,30 @@ import string
|
||||||
import sys
|
import sys
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import sass
|
import sass
|
||||||
from django.contrib.messages import constants as messages
|
from django.contrib.messages import constants as messages
|
||||||
from django.utils.crypto import get_random_string
|
from django.utils.crypto import get_random_string
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
def get_data_dir(setting: str, fallback: Path, create: bool = True, parents: bool = False,
|
||||||
|
config_section: str = 'c3nav', config_option: Optional[str] = None):
|
||||||
|
if not config_option:
|
||||||
|
config_option = setting.lower()
|
||||||
|
subdir = config.get(config_section, config_option, fallback=os.environ.get('C3NAV_%s' % setting, None))
|
||||||
|
subdir = Path(subdir).resolve() if subdir else fallback
|
||||||
|
if not subdir.exists():
|
||||||
|
if create:
|
||||||
|
subdir.mkdir(parents=parents)
|
||||||
|
else:
|
||||||
|
raise FileNotFoundError('The %s directory [%s] doesn\'t exist.' % (config_option, subdir))
|
||||||
|
elif not subdir.is_dir():
|
||||||
|
raise NotADirectoryError('The path set for the %s directory [%s] is not a directory.' % (config_option, subdir))
|
||||||
|
return subdir
|
||||||
|
|
||||||
|
|
||||||
config = configparser.RawConfigParser()
|
config = configparser.RawConfigParser()
|
||||||
if 'C3NAV_CONFIG' in os.environ:
|
if 'C3NAV_CONFIG' in os.environ:
|
||||||
open(os.environ.get('C3NAV_CONFIG'), encoding='utf-8')
|
open(os.environ.get('C3NAV_CONFIG'), encoding='utf-8')
|
||||||
|
@ -35,23 +53,18 @@ with suppress(ImportError):
|
||||||
# Build paths inside the project like this: BASE_DIR / 'something'
|
# Build paths inside the project like this: BASE_DIR / 'something'
|
||||||
PROJECT_DIR = Path(__file__).resolve().parent
|
PROJECT_DIR = Path(__file__).resolve().parent
|
||||||
BASE_DIR = PROJECT_DIR.parent
|
BASE_DIR = PROJECT_DIR.parent
|
||||||
DATA_DIR = config.get('c3nav', 'datadir', fallback=os.environ.get('C3NAV_DATA_DIR', None))
|
DATA_DIR = get_data_dir('DATA_DIR', BASE_DIR / 'data', parents=True, config_option='datadir')
|
||||||
DATA_DIR = Path(DATA_DIR).resolve() if DATA_DIR else BASE_DIR / 'data'
|
LOG_DIR = get_data_dir('LOG_DIR', DATA_DIR / 'logs', config_option='logdir')
|
||||||
LOG_DIR = config.get('c3nav', 'logdir', fallback=None)
|
MEDIA_ROOT = get_data_dir('MEDIA_ROOT', DATA_DIR / 'media', config_section='django')
|
||||||
LOG_DIR = Path(LOG_DIR).resolve() if LOG_DIR else DATA_DIR / 'logs'
|
SOURCES_ROOT = get_data_dir('SOURCES_ROOT', DATA_DIR / 'sources')
|
||||||
MEDIA_ROOT = DATA_DIR / 'media'
|
MAP_ROOT = get_data_dir('MAP_ROOT', DATA_DIR / 'map')
|
||||||
SOURCES_ROOT = DATA_DIR / 'sources'
|
RENDER_ROOT = get_data_dir('RENDER_ROOT', DATA_DIR / 'render')
|
||||||
MAP_ROOT = DATA_DIR / 'map'
|
TILES_ROOT = get_data_dir('TILES_ROOT', DATA_DIR / 'tiles')
|
||||||
RENDER_ROOT = DATA_DIR / 'render'
|
CACHE_ROOT = get_data_dir('CACHE_ROOT', DATA_DIR / 'cache')
|
||||||
TILES_ROOT = DATA_DIR / 'tiles'
|
STATS_ROOT = get_data_dir('STATS_ROOT', DATA_DIR / 'stats')
|
||||||
CACHE_ROOT = DATA_DIR / 'cache'
|
|
||||||
STATS_ROOT = DATA_DIR / 'stats'
|
|
||||||
|
|
||||||
if not DATA_DIR.exists():
|
# override the matplotlib default config directory if it's not configured
|
||||||
DATA_DIR.mkdir(parents=True)
|
os.environ.setdefault('MPLCONFIGDIR', str(get_data_dir('MPLCONFIGDIR', CACHE_ROOT / 'matplotlib')))
|
||||||
for subdir in (LOG_DIR, MEDIA_ROOT, SOURCES_ROOT, MAP_ROOT, RENDER_ROOT, TILES_ROOT, CACHE_ROOT, STATS_ROOT):
|
|
||||||
if not subdir.exists():
|
|
||||||
subdir.mkdir()
|
|
||||||
|
|
||||||
MEDIA_URL = '/media/'
|
MEDIA_URL = '/media/'
|
||||||
|
|
||||||
|
@ -66,7 +79,8 @@ if RANDOM_LOCATION_GROUPS:
|
||||||
if config.has_option('django', 'secret'):
|
if config.has_option('django', 'secret'):
|
||||||
SECRET_KEY = config.get('django', 'secret')
|
SECRET_KEY = config.get('django', 'secret')
|
||||||
else:
|
else:
|
||||||
SECRET_FILE = DATA_DIR / '.secret'
|
SECRET_FILE = config.get('django', 'secret_file',
|
||||||
|
fallback=os.environ.get('C3NAV_SECRET_FILE', default=DATA_DIR / '.secret'))
|
||||||
if SECRET_FILE.exists():
|
if SECRET_FILE.exists():
|
||||||
with open(SECRET_FILE, 'r') as f:
|
with open(SECRET_FILE, 'r') as f:
|
||||||
SECRET_KEY = f.read().strip()
|
SECRET_KEY = f.read().strip()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue