This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
md-pdf/md_pdf/config/validate.py

43 lines
1.3 KiB
Python
Raw Normal View History

2017-03-29 18:43:35 +01:00
from md_pdf.exceptions import ConfigValidationException
2017-03-29 19:16:12 +01:00
import os
2017-03-29 18:43:35 +01:00
REQUIRED_KEYS = [
2017-03-29 19:16:12 +01:00
'output_dir',
'output_formats',
2017-03-29 18:43:35 +01:00
'input'
]
def check_required_keys(config):
missing_keys = [key for key in REQUIRED_KEYS if key not in config]
if missing_keys:
2017-03-29 18:50:12 +01:00
raise ConfigValidationException("Missing required keys: {}".format(", ".join(missing_keys)))
2017-03-29 18:43:35 +01:00
2017-03-29 19:16:12 +01:00
def test_output(config):
abs_output_dir = os.path.abspath(config.output_dir)
if not os.path.isdir(abs_output_dir):
raise ConfigValidationException("Can't find output directory '{}'".format(abs_output_dir))
if not config.output_formats:
raise ConfigValidationException("No output formats specified")
invalid_formats = [key for key in config.output_formats if key not in ['html', 'pdf']]
if invalid_formats:
raise ConfigValidationException("Invalid output formats provided: '{}'".format(", ".join(invalid_formats)))
2017-03-29 19:32:15 +01:00
def validate_bibliography(config):
if 'bibliography' not in config:
return
abs_bibliography = os.path.abspath(config.bibliography)
if not os.path.isfile(abs_bibliography):
raise ConfigValidationException("Invalid bibliography path: '{}'".format(abs_bibliography))
2017-03-29 18:43:35 +01:00
def validate_config(config):
check_required_keys(config)
2017-03-29 19:16:12 +01:00
test_output(config)
2017-03-29 19:32:15 +01:00
validate_bibliography(config)
2017-03-29 18:43:35 +01:00