2022-12-04 12:09:27 -08:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from collections import namedtuple
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from groove.exceptions import ThemeConfigurationError, ConfigurationError
|
|
|
|
|
|
|
|
import groove.path
|
|
|
|
|
|
|
|
|
2022-12-05 01:06:57 -08:00
|
|
|
Theme = namedtuple('Theme', 'name,path,author,author_link,version,about')
|
2022-12-04 12:09:27 -08:00
|
|
|
|
|
|
|
|
|
|
|
def load_theme(name=None):
|
2022-12-05 01:06:57 -08:00
|
|
|
name = name or os.environ.get('DEFAULT_THEME', None)
|
|
|
|
if not name: # pragma: no cover
|
2022-12-04 12:09:27 -08:00
|
|
|
raise ConfigurationError(
|
|
|
|
"It seems like DEFAULT_THEME is not set in your current environment.\n"
|
|
|
|
"Running 'groove setup' may help you fix this problem."
|
|
|
|
)
|
|
|
|
theme_path = groove.path.theme(name)
|
|
|
|
theme_info = _get_theme_info(theme_path)
|
2022-12-05 01:06:57 -08:00
|
|
|
try:
|
|
|
|
return Theme(
|
|
|
|
name=name,
|
|
|
|
path=theme_path,
|
|
|
|
**theme_info
|
|
|
|
)
|
|
|
|
except TypeError:
|
|
|
|
raise ThemeConfigurationError(f"The {name} them is misconfigured. Does the README.md contain a credits secton?")
|
2022-12-04 12:09:27 -08:00
|
|
|
|
|
|
|
|
|
|
|
def _get_theme_info(theme_path):
|
|
|
|
readme = theme_path / Path('README.md')
|
2022-12-05 01:06:57 -08:00
|
|
|
if not readme.exists: # pragma: no cover
|
2022-12-04 12:09:27 -08:00
|
|
|
raise ThemeConfigurationError(
|
|
|
|
"The theme is missing a required file: README.md.\n"
|
|
|
|
"Refer to the Groove On Demand documentation for help creating themes."
|
|
|
|
)
|
2022-12-05 01:06:57 -08:00
|
|
|
config = {
|
|
|
|
'about': '',
|
|
|
|
}
|
2022-12-04 12:09:27 -08:00
|
|
|
with readme.open() as fh:
|
|
|
|
in_credits = False
|
|
|
|
in_block = False
|
|
|
|
for line in fh.readlines():
|
|
|
|
line = line.strip()
|
|
|
|
if line == '## Credits':
|
|
|
|
in_credits = True
|
|
|
|
continue
|
|
|
|
config['about'] += line
|
|
|
|
if not in_credits:
|
|
|
|
continue
|
|
|
|
if line == '```':
|
|
|
|
if not in_block:
|
|
|
|
in_block = True
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
try:
|
|
|
|
(key, value) = line.split(':', 1)
|
|
|
|
key = key.strip()
|
|
|
|
value = value.strip()
|
|
|
|
except ValueError:
|
2022-12-05 01:06:57 -08:00
|
|
|
logging.warning(f"Could not parse credits line: {line}")
|
2022-12-04 12:09:27 -08:00
|
|
|
continue
|
|
|
|
logging.debug(f"Setting theme '{key}' to '{value}'.")
|
|
|
|
config[key] = value
|
|
|
|
return config
|