Manage multiple style guides for a single run.
| 200 | |
| 201 | |
| 202 | class StyleGuideManager: |
| 203 | """Manage multiple style guides for a single run.""" |
| 204 | |
| 205 | def __init__( |
| 206 | self, |
| 207 | options: argparse.Namespace, |
| 208 | formatter: base_formatter.BaseFormatter, |
| 209 | decider: DecisionEngine | None = None, |
| 210 | ) -> None: |
| 211 | """Initialize our StyleGuide. |
| 212 | |
| 213 | .. todo:: Add parameter documentation. |
| 214 | """ |
| 215 | self.options = options |
| 216 | self.formatter = formatter |
| 217 | self.stats = statistics.Statistics() |
| 218 | self.decider = decider or DecisionEngine(options) |
| 219 | self.style_guides: list[StyleGuide] = [] |
| 220 | self.default_style_guide = StyleGuide( |
| 221 | options, formatter, self.stats, decider=decider, |
| 222 | ) |
| 223 | self.style_guides = [ |
| 224 | self.default_style_guide, |
| 225 | *self.populate_style_guides_with(options), |
| 226 | ] |
| 227 | |
| 228 | self.style_guide_for = functools.cache(self._style_guide_for) |
| 229 | |
| 230 | def populate_style_guides_with( |
| 231 | self, options: argparse.Namespace, |
| 232 | ) -> Generator[StyleGuide]: |
| 233 | """Generate style guides from the per-file-ignores option. |
| 234 | |
| 235 | :param options: |
| 236 | The original options parsed from the CLI and config file. |
| 237 | :returns: |
| 238 | A copy of the default style guide with overridden values. |
| 239 | """ |
| 240 | per_file = utils.parse_files_to_codes_mapping(options.per_file_ignores) |
| 241 | for filename, violations in per_file: |
| 242 | yield self.default_style_guide.copy( |
| 243 | filename=filename, extend_ignore_with=violations, |
| 244 | ) |
| 245 | |
| 246 | def _style_guide_for(self, filename: str) -> StyleGuide: |
| 247 | """Find the StyleGuide for the filename in particular.""" |
| 248 | return max( |
| 249 | (g for g in self.style_guides if g.applies_to(filename)), |
| 250 | key=lambda g: len(g.filename or ""), |
| 251 | ) |
| 252 | |
| 253 | @contextlib.contextmanager |
| 254 | def processing_file(self, filename: str) -> Generator[StyleGuide]: |
| 255 | """Record the fact that we're processing the file's results.""" |
| 256 | guide = self.style_guide_for(filename) |
| 257 | with guide.processing_file(filename): |
| 258 | yield guide |
| 259 |
nothing calls this directly
no outgoing calls
no test coverage detected