From 39db14d294f6c58df7440a33ba22baa4eb963110 Mon Sep 17 00:00:00 2001 From: cytopia Date: Thu, 8 Dec 2022 12:41:44 +0100 Subject: [PATCH] Fix Python linter warnings --- bin/Makefile | 2 +- bin/gen-docs-php-tools.py | 146 ++++++++++++-------------------------- bin/gen-php-modules.py | 2 +- bin/gen-php-tools.py | 2 +- 4 files changed, 50 insertions(+), 102 deletions(-) diff --git a/bin/Makefile b/bin/Makefile index 46afe76..d27d036 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -23,7 +23,7 @@ MYPY_ARGS = --strict --disable-error-code no-any-return PYLINT_DIR = *.py PYLINT_PIP_PKGS = yamllint -PYLINT_ARGS = --disable=invalid-name,duplicate-code +PYLINT_ARGS = --disable=invalid-name,duplicate-code,consider-using-f-string PYCODE_ARGS = --max-line-length=100 diff --git a/bin/gen-docs-php-tools.py b/bin/gen-docs-php-tools.py index 3d2d91f..3c783ba 100755 --- a/bin/gen-docs-php-tools.py +++ b/bin/gen-docs-php-tools.py @@ -4,7 +4,6 @@ import os import re import sys -from collections import OrderedDict from typing import Dict, List, Any import yaml @@ -19,44 +18,30 @@ PHP_TOOL_PATH = str(os.path.join(REPOSITORY_PATH, "php_tools")) DOC_FILE = str(os.path.join(REPOSITORY_PATH, "doc", "available-tools.md")) -PHP_VERSIONS = ["5.2", "5.3", "5.4", "5.5", "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2"] +PHP_VERSIONS = [ + "5.2", + "5.3", + "5.4", + "5.5", + "5.6", + "7.0", + "7.1", + "7.2", + "7.3", + "7.4", + "8.0", + "8.1", + "8.2", +] DEFAULT_TOOLS = [ - { - "name": "**composer**", - "dir": "../.ansible/group_vars/all/work-help.yml", - "exclude": [] - }, - { - "name": "**corepack**", - "dir": "../.ansible/group_vars/all/work-help.yml", - "exclude": [] - }, - { - "name": "**nvm**", - "dir": "../.ansible/group_vars/all/work-help.yml", - "exclude": [] - }, - { - "name": "**npm**", - "dir": "../.ansible/group_vars/all/work-help.yml", - "exclude": [] - }, - { - "name": "**node**", - "dir": "../.ansible/group_vars/all/work-help.yml", - "exclude": [] - }, - { - "name": "**yarn**", - "dir": "../.ansible/group_vars/all/work-help.yml", - "exclude": [] - }, - { - "name": "**pip**", - "dir": "../.ansible/group_vars/all/work-help.yml", - "exclude": [] - } + {"name": "**composer**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []}, + {"name": "**corepack**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []}, + {"name": "**nvm**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []}, + {"name": "**npm**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []}, + {"name": "**node**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []}, + {"name": "**yarn**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []}, + {"name": "**pip**", "dir": "../.ansible/group_vars/all/work-help.yml", "exclude": []}, ] @@ -115,12 +100,11 @@ def get_tools() -> List[Dict[str, Any]]: data = get_tool_options(item.name) tools.append( { - "dir": "../php_tools/"+item.name, + "dir": "../php_tools/" + item.name, "name": data["name"], - "exclude": [str(x) for x in data["exclude"]] + "exclude": [str(x) for x in data["exclude"]], } ) - #return tools return sorted(DEFAULT_TOOLS + tools, key=lambda tool: tool["name"].replace("*", "")) @@ -131,18 +115,18 @@ def get_tools() -> List[Dict[str, Any]]: def print_terminal(tools: List[Dict[str, Any]]) -> None: """Print directory tools.""" - padding=15 + padding = 15 # First Row - print('| {name: <{padding}}| '.format(name="Tool", padding=padding), end="") + print("| {name: <{padding}}| ".format(name="Tool", padding=padding), end="") print(" | ".join(PHP_VERSIONS), end="") print(" |") # Second Row - print('|{name:-<{padding}}-|'.format(name="", padding=padding), end="") + print("|{name:-<{padding}}-|".format(name="", padding=padding), end="") for php in PHP_VERSIONS: print("-----|", end="") print() for tool in tools: - print('| {name: <{padding}}|'.format(name=tool["name"], padding=padding), end="") + print("| {name: <{padding}}|".format(name=tool["name"], padding=padding), end="") for php in PHP_VERSIONS: if str(php) in tool["exclude"]: print(" |", end="") @@ -151,21 +135,23 @@ def print_terminal(tools: List[Dict[str, Any]]) -> None: print() -def get_markdown(tools: List[Dict[str, Any]]) -> None: - """Print directory tools.""" +def get_markdown(tools: List[Dict[str, Any]]) -> str: + """Get markdown tools table.""" padding = 35 # First Row - markdown = '| {name: <{padding}}| '.format(name="Tool", padding=padding) + markdown = "| {name: <{padding}}| ".format(name="Tool", padding=padding) markdown += " | ".join(PHP_VERSIONS) markdown += " |\n" # Second Row - markdown += '|{name:-<{padding}}-|'.format(name="", padding=padding) + markdown += "|{name:-<{padding}}-|".format(name="", padding=padding) for php in PHP_VERSIONS: markdown += "-----|" markdown += "\n" for tool in tools: - markdown += '| {name: <{padding}}|'.format(name="["+tool["name"]+"][lnk_"+tool["name"]+"]", padding=padding) + markdown += "| {name: <{padding}}|".format( + name="[" + tool["name"] + "][lnk_" + tool["name"] + "]", padding=padding + ) for php in PHP_VERSIONS: if str(php) in tool["exclude"]: markdown += " |" @@ -175,62 +161,18 @@ def get_markdown(tools: List[Dict[str, Any]]) -> None: markdown += "\n" for tool in tools: - markdown += "[lnk_"+tool["name"]+"]: "+tool["dir"]+"\n" + markdown += "[lnk_" + tool["name"] + "]: " + tool["dir"] + "\n" return markdown - # -------------------------------------------------------------------------------------------------- # MAIN FUNCTION # -------------------------------------------------------------------------------------------------- -def print_help() -> None: - """Show help screen.""" - print("Usage:", os.path.basename(__file__), "[options] [PHP-TOOL]...") - print(" ", os.path.basename(__file__), "-h, --help") - print() - print("This script will generate the Ansible group_vars file: .ansible/group_vars/all/work.yml") - print("based on all the tools found in php_tools/ directory.") - print() - print("Positional arguments:") - print(" [PHP-TOOL] Specify None, one or more PHP tools to generate group_vars for.") - print(" When no PHP tool is specified (argument is omitted), group_vars") - print(" for all tools will be genrated.") - print(" When one or more PHP tool are specified, only group_vars for") - print(" these tools will be created.") - print(" only be generated for this single tool (and its dependencies).") - print(" This is useful if you want to test new tools and not build all") - print(" previous tools in the Dockerfile.") - print() - print(" Note: You still need to generate the Dockerfiles via Ansible for") - print(" the changes to take effect, before building the image.") - print("Optional arguments:") - print(" -i Ignore dependent tools.") - print(" By default each tool is checked for dependencies of other") - print(" tools.") - print(" By specifying -i, those dependent tools are not beeing added to") - print(" ansible group_vars. Use at your own risk.") -def main(argv: List[str]) -> None: +def main() -> None: """Main entrypoint.""" - ignore_dependencies = False - selected_tools = [] - if len(argv): - for arg in argv: - if arg in ("-h", "--help"): - print_help() - sys.exit(0) - for arg in argv: - if arg.startswith("-") and arg != "-i": - print("Invalid argument:", arg) - print("Use -h or --help for help") - sys.exit(1) - if arg == "-i": - ignore_dependencies = True - else: - selected_tools.append(arg) - # Get tools in order of dependencies tools = get_tools() @@ -246,12 +188,18 @@ def main(argv: List[str]) -> None: markdown = get_markdown(tools) print() - with open(DOC_FILE, "r") as f: + with open(DOC_FILE, "r", encoding="utf8") as f: content = f.read() - content_new = re.sub(r'(\<\!\-\- TOOLS_WORK_START \-\-\>)(.*)(\<\!\-\- TOOLS_WORK_END \-\-\>)', r"\1\n\n"+markdown+r"\n\3", content, flags = re.DOTALL) + content_new = re.sub( + r"(\<\!\-\- TOOLS_WORK_START \-\-\>)(.*)(\<\!\-\- TOOLS_WORK_END \-\-\>)", + r"\1\n\n" + markdown + r"\n\3", + content, + flags=re.DOTALL, + ) - with open(DOC_FILE, "w") as f: + with open(DOC_FILE, "w", encoding="utf8") as f: f.write(content_new) + if __name__ == "__main__": - main(sys.argv[1:]) + main() diff --git a/bin/gen-php-modules.py b/bin/gen-php-modules.py index 4428c6d..97a5ed1 100755 --- a/bin/gen-php-modules.py +++ b/bin/gen-php-modules.py @@ -91,7 +91,7 @@ def get_modules(selected_modules: List[str], ignore_dependencies: bool) -> List[ "dir": item.name, "name": data["name"], "deps": data["depends_build"], - "exclude": data["exclude"] + "exclude": data["exclude"], } ) # Convert list of deps into dict(dir, name, deps) diff --git a/bin/gen-php-tools.py b/bin/gen-php-tools.py index 5b6bf2f..537607b 100755 --- a/bin/gen-php-tools.py +++ b/bin/gen-php-tools.py @@ -86,7 +86,7 @@ def get_tools(selected_tools: List[str], ignore_dependencies: bool) -> List[Dict "dir": item.name, "name": data["name"], "deps": data["depends"], - "exclude": data["exclude"] + "exclude": data["exclude"], } ) # Convert list of deps into dict(dir, name, deps)