From 2e08bdf7980a28dbf1715f81a870e77b7377aaa2 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 30 Mar 2017 18:26:06 +0100 Subject: [PATCH] Remove the damn dotmap --- md_pdf/build/__init__.py | 12 ++++++------ md_pdf/build/pandoc.py | 6 +++--- md_pdf/build/pdf.py | 6 +++--- md_pdf/config/read.py | 3 +-- md_pdf/config/validate.py | 23 ++++++++++------------- setup.py | 1 - 6 files changed, 23 insertions(+), 28 deletions(-) diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index 14fbcf5..3aebf19 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -6,10 +6,10 @@ import os def build(config): - data = read_files(os.path.abspath(config.input)) - doc = build_document(data, getattr(config, 'bibliography', None), getattr(config, 'context', None)) - if 'html' in config.output_formats: - output_html(doc, os.path.abspath(config.output_dir)) - if 'pdf' in config.output_formats: - render_cover(config.context.toDict()) + data = read_files(os.path.abspath(config['input'])) + doc = build_document(data, config.get('bibliography'), config.get('context')) + if 'html' in config['output_formats']: + output_html(doc, os.path.abspath(config['output_dir'])) + if 'pdf' in config['output_formats']: + render_cover(config['context']) export_pdf(doc, config) diff --git a/md_pdf/build/pandoc.py b/md_pdf/build/pandoc.py index 4541647..ffe4b76 100644 --- a/md_pdf/build/pandoc.py +++ b/md_pdf/build/pandoc.py @@ -35,8 +35,8 @@ def build_document(files_content, bibliography, context): filters = [] if bibliography is not None: args += [ - '--bibliography={}'.format(os.path.abspath(bibliography.references)), - '--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography.csl))) + '--bibliography={}'.format(os.path.abspath(bibliography['references'])), + '--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography['csl']))) ] filters.append('pandoc-citeproc') @@ -48,4 +48,4 @@ def build_document(files_content, bibliography, context): filters=filters )) - return parse_template(html, context.toDict()) + return parse_template(html, context) diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index 8cd3781..c921283 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -24,12 +24,12 @@ PDF_OPTIONS = { def export_pdf(content, config): - PDF_OPTIONS['title'] = getattr(config, 'title', 'output') - PDF_OPTIONS['replace'] = list(config.context.items()) + PDF_OPTIONS['title'] = config.get('title', 'Output') + PDF_OPTIONS['replace'] = list(config['context'].items()) return pdfkit.from_string( content, - os.path.join(os.path.abspath(config.output_dir), 'output.pdf'), + os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'), options=PDF_OPTIONS, css=STYLE_FILE, cover=OUTPUT_COVER_FILE diff --git a/md_pdf/config/read.py b/md_pdf/config/read.py index 6d32c95..8af3dbc 100644 --- a/md_pdf/config/read.py +++ b/md_pdf/config/read.py @@ -1,5 +1,4 @@ import yaml -from dotmap import DotMap import os from md_pdf.consts import CONFIG_FILE from md_pdf.exceptions import ConfigValidationException @@ -8,6 +7,6 @@ from md_pdf.exceptions import ConfigValidationException def load_config(): try: with open(os.path.join(CONFIG_FILE)) as f: - return DotMap(yaml.load(f)) + return yaml.load(f) except FileNotFoundError: raise ConfigValidationException("Can't find config file at {}".format(CONFIG_FILE)) diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py index 535c7bc..3a31a17 100644 --- a/md_pdf/config/validate.py +++ b/md_pdf/config/validate.py @@ -2,7 +2,6 @@ from md_pdf.exceptions import ConfigValidationException from md_pdf.consts import CSL_DIR import glob import os -from dotmap import DotMap REQUIRED_KEYS = [ @@ -19,18 +18,16 @@ def check_required_keys(config): def test_output(config): - abs_output_dir = os.path.abspath(config.output_dir) + 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']] + 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 test_input(config): - abs_input = os.path.abspath(config.input) + abs_input = os.path.abspath(config['input']) if len(glob.glob(abs_input)) == 0: raise ConfigValidationException("No files found at {}".format(abs_input)) @@ -38,14 +35,14 @@ def test_input(config): def validate_bibliography(config): if 'bibliography' not in config: return - if 'references' not in config.bibliography: + if 'references' not in config['bibliography']: raise ConfigValidationException("Missing References Path") - abs_bibliography = os.path.abspath(config.bibliography.references) + abs_bibliography = os.path.abspath(config['bibliography']['references']) if not os.path.isfile(abs_bibliography): raise ConfigValidationException("Invalid bibliography path: '{}'".format(abs_bibliography)) - if 'csl' in config.bibliography: - if not os.path.isfile(os.path.join(CSL_DIR, "{}.csl".format(config.bibliography.csl))): + if 'csl' in config['bibliography']: + if not os.path.isfile(os.path.join(CSL_DIR, "{}.csl".format(config['bibliography']['csl']))): raise ConfigValidationException("Could not find CSL '{}'".format(config.bibliography.csl)) @@ -53,14 +50,14 @@ def validate_context(config): if 'context' not in config: return - if type(config.context) != DotMap: + if type(config['context']) != dict: raise ConfigValidationException("Context must be key:value store") - non_str_keys = [key for key in config.context.keys() if type(key) != str] + non_str_keys = [key for key in config['context'].keys() if type(key) != str] if non_str_keys: raise ConfigValidationException("Context keys must be strings. Non-strings: {}".format(", ".join(non_str_keys))) - invalid_values = [value for value in config.context.values() if type(value) in [list, dict, DotMap]] + invalid_values = [value for value in config['context'].values() if type(value) in [list, dict]] if invalid_values: raise ConfigValidationException("Context keys must be plain. Invalid values: {}".format(", ".join(invalid_values))) diff --git a/setup.py b/setup.py index 515d2c0..55b1fee 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,6 @@ setup( version="1.0", install_requires=[ "beautifulsoup4==4.5.3", - "dotmap==1.2.17", "jinja2==2.9.5", "pdfkit==0.6.1", "progressbar2==3.16.0",