-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i199 Refactor options & commands & Option color output (#205)
- Loading branch information
1 parent
ecfcb4e
commit 04f6a9f
Showing
19 changed files
with
543 additions
and
222 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,4 +60,4 @@ repos: | |
- id: pyall | ||
args: | ||
- --refactor | ||
- --exclude=tests|setup.py | ||
- --exclude=tests|setup.py|__init__.py| |
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,22 @@ | ||
import textwrap | ||
import unittest | ||
|
||
from unimport.analyzer import Analyzer | ||
from unimport.statement import Import | ||
|
||
|
||
class AnalyzerTestCase(unittest.TestCase): | ||
def setUp(self) -> None: | ||
Analyzer.clear() | ||
|
||
def test_context_manager(self): | ||
with Analyzer( | ||
source=textwrap.dedent( | ||
"""\ | ||
import x | ||
""" | ||
) | ||
): | ||
self.assertEqual(1, len(Import.imports)) | ||
|
||
self.assertEqual(0, len(Import.imports)) |
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
import sys | ||
|
||
from unimport.main import main | ||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) | ||
raise SystemExit(main()) |
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,15 @@ | ||
from unimport.commands.check import check, requirements_check | ||
from unimport.commands.diff import diff, requirements_diff | ||
from unimport.commands.permission import permission, requirements_permission | ||
from unimport.commands.remove import remove, requirements_remove | ||
|
||
__all__ = [ | ||
"check", | ||
"diff", | ||
"permission", | ||
"remove", | ||
"requirements_check", | ||
"requirements_diff", | ||
"requirements_permission", | ||
"requirements_remove", | ||
] |
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,43 @@ | ||
from pathlib import Path | ||
from typing import List, Union | ||
|
||
from unimport.color import CYAN, GREEN, RED, YELLOW, paint | ||
from unimport.statement import Import, ImportFrom | ||
|
||
__all__ = ["check", "requirements_check"] | ||
|
||
|
||
def check( | ||
path: Path, | ||
unused_imports: List[Union[Import, ImportFrom]], | ||
use_color_setting: bool, | ||
) -> None: | ||
for imp in unused_imports: | ||
if isinstance(imp, ImportFrom) and imp.star and imp.suggestions: | ||
context = ( | ||
paint(f"from {imp.name} import *", RED, use_color_setting) | ||
+ " -> " | ||
+ paint( | ||
f"from {imp.name} import {', '.join(imp.suggestions)}", | ||
GREEN, | ||
use_color_setting, | ||
) | ||
) | ||
else: | ||
context = paint(imp.name, YELLOW, use_color_setting) | ||
print( | ||
context | ||
+ " at " | ||
+ paint(path.as_posix(), GREEN, use_color_setting) | ||
+ ":" | ||
+ paint(str(imp.lineno), GREEN, use_color_setting) | ||
) | ||
|
||
|
||
def requirements_check( | ||
path: Path, index: int, requirement: str, use_color_setting: bool | ||
) -> None: | ||
print( | ||
f"{paint(requirement, CYAN), use_color_setting} at " | ||
f"{paint(path.as_posix(), CYAN, use_color_setting)}:{paint(str(index + 1), CYAN, use_color_setting)}" | ||
) |
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,34 @@ | ||
from pathlib import Path | ||
|
||
from unimport import utils | ||
from unimport.color import difference as color_difference | ||
|
||
__all__ = ["diff", "requirements_diff"] | ||
|
||
|
||
def diff( | ||
path: Path, | ||
source: str, | ||
refactor_result: str, | ||
) -> bool: | ||
diff = utils.diff( | ||
source=source, refactor_result=refactor_result, fromfile=path | ||
) | ||
exists_diff = bool(diff) | ||
if exists_diff: | ||
print(color_difference(diff)) | ||
|
||
return exists_diff | ||
|
||
|
||
def requirements_diff(path: Path, source: str, refactor_result: str) -> bool: | ||
diff = utils.diff( | ||
source=source, | ||
refactor_result=refactor_result, | ||
fromfile=path, | ||
) | ||
exists_diff = bool(diff) | ||
if exists_diff: | ||
print(color_difference(diff)) | ||
|
||
return exists_diff |
Oops, something went wrong.