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

Honor all configuration passed via toml files #663

Merged
merged 1 commit into from
Oct 26, 2024
Merged
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
2 changes: 1 addition & 1 deletion precli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def main():
)

# Invoke the run
run = Run(config["enabled"], config["disabled"], artifacts, debug)
run = Run(config, artifacts, debug)
run.invoke()

# Render the results
Expand Down
20 changes: 6 additions & 14 deletions precli/core/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
parsers = loader.load_extension(group="precli.parsers")


def parse_file(
artifact: Artifact, enabled: list[str], disabled: list[str]
) -> list[Result]:
def parse_file(artifact: Artifact, config: dict) -> list[Result]:
parser = None
results = []
try:
Expand Down Expand Up @@ -66,7 +64,7 @@ def parse_file(
if artifact.contents is None:
with open(artifact.file_name, "rb") as f:
artifact.contents = f.read()
return parser.parse(artifact, enabled, disabled)
return parser.parse(artifact, config)
except OSError as e:
results.append(
Result(
Expand Down Expand Up @@ -116,13 +114,11 @@ def parse_file(
class Run:
def __init__(
self,
enabled: list[str],
disabled: list[str],
config: dict,
artifacts: list[Artifact],
debug: int,
):
self._enabled = enabled
self._disabled = disabled
self._config = config
self._artifacts = artifacts
self._init_logger(debug)
self._start_time = None
Expand Down Expand Up @@ -175,9 +171,7 @@ def invoke(self):
len(self._artifacts) > PROGRESS_THRESHOLD
and LOG.getEffectiveLevel() <= logging.INFO
):
parse_artifact = partial(
parse_file, enabled=self._enabled, disabled=self._disabled
)
parse_artifact = partial(parse_file, config=self._config)

progress = Progress(
TextColumn("[progress.description]{task.description}"),
Expand Down Expand Up @@ -205,9 +199,7 @@ def invoke(self):
with open(artifact.file_name, "rb") as f:
lines += sum(1 for _ in f)
try:
results += parse_file(
artifact, self._enabled, self._disabled
)
results += parse_file(artifact, self._config)
except KeyboardInterrupt:
sys.exit(2)

Expand Down
18 changes: 16 additions & 2 deletions precli/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tree_sitter import Node

from precli.core.artifact import Artifact
from precli.core.level import Level
from precli.core.location import Location
from precli.core.result import Result
from precli.core.suppression import Suppression
Expand Down Expand Up @@ -104,15 +105,20 @@ def _expand_rule_list(rule_list: list[str]) -> list[str]:
def parse(
self,
artifact: Artifact,
enabled: Optional[list[str]] = None,
disabled: Optional[list[str]] = None,
config: Optional[dict] = None,
) -> list[Result]:
"""File extension of files this parser can handle."""
config = {} if config is None else config
enabled = config.get("enabled")
disabled = config.get("disabled")

if enabled is not None:
enabled = Parser._expand_rule_list(enabled)
if disabled is not None:
disabled = Parser._expand_rule_list(disabled)

rule_conf = config["rule"] if "rule" in config else {}

for rule in self.rules.values():
if enabled is not None:
if (
Expand All @@ -130,6 +136,14 @@ def parse(
):
rule.enabled = False

# Override the level and parameters if given in config
if rule.id in rule_conf:
if "level" in rule_conf[rule.id]:
rule.config.level = Level(rule_conf[rule.id].get("level"))
for parameter, value in rule_conf[rule.id].items():
if parameter not in ("enabled", "level"):
rule.config.parameters[parameter] = value

self.results = []
self.context = {"artifact": artifact}
if artifact.contents is None:
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/rules/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def check(self, filename, enabled=None, disabled=None):
end_column,
) = self.expected(filename)
artifact = Artifact(os.path.join(self.base_path, filename))
results = self.parser.parse(artifact, enabled, disabled)
config = {
"enabled": enabled,
"disabled": disabled,
}
results = self.parser.parse(artifact, config)
if level == Level.NONE:
assert len(results) == 0
else:
Expand Down