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:
pull_request:
@ -17,4 +17,8 @@ jobs:
name: colmena
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 ];
packages = with pkgs; [
bashInteractive
python3 editorconfig-checker
editorconfig-checker
clippy rust-analyzer cargo-outdated
python3 python3Packages.flake8
];
};
}) // {

View file

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