Created
May 23, 2024 16:00
-
-
Save Teraskull/63719155899c89c65a23e02ce0c30b27 to your computer and use it in GitHub Desktop.
Remove `pylint: disable` comments from Python files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import re | |
from pathlib import Path | |
class TypedNamespace(argparse.Namespace): | |
"""Typed `argparse.Namespace()` class for argparse arguments.""" | |
path: str | |
"""Path to the file or directory.""" | |
def remove_pylint_disable_comments(file_path: Path) -> None: | |
"""Remove `pylint: disable` comments from a Python file.""" | |
with file_path.open("r", encoding="utf-8") as file: | |
lines = file.readlines() | |
with file_path.open("w", encoding="utf-8") as file: | |
for line in lines: | |
cleaned_line = re.sub(r"\s*# pylint: disable=.*$", "", line) | |
file.write(cleaned_line.rstrip() + "\n") | |
def process_directory(directory: Path) -> None: | |
"""Remove `pylint: disable` comments from all Python files in a directory and its subdirectories.""" | |
for file_path in directory.rglob("*.py"): | |
remove_pylint_disable_comments(file_path) | |
def main() -> None: | |
"""Parse the `path` argument and process Python files.""" | |
parser = argparse.ArgumentParser(description="Remove `pylint: disable` comments from Python files.") | |
parser.add_argument("path", type=str, help="Path to the file or directory.") | |
args: TypedNamespace = parser.parse_args() | |
file_path = Path(args.path) | |
if file_path.is_file(): | |
if file_path.suffix == ".py": | |
remove_pylint_disable_comments(file_path) | |
else: | |
print("The specified file is not a Python file.") | |
elif file_path.is_dir(): | |
process_directory(file_path) | |
else: | |
print("The specified path is neither a file nor a directory.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment