diff options
author | Timothy Crosley <timothy.crosley@gmail.com> | 2019-11-09 00:23:26 -0800 |
---|---|---|
committer | Timothy Crosley <timothy.crosley@gmail.com> | 2019-11-09 00:23:26 -0800 |
commit | 6f97ca7b603fa26bcccb04d4b8b375a15abac9d8 (patch) | |
tree | 6de0fe4f799f60ab0503e75293e19812e462406b | |
parent | e38174e97be6650ebbea873e0f54e41ad3f714a9 (diff) | |
download | isort-6f97ca7b603fa26bcccb04d4b8b375a15abac9d8.tar.gz |
Add initial profile support
-rw-r--r-- | isort/exceptions.py | 15 | ||||
-rw-r--r-- | isort/main.py | 16 | ||||
-rw-r--r-- | isort/settings.py | 16 |
3 files changed, 38 insertions, 9 deletions
diff --git a/isort/exceptions.py b/isort/exceptions.py index 4c512fd3..b62b2c5d 100644 --- a/isort/exceptions.py +++ b/isort/exceptions.py @@ -1,7 +1,7 @@ """All isort specific exception classes should be defined here""" from pathlib import Path -from .settings import FILE_SKIP_COMMENT +from .profiles import profiles class ISortError(Exception): @@ -54,8 +54,7 @@ class FileSkipComment(FileSkipped): def __init__(self, file_path: str): super().__init__( - f"{file_path} contains an {FILE_SKIP_COMMENT} comment and was skipped.", - file_path=file_path, + f"{file_path} contains an file skip comment and was skipped.", file_path=file_path ) @@ -68,3 +67,13 @@ class FileSkipSetting(FileSkipped): " or matches a glob in 'skip_glob' setting", file_path=file_path, ) + + +class ProfileDoesNotExist(ISortError): + """Raised when a profile is set by the user that doesn't exist""" + + def __init__(self, profile: str): + super().__init__( + f"Specified profile of {profile} does not exist. " + f"Available profiles: {','.join(profiles)}." + ) diff --git a/isort/main.py b/isort/main.py index 2d47f79b..a8e17193 100644 --- a/isort/main.py +++ b/isort/main.py @@ -11,11 +11,10 @@ from warnings import warn import setuptools -from isort import SortImports, __version__ -from isort.logo import ASCII_ART -from isort.settings import DEFAULT_CONFIG, VALID_PY_TARGETS, Config, WrapModes - -from . import sections +from . import SortImports, __version__, sections +from .logo import ASCII_ART +from .profiles import profiles +from .settings import DEFAULT_CONFIG, VALID_PY_TARGETS, Config, WrapModes shebang_re = re.compile(br"^#!.*\bpython[23w]?\b") @@ -524,6 +523,13 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]: "interpreter used to run isort " f"(currently: {sys.version_info.major}{sys.version_info.minor}) will be used.", ) + parser.add_argument( + "--profile", + dest="profile", + choices=list(profiles.keys()), + type=str, + help="Base profile type to use for configuration.", + ) arguments = {key: value for key, value in vars(parser.parse_args(argv)).items() if value} if "dont_order_by_type" in arguments: diff --git a/isort/settings.py b/isort/settings.py index 48934b74..709f230c 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -34,6 +34,8 @@ from warnings import warn from . import stdlibs from ._future import dataclass, field +from .exceptions import ProfileDoesNotExist +from .profiles import profiles from .sections import DEFAULT as SECTION_DEFAULTS from .utils import difference, union from .wrap_modes import WrapModes @@ -167,6 +169,7 @@ class _Config: conda_env: str = "" ensure_newline_before_comments: bool = False directory: str = "" + profile: str = "" def __post_init__(self): py_version = self.py_version @@ -220,13 +223,24 @@ class Config(_Config): else: config_settings = {} + profile_name = config_overrides.get("profile", config_settings.get("profile", "")) + if profile_name: + if profile_name not in profiles: + raise ProfileDoesNotExist(profile) + + profile = profiles[profile_name].copy() + profile["source"] = f"{profile_name} profile" + sources.append(profile) + else: + profile = {} + if config_settings: sources.append(config_settings) if config_overrides: config_overrides["source"] = "runtime" sources.append(config_overrides) - combined_config = {**config_settings, **config_overrides} + combined_config = {**profile, **config_settings, **config_overrides} if "indent" in combined_config: indent = str(combined_config["indent"]) if indent.isdigit(): |