spiff-arena/spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py
dependabot[bot] 4a373be939
Bump black from 23.1.0 to 24.3.0 (#1285)
* Bump black from 23.1.0 to 24.3.0

Bumps [black](https://github.com/psf/black) from 23.1.0 to 24.3.0.
- [Release notes](https://github.com/psf/black/releases)
- [Changelog](https://github.com/psf/black/blob/main/CHANGES.md)
- [Commits](https://github.com/psf/black/compare/23.1.0...24.3.0)

---
updated-dependencies:
- dependency-name: black
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fixed pre commit black issues w/ burnettk

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: jasquat <jasquat@users.noreply.github.com>
2024-03-27 14:05:49 -04:00

39 lines
1.4 KiB
Python

"""This is used by bin/codemod/remove_all_unused_functions to remove a function from a file."""
from bowler import Query
# This came about because vulture (actually dead, from the list of Similar programs at https://pypi.org/project/vulture/)
# actually found unused stuff, and I wanted to remove it.
# See also https://github.com/craigds/decrapify
def remove_function(filename: str, function_name: str) -> None:
"""Does the dirty work of actually removing the function from the file in place, or failing if it cannot."""
def remove_statement(node, capture, filename):
node.remove()
bowler_query = (
Query(filename)
.select_function(function_name)
.modify(remove_statement)
.execute(write=True, silent=True, interactive=False)
)
if len(bowler_query.exceptions) > 0:
print(f"Failed to remove function {function_name} from {filename}.")
raise Exception(bowler_query.exceptions[0])
print(f"Function {function_name} successfully removed from {filename}.")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Remove a function from a Python file while preserving comments.")
parser.add_argument("filename", help="the file to modify")
parser.add_argument("function_name", help="the name of the function to remove")
args = parser.parse_args()
remove_function(args.filename, args.function_name)