Coverage for C:\Repos\ekr-pylint\pylint\config\options_provider_mixin.py: 24%
75 statements
« prev ^ index » next coverage.py v6.4, created at 2022-05-24 10:21 -0500
« prev ^ index » next coverage.py v6.4, created at 2022-05-24 10:21 -0500
1# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
3# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
5import optparse # pylint: disable=deprecated-module
6import warnings
8from pylint.config.callback_actions import _CallbackAction
9from pylint.config.option import _validate
10from pylint.typing import Options
13class UnsupportedAction(Exception):
14 """Raised by set_option when it doesn't know what to do for an action."""
17class OptionsProviderMixIn:
18 """Mixin to provide options to an OptionsManager."""
20 # those attributes should be overridden
21 name = "default"
22 options: Options = ()
23 level = 0
25 def __init__(self):
26 # TODO: 3.0: Remove deprecated class
27 warnings.warn(
28 "OptionsProviderMixIn has been deprecated and will be removed in pylint 3.0",
29 DeprecationWarning,
30 )
31 self.config = optparse.Values()
32 self.load_defaults()
34 def load_defaults(self):
35 """Initialize the provider using default values."""
36 for opt, optdict in self.options:
37 action = optdict.get("action")
38 if action != "callback":
39 # callback action have no default
40 if optdict is None:
41 optdict = self.get_option_def(opt)
42 default = optdict.get("default")
43 self.set_option(opt, default, action, optdict)
45 def option_attrname(self, opt, optdict=None):
46 """Get the config attribute corresponding to opt."""
47 if optdict is None:
48 optdict = self.get_option_def(opt)
49 return optdict.get("dest", opt.replace("-", "_"))
51 def option_value(self, opt):
52 """Get the current value for the given option."""
53 return getattr(self.config, self.option_attrname(opt), None)
55 def set_option(self, optname, value, action=None, optdict=None):
56 """Method called to set an option (registered in the options list)."""
57 if optdict is None:
58 optdict = self.get_option_def(optname)
59 if value is not None:
60 value = _validate(value, optdict, optname)
61 if action is None:
62 action = optdict.get("action", "store")
63 if action == "store":
64 setattr(self.config, self.option_attrname(optname, optdict), value)
65 elif action in {"store_true", "count"}:
66 setattr(self.config, self.option_attrname(optname, optdict), value)
67 elif action == "store_false":
68 setattr(self.config, self.option_attrname(optname, optdict), value)
69 elif action == "append":
70 optname = self.option_attrname(optname, optdict)
71 _list = getattr(self.config, optname, None)
72 if _list is None:
73 if isinstance(value, (list, tuple)):
74 _list = value
75 elif value is not None:
76 _list = [value]
77 setattr(self.config, optname, _list)
78 elif isinstance(_list, tuple):
79 setattr(self.config, optname, _list + (value,))
80 else:
81 _list.append(value)
82 elif (
83 action == "callback"
84 or (not isinstance(action, str))
85 and issubclass(action, _CallbackAction)
86 ):
87 return
88 else:
89 raise UnsupportedAction(action)
91 def get_option_def(self, opt):
92 """Return the dictionary defining an option given its name."""
93 assert self.options
94 for option in self.options:
95 if option[0] == opt:
96 return option[1]
97 raise optparse.OptionError(
98 f"no such option {opt} in section {self.name!r}", opt
99 )
101 def options_by_section(self):
102 """Return an iterator on options grouped by section.
104 (section, [list of (optname, optdict, optvalue)])
105 """
106 sections = {}
107 for optname, optdict in self.options:
108 sections.setdefault(optdict.get("group"), []).append(
109 (optname, optdict, self.option_value(optname))
110 )
111 if None in sections:
112 yield None, sections.pop(None)
113 for section, options in sorted(sections.items()):
114 yield section.upper(), options
116 def options_and_values(self, options=None):
117 if options is None:
118 options = self.options
119 for optname, optdict in options:
120 yield optname, optdict, self.option_value(optname)