From 1a60c4c1bca8ca214e85458a99d1f3a1ed1b99a5 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 29 Mar 2017 19:16:12 +0100 Subject: [PATCH] Specify output formats --- md_pdf/build/__init__.py | 10 ++++++---- md_pdf/cli.py | 6 +++--- md_pdf/config/validate.py | 16 +++++++++++++++- md_pdf/exceptions.py | 8 ++++++-- test-files/mdp.yml | 5 +++++ 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index f160766..b5c3bd5 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -6,9 +6,11 @@ from md_pdf.build.pdf import export_pdf import os -def build(args, config): +def build(config): data = read_files(os.path.join(WORKING_DIR, '*.md')) doc = build_document(data, os.path.join(WORKING_DIR, 'bib.yaml')) - output_html(doc, os.path.join(WORKING_DIR, 'out')) - render_cover() - export_pdf(doc, os.path.join(WORKING_DIR, 'out')) + if 'html' in config.output_formats: + output_html(doc, os.path.join(WORKING_DIR, 'out')) + if 'pdf' in config.output_formats: + render_cover() + export_pdf(doc, os.path.join(WORKING_DIR, 'out')) diff --git a/md_pdf/cli.py b/md_pdf/cli.py index bf10bed..607b04f 100644 --- a/md_pdf/cli.py +++ b/md_pdf/cli.py @@ -1,6 +1,6 @@ import logging from md_pdf.args import parse_args -from md_pdf.exceptions import PrematureExit +from md_pdf.exceptions import PrematureExit, BaseException from md_pdf.build import build from md_pdf.config.read import load_config from md_pdf.config.validate import validate_config @@ -15,10 +15,10 @@ def cli(): try: config = load_config() validate_config(config) - build(args, config) + build(config) except PrematureExit: return 0 - except Exception as e: + except BaseException as e: logging.error(str(e)) return 1 return 0 diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py index a0d4c0f..9350e20 100644 --- a/md_pdf/config/validate.py +++ b/md_pdf/config/validate.py @@ -1,8 +1,10 @@ from md_pdf.exceptions import ConfigValidationException +import os REQUIRED_KEYS = [ - 'output', + 'output_dir', + 'output_formats', 'input' ] @@ -13,7 +15,19 @@ def check_required_keys(config): raise ConfigValidationException("Missing required keys: {}".format(", ".join(missing_keys))) +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))) + + def validate_config(config): check_required_keys(config) + test_output(config) diff --git a/md_pdf/exceptions.py b/md_pdf/exceptions.py index c608b52..a11a747 100644 --- a/md_pdf/exceptions.py +++ b/md_pdf/exceptions.py @@ -1,6 +1,10 @@ -class PrematureExit(Exception): +class BaseException(Exception): pass -class ConfigValidationException(Exception): +class PrematureExit(BaseException): + pass + + +class ConfigValidationException(BaseException): pass diff --git a/test-files/mdp.yml b/test-files/mdp.yml index e69de29..dce2520 100644 --- a/test-files/mdp.yml +++ b/test-files/mdp.yml @@ -0,0 +1,5 @@ +input: "*.md" +output_formats: + - html + - pdf +output_dir: out/