1
0
mirror of https://github.com/venthur/blag.git synced 2025-11-25 20:52:43 +00:00

fixed for mypy --strict testing

This commit is contained in:
Bastian Venthur
2022-08-31 22:59:55 +02:00
parent 2adc7b3bd4
commit ebac0a8fc4
12 changed files with 102 additions and 88 deletions

View File

@@ -1 +1 @@
from blag.version import __VERSION__ # noqa
from blag.version import __VERSION__ as __VERSION__ # noqa

View File

@@ -6,6 +6,7 @@
# remove when we don't support py38 anymore
from __future__ import annotations
from typing import Any
import argparse
import os
import shutil
@@ -34,7 +35,7 @@ logging.basicConfig(
)
def main(arguments: list[str] = None) -> None:
def main(arguments: list[str] | None = None) -> None:
"""Main entrypoint for the CLI.
This method parses the CLI arguments and executes the respective
@@ -54,7 +55,7 @@ def main(arguments: list[str] = None) -> None:
args.func(args)
def parse_args(args: list[str] = None) -> argparse.Namespace:
def parse_args(args: list[str] | None = None) -> argparse.Namespace:
"""Parse command line arguments.
Parameters
@@ -175,8 +176,8 @@ def get_config(configfile: str) -> configparser.SectionProxy:
def environment_factory(
template_dir: str = None,
globals_: dict = None,
template_dir: str | None = None,
globals_: dict[str, object] | None = None,
) -> Environment:
"""Environment factory.
@@ -188,7 +189,7 @@ def environment_factory(
Parameters
----------
template_dir : str
globals_ : dict
globals_ : dict[str, object]
Returns
-------
@@ -278,7 +279,7 @@ def process_markdown(
output_dir: str,
page_template: Template,
article_template: Template,
) -> tuple[list[tuple[str, dict]], list[tuple[str, dict]]]:
) -> tuple[list[tuple[str, dict[str, Any]]], list[tuple[str, dict[str, Any]]]]:
"""Process markdown files.
This method processes the convertibles, converts them to html and
@@ -298,7 +299,7 @@ def process_markdown(
Returns
-------
articles, pages : list[tuple[str, dict]]
articles, pages : list[tuple[str, dict[str, Any]]]
"""
logger.info("Converting Markdown files...")
@@ -334,7 +335,7 @@ def process_markdown(
def generate_feed(
articles: list[tuple[str, dict]],
articles: list[tuple[str, dict[str, Any]]],
output_dir: str,
base_url: str,
blog_title: str,
@@ -345,7 +346,7 @@ def generate_feed(
Parameters
----------
articles : list[tuple[str, dict]]
articles : list[tuple[str, dict[str, Any]]]
list of relative output path and article dictionary
output_dir : str
where the feed is stored
@@ -386,7 +387,7 @@ def generate_feed(
def generate_archive(
articles: list[tuple[str, dict]],
articles: list[tuple[str, dict[str, Any]]],
template: Template,
output_dir: str,
) -> None:
@@ -394,7 +395,7 @@ def generate_archive(
Parameters
----------
articles : list[tuple[str, dict]]
articles : list[tuple[str, dict[str, Any]]]
List of articles. Each article has the destination path and a
dictionary with the content.
template : jinja2.Template instance
@@ -413,7 +414,7 @@ def generate_archive(
def generate_tags(
articles: list[tuple[str, dict]],
articles: list[tuple[str, dict[str, Any]]],
tags_template: Template,
tag_template: Template,
output_dir: str,
@@ -422,7 +423,7 @@ def generate_tags(
Parameters
----------
articles : list[tuple[str, dict]]
articles : list[tuple[str, dict[str, Any]]]
List of articles. Each article has the destination path and a
dictionary with the content.
tags_template, tag_template : jinja2.Template instance
@@ -431,11 +432,10 @@ def generate_tags(
"""
logger.info("Generating Tag-pages.")
os.makedirs(f'{output_dir}/tags', exist_ok=True)
# get tags number of occurrences
all_tags: dict = {}
all_tags: dict[str, int] = {}
for _, context in articles:
tags = context.get('tags', [])
tags: list[str] = context.get('tags', [])
for tag in tags:
all_tags[tag] = all_tags.get(tag, 0) + 1
# sort by occurrence
@@ -448,17 +448,17 @@ def generate_tags(
fh.write(result)
# get tags and archive per tag
all_tags = {}
all_tags2: dict[str, list[dict[str, Any]]] = {}
for dst, context in articles:
tags = context.get('tags', [])
for tag in tags:
archive = all_tags.get(tag, [])
archive: list[dict[str, Any]] = all_tags2.get(tag, [])
entry = context.copy()
entry['dst'] = dst
archive.append(entry)
all_tags[tag] = archive
all_tags2[tag] = archive
for tag, archive in all_tags.items():
for tag, archive in all_tags2.items():
result = tag_template.render(dict(archive=archive, tag=tag))
with open(f'{output_dir}/tags/{tag}.html', 'w') as fh:
fh.write(result)

View File

@@ -41,7 +41,10 @@ def markdown_factory() -> Markdown:
return md
def convert_markdown(md: Markdown, markdown: str) -> tuple[str, dict]:
def convert_markdown(
md: Markdown,
markdown: str,
) -> tuple[str, dict[str, str]]:
"""Convert markdown into html and extract meta data.
Some meta data is treated special:
@@ -56,13 +59,13 @@ def convert_markdown(md: Markdown, markdown: str) -> tuple[str, dict]:
Returns
-------
str, dict :
str, dict[str, str] :
html and metadata
"""
md.reset()
content = md.convert(markdown)
meta = md.Meta
meta = md.Meta # type: ignore
# markdowns metadata consists as list of strings -- one item per
# line. let's convert into single strings.
@@ -90,7 +93,7 @@ class MarkdownLinkTreeprocessor(Treeprocessor):
"""
def run(self, root: Element):
def run(self, root: Element) -> Element:
for element in root.iter():
if element.tag == 'a':
url = element.get('href')
@@ -102,7 +105,7 @@ class MarkdownLinkTreeprocessor(Treeprocessor):
element.set('href', converted)
return root
def convert(self, url: str):
def convert(self, url: str) -> str:
scheme, netloc, path, query, fragment = urlsplit(url)
logger.debug(
f'{url}: {scheme=} {netloc=} {path=} {query=} {fragment=}'
@@ -120,7 +123,7 @@ class MarkdownLinkExtension(Extension):
"""markdown.extension that converts relative .md- to .html-links.
"""
def extendMarkdown(self, md: Markdown):
def extendMarkdown(self, md: Markdown) -> None:
md.treeprocessors.register(
MarkdownLinkTreeprocessor(md), 'mdlink', 0,
)

View File

@@ -32,7 +32,7 @@ def get_input(question: str, default: str) -> str:
return reply
def quickstart(args: argparse.Namespace) -> None:
def quickstart(args: argparse.Namespace | None) -> None:
"""Quickstart.
This method asks the user some questions and generates a