From bbfc3cac4cac193f40680a900127ab632586c74c Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 9 Jun 2017 23:07:30 +0100 Subject: [PATCH] Add type annotations to most places --- md_pdf/build/__init__.py | 2 +- md_pdf/build/context.py | 10 +++++----- md_pdf/build/jinja.py | 2 +- md_pdf/build/md.py | 5 +++-- md_pdf/build/pandoc.py | 4 ++-- md_pdf/build/pdf.py | 2 +- md_pdf/build/templates.py | 4 ++-- md_pdf/config/read.py | 2 +- md_pdf/utils.py | 8 +++++--- 9 files changed, 21 insertions(+), 18 deletions(-) diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index 177216b..6b94e00 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -11,7 +11,7 @@ import time logger = logging.getLogger(__file__) -def build(config): +def build(config: dict): logger.debug("Starting Build...") start_time = time.time() data = read_files(os.path.abspath(config['input'])) diff --git a/md_pdf/build/context.py b/md_pdf/build/context.py index 2e2f018..958caec 100644 --- a/md_pdf/build/context.py +++ b/md_pdf/build/context.py @@ -19,14 +19,14 @@ EXTRA_CONTEXT = { } -def get_context(config, content): +def get_context(config: dict, content: str) -> dict: config = config.copy() if 'context' in config: context = config['context'].copy() del config['context'] else: context = {} - context = dict( + merged_context = dict( config, **EXTRA_CONTEXT, **context, @@ -35,11 +35,11 @@ def get_context(config, content): } ) if config.get('show_word_count'): - context['word_count'] = word_count(get_plain_text(content)) + merged_context['word_count'] = word_count(get_plain_text(content)) if config.get('submission_date'): if type(config['submission_date']) in [datetime.date, datetime.datetime, datetime.time]: submission_date = config['submission_date'] else: submission_date = parser.parse(config['submission_date']) - context['submission_date'] = submission_date.strftime(DATE_FORMAT) - return context + merged_context['submission_date'] = submission_date.strftime(DATE_FORMAT) + return merged_context diff --git a/md_pdf/build/jinja.py b/md_pdf/build/jinja.py index d5f0687..2b068de 100644 --- a/md_pdf/build/jinja.py +++ b/md_pdf/build/jinja.py @@ -1,7 +1,7 @@ from jinja2 import Environment -def render_content(content, context): +def render_content(content: str, context: dict) -> str: env = Environment( autoescape=True, trim_blocks=True, diff --git a/md_pdf/build/md.py b/md_pdf/build/md.py index 93ba6a0..d08729d 100644 --- a/md_pdf/build/md.py +++ b/md_pdf/build/md.py @@ -1,12 +1,13 @@ import glob +from typing import Generator, List -def get_files_content(filenames): +def get_files_content(filenames: List[str]) -> Generator[str, None, None]: for filename in filenames: with open(filename) as f: yield f.read() -def read_files(files_glob): +def read_files(files_glob: str) -> str: filenames = sorted(glob.glob(files_glob)) return '\n'.join(list(get_files_content(filenames))) diff --git a/md_pdf/build/pandoc.py b/md_pdf/build/pandoc.py index 1c7ab2d..7fd4362 100644 --- a/md_pdf/build/pandoc.py +++ b/md_pdf/build/pandoc.py @@ -6,13 +6,13 @@ import logging logger = logging.getLogger(__file__) -def output_html(html, out_dir): +def output_html(html: str, out_dir: str): logger.info("Outputting HTML...") with open(os.path.join(out_dir, 'output.html'), 'w') as f: f.write(html) -def build_document(files_content, bibliography): +def build_document(files_content: str, bibliography: dict) -> str: args = [ '-s', ] diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index 40200a0..9fe4612 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -33,7 +33,7 @@ PDF_OPTIONS = { } -def export_pdf(content, config): +def export_pdf(content: str, config: dict) -> dict: if logger.getEffectiveLevel() > logging.DEBUG: PDF_OPTIONS['quiet'] = "" PDF_OPTIONS['title'] = config.get('title', 'Output') diff --git a/md_pdf/build/templates.py b/md_pdf/build/templates.py index e0cf1f0..e62966c 100644 --- a/md_pdf/build/templates.py +++ b/md_pdf/build/templates.py @@ -11,7 +11,7 @@ FILE_NAME_FORMAT = os.path.join(TEMPLATES_DIR, "{}.html") TEMPLATE_FORMAT = os.path.join(INTERNAL_TEMPLATES_DIR, "{}-template.html") -def render_page(input_file, output_file, context): +def render_page(input_file: str, output_file: str, context: dict) -> str: logger.debug("Rendering {}...".format(os.path.splitext(os.path.basename(output_file))[0].title())) with open(input_file) as f: content = render_content(f.read(), context) @@ -20,7 +20,7 @@ def render_page(input_file, output_file, context): return content -def render_templates(config, content): +def render_templates(config: dict, content: str): context = get_context(config, content) for template in [ 'cover', diff --git a/md_pdf/config/read.py b/md_pdf/config/read.py index 27eb42e..6950fe1 100644 --- a/md_pdf/config/read.py +++ b/md_pdf/config/read.py @@ -3,7 +3,7 @@ from md_pdf.consts import CONFIG_FILE from md_pdf.exceptions import ConfigValidationException -def load_config(location=CONFIG_FILE): +def load_config(location: str=CONFIG_FILE) -> str: try: with open(location) as f: return yaml.safe_load(f) diff --git a/md_pdf/utils.py b/md_pdf/utils.py index 4c39d48..33dc229 100644 --- a/md_pdf/utils.py +++ b/md_pdf/utils.py @@ -2,11 +2,13 @@ import shutil import os import logging from bs4 import BeautifulSoup +from typing import List + logger = logging.getLogger(__file__) -def remove_dir(dir): +def remove_dir(dir: str): logger.debug("Removing directory {}.".format(dir)) try: shutil.rmtree(dir) @@ -15,14 +17,14 @@ def remove_dir(dir): pass -def safe_list_get(l, idx, default): +def safe_list_get(l: List, idx: int, default): try: return l[idx] except IndexError: return default -def get_plain_text(content): +def get_plain_text(content: str) -> str: soup = BeautifulSoup(content, 'html.parser') body = soup.find('body') if body is None: