~eightytwo/idspispopd

b946cf7b81791fcad56871a24f95727b63461380 — eightytwo 4 years ago ff02254
Add pre-commit and fix linter and mypy issues
3 files changed, 96 insertions(+), 22 deletions(-)

A .flake8
A .pre-commit-config.yaml
M build.py
A .flake8 => .flake8 +5 -0
@@ 0,0 1,5 @@
[flake8]
ignore = A003,B011,C408,E122,E203,E303,E302,E402,E501,W503,W293,W391
exclude =
    .git/*
max-complexity = 20

A .pre-commit-config.yaml => .pre-commit-config.yaml +61 -0
@@ 0,0 1,61 @@
default_language_version:
  python: python3.8
exclude: reports
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.2.3
    hooks:
      - id: check-byte-order-marker
      - id: check-case-conflict
      - id: check-merge-conflict
      - id: check-yaml
      - id: debug-statements
      - id: check-builtin-literals
      - id: fix-encoding-pragma
        args: [--remove]
      - id: mixed-line-ending
        args: [--fix, lf]
      - id: end-of-file-fixer
      - id: trailing-whitespace
      - id: requirements-txt-fixer
      - id: detect-aws-credentials
        args: [--allow-missing-credentials]
      - id: detect-private-key
  - repo: https://github.com/pre-commit/pygrep-hooks
    rev: v1.4.1
    hooks:
      - id: python-check-blanket-noqa
      - id: python-check-mock-methods
  - repo: https://github.com/asottile/reorder_python_imports
    rev: v1.6.1
    hooks:
    - id: reorder-python-imports
      args: [--py37-plus]
  - repo: https://gitlab.com/pycqa/flake8
    rev: 3.7.8
    hooks:
    - id: flake8
      args: [--config=.flake8]
      additional_dependencies:
        - flake8-builtins==1.4.1
        - flake8-comprehensions==2.1.0
        - flake8-bugbear==19.3.0
        - flake8-eradicate==0.2.1
  - repo: https://github.com/ambv/black
    rev: 19.10b0
    hooks:
      - id: black
        args: [-S]
  - repo: https://github.com/asottile/pyupgrade
    rev: v1.21.0
    hooks:
      - id: pyupgrade
        args: [--py36-plus]
  - repo: https://github.com/PyCQA/bandit
    rev: 1.6.2
    hooks:
      - id: bandit
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.761
    hooks:
      - id: mypy

M build.py => build.py +30 -22
@@ 8,12 8,14 @@ from pathlib import Path
from shutil import copytree
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple

import markdown
from markdown.extensions.toc import TocExtension
from jinja2 import Environment
from jinja2 import FileSystemLoader
from markdown.extensions.codehilite import CodeHiliteExtension
from jinja2 import Environment, FileSystemLoader
from markdown.extensions.toc import TocExtension
from slugify import slugify




@@ 23,8 25,8 @@ class Page:
    template: str
    listing: bool = False
    create_detail_pages: bool = False
    detail_page_template: str = None
    source_dir: str = None
    detail_page_template: Optional[str] = None
    source_dir: Optional[str] = None


BUILD_PATH = 'build'


@@ 34,16 36,20 @@ TEMPLATES_PATH = 'templates'
PAGES = [
    Page(category='404', template='404'),
    Page(category='about', template='about'),
    Page(category='blog',
         template='blog',
         listing=True,
         create_detail_pages=True,
         detail_page_template='post',
         source_dir='content/blog'),
    Page(category='projects',
         template='projects',
         listing=True,
         source_dir='content/projects'),
    Page(
        category='blog',
        template='blog',
        listing=True,
        create_detail_pages=True,
        detail_page_template='post',
        source_dir='content/blog',
    ),
    Page(
        category='projects',
        template='projects',
        listing=True,
        source_dir='content/projects',
    ),
]




@@ 71,7 77,7 @@ def parse_markdown(filepath: str) -> Dict:
            TocExtension(title='Contents', permalink=True),
            CodeHiliteExtension(guess_lang=False),
        ],
        output_format='html5'
        output_format='html5',
    )

    return {


@@ 115,7 121,7 @@ def build_tag_page(page_category: str, tag: str, pages: List[Dict]):
    template = env.get_template(f'{page_category}.html')
    write_file(
        f'{page_category}/tag/{tag}',
        template.render(category=page_category, tag=tag, items=items)
        template.render(category=page_category, tag=tag, items=items),
    )




@@ 124,7 130,7 @@ def build_detail_pages(parent_page, detail_pages: List[Dict]):
        template = env.get_template(f'{parent_page.detail_page_template}.html')
        write_file(
            f"{parent_page.category}/{detail_page['slug']}",
            template.render(category=parent_page.category, page=detail_page)
            template.render(category=parent_page.category, page=detail_page),
        )




@@ 135,10 141,11 @@ def build_list_page(page: Page) -> Tuple[List[Dict], Dict]:
    :return:
    """
    items = []
    tags = defaultdict(list)
    tags: Dict[str, List] = defaultdict(list)
    source_dir = str(page.source_dir)

    for f in os.listdir(page.source_dir):
        filepath = join(page.source_dir, f)
    for f in os.listdir(source_dir):
        filepath = join(source_dir, f)

        if not isfile(filepath):
            continue


@@ 175,16 182,17 @@ def build_simple_page(page: Page):

if __name__ == "__main__":
    env = Environment(
        autoescape=True,
        loader=FileSystemLoader(searchpath=TEMPLATES_PATH),
        trim_blocks=True,
        lstrip_blocks=True
        lstrip_blocks=True,
    )

    # Ensure some build directories exist
    Path(f'{BUILD_PATH}/blog/tag/').mkdir(parents=True, exist_ok=True)

    # A place to keep track of all tags
    all_tags = defaultdict(list)
    all_tags: Dict[Tuple[str, str], List] = defaultdict(list)

    # Build each top level page
    for page in PAGES: