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

Allow to ignore a list of RegEx expressions in layer contract #46

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
9 changes: 7 additions & 2 deletions src/importlinter/contracts/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class LayersContract(Contract):
containers = fields.ListField(subfield=fields.StringField())
layers = fields.ListField(subfield=LayerField())
ignore_imports = fields.ListField(subfield=fields.DirectImportField(), required=False)
ignore = fields.ListField(subfield=fields.RegExField(), required=False)

def check(self, graph: ImportGraph) -> ContractCheck:
is_kept = True
Expand All @@ -72,11 +73,12 @@ def check(self, graph: ImportGraph) -> ContractCheck:
if lower_layer_package.name not in graph.modules:
continue

descendants = set(
map(Module, graph.find_descendants(higher_layer_package.name)))
descendants = set(map(Module, graph.find_descendants(higher_layer_package.name)))
descendants = self._exclude_ignored(descendants) if self.ignore else descendants
higher_layer_modules = {higher_layer_package} | descendants

descendants = set(map(Module, graph.find_descendants(lower_layer_package.name)))
descendants = self._exclude_ignored(descendants) if self.ignore else descendants
lower_layer_modules = {lower_layer_package} | descendants

layer_chain_data = {
Expand Down Expand Up @@ -138,6 +140,9 @@ def render_broken_contract(self, check: ContractCheck) -> None:

output.new_line()

def _exclude_ignored(self, descendants):
return {d for d in descendants if not any(lambda reg: reg.match(d.name), self.ignore)}

def _validate_containers(self) -> None:
root_package_name = self.session_options['root_package']
for container in self.containers: # type: ignore
Expand Down
12 changes: 11 additions & 1 deletion src/importlinter/domain/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Union, Any
from typing import List, Union, Any, Pattern
import re
import abc

Expand Down Expand Up @@ -40,6 +40,16 @@ def parse(self, raw_data: Union[str, List]) -> str:
return str(raw_data)


class RegExField(Field):
"""
A field for single regex expressions.
"""
def parse(self, raw_data: Union[str, List]) -> Pattern[str]:
if isinstance(raw_data, list):
raise ValidationError('Expected a single value, got multiple values.')
return re.compile(str(raw_data))


class ListField(Field):
"""
A field for multiple values of any type.
Expand Down