88 lines
2.5 KiB
Python
88 lines
2.5 KiB
Python
from importlib import import_module
|
|
from itertools import chain
|
|
from time import time
|
|
from typing import Union
|
|
import logging.config
|
|
import typesystem
|
|
from website_generator.config import Config
|
|
from website_generator.actions import registry
|
|
|
|
DEFAULT_LOGGING = {
|
|
'version': 1,
|
|
'formatters': {
|
|
'print': {
|
|
'format': '%(message)s',
|
|
},
|
|
},
|
|
'handlers': {
|
|
'console': {
|
|
'class': 'logging.StreamHandler',
|
|
'formatter': 'print',
|
|
'level': 'INFO',
|
|
'stream': 'ext://sys.stdout',
|
|
},
|
|
},
|
|
'loggers': {
|
|
'website_generator': {
|
|
'level': 'INFO',
|
|
'propagate': True,
|
|
'handlers': ['console'],
|
|
},
|
|
},
|
|
}
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class WebsiteGenerator(object):
|
|
|
|
def __init__(self, config: Union[str, Config]):
|
|
if not isinstance(config, Config):
|
|
config = typesystem.validate_yaml(
|
|
config,
|
|
validator=Config,
|
|
)
|
|
self.config = config
|
|
|
|
def __call__(self, tags=[]):
|
|
logging.config.dictConfig(self.config.logging or DEFAULT_LOGGING)
|
|
|
|
logger.debug('Configuration version {!s}'.format(self.config.version))
|
|
logger.debug('Content root: {!s}'.format(self.config.content_root))
|
|
logger.debug('Output root: {!s}'.format(self.config.output_root))
|
|
|
|
if self.config.use:
|
|
logger.debug('Loading modules…')
|
|
for name in self.config.use:
|
|
logger.debug('Loading {}'.format(name))
|
|
import_module(name)
|
|
|
|
action_configs = self.config.actions
|
|
if tags:
|
|
action_configs = list(filter(
|
|
lambda a: any((tag in a.tags for tag in tags)),
|
|
action_configs,
|
|
))
|
|
else:
|
|
tags = list(set(chain(*[
|
|
action.tags for action in action_configs
|
|
])))
|
|
|
|
logger.info('Running {} actions'.format(len(action_configs)))
|
|
|
|
start_time = time()
|
|
for action_config in action_configs:
|
|
logger.info(" {}…".format(action_config.name))
|
|
action_class = registry[action_config.type]
|
|
action = action_class(
|
|
main_config=self.config,
|
|
**action_config,
|
|
)
|
|
action()
|
|
logger.info(" {}: done".format(action_config.name))
|
|
run_time = time() - start_time
|
|
|
|
logger.info('Completed {} actions in {:.1f} seconds'.format(
|
|
len(action_configs),
|
|
run_time,
|
|
))
|