Commit 0ef43661 authored by Matthieu Boileau's avatar Matthieu Boileau
Browse files

Fix #16

parent 924981d7
Pipeline #5486 passed with stage
in 31 seconds
......@@ -60,23 +60,7 @@ class NbCourse:
def __init__(self, user_conf=None):
"""Build from user_conf"""
self.conf = self.default_conf.copy()
if type(user_conf) is Path:
# Load user conf as file and update default conf
self.config_file = user_conf
update_dict(self.conf, self._get_user_config(self.config_file))
self.conf['project_path'] = self.config_file.absolute().parent
else:
self.conf['project_path'] = Path.cwd()
self.config_file = None
if type(user_conf) is dict:
# Load user conf as dict and update default conf
update_dict(self.conf, user_conf)
else:
# Only default config is loaded (only useful for tests)
log.debug("Default configuration is used")
self.conf = self._get_config(user_conf)
self.conf['template_path'] = Path(self.conf['theme']['dir'],
'templates')
for key in 'nb', 'theme':
......@@ -126,6 +110,43 @@ class NbCourse:
else:
self.slide_exporter.reveal_url_prefix = reveal_url
def _get_config(self, user_conf):
"""Get conf dict from default and user_conf"""
def sanitize(user_conf_dict: dict):
"""
Sanitize user_conf_dict: remove first level keys that are
not present in default dict
"""
for k in user_conf_dict:
if k not in conf.keys():
log.warning(f'Unknown configuration parameter {k}: '
'ignored.')
del(k)
conf = self.default_conf.copy()
if type(user_conf) is Path:
# Load user conf as file and update default conf
config_file = user_conf
# Load YAML file as dict
with open(config_file, 'r') as f:
user_conf_dict = yaml.safe_load(f)
sanitize(user_conf_dict)
update_dict(conf, user_conf_dict)
conf['project_path'] = config_file.absolute().parent
conf['config_file'] = config_file
else:
if type(user_conf) is dict:
# Load user conf as dict and update default conf
sanitize(user_conf)
update_dict(conf, user_conf)
else:
# Only default config is loaded (used for tests only)
log.debug("Default configuration is used")
conf['project_path'] = Path.cwd()
conf['config_file'] = None
return conf
def _get_pages(self):
"""get a list of target html pages from markdown source"""
# Start html_pages list with index.html
......@@ -135,14 +156,6 @@ class NbCourse:
Path(path.name).with_suffix('.html'))
return html_pages
@staticmethod
def _get_user_config(config_file: Path) -> dict:
"""
Parse yaml file to build the corresponding configuration dictionary
"""
with open(config_file, 'r') as f:
return yaml.safe_load(f)
def build_pages(self):
"""
Build a mini website using html templating and markdown conversion
......@@ -326,8 +339,8 @@ class NbCourse:
def task_build_pages(self):
"""Build html pages"""
deps = self.executed_notebooks + self.md_page_paths + self.theme_files
if self.config_file:
deps.append(self.config_file)
if self.conf['config_file']:
deps.append(self.conf['config_file'])
return {
'file_dep': deps,
'task_dep': ['copy_material'],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment