Skip to content

Commit

Permalink
Add support like command line commands in configuration files. (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev authored Apr 3, 2023
1 parent 76e3d30 commit 563c9a1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased] - YYYY-MM-DD

### Added

- Add support like command line commands in configuration files. #287

### Changed

- Raise more human-readable exceptions when the key is mistyped in the configuration.
Expand Down
16 changes: 16 additions & 0 deletions docs/tutorial/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ diff = true
include_star_import = true
ignore_init = true
```

## Manage like CLI in configuration

```ini
[tool.unimport]
include-star-import = true
ignore-init = true
```

**setup.cfg**

```ini
[unimport]
include-star-import = true
ignore-init = true
```
29 changes: 24 additions & 5 deletions src/unimport/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
"check": bool,
"ignore_init": bool,
"color": str,
#
"include-star-import": bool,
"ignore-init": bool,
}

CONFIG_LIKE_COMMANDS_MAPPING = {
"include-star-import": "include_star_import",
"ignore-init": "ignore_init",
}


Expand Down Expand Up @@ -167,29 +175,40 @@ def get_config_as_list(name: str) -> List[str]:
for key, value in parser[self.config_section].items():
if key not in CONFIG_ANNOTATIONS_MAPPING:
raise UnknownConfigKeyException(key)

key_type = CONFIG_ANNOTATIONS_MAPPING[key]
if key_type == bool:
cfg_context[key] = parser.getboolean(self.config_section, key)
elif key_type == str:
cfg_context[key] = value # type: ignore
elif key_type == List[Path]:
cfg_context[key] = [Path(p) for p in get_config_as_list(key)] # type: ignore

expected_key = CONFIG_LIKE_COMMANDS_MAPPING.get(key, None)
if expected_key is not None:
cfg_context[expected_key] = cfg_context.pop(key)
return cfg_context
else:
return {}

def parse_toml(self) -> Dict[str, Any]:
parsed_toml = toml.loads(self.config_file.read_text())
toml_context: Dict[str, Any] = dict(
toml_context_from_conf: Dict[str, Any] = dict(
functools.reduce(lambda x, y: x.get(y, {}), self.config_section.split("."), parsed_toml) # type: ignore[attr-defined]
)
if toml_context:
for key, value in toml_context.items():
toml_context: Dict[str, Any] = {}
if toml_context_from_conf:
for key, value in toml_context_from_conf.items():
key = CONFIG_LIKE_COMMANDS_MAPPING.get(key, key)

if key not in CONFIG_ANNOTATIONS_MAPPING:
raise UnknownConfigKeyException(key)

sources = toml_context.get("sources", Config.default_sources)
toml_context["sources"] = [Path(path) for path in sources]
toml_context[key] = value

sources = toml_context.get("sources", None)
if sources is not None:
toml_context["sources"] = [Path(path) for path in sources]
return toml_context

@classmethod
Expand Down
3 changes: 3 additions & 0 deletions tests/config/configs/like-commands/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.unimport]
ignore-init = true
include-star-import = true
3 changes: 3 additions & 0 deletions tests/config/configs/like-commands/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[unimport]
ignore-init = true
include-star-import = true
15 changes: 13 additions & 2 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,26 @@ def test_disable_auto_discovery_config_not_found_config_files():


def test_mistyped_config_file():
setup_cfg = TEST_DIR / "mistyped" / "setup.cfg"
setup_cfg_config_file = TEST_DIR / "mistyped" / "setup.cfg"
pyproject_config_file = TEST_DIR / "mistyped" / "pyproject.toml"

exc = UnknownConfigKeyException("there-is-no-such-config-key")

with pytest.raises(UnknownConfigKeyException) as cm:
ParseConfig(config_file=setup_cfg).parse()
ParseConfig(config_file=setup_cfg_config_file).parse()
assert cm.value is exc

with pytest.raises(UnknownConfigKeyException) as cm:
ParseConfig(config_file=pyproject_config_file).parse()
assert cm.value is exc


def test_like_commands_config_file():
setup_cfg_config_file = TEST_DIR / "like-commands" / "setup.cfg"
pyproject_config_file = TEST_DIR / "like-commands" / "pyproject.toml"

parsed_config = ParseConfig(config_file=setup_cfg_config_file).parse()
assert parsed_config == {"ignore_init": True, "include_star_import": True}

parsed_config = ParseConfig(config_file=pyproject_config_file).parse()
assert parsed_config == {"ignore_init": True, "include_star_import": True}

0 comments on commit 563c9a1

Please sign in to comment.