From 3dc2834bfae48e7c0825ed3965fa04187c7482de Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 12 Feb 2021 12:31:46 +0000 Subject: [PATCH] Docstring auto fixes are now done by salt-rewrite --- .pre-commit-config.yaml | 20 +++++++- tasks/docstrings.py | 103 ---------------------------------------- 2 files changed, 18 insertions(+), 105 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4fb45ee7a8d..8a82e2a0d65 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1113,11 +1113,27 @@ repos: - repo: https://github.com/s0undt3ch/salt-rewrite # Automatically rewrite code with known rules - rev: 1.0.0 + rev: 1.3.2 hooks: - id: salt-rewrite + alias: rewrite-docstrings + name: Salt docstrings auto-fixes + files: ^salt/.*\.py$ + args: [--silent, -F, fix_docstrings] + exclude: > + (?x)^( + salt/ext/.* + )$ + + - repo: https://github.com/s0undt3ch/salt-rewrite + # Automatically rewrite code with known rules + rev: 1.3.2 + hooks: + - id: salt-rewrite + alias: rewrite-tests + name: Rewrite Salt's Test Suite files: ^tests/.*\.py$ - args: [-E, fix_asserts] + args: [--silent, -E, fix_asserts, -E, fix_docstrings] - repo: https://github.com/timothycrosley/isort rev: "1e78a9acf3110e1f9721feb591f89a451fc9876a" diff --git a/tasks/docstrings.py b/tasks/docstrings.py index ccf77a998f3..825fe51f4a8 100644 --- a/tasks/docstrings.py +++ b/tasks/docstrings.py @@ -50,10 +50,6 @@ def check(ctx, files, check_proper_formatting=False): module = ast.parse(path.read_text(), filename=str(path)) module_docstring = ast.get_docstring(module, clean=False) if module_docstring: - new_module_docstring = _autofix_docstring(module_docstring) - if module_docstring != new_module_docstring: - contents = contents.replace(module_docstring, new_module_docstring) - module_docstring = new_module_docstring error = _check_valid_versions_on_docstrings(module_docstring) if error: errors += 1 @@ -69,10 +65,6 @@ def check(ctx, files, check_proper_formatting=False): ]: docstring = ast.get_docstring(funcdef, clean=False) if docstring: - new_docstring = _autofix_docstring(docstring) - if docstring != new_docstring: - contents = contents.replace(docstring, new_docstring) - docstring = new_docstring error = _check_valid_versions_on_docstrings(docstring) if error: errors += 1 @@ -187,98 +179,3 @@ CLI_EXAMPLE_PROPER_FORMATTING_RE = re.compile( def _check_cli_example_proper_formatting(docstring): return CLI_EXAMPLE_PROPER_FORMATTING_RE.search(docstring) is not None - - -def _autofix_docstring(docstring): - return _fix_codeblocks( - _convert_version_names_to_numbers( - _fix_directives_formatting( - _fix_simple_cli_example_spacing_issues(docstring) - ) - ) - ) - - -CONVERT_VERSION_NAMES_TO_NUMBERS_RE = re.compile( - ".. ((?P(versionadded|versionchanged|deprecated))(?:[:]+)(?:[ ]+)?(?P.*))" -) - - -def _convert_version_names_to_numbers(docstring): - for match in CONVERT_VERSION_NAMES_TO_NUMBERS_RE.finditer(docstring): - vtype = match.group("vtype") - version = match.group("version") - versions = [vs.strip() for vs in version.split(",")] - parsed_versions = [] - for vs in versions: - try: - vs = SaltStackVersion.from_name(vs).string - except ValueError: - if vs.startswith("v"): - try: - vs = SaltStackVersion.parse(vs[1:]).string - except ValueError: - pass - parsed_versions.append(vs) - replace_contents = ".. {}:: {}".format(vtype, ", ".join(parsed_versions)) - docstring = docstring.replace(match.group(0), replace_contents.rstrip()) - return docstring - - -CLI_EXAMPLE_CASE_AND_SPACING_RE = re.compile( - r"(?:[\n]+)([ ]+)CLI Example(?Ps)?(?:[\s]+)?:(?:[^\n]+)?(?:[\n]+)", - flags=re.I | re.MULTILINE, -) -CLI_EXAMPLE_MISSING_CODE_BLOCK_RE = re.compile( - r"\n([ ]+)CLI Example(?Ps)?:\n\n([\s]+)salt ", flags=re.I | re.MULTILINE -) - - -def _fix_simple_cli_example_spacing_issues(docstring): - return CLI_EXAMPLE_MISSING_CODE_BLOCK_RE.sub( - r"\n\1CLI Example\2:\n\n\1..code-block:: bash\n\n\3salt ", - CLI_EXAMPLE_CASE_AND_SPACING_RE.sub(r"\n\n\1CLI Example\2:\n\n", docstring), - ) - - -DIRECTIVES_FORMATTING_RE = re.compile( - r"(\n(?P[ ]+)?((?P[.]{2,})(?P[ ]+)?(?P(?:[^ :]+)))(?:[:]{2})(?P[ ]+)?(?P[^\n]+)?\n)" -) - - -def _fix_directives_formatting(docstring): - for match in DIRECTIVES_FORMATTING_RE.finditer(docstring): - replacement = ( - "\n{}.. {}:: {}".format( - match.group("spc1") or "", - match.group("directive"), - match.group("remaining") or "", - ).rstrip() - + "\n" - ) - docstring = docstring.replace(match.group(0), replacement) - return docstring - - -FIX_CODE_BLOCKS_RE = re.compile( - r"^(?P[ ]+)?(?P[.]{2}) (?Pcode-block)::(?P.*)\n$" -) - - -def _fix_codeblocks(docstring): - output = [] - found_codeblock = False - for line in docstring.splitlines(True): - match = FIX_CODE_BLOCKS_RE.match(line) - if found_codeblock: - if line.strip() and line.strip().startswith(":"): - output.append(line) - continue - if line.strip(): - # We need an empty line after the code-block - output.append("\n") - found_codeblock = False - if match: - found_codeblock = True - output.append(line) - return "".join(output)