Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9147f02
commit f3efe47
Showing
11 changed files
with
199 additions
and
212 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import argparse | ||
import pathlib | ||
import sys | ||
|
||
from unimport.session import Session | ||
|
||
parser = argparse.ArgumentParser( | ||
description="Detect or remove unused Python imports." | ||
) | ||
parser.add_argument( | ||
"sources", | ||
default=".", | ||
nargs="*", | ||
help="files and folders to find the unused imports.", | ||
type=pathlib.Path, | ||
) | ||
parser.add_argument( | ||
"-c", | ||
"--config", | ||
help="read configuration from PATH.", | ||
metavar="PATH", | ||
type=pathlib.Path, | ||
) | ||
parser.add_argument( | ||
"-w", | ||
"--write", | ||
action="store_true", | ||
help="remove unused imports automatically.", | ||
) | ||
parser.add_argument( | ||
"-d", | ||
"--diff", | ||
action="store_true", | ||
help="Prints a diff of all the changes unimport would make to a file.", | ||
) | ||
|
||
|
||
def main(argv=None): | ||
namespace = parser.parse_args(argv) | ||
session = Session(config_file=namespace.config) | ||
sources = [] | ||
for source in namespace.sources: | ||
sources.extend(session._list_paths(source, "**/*.py")) | ||
|
||
if namespace.diff and namespace.write: | ||
for source in sources: | ||
print(*session.diff_file(source), sep="\n") | ||
session.refactor_file(source, apply=True) | ||
elif namespace.diff: | ||
for source in sources: | ||
print(*session.diff_file(source), sep="\n") | ||
action = input( | ||
f"Apply suggested changes to '{source}' [y/n/q] ? > " | ||
) | ||
if action == "q": | ||
break | ||
elif action == "y": | ||
session.refactor_file(source, apply=True) | ||
elif namespace.write: | ||
for source in sources: | ||
session.refactor_file(source, apply=True) | ||
else: | ||
for source in sources: | ||
print(*session.scan_file(source), sep="\n") | ||
print("All done!") | ||
|
||
if __name__ == "__main__": | ||
main(sys.argv[1:]) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Oops, something went wrong.