Add type annotations to most places
This commit is contained in:
parent
f0d7900fc2
commit
bbfc3cac4c
9 changed files with 21 additions and 18 deletions
|
@ -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']))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)))
|
||||
|
|
|
@ -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',
|
||||
]
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
Reference in a new issue