Apply flake8 to manual preprocessor

This commit is contained in:
Zhaofeng Li 2022-06-24 17:34:37 -07:00
parent a17e4eda17
commit d37270f292
3 changed files with 20 additions and 14 deletions

View file

@ -1,4 +1,4 @@
name: Check EditorConfig name: Linters
on: on:
pull_request: pull_request:
@ -17,4 +17,8 @@ jobs:
name: colmena name: colmena
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- run: nix-shell --run "editorconfig-checker" - name: Check EditorConfig
run: nix-shell --run "editorconfig-checker"
- name: Check flake8
run: nix-shell --run "flake8 manual/preprocess.py"

View file

@ -65,8 +65,9 @@
inputsFrom = [ defaultPackage packages.manualFast ]; inputsFrom = [ defaultPackage packages.manualFast ];
packages = with pkgs; [ packages = with pkgs; [
bashInteractive bashInteractive
python3 editorconfig-checker editorconfig-checker
clippy rust-analyzer cargo-outdated clippy rust-analyzer cargo-outdated
python3 python3Packages.flake8
]; ];
}; };
}) // { }) // {

View file

@ -11,23 +11,25 @@
import json import json
import os import os
import pprint
import re import re
import sys import sys
def eprint(*args, **kwargs): def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs) print(*args, file=sys.stderr, **kwargs)
def process(book, version, unstable): def process(book, version, unstable):
marker_to_remove = "STABLE" if unstable else "UNSTABLE" marker_to_remove = "STABLE" if unstable else "UNSTABLE"
api_version = re.match("^[0-9]+\.[0-9]+(?=$|\.[0-9]+(.*)?$)", version) api_version = re.match(r"^[0-9]+\.[0-9]+(?=$|\.[0-9]+(.*)?$)", version)
if api_version: if api_version:
api_version = api_version.group() api_version = api_version.group()
else: else:
api_version = version api_version = version
version_debug = f"{version} (apiVersion={api_version}, unstable={str(unstable)})" version_debug = f"{version} " \
f"(apiVersion={api_version}, unstable={str(unstable)})"
def replace_version_markers(s): def replace_version_markers(s):
s = s.replace("@version@", version) s = s.replace("@version@", version)
@ -40,29 +42,28 @@ def process(book, version, unstable):
for sub_item in chapter["sub_items"]: for sub_item in chapter["sub_items"]:
process_item(sub_item) process_item(sub_item)
regex = r".*\b{marker}_BEGIN\b(.|\n|\r)*?\b{marker}_END\b.*".format(marker=marker_to_remove) regex = r".*\b{marker}_BEGIN\b(.|\n|\r)*?\b{marker}_END\b.*" \
.format(marker=marker_to_remove)
chapter["content"] = f"<!-- Generated from version {version_debug} -->\n" + chapter["content"] chapter["content"] = \
chapter["content"] = re.sub(regex, "", chapter["content"]) f"<!-- Generated from version {version_debug} -->\n" \
chapter["content"] = replace_version_markers(chapter["content"]) + replace_version_markers(re.sub(regex, "", chapter["content"]))
#eprint(f"Processed {chapter['name']}")
eprint(f"Version is {version_debug}") eprint(f"Version is {version_debug}")
for section in book['sections']: for section in book['sections']:
process_item(section) process_item(section)
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) > 1: if len(sys.argv) > 1:
if sys.argv[1] == "supports": if sys.argv[1] == "supports":
#eprint("mdbook asked about support for", sys.argv[2])
sys.exit(0) sys.exit(0)
version = os.environ.get("COLMENA_VERSION", "unstable") version = os.environ.get("COLMENA_VERSION", "unstable")
unstable = bool(os.environ.get("COLMENA_UNSTABLE", "")) unstable = bool(os.environ.get("COLMENA_UNSTABLE", ""))
if version in [ "" "unstable" ]: if version in ["", "unstable"]:
unstable = True unstable = True
context, book = json.load(sys.stdin) context, book = json.load(sys.stdin)