From d37270f292172b0daabf3f7805ea3d45028d5314 Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Fri, 24 Jun 2022 17:34:37 -0700 Subject: [PATCH] Apply flake8 to manual preprocessor --- .../{editorconfig.yml => linters.yml} | 8 +++++-- flake.nix | 3 ++- manual/preprocess.py | 23 ++++++++++--------- 3 files changed, 20 insertions(+), 14 deletions(-) rename .github/workflows/{editorconfig.yml => linters.yml} (64%) diff --git a/.github/workflows/editorconfig.yml b/.github/workflows/linters.yml similarity index 64% rename from .github/workflows/editorconfig.yml rename to .github/workflows/linters.yml index 28607ab..2189d95 100644 --- a/.github/workflows/editorconfig.yml +++ b/.github/workflows/linters.yml @@ -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" diff --git a/flake.nix b/flake.nix index 993a77c..1bf1ee4 100644 --- a/flake.nix +++ b/flake.nix @@ -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 ]; }; }) // { diff --git a/manual/preprocess.py b/manual/preprocess.py index 347b4c4..ebb30dc 100644 --- a/manual/preprocess.py +++ b/manual/preprocess.py @@ -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"\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"\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)