| 1 |
import ast |
| 2 |
import re |
| 3 |
from itertools import chain |
| 4 |
|
| 5 |
import pycodestyle |
| 6 |
|
| 7 |
from flake8_import_order import ImportVisitor |
| 8 |
from flake8_import_order import NewLine |
| 9 |
from flake8_import_order.styles import lookup_entry_point |
| 10 |
|
| 11 |
DEFAULT_IMPORT_ORDER_STYLE = "cryptography" |
| 12 |
NOQA_INLINE_REGEXP = re.compile( |
| 13 |
# We're looking for items that look like this: |
| 14 |
# ``# noqa`` |
| 15 |
# ``# noqa: E123`` |
| 16 |
# ``# noqa: E123,W451,F921`` |
| 17 |
# ``# NoQA: E123,W451,F921`` |
| 18 |
# ``# NOQA: E123,W451,F921`` |
| 19 |
# We do not care about the ``: `` that follows ``noqa`` |
| 20 |
# We do not care about the casing of ``noqa`` |
| 21 |
# We want a comma-separated list of errors |
| 22 |
r"# noqa(?:: (?P<codes>([A-Z][0-9]+(?:[,\s]+)?)+))?", |
| 23 |
re.IGNORECASE, |
| 24 |
) |
| 25 |
COMMA_SEPARATED_LIST_RE = re.compile(r"[,\s]") |
| 26 |
BLANK_LINE_RE = re.compile(r"\s*\n") |
| 27 |
|
| 28 |
|
| 29 |
class ImportOrderChecker: |
| 30 |
visitor_class = ImportVisitor |
| 31 |
options = None |
| 32 |
|
| 33 |
def __init__(self, filename, tree): |
| 34 |
self.tree = tree |
| 35 |
self.filename = filename |
| 36 |
self.lines = None |
| 37 |
|
| 38 |
def load_file(self): |
| 39 |
if self.filename in ("stdin", "-", None): |
| 40 |
self.filename = "stdin" |
| 41 |
self.lines = pycodestyle.stdin_get_value().splitlines(True) |
| 42 |
else: |
| 43 |
self.lines = pycodestyle.readlines(self.filename) |
| 44 |
|
| 45 |
if self.tree is None: |
| 46 |
self.tree = ast.parse("".join(self.lines)) |
| 47 |
|
| 48 |
def error(self, error): |
| 49 |
return error |
| 50 |
|
| 51 |
def check_order(self): |
| 52 |
if not self.tree or not self.lines: |
| 53 |
self.load_file() |
| 54 |
|
| 55 |
try: |
| 56 |
style_entry_point = self.options["import_order_style"] |
| 57 |
except KeyError: |
| 58 |
style_entry_point = lookup_entry_point(DEFAULT_IMPORT_ORDER_STYLE) |
| 59 |
style_cls = style_entry_point.load() |
| 60 |
|
| 61 |
if style_cls.accepts_application_package_names: |
| 62 |
visitor = self.visitor_class( |
| 63 |
self.options.get("application_import_names", []), |
| 64 |
self.options.get("application_package_names", []), |
| 65 |
) |
| 66 |
else: |
| 67 |
visitor = self.visitor_class( |
| 68 |
self.options.get("application_import_names", []), |
| 69 |
[], |
| 70 |
) |
| 71 |
visitor.visit(self.tree) |
| 72 |
|
| 73 |
newlines = [ |
| 74 |
NewLine(lineno) # Lines are ordinal, no zero line |
| 75 |
for lineno, line in enumerate(self.lines, start=1) |
| 76 |
if BLANK_LINE_RE.match(line) |
| 77 |
] |
| 78 |
# Replace the below with heapq merge, when Python2 is dropped. |
| 79 |
combined = sorted( |
| 80 |
chain(newlines, visitor.imports), |
| 81 |
key=lambda element: element.lineno, |
| 82 |
) |
| 83 |
style = style_cls(combined) |
| 84 |
|
| 85 |
for error in style.check(): |
| 86 |
if not self.error_is_ignored(error): |
| 87 |
yield self.error(error) |
| 88 |
|
| 89 |
def error_is_ignored(self, error): |
| 90 |
noqa_match = NOQA_INLINE_REGEXP.search(self.lines[error.lineno - 1]) |
| 91 |
if noqa_match is None: |
| 92 |
return False |
| 93 |
|
| 94 |
codes_str = noqa_match.group("codes") |
| 95 |
if codes_str is None: |
| 96 |
return True |
| 97 |
|
| 98 |
codes = parse_comma_separated_list(codes_str) |
| 99 |
if error.code in codes: |
| 100 |
return True |
| 101 |
|
| 102 |
return False |
| 103 |
|
| 104 |
|
| 105 |
def parse_comma_separated_list(value): |
| 106 |
value = COMMA_SEPARATED_LIST_RE.split(value) |
| 107 |
item_gen = (item.strip() for item in value) |
| 108 |
return {item for item in item_gen if item} |