-
Notifications
You must be signed in to change notification settings - Fork 25
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
Showing
12 changed files
with
204 additions
and
20 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,6 @@ | ||
import sys | ||
|
||
from ._cli import main | ||
|
||
if __name__ == '__main__': | ||
exit(main(sys.argv[1:])) |
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,94 @@ | ||
import ast | ||
import json | ||
from argparse import ArgumentParser | ||
from pathlib import Path | ||
from textwrap import dedent, indent | ||
from typing import Iterable, Iterator, Union | ||
|
||
from ._checker import Checker | ||
|
||
|
||
COLORS = dict( | ||
red='\033[91m', | ||
green='\033[92m', | ||
yellow='\033[93m', | ||
blue='\033[94m', | ||
magenta='\033[95m', | ||
end='\033[0m', | ||
) | ||
TEMPLATE = ' {blue}{row}{end}:{blue}{col}{end} {yellow}{text}{end}' | ||
POINTER = '{magenta}^{end}' | ||
|
||
|
||
def get_paths(path: Path) -> Iterator[Path]: | ||
"""Recursively yields python files. | ||
""" | ||
if not path.exists(): | ||
raise FileNotFoundError(str(path)) | ||
if path.is_file(): | ||
if path.suffix == '.py': | ||
yield path | ||
return | ||
for subpath in path.iterdir(): | ||
if subpath.name[0] == '.': | ||
continue | ||
if subpath.name == '__pycache__': | ||
continue | ||
yield from get_paths(subpath) | ||
|
||
|
||
def get_errors(paths: Iterable[Union[str, Path]]) -> Iterator[dict]: | ||
for arg in paths: | ||
for path in get_paths(Path(arg)): | ||
content = path.read_text() | ||
checker = Checker( | ||
filename=str(path), | ||
tree=ast.parse(content), | ||
) | ||
lines = content.split('\n') | ||
for error in checker.get_errors(): | ||
yield dict( | ||
path=str(path), | ||
row=error.row, | ||
col=error.col, | ||
code=error.code, | ||
text=error.text, | ||
value=error.value, | ||
content=lines[error.row - 1], | ||
) | ||
|
||
|
||
def get_parser() -> ArgumentParser: | ||
parser = ArgumentParser() | ||
parser.add_argument('--json', action='store_true', help='json output') | ||
parser.add_argument('paths', nargs='*', default='.') | ||
return parser | ||
|
||
|
||
def main(argv: Iterable) -> int: | ||
parser = get_parser() | ||
args = parser.parse_args(argv) | ||
prev = None | ||
errors = list(get_errors(paths=args.paths)) | ||
for error in errors: | ||
if args.json: | ||
print(json.dumps(error)) | ||
continue | ||
|
||
# print file path | ||
if error['path'] != prev: | ||
print('{green}{path}{end}'.format(**COLORS, **error)) | ||
prev = error['path'] | ||
|
||
# print message | ||
line = TEMPLATE.format(**COLORS, **error) | ||
if error['value']: | ||
line += ' {magenta}({value}){end}'.format(**COLORS, **error) | ||
print(line) | ||
|
||
# print code line | ||
pointer = ' ' * error['col'] + POINTER.format(**COLORS) | ||
content = error['content'] + '\n' + pointer | ||
content = indent(dedent(content), prefix=' ') | ||
print(content) | ||
return len(errors) |
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
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,61 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from deal.linter._cli import get_errors, get_paths, main | ||
|
||
|
||
TEXT = """ | ||
import deal | ||
@deal.post(lambda x: x > 0) | ||
def f(x): | ||
return -1 | ||
""" | ||
|
||
|
||
def test_get_paths(tmp_path: Path): | ||
(tmp_path / 'subdir').mkdir() | ||
(tmp_path / 'subdir' / '__pycache__').mkdir() | ||
(tmp_path / '.hidden').mkdir() | ||
|
||
(tmp_path / 'setup.py').touch() | ||
(tmp_path / 'subdir' / 'ex.py').touch() | ||
(tmp_path / '.hidden' / 'ex.py').touch() | ||
(tmp_path / 'subdir' / '__pycache__' / 'ex.py').touch() | ||
(tmp_path / 'setup.pl').touch() | ||
actual = {p.relative_to(tmp_path) for p in get_paths(tmp_path)} | ||
assert actual == {Path('setup.py'), Path('subdir/ex.py')} | ||
|
||
with pytest.raises(FileNotFoundError): | ||
list(get_paths(tmp_path / 'not_exists')) | ||
|
||
|
||
def test_get_errors(tmp_path: Path): | ||
(tmp_path / 'example.py').write_text(TEXT) | ||
errors = list(get_errors(paths=[tmp_path])) | ||
assert len(errors) == 1 | ||
assert errors[0]['code'] == 11 | ||
assert errors[0]['content'] == ' return -1' | ||
|
||
|
||
def test_main(tmp_path: Path, capsys): | ||
(tmp_path / 'example.py').write_text(TEXT) | ||
count = main([str(tmp_path)]) | ||
assert count == 1 | ||
|
||
captured = capsys.readouterr() | ||
assert 'return -1' in captured.out | ||
assert '(-1)' in captured.out | ||
assert '^' in captured.out | ||
|
||
|
||
def test_main_json(tmp_path: Path, capsys): | ||
(tmp_path / 'example.py').write_text(TEXT) | ||
count = main(['--json', str(tmp_path)]) | ||
assert count == 1 | ||
|
||
captured = capsys.readouterr() | ||
assert '" return -1"' in captured.out | ||
assert '"-1"' in captured.out | ||
assert '^' not in captured.out |
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,7 @@ | ||
import subprocess | ||
import sys | ||
|
||
|
||
def test_cli_calling(): | ||
result = subprocess.run([sys.executable, '-m', 'deal.linter', __file__]) | ||
assert result.returncode == 0 |
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