| 1 |
import optparse |
| 2 |
|
| 3 |
from flake8_import_order import __version__ |
| 4 |
from flake8_import_order.checker import DEFAULT_IMPORT_ORDER_STYLE |
| 5 |
from flake8_import_order.checker import ImportOrderChecker |
| 6 |
from flake8_import_order.styles import list_entry_points |
| 7 |
from flake8_import_order.styles import lookup_entry_point |
| 8 |
|
| 9 |
|
| 10 |
class Linter(ImportOrderChecker): |
| 11 |
name = "import-order" |
| 12 |
version = __version__ |
| 13 |
|
| 14 |
def __init__(self, tree, filename, lines=None): |
| 15 |
super().__init__(filename, tree) |
| 16 |
self.lines = lines |
| 17 |
|
| 18 |
@classmethod |
| 19 |
def add_options(cls, parser): |
| 20 |
# List of application import names. They go last. |
| 21 |
register_opt( |
| 22 |
parser, |
| 23 |
"--application-import-names", |
| 24 |
default="", |
| 25 |
action="store", |
| 26 |
type=str, |
| 27 |
help="Import names to consider as application-specific", |
| 28 |
parse_from_config=True, |
| 29 |
comma_separated_list=True, |
| 30 |
) |
| 31 |
register_opt( |
| 32 |
parser, |
| 33 |
"--application-package-names", |
| 34 |
default="", |
| 35 |
action="store", |
| 36 |
type=str, |
| 37 |
help=( |
| 38 |
"Package names to consider as company-specific " |
| 39 |
"(used only by 'appnexus' style)" |
| 40 |
), |
| 41 |
parse_from_config=True, |
| 42 |
comma_separated_list=True, |
| 43 |
) |
| 44 |
register_opt( |
| 45 |
parser, |
| 46 |
"--import-order-style", |
| 47 |
default=DEFAULT_IMPORT_ORDER_STYLE, |
| 48 |
action="store", |
| 49 |
type=str, |
| 50 |
help=( |
| 51 |
"Style to follow. Available: " |
| 52 |
", ".join(cls.list_available_styles()) |
| 53 |
), |
| 54 |
parse_from_config=True, |
| 55 |
) |
| 56 |
|
| 57 |
@staticmethod |
| 58 |
def list_available_styles(): |
| 59 |
entry_points = list_entry_points() |
| 60 |
return sorted(entry_point.name for entry_point in entry_points) |
| 61 |
|
| 62 |
@classmethod |
| 63 |
def parse_options(cls, options): |
| 64 |
optdict = {} |
| 65 |
|
| 66 |
names = options.application_import_names |
| 67 |
if not isinstance(names, list): |
| 68 |
names = options.application_import_names.split(",") |
| 69 |
|
| 70 |
pkg_names = options.application_package_names |
| 71 |
if not isinstance(pkg_names, list): |
| 72 |
pkg_names = options.application_package_names.split(",") |
| 73 |
|
| 74 |
style_entry_point = lookup_entry_point(options.import_order_style) |
| 75 |
|
| 76 |
optdict = dict( |
| 77 |
application_import_names=[n.strip() for n in names], |
| 78 |
application_package_names=[p.strip() for p in pkg_names], |
| 79 |
import_order_style=style_entry_point, |
| 80 |
) |
| 81 |
|
| 82 |
cls.options = optdict |
| 83 |
|
| 84 |
def error(self, error): |
| 85 |
return ( |
| 86 |
error.lineno, |
| 87 |
0, |
| 88 |
f"{error.code} {error.message}", |
| 89 |
Linter, |
| 90 |
) |
| 91 |
|
| 92 |
def run(self): |
| 93 |
yield from self.check_order() |
| 94 |
|
| 95 |
|
| 96 |
def register_opt(parser, *args, **kwargs): |
| 97 |
try: |
| 98 |
# Flake8 3.x registration |
| 99 |
parser.add_option(*args, **kwargs) |
| 100 |
except (optparse.OptionError, TypeError): |
| 101 |
# Flake8 2.x registration |
| 102 |
parse_from_config = kwargs.pop("parse_from_config", False) |
| 103 |
kwargs.pop("comma_separated_list", False) |
| 104 |
kwargs.pop("normalize_paths", False) |
| 105 |
parser.add_option(*args, **kwargs) |
| 106 |
if parse_from_config: |
| 107 |
parser.config_options.append(args[-1].lstrip("-")) |