2022-12-05 01:06:57 -08:00
|
|
|
import logging
|
2022-12-04 12:09:27 -08:00
|
|
|
import os
|
|
|
|
|
|
|
|
from pathlib import Path
|
2022-12-05 01:06:57 -08:00
|
|
|
from groove.exceptions import ConfigurationError, ThemeMissingException, ThemeConfigurationError
|
2022-12-04 12:09:27 -08:00
|
|
|
|
2022-12-31 10:45:22 -08:00
|
|
|
_setup_hint = "You may be able to solve this error by running 'groove setup' or specifying the --root parameter."
|
2022-12-04 12:09:27 -08:00
|
|
|
_reinstall_hint = "You might need to reinstall Groove On Demand to fix this error."
|
|
|
|
|
|
|
|
|
|
|
|
def root():
|
2022-12-21 22:42:36 -08:00
|
|
|
path = Path(__file__).parent.expanduser().absolute() / Path('static')
|
2022-12-05 01:06:57 -08:00
|
|
|
logging.debug(f"Root is {path}")
|
2022-12-04 12:09:27 -08:00
|
|
|
return Path(path)
|
|
|
|
|
|
|
|
|
|
|
|
def media_root():
|
|
|
|
path = os.environ.get('MEDIA_ROOT', None)
|
|
|
|
if not path:
|
|
|
|
raise ConfigurationError(f"MEDIA_ROOT is not defined in your environment.\n\n{_setup_hint}")
|
2022-12-05 01:06:57 -08:00
|
|
|
path = Path(path).expanduser()
|
|
|
|
if not path.exists() or not path.is_dir():
|
2022-12-04 12:09:27 -08:00
|
|
|
raise ConfigurationError(
|
|
|
|
"The media_root directory (MEDIA_ROOT) doesn't exist, or isn't a directory.\n\n{_setup_hint}"
|
|
|
|
)
|
2022-12-05 01:06:57 -08:00
|
|
|
logging.debug(f"Media root is {path}")
|
2022-12-04 12:09:27 -08:00
|
|
|
return path
|
|
|
|
|
|
|
|
|
2022-12-31 10:45:22 -08:00
|
|
|
def cache_root():
|
|
|
|
path = os.environ.get('CACHE_ROOT', None)
|
|
|
|
if not path:
|
|
|
|
raise ConfigurationError(f"CACHE_ROOT is not defined in your environment.\n\n{_setup_hint}")
|
|
|
|
path = Path(path).expanduser()
|
|
|
|
logging.debug(f"Media cache root is {path}")
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2022-12-04 12:09:27 -08:00
|
|
|
def media(relpath):
|
2022-12-05 01:06:57 -08:00
|
|
|
path = media_root() / Path(relpath)
|
|
|
|
return path
|
2022-12-04 12:09:27 -08:00
|
|
|
|
|
|
|
|
2022-12-31 10:45:22 -08:00
|
|
|
def transcoded_media(relpath):
|
2022-12-31 12:28:07 -08:00
|
|
|
path = cache_root() / Path(relpath + '.webm')
|
2022-12-31 10:45:22 -08:00
|
|
|
return path
|
|
|
|
|
|
|
|
|
2022-12-04 12:09:27 -08:00
|
|
|
def static_root():
|
|
|
|
dirname = os.environ.get('STATIC_PATH', 'static')
|
|
|
|
path = root() / Path(dirname)
|
2022-12-21 22:27:30 -08:00
|
|
|
logging.debug(f"Static root is {path}")
|
2022-12-05 01:06:57 -08:00
|
|
|
if not path.exists() or not path.is_dir():
|
2022-12-07 23:41:49 -08:00
|
|
|
raise ConfigurationError( # pragma: no cover
|
2022-12-04 12:09:27 -08:00
|
|
|
f"The static assets directory {dirname} (STATIC_PATH) "
|
|
|
|
f"doesn't exist, or isn't a directory.\n\n{_reinstall_hint}"
|
|
|
|
)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2022-12-05 01:06:57 -08:00
|
|
|
def static(relpath, theme=None):
|
|
|
|
if theme:
|
|
|
|
root = theme.path / Path('static')
|
|
|
|
if not root.is_dir():
|
2022-12-07 23:41:49 -08:00
|
|
|
raise ThemeConfigurationError( # pragma: no cover
|
2022-12-05 01:06:57 -08:00
|
|
|
f"The themes directory {relpath} (THEMES_PATH) "
|
|
|
|
f"doesn't contain a 'static' directory."
|
|
|
|
)
|
|
|
|
path = root / Path(relpath)
|
|
|
|
logging.debug(f"Checking for {path}")
|
|
|
|
if path.exists():
|
|
|
|
return path
|
|
|
|
path = static_root() / Path(relpath)
|
|
|
|
logging.debug(f"Defaulting to {path}")
|
|
|
|
return path
|
2022-12-04 12:09:27 -08:00
|
|
|
|
|
|
|
|
|
|
|
def themes_root():
|
|
|
|
dirname = os.environ.get('THEMES_PATH', 'themes')
|
|
|
|
path = root() / Path(dirname)
|
2022-12-05 01:06:57 -08:00
|
|
|
if not path.exists() or not path.is_dir():
|
2022-12-07 23:41:49 -08:00
|
|
|
raise ConfigurationError( # pragma: no cover
|
2022-12-04 12:09:27 -08:00
|
|
|
f"The themes directory {dirname} (THEMES_PATH) "
|
|
|
|
f"doesn't exist, or isn't a directory.\n\n{_reinstall_hint}"
|
|
|
|
)
|
2022-12-05 01:06:57 -08:00
|
|
|
logging.debug(f"Themes root is {path}")
|
2022-12-04 12:09:27 -08:00
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def theme(name):
|
|
|
|
path = themes_root() / Path(name)
|
2022-12-21 21:16:06 -08:00
|
|
|
if not path.exists():
|
2022-12-07 23:41:49 -08:00
|
|
|
available = ','.join(available_themes())
|
2022-12-04 12:09:27 -08:00
|
|
|
raise ThemeMissingException(
|
|
|
|
f"A theme directory named {name} does not exist or isn't a directory. "
|
|
|
|
"Perhaps there is a typo in the name?\n"
|
|
|
|
f"Available themes: {available}"
|
|
|
|
)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def theme_template(template_name):
|
|
|
|
return Path('templates') / Path(f"{template_name}.tpl")
|
|
|
|
|
|
|
|
|
|
|
|
def available_themes():
|
|
|
|
return [theme.name for theme in themes_root().iterdir() if theme.is_dir()]
|
|
|
|
|
|
|
|
|
|
|
|
def database():
|
2022-12-21 21:16:06 -08:00
|
|
|
path = Path(os.environ.get('DATABASE_PATH', '~')).expanduser()
|
|
|
|
if not path.exists() or not path.is_dir():
|
|
|
|
raise ConfigurationError(
|
|
|
|
"DATABASE_PATH doesn't exist or isn't a directory.\n\n{_setup_hint}"
|
|
|
|
)
|
2022-12-21 15:17:13 -08:00
|
|
|
return path / Path('groove_on_demand.db')
|