Coverage for lintro / tools / core / config_injection.py: 81%
48 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Configuration injection helpers for BaseTool.
3Provides helper functions for tools to inject Lintro configuration
4into their CLI arguments.
5"""
7from __future__ import annotations
9from pathlib import Path
11from lintro.config.config_loader import get_config
12from lintro.config.lintro_config import LintroConfig
13from lintro.config.tool_config_generator import (
14 generate_defaults_config,
15 get_defaults_injection_args,
16 get_enforce_cli_args,
17)
20def _get_lintro_config() -> LintroConfig:
21 """Get the current Lintro configuration.
23 Returns:
24 LintroConfig: Current Lintro configuration instance.
25 """
26 return get_config()
29def _get_enforced_settings(
30 lintro_config: LintroConfig | None = None,
31) -> dict[str, object]:
32 """Get enforced settings as a dictionary.
34 Args:
35 lintro_config: Optional Lintro config. If None, loads current config.
37 Returns:
38 dict[str, object]: Dictionary of enforced settings.
39 """
40 if lintro_config is None:
41 lintro_config = _get_lintro_config()
43 settings: dict[str, object] = {}
44 if lintro_config.enforce.line_length is not None:
45 settings["line_length"] = lintro_config.enforce.line_length
46 if lintro_config.enforce.target_python is not None:
47 settings["target_python"] = lintro_config.enforce.target_python
49 return settings
52def _get_enforce_cli_args(
53 tool_name: str,
54 lintro_config: LintroConfig | None = None,
55) -> list[str]:
56 """Get CLI arguments for enforced settings.
58 Args:
59 tool_name: Name of the tool.
60 lintro_config: Optional Lintro config. If None, loads current config.
62 Returns:
63 list[str]: CLI arguments to inject enforced settings.
64 """
65 if lintro_config is None:
66 lintro_config = _get_lintro_config()
68 args: list[str] = get_enforce_cli_args(
69 tool_name=tool_name,
70 lintro_config=lintro_config,
71 )
72 return args
75def _get_defaults_config_args(
76 tool_name: str,
77 lintro_config: LintroConfig | None = None,
78) -> list[str]:
79 """Get CLI arguments for defaults config injection.
81 Args:
82 tool_name: Name of the tool.
83 lintro_config: Optional Lintro config. If None, loads current config.
85 Returns:
86 list[str]: CLI arguments to inject defaults config file.
87 """
88 if lintro_config is None:
89 lintro_config = _get_lintro_config()
91 # Generate defaults config file if needed
92 config_path: Path | None = generate_defaults_config(
93 tool_name=tool_name,
94 lintro_config=lintro_config,
95 )
97 args: list[str] = get_defaults_injection_args(
98 tool_name=tool_name,
99 config_path=config_path,
100 )
101 return args
104def _should_use_lintro_config(
105 tool_name: str,
106 lintro_config: LintroConfig | None = None,
107) -> bool:
108 """Check if Lintro config should be used for this tool.
110 Args:
111 tool_name: Name of the tool.
112 lintro_config: Optional Lintro config. If None, loads current config.
114 Returns:
115 bool: True if Lintro config should be injected.
116 """
117 if lintro_config is None:
118 lintro_config = _get_lintro_config()
120 # Check if enforce settings are configured
121 if lintro_config.enforce.line_length is not None:
122 return True
123 if lintro_config.enforce.target_python is not None:
124 return True
126 # Check if defaults are configured for this tool
127 defaults = lintro_config.get_tool_defaults(tool_name.lower())
128 if defaults:
129 return True
131 # Check if builtin defaults exist for this tool
132 from lintro.config.tool_config_generator import TOOL_BUILTIN_DEFAULTS
134 return tool_name.lower() in TOOL_BUILTIN_DEFAULTS
137def _build_config_args(
138 tool_name: str,
139 lintro_config: LintroConfig | None = None,
140) -> list[str]:
141 """Build combined CLI arguments for config injection.
143 Combines enforce CLI args and defaults config args.
145 Args:
146 tool_name: Name of the tool.
147 lintro_config: Optional Lintro config. If None, loads current config.
149 Returns:
150 list[str]: Combined CLI arguments for config injection.
151 """
152 if lintro_config is None:
153 lintro_config = _get_lintro_config()
155 # Check if Lintro config should be used
156 if not _should_use_lintro_config(tool_name, lintro_config):
157 return []
159 args: list[str] = []
161 # Add enforce CLI args
162 args.extend(_get_enforce_cli_args(tool_name=tool_name, lintro_config=lintro_config))
164 # Add defaults config args
165 args.extend(
166 _get_defaults_config_args(tool_name=tool_name, lintro_config=lintro_config),
167 )
169 return args