]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/flake8/api/legacy.py
446df293d8f15f899315295bf234c26f9a9b6ae1
1 """Module containing shims around Flake8 2.x behaviour.
3 Previously, users would import :func:`get_style_guide` from ``flake8.engine``.
4 In 3.0 we no longer have an "engine" module but we maintain the API from it.
6 from __future__
import annotations
11 from typing
import Any
13 from flake8
.discover_files
import expand_paths
14 from flake8
.formatting
import base
as formatter
15 from flake8
.main
import application
as app
16 from flake8
.options
.parse_args
import parse_args
18 LOG
= logging
.getLogger(__name__
)
21 __all__
= ("get_style_guide",)
25 """Public facing object that mimic's Flake8 2.0's API.
29 There are important changes in how this object behaves compared to
30 the object provided in Flake8 2.x.
34 This should not be instantiated by users.
36 .. versionchanged:: 3.0.0
39 def __init__(self
, application
: app
.Application
) -> None:
40 """Initialize the Report for the user.
42 .. warning:: This should not be instantiated by users.
44 assert application
.guide
is not None
45 self
._application
= application
46 self
._style
_guide
= application
.guide
47 self
._stats
= self
._style
_guide
.stats
50 def total_errors(self
) -> int:
51 """Return the total number of errors."""
52 return self
._application
.result_count
54 def get_statistics(self
, violation
: str) -> list[str]:
55 """Get the list of occurrences of a violation.
58 List of occurrences of a violation formatted as:
59 {Count} {Error Code} {Message}, e.g.,
60 ``8 E531 Some error message about the error``
63 f
"{s.count} {s.error_code} {s.message}"
64 for s
in self
._stats
.statistics_for(violation
)
69 """Public facing object that mimic's Flake8 2.0's StyleGuide.
73 There are important changes in how this object behaves compared to
74 the StyleGuide object provided in Flake8 2.x.
78 This object should not be instantiated directly by users.
80 .. versionchanged:: 3.0.0
83 def __init__(self
, application
: app
.Application
) -> None:
84 """Initialize our StyleGuide."""
85 self
._application
= application
86 self
._file
_checker
_manager
= application
.file_checker_manager
89 def options(self
) -> argparse
.Namespace
:
90 """Return application's options.
92 An instance of :class:`argparse.Namespace` containing parsed options.
94 assert self
._application
.options
is not None
95 return self
._application
.options
98 def paths(self
) -> list[str]:
99 """Return the extra arguments passed as paths."""
100 assert self
._application
.options
is not None
101 return self
._application
.options
.filenames
103 def check_files(self
, paths
: list[str] |
None = None) -> Report
:
104 """Run collected checks on the files provided.
106 This will check the files passed in and return a :class:`Report`
110 List of filenames (or paths) to check.
112 Object that mimic's Flake8 2.0's Reporter class.
114 assert self
._application
.options
is not None
115 self
._application
.options
.filenames
= paths
116 self
._application
.run_checks()
117 self
._application
.report_errors()
118 return Report(self
._application
)
120 def excluded(self
, filename
: str, parent
: str |
None = None) -> bool:
121 """Determine if a file is excluded.
124 Path to the file to check if it is excluded.
126 Name of the parent directory containing the file.
128 True if the filename is excluded, False otherwise.
131 def excluded(path
: str) -> bool:
135 stdin_display_name
=self
.options
.stdin_display_name
,
136 filename_patterns
=self
.options
.filename
,
137 exclude
=self
.options
.exclude
,
142 return excluded(filename
) or (
143 parent
is not None and excluded(os
.path
.join(parent
, filename
))
148 reporter
: type[formatter
.BaseFormatter
] |
None = None,
150 """Set up a formatter for this run of Flake8."""
153 if not issubclass(reporter
, formatter
.BaseFormatter
):
155 "Report should be subclass of "
156 "flake8.formatter.BaseFormatter."
158 self
._application
.formatter
= reporter(self
.options
)
159 self
._application
.guide
= None
160 # NOTE(sigmavirus24): This isn't the intended use of
161 # Application#make_guide but it works pretty well.
162 # Stop cringing... I know it's gross.
163 self
._application
.make_guide()
164 self
._application
.file_checker_manager
= None
165 self
._application
.make_file_checker_manager([])
170 lines
: Any |
None = None,
171 expected
: Any |
None = None,
172 line_offset
: Any |
None = 0,
174 """Run collected checks on a single file.
176 This will check the file passed in and return a :class:`Report`
180 The path to the file to check.
182 Ignored since Flake8 3.0.
184 Ignored since Flake8 3.0.
186 Ignored since Flake8 3.0.
188 Object that mimic's Flake8 2.0's Reporter class.
190 return self
.check_files([filename
])
193 def get_style_guide(**kwargs
: Any
) -> StyleGuide
:
194 r
"""Provision a StyleGuide for use.
197 Keyword arguments that provide some options for the StyleGuide.
199 An initialized StyleGuide
201 application
= app
.Application()
202 application
.plugins
, application
.options
= parse_args([])
203 # We basically want application.initialize to be called but with these
204 # options set instead before we make our formatter, notifier, internal
205 # style guide and file checker manager.
206 options
= application
.options
207 for key
, value
in kwargs
.items():
209 getattr(options
, key
)
210 setattr(options
, key
, value
)
211 except AttributeError:
212 LOG
.error('Could not update option "%s"', key
)
213 application
.make_formatter()
214 application
.make_guide()
215 application
.make_file_checker_manager([])
216 return StyleGuide(application
)