]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/flake8/options/manager.py
1 """Option handling and Option management logic."""
2 from __future__
import annotations
9 from typing
import Callable
10 from typing
import Sequence
12 from flake8
import utils
13 from flake8
.plugins
.finder
import Plugins
15 LOG
= logging
.getLogger(__name__
)
17 # represent a singleton of "not passed arguments".
18 # an enum is chosen to trick mypy
19 _ARG
= enum
.Enum("_ARG", "NO")
22 def _flake8_normalize(
25 comma_separated_list
: bool = False,
26 normalize_paths
: bool = False,
28 ret
: str |
list[str] = value
29 if comma_separated_list
and isinstance(ret
, str):
30 ret
= utils
.parse_comma_separated_list(value
)
33 if isinstance(ret
, str):
34 ret
= utils
.normalize_path(ret
, *args
)
36 ret
= utils
.normalize_paths(ret
, *args
)
42 """Our wrapper around an argparse argument parsers to add features."""
46 short_option_name
: str | _ARG
= _ARG
.NO
,
47 long_option_name
: str | _ARG
= _ARG
.NO
,
48 # Options below are taken from argparse.ArgumentParser.add_argument
49 action
: str |
type[argparse
.Action
] | _ARG
= _ARG
.NO
,
50 default
: Any | _ARG
= _ARG
.NO
,
51 type: Callable
[..., Any
] | _ARG
= _ARG
.NO
,
52 dest
: str | _ARG
= _ARG
.NO
,
53 nargs
: int |
str | _ARG
= _ARG
.NO
,
54 const
: Any | _ARG
= _ARG
.NO
,
55 choices
: Sequence
[Any
] | _ARG
= _ARG
.NO
,
56 help: str | _ARG
= _ARG
.NO
,
57 metavar
: str | _ARG
= _ARG
.NO
,
58 required
: bool | _ARG
= _ARG
.NO
,
59 # Options below here are specific to Flake8
60 parse_from_config
: bool = False,
61 comma_separated_list
: bool = False,
62 normalize_paths
: bool = False,
64 """Initialize an Option instance.
66 The following are all passed directly through to argparse.
68 :param short_option_name:
69 The short name of the option (e.g., ``-x``). This will be the
70 first argument passed to ``ArgumentParser.add_argument``
71 :param long_option_name:
72 The long name of the option (e.g., ``--xtra-long-option``). This
73 will be the second argument passed to
74 ``ArgumentParser.add_argument``
76 Default value of the option.
78 Attribute name to store parsed option value as.
80 Number of arguments to parse for this option.
82 Constant value to store on a common destination. Usually used in
83 conjunction with ``action="store_const"``.
85 Possible values for the option.
87 Help text displayed in the usage information.
89 Name to use instead of the long option name for help text.
91 Whether this option is required or not.
93 The following options may be passed directly through to :mod:`argparse`
94 but may need some massaging.
97 A callable to normalize the type (as is the case in
100 Any action allowed by :mod:`argparse`.
102 The following parameters are for Flake8's option handling alone.
104 :param parse_from_config:
105 Whether or not this option should be parsed out of config files.
106 :param comma_separated_list:
107 Whether the option is a comma separated list when parsing from a
109 :param normalize_paths:
110 Whether the option is expecting a path or list of paths and should
111 attempt to normalize the paths to absolute paths.
114 long_option_name
is _ARG
.NO
115 and short_option_name
is not _ARG
.NO
116 and short_option_name
.startswith("--")
118 short_option_name
, long_option_name
= _ARG
.NO
, short_option_name
120 # flake8 special type normalization
121 if comma_separated_list
or normalize_paths
:
122 type = functools
.partial(
124 comma_separated_list
=comma_separated_list
,
125 normalize_paths
=normalize_paths
,
128 self
.short_option_name
= short_option_name
129 self
.long_option_name
= long_option_name
132 for x
in (short_option_name
, long_option_name
)
136 self
.default
= default
141 self
.choices
= choices
143 self
.metavar
= metavar
144 self
.required
= required
145 self
.option_kwargs
: dict[str, Any | _ARG
] = {
146 "action": self
.action
,
147 "default": self
.default
,
152 "choices": self
.choices
,
154 "metavar": self
.metavar
,
155 "required": self
.required
,
158 # Set our custom attributes
159 self
.parse_from_config
= parse_from_config
160 self
.comma_separated_list
= comma_separated_list
161 self
.normalize_paths
= normalize_paths
163 self
.config_name
: str |
None = None
164 if parse_from_config
:
165 if long_option_name
is _ARG
.NO
:
167 "When specifying parse_from_config=True, "
168 "a long_option_name must also be specified."
170 self
.config_name
= long_option_name
[2:].replace("-", "_")
175 def filtered_option_kwargs(self
) -> dict[str, Any
]:
176 """Return any actually-specified arguments."""
178 k
: v
for k
, v
in self
.option_kwargs
.items() if v
is not _ARG
.NO
181 def __repr__(self
) -> str: # noqa: D105
183 for arg
in self
.option_args
:
185 for k
, v
in self
.filtered_option_kwargs
.items():
186 parts
.append(f
"{k}={v!r}")
187 return f
"Option({', '.join(parts)})"
189 def normalize(self
, value
: Any
, *normalize_args
: str) -> Any
:
190 """Normalize the value based on the option configuration."""
191 if self
.comma_separated_list
and isinstance(value
, str):
192 value
= utils
.parse_comma_separated_list(value
)
194 if self
.normalize_paths
:
195 if isinstance(value
, list):
196 value
= utils
.normalize_paths(value
, *normalize_args
)
198 value
= utils
.normalize_path(value
, *normalize_args
)
202 def to_argparse(self
) -> tuple[list[str], dict[str, Any
]]:
203 """Convert a Flake8 Option to argparse ``add_argument`` arguments."""
204 return self
.option_args
, self
.filtered_option_kwargs
208 """Manage Options and OptionParser while adding post-processing."""
214 plugin_versions
: str,
215 parents
: list[argparse
.ArgumentParser
],
216 formatter_names
: list[str],
218 """Initialize an instance of an OptionManager."""
219 self
.formatter_names
= formatter_names
220 self
.parser
= argparse
.ArgumentParser(
222 usage
="%(prog)s [options] file file ...",
224 epilog
=f
"Installed plugins: {plugin_versions}",
226 self
.parser
.add_argument(
230 f
"{version} ({plugin_versions}) "
231 f
"{utils.get_python_version()}"
234 self
.parser
.add_argument("filenames", nargs
="*", metavar
="filename")
236 self
.config_options_dict
: dict[str, Option
] = {}
237 self
.options
: list[Option
] = []
238 self
.extended_default_ignore
: list[str] = []
239 self
.extended_default_select
: list[str] = []
241 self
._current
_group
: argparse
._ArgumentGroup |
None = None
243 # TODO: maybe make this a free function to reduce api surface area
244 def register_plugins(self
, plugins
: Plugins
) -> None:
245 """Register the plugin options (if needed)."""
246 groups
: dict[str, argparse
._ArgumentGroup
] = {}
248 def _set_group(name
: str) -> None:
250 self
._current
_group
= groups
[name
]
252 group
= self
.parser
.add_argument_group(name
)
253 self
._current
_group
= groups
[name
] = group
255 for loaded
in plugins
.all_plugins():
256 add_options
= getattr(loaded
.obj
, "add_options", None)
258 _set_group(loaded
.plugin
.package
)
261 if loaded
.plugin
.entry_point
.group
== "flake8.extension":
262 self
.extend_default_select([loaded
.entry_name
])
264 # isn't strictly necessary, but seems cleaner
265 self
._current
_group
= None
267 def add_option(self
, *args
: Any
, **kwargs
: Any
) -> None:
268 """Create and register a new option.
270 See parameters for :class:`~flake8.options.manager.Option` for
271 acceptable arguments to this method.
275 ``short_option_name`` and ``long_option_name`` may be specified
276 positionally as they are with argparse normally.
278 option
= Option(*args
, **kwargs
)
279 option_args
, option_kwargs
= option
.to_argparse()
280 if self
._current
_group
is not None:
281 self
._current
_group
.add_argument(*option_args
, **option_kwargs
)
283 self
.parser
.add_argument(*option_args
, **option_kwargs
)
284 self
.options
.append(option
)
285 if option
.parse_from_config
:
286 name
= option
.config_name
287 assert name
is not None
288 self
.config_options_dict
[name
] = option
289 self
.config_options_dict
[name
.replace("_", "-")] = option
290 LOG
.debug('Registered option "%s".', option
)
292 def extend_default_ignore(self
, error_codes
: Sequence
[str]) -> None:
293 """Extend the default ignore list with the error codes provided.
296 List of strings that are the error/warning codes with which to
297 extend the default ignore list.
299 LOG
.debug("Extending default ignore list with %r", error_codes
)
300 self
.extended_default_ignore
.extend(error_codes
)
302 def extend_default_select(self
, error_codes
: Sequence
[str]) -> None:
303 """Extend the default select list with the error codes provided.
306 List of strings that are the error/warning codes with which
307 to extend the default select list.
309 LOG
.debug("Extending default select list with %r", error_codes
)
310 self
.extended_default_select
.extend(error_codes
)
314 args
: Sequence
[str] |
None = None,
315 values
: argparse
.Namespace |
None = None,
316 ) -> argparse
.Namespace
:
317 """Proxy to calling the OptionParser's parse_args method."""
319 self
.parser
.set_defaults(**vars(values
))
320 return self
.parser
.parse_args(args
)