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

114 lines
3.8 KiB
Python
Raw Normal View History

2017-03-29 18:43:35 +01:00
from md_pdf.exceptions import ConfigValidationException
2017-03-30 08:44:50 +01:00
from md_pdf.consts import CSL_DIR
2017-03-30 08:56:29 +01:00
import glob
2017-03-29 19:16:12 +01:00
import os
2017-04-01 16:12:03 +01:00
import logging
2017-05-25 21:33:18 +01:00
from dateutil import parser
import datetime
2017-04-01 16:12:03 +01:00
logger = logging.getLogger(__file__)
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):
2017-03-30 18:26:06 +01:00
abs_output_dir = os.path.abspath(config['output_dir'])
2017-03-29 19:16:12 +01:00
if not os.path.isdir(abs_output_dir):
2017-05-03 21:10:18 +01:00
logger.debug("Creating output directory...")
os.mkdir(abs_output_dir)
2017-03-30 18:26:06 +01:00
invalid_formats = [key for key in config['output_formats'] if key not in ['html', 'pdf']]
2017-03-29 19:16:12 +01:00
if invalid_formats:
raise ConfigValidationException("Invalid output formats provided: '{}'".format(", ".join(invalid_formats)))
2017-03-30 08:56:29 +01:00
def test_input(config):
2017-03-30 18:26:06 +01:00
abs_input = os.path.abspath(config['input'])
2017-03-30 08:56:29 +01:00
if len(glob.glob(abs_input)) == 0:
raise ConfigValidationException("No files found at {}".format(abs_input))
2017-05-27 22:06:55 +01:00
for file in glob.iglob(abs_input):
if not os.path.isfile(file):
raise ConfigValidationException("Input must be a glob of files")
2017-03-30 08:56:29 +01:00
2017-03-29 19:32:15 +01:00
def validate_bibliography(config):
if 'bibliography' not in config:
return
2017-03-30 18:26:06 +01:00
if 'references' not in config['bibliography']:
2017-03-30 08:44:50 +01:00
raise ConfigValidationException("Missing References Path")
2017-04-04 20:20:58 +01:00
if 'csl' not in config['bibliography']:
raise ConfigValidationException("Missing CSL Name")
2017-03-30 08:44:50 +01:00
2017-03-30 18:26:06 +01:00
abs_bibliography = os.path.abspath(config['bibliography']['references'])
2017-03-29 19:32:15 +01:00
if not os.path.isfile(abs_bibliography):
raise ConfigValidationException("Invalid bibliography path: '{}'".format(abs_bibliography))
2017-05-27 22:06:55 +01:00
if not os.path.isfile(os.path.join(CSL_DIR, "{}.csl".format(config['bibliography']['csl']))):
2017-05-27 22:34:01 +01:00
raise ConfigValidationException("Could not find CSL '{}' in {}".format(config['bibliography']['csl'], CSL_DIR))
2017-03-29 19:32:15 +01:00
2017-03-30 17:08:00 +01:00
def validate_context(config):
if 'context' not in config:
return
2017-03-30 18:26:06 +01:00
if type(config['context']) != dict:
2017-03-30 17:08:00 +01:00
raise ConfigValidationException("Context must be key:value store")
2017-03-30 18:26:06 +01:00
non_str_keys = [key for key in config['context'].keys() if type(key) != str]
2017-03-30 17:08:00 +01:00
if non_str_keys:
2017-05-27 22:06:55 +01:00
raise ConfigValidationException("Context keys must be strings. Non-strings: {}".format(non_str_keys))
2017-03-30 17:08:00 +01:00
2017-03-30 18:26:06 +01:00
invalid_values = [value for value in config['context'].values() if type(value) in [list, dict]]
2017-03-30 17:08:00 +01:00
if invalid_values:
2017-05-27 22:06:55 +01:00
raise ConfigValidationException("Context keys must be plain. Invalid values: {}".format(invalid_values))
2017-03-30 17:08:00 +01:00
2017-05-17 20:29:03 +01:00
def validate_toc(config):
if 'toc' not in config:
return
if type(config['toc']) != bool:
raise ConfigValidationException("Table of contents key should be either true or false")
def validate_wordcount(config):
if 'show_word_count' not in config:
return
if type(config['show_word_count']) != bool:
raise ConfigValidationException("Show word count key should be either true or false")
2017-05-25 21:33:18 +01:00
def validate_submission_date(config):
if 'submission_date' not in config:
return
if type(config['submission_date']) in [datetime.date, datetime.datetime, datetime.time]:
return
try:
parser.parse(config['submission_date'])
except ValueError:
2017-05-27 22:06:55 +01:00
raise ConfigValidationException("Invalid Submission Date format {}".format(config['submission_date']))
2017-05-25 21:33:18 +01:00
2017-03-29 18:43:35 +01:00
def validate_config(config):
2017-05-17 21:38:01 +01:00
logger.debug("Validating Config...")
2017-03-30 08:58:05 +01:00
for validator in [
check_required_keys,
test_input,
test_output,
2017-03-30 17:08:00 +01:00
validate_bibliography,
2017-05-17 20:29:03 +01:00
validate_context,
validate_toc,
2017-05-25 21:33:18 +01:00
validate_wordcount,
validate_submission_date
2017-03-30 08:58:05 +01:00
]:
validator(config)
2017-05-17 21:38:01 +01:00
logger.debug("Config Ok!")