Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for extended interpolations for .ini files. #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Snapshot
--------

* Added support for extended interpolation for .ini files.

1.2.7 (2022-04-04)
------------------

Expand Down
23 changes: 23 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ Or, with multiple root packages:

.. _the Grimp build_graph documentation: https://grimp.readthedocs.io/en/latest/usage.html#grimp.build_graph

**Interpolations**

When using `.ini` file, extended interpolation may be used, which may be helpful when
groups of modules need to be referenced in multiple places.

.. code-block:: ini

[importlinter:contract:1]
name = Ports should not depend on adapters.
type = forbidden
source_modules = ${ports:all}
forbidden_modules = ${adapters:all}

[ports]
all =
mypackage.inbound
yourpackage.outbound

[adapters]
all =
mypackage.adapters
yourpackage.adapters

Contracts
---------

Expand Down
4 changes: 3 additions & 1 deletion src/importlinter/adapters/user_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class IniFileUserOptionReader(AbstractUserOptionReader):
section_name = "importlinter"

def _read_config_filename(self, config_filename: str) -> Optional[UserOptions]:
config = configparser.ConfigParser()
config = configparser.ConfigParser(
interpolation=configparser.ExtendedInterpolation(),
)
file_contents = settings.FILE_SYSTEM.read(config_filename)
config.read_string(file_contents)
if self.section_name in config.sections():
Expand Down
15 changes: 15 additions & 0 deletions tests/assets/testpackage/.interpolationcontract.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[importlinter]
root_package = testpackage
include_external_packages = True

[importlinter:contract:one]
name=Expected kept contract
type=forbidden
source_modules=
${section_to_interpolate:source}
forbidden_modules=
sqlalchemy

[section_to_interpolate]
source=
testpackage.high.green
1 change: 1 addition & 0 deletions tests/functional/test_lint_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
(
(testpackage_directory, None, cli.EXIT_STATUS_SUCCESS),
(testpackage_directory, ".brokencontract.ini", cli.EXIT_STATUS_ERROR),
(testpackage_directory, ".interpolationcontract.ini", cli.EXIT_STATUS_SUCCESS),
(testpackage_directory, ".malformedcontract.ini", cli.EXIT_STATUS_ERROR),
(testpackage_directory, ".customkeptcontract.ini", cli.EXIT_STATUS_SUCCESS),
(testpackage_directory, ".externalbrokencontract.ini", cli.EXIT_STATUS_ERROR),
Expand Down