Remove the damn dotmap

This commit is contained in:
Jake Howard 2017-03-30 18:26:06 +01:00
parent 339c4a3df2
commit 2e08bdf798
6 changed files with 23 additions and 28 deletions

View file

@ -6,10 +6,10 @@ import os
def build(config): def build(config):
data = read_files(os.path.abspath(config.input)) data = read_files(os.path.abspath(config['input']))
doc = build_document(data, getattr(config, 'bibliography', None), getattr(config, 'context', None)) doc = build_document(data, config.get('bibliography'), config.get('context'))
if 'html' in config.output_formats: if 'html' in config['output_formats']:
output_html(doc, os.path.abspath(config.output_dir)) output_html(doc, os.path.abspath(config['output_dir']))
if 'pdf' in config.output_formats: if 'pdf' in config['output_formats']:
render_cover(config.context.toDict()) render_cover(config['context'])
export_pdf(doc, config) export_pdf(doc, config)

View file

@ -35,8 +35,8 @@ def build_document(files_content, bibliography, context):
filters = [] filters = []
if bibliography is not None: if bibliography is not None:
args += [ args += [
'--bibliography={}'.format(os.path.abspath(bibliography.references)), '--bibliography={}'.format(os.path.abspath(bibliography['references'])),
'--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography.csl))) '--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography['csl'])))
] ]
filters.append('pandoc-citeproc') filters.append('pandoc-citeproc')
@ -48,4 +48,4 @@ def build_document(files_content, bibliography, context):
filters=filters filters=filters
)) ))
return parse_template(html, context.toDict()) return parse_template(html, context)

View file

@ -24,12 +24,12 @@ PDF_OPTIONS = {
def export_pdf(content, config): def export_pdf(content, config):
PDF_OPTIONS['title'] = getattr(config, 'title', 'output') PDF_OPTIONS['title'] = config.get('title', 'Output')
PDF_OPTIONS['replace'] = list(config.context.items()) PDF_OPTIONS['replace'] = list(config['context'].items())
return pdfkit.from_string( return pdfkit.from_string(
content, 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, options=PDF_OPTIONS,
css=STYLE_FILE, css=STYLE_FILE,
cover=OUTPUT_COVER_FILE cover=OUTPUT_COVER_FILE

View file

@ -1,5 +1,4 @@
import yaml import yaml
from dotmap import DotMap
import os import os
from md_pdf.consts import CONFIG_FILE from md_pdf.consts import CONFIG_FILE
from md_pdf.exceptions import ConfigValidationException from md_pdf.exceptions import ConfigValidationException
@ -8,6 +7,6 @@ from md_pdf.exceptions import ConfigValidationException
def load_config(): def load_config():
try: try:
with open(os.path.join(CONFIG_FILE)) as f: with open(os.path.join(CONFIG_FILE)) as f:
return DotMap(yaml.load(f)) return yaml.load(f)
except FileNotFoundError: except FileNotFoundError:
raise ConfigValidationException("Can't find config file at {}".format(CONFIG_FILE)) raise ConfigValidationException("Can't find config file at {}".format(CONFIG_FILE))

View file

@ -2,7 +2,6 @@ from md_pdf.exceptions import ConfigValidationException
from md_pdf.consts import CSL_DIR from md_pdf.consts import CSL_DIR
import glob import glob
import os import os
from dotmap import DotMap
REQUIRED_KEYS = [ REQUIRED_KEYS = [
@ -19,18 +18,16 @@ def check_required_keys(config):
def test_output(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): if not os.path.isdir(abs_output_dir):
raise ConfigValidationException("Can't find output directory '{}'".format(abs_output_dir)) raise ConfigValidationException("Can't find output directory '{}'".format(abs_output_dir))
if not config.output_formats: invalid_formats = [key for key in config['output_formats'] if key not in ['html', 'pdf']]
raise ConfigValidationException("No output formats specified")
invalid_formats = [key for key in config.output_formats if key not in ['html', 'pdf']]
if invalid_formats: if invalid_formats:
raise ConfigValidationException("Invalid output formats provided: '{}'".format(", ".join(invalid_formats))) raise ConfigValidationException("Invalid output formats provided: '{}'".format(", ".join(invalid_formats)))
def test_input(config): 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: if len(glob.glob(abs_input)) == 0:
raise ConfigValidationException("No files found at {}".format(abs_input)) raise ConfigValidationException("No files found at {}".format(abs_input))
@ -38,14 +35,14 @@ def test_input(config):
def validate_bibliography(config): def validate_bibliography(config):
if 'bibliography' not in config: if 'bibliography' not in config:
return return
if 'references' not in config.bibliography: if 'references' not in config['bibliography']:
raise ConfigValidationException("Missing References Path") 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): if not os.path.isfile(abs_bibliography):
raise ConfigValidationException("Invalid bibliography path: '{}'".format(abs_bibliography)) raise ConfigValidationException("Invalid bibliography path: '{}'".format(abs_bibliography))
if 'csl' in config.bibliography: if 'csl' in config['bibliography']:
if not os.path.isfile(os.path.join(CSL_DIR, "{}.csl".format(config.bibliography.csl))): 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)) raise ConfigValidationException("Could not find CSL '{}'".format(config.bibliography.csl))
@ -53,14 +50,14 @@ def validate_context(config):
if 'context' not in config: if 'context' not in config:
return return
if type(config.context) != DotMap: if type(config['context']) != dict:
raise ConfigValidationException("Context must be key:value store") 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: if non_str_keys:
raise ConfigValidationException("Context keys must be strings. Non-strings: {}".format(", ".join(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: if invalid_values:
raise ConfigValidationException("Context keys must be plain. Invalid values: {}".format(", ".join(invalid_values))) raise ConfigValidationException("Context keys must be plain. Invalid values: {}".format(", ".join(invalid_values)))

View file

@ -6,7 +6,6 @@ setup(
version="1.0", version="1.0",
install_requires=[ install_requires=[
"beautifulsoup4==4.5.3", "beautifulsoup4==4.5.3",
"dotmap==1.2.17",
"jinja2==2.9.5", "jinja2==2.9.5",
"pdfkit==0.6.1", "pdfkit==0.6.1",
"progressbar2==3.16.0", "progressbar2==3.16.0",