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__)
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
def build(config):
|
def build(config: dict):
|
||||||
logger.debug("Starting Build...")
|
logger.debug("Starting Build...")
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
data = read_files(os.path.abspath(config['input']))
|
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()
|
config = config.copy()
|
||||||
if 'context' in config:
|
if 'context' in config:
|
||||||
context = config['context'].copy()
|
context = config['context'].copy()
|
||||||
del config['context']
|
del config['context']
|
||||||
else:
|
else:
|
||||||
context = {}
|
context = {}
|
||||||
context = dict(
|
merged_context = dict(
|
||||||
config,
|
config,
|
||||||
**EXTRA_CONTEXT,
|
**EXTRA_CONTEXT,
|
||||||
**context,
|
**context,
|
||||||
|
@ -35,11 +35,11 @@ def get_context(config, content):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
if config.get('show_word_count'):
|
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 config.get('submission_date'):
|
||||||
if type(config['submission_date']) in [datetime.date, datetime.datetime, datetime.time]:
|
if type(config['submission_date']) in [datetime.date, datetime.datetime, datetime.time]:
|
||||||
submission_date = config['submission_date']
|
submission_date = config['submission_date']
|
||||||
else:
|
else:
|
||||||
submission_date = parser.parse(config['submission_date'])
|
submission_date = parser.parse(config['submission_date'])
|
||||||
context['submission_date'] = submission_date.strftime(DATE_FORMAT)
|
merged_context['submission_date'] = submission_date.strftime(DATE_FORMAT)
|
||||||
return context
|
return merged_context
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from jinja2 import Environment
|
from jinja2 import Environment
|
||||||
|
|
||||||
|
|
||||||
def render_content(content, context):
|
def render_content(content: str, context: dict) -> str:
|
||||||
env = Environment(
|
env = Environment(
|
||||||
autoescape=True,
|
autoescape=True,
|
||||||
trim_blocks=True,
|
trim_blocks=True,
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
import glob
|
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:
|
for filename in filenames:
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
yield f.read()
|
yield f.read()
|
||||||
|
|
||||||
|
|
||||||
def read_files(files_glob):
|
def read_files(files_glob: str) -> str:
|
||||||
filenames = sorted(glob.glob(files_glob))
|
filenames = sorted(glob.glob(files_glob))
|
||||||
return '\n'.join(list(get_files_content(filenames)))
|
return '\n'.join(list(get_files_content(filenames)))
|
||||||
|
|
|
@ -6,13 +6,13 @@ import logging
|
||||||
logger = logging.getLogger(__file__)
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
def output_html(html, out_dir):
|
def output_html(html: str, out_dir: str):
|
||||||
logger.info("Outputting HTML...")
|
logger.info("Outputting HTML...")
|
||||||
with open(os.path.join(out_dir, 'output.html'), 'w') as f:
|
with open(os.path.join(out_dir, 'output.html'), 'w') as f:
|
||||||
f.write(html)
|
f.write(html)
|
||||||
|
|
||||||
|
|
||||||
def build_document(files_content, bibliography):
|
def build_document(files_content: str, bibliography: dict) -> str:
|
||||||
args = [
|
args = [
|
||||||
'-s',
|
'-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:
|
if logger.getEffectiveLevel() > logging.DEBUG:
|
||||||
PDF_OPTIONS['quiet'] = ""
|
PDF_OPTIONS['quiet'] = ""
|
||||||
PDF_OPTIONS['title'] = config.get('title', 'Output')
|
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")
|
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()))
|
logger.debug("Rendering {}...".format(os.path.splitext(os.path.basename(output_file))[0].title()))
|
||||||
with open(input_file) as f:
|
with open(input_file) as f:
|
||||||
content = render_content(f.read(), context)
|
content = render_content(f.read(), context)
|
||||||
|
@ -20,7 +20,7 @@ def render_page(input_file, output_file, context):
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
def render_templates(config, content):
|
def render_templates(config: dict, content: str):
|
||||||
context = get_context(config, content)
|
context = get_context(config, content)
|
||||||
for template in [
|
for template in [
|
||||||
'cover',
|
'cover',
|
||||||
|
|
|
@ -3,7 +3,7 @@ from md_pdf.consts import CONFIG_FILE
|
||||||
from md_pdf.exceptions import ConfigValidationException
|
from md_pdf.exceptions import ConfigValidationException
|
||||||
|
|
||||||
|
|
||||||
def load_config(location=CONFIG_FILE):
|
def load_config(location: str=CONFIG_FILE) -> str:
|
||||||
try:
|
try:
|
||||||
with open(location) as f:
|
with open(location) as f:
|
||||||
return yaml.safe_load(f)
|
return yaml.safe_load(f)
|
||||||
|
|
|
@ -2,11 +2,13 @@ import shutil
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__file__)
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
def remove_dir(dir):
|
def remove_dir(dir: str):
|
||||||
logger.debug("Removing directory {}.".format(dir))
|
logger.debug("Removing directory {}.".format(dir))
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(dir)
|
shutil.rmtree(dir)
|
||||||
|
@ -15,14 +17,14 @@ def remove_dir(dir):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def safe_list_get(l, idx, default):
|
def safe_list_get(l: List, idx: int, default):
|
||||||
try:
|
try:
|
||||||
return l[idx]
|
return l[idx]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def get_plain_text(content):
|
def get_plain_text(content: str) -> str:
|
||||||
soup = BeautifulSoup(content, 'html.parser')
|
soup = BeautifulSoup(content, 'html.parser')
|
||||||
body = soup.find('body')
|
body = soup.find('body')
|
||||||
if body is None:
|
if body is None:
|
||||||
|
|
Reference in a new issue