21
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
24
- """Variable type for list Variables.
24
+ """Variable type for List Variables.
25
25
26
- A ' list' option may either be 'all', 'none' or a list of names
27
- separated by comma. After the option has been processed, the option
26
+ A list variable may given as 'all', 'none' or a list of names
27
+ separated by comma. After the variable has been processed, the variable
28
28
value holds either the named list elements, all list elements or no
29
29
list elements at all.
30
30
41
41
elems=list_of_libs,
42
42
)
43
43
)
44
- ...
44
+ env = Environment(variables=opts)
45
45
for lib in list_of_libs:
46
46
if lib in env['shared']:
47
47
env.SharedObject(...)
53
53
# since elements can occur twice.
54
54
55
55
import collections
56
- from typing import Tuple , Callable
56
+ from typing import Callable , List , Optional , Tuple , Union
57
57
58
58
import SCons .Util
59
59
60
60
__all__ = ['ListVariable' ,]
61
61
62
62
63
63
class _ListVariable (collections .UserList ):
64
+ """Internal class holding the data for a List Variable.
65
+
66
+ The initializer accepts two arguments, the list of actual values
67
+ given, and the list of allowable values. Not normally instantiated
68
+ by hand, but rather by the ListVariable converter function.
69
+ """
70
+
64
71
def __init__ (self , initlist = None , allowedElems = None ) -> None :
65
72
if initlist is None :
66
73
initlist = []
@@ -88,19 +95,18 @@ def __lt__(self, other):
88
95
return NotImplemented
89
96
90
97
def __str__ (self ) -> str :
91
- if not len ( self ) :
98
+ if not self . data :
92
99
return 'none'
93
100
self .data .sort ()
94
101
if self .data == self .allowedElems :
95
102
return 'all'
96
- else :
97
- return ',' .join (self )
103
+ return ',' .join (self )
98
104
99
105
def prepare_to_store (self ):
100
- return self . __str__ ( )
106
+ return str ( self )
101
107
102
108
def _converter (val , allowedElems , mapdict ) -> _ListVariable :
103
- """ """
109
+ """Convert list variables. """
104
110
if val == 'none' :
105
111
val = []
106
112
elif val == 'all' :
@@ -111,39 +117,57 @@ def _converter(val, allowedElems, mapdict) -> _ListVariable:
111
117
notAllowed = [v for v in val if v not in allowedElems ]
112
118
if notAllowed :
113
119
raise ValueError (
114
- "Invalid value(s) for option: %s" % ',' .join (notAllowed )
120
+ f "Invalid value(s) for option: { ',' .join (notAllowed )} "
115
121
)
116
122
return _ListVariable (val , allowedElems )
117
123
118
124
119
125
# def _validator(key, val, env) -> None:
120
126
# """ """
121
- # # TODO: write validator for pgk list
127
+ # # TODO: write validator for list variable
122
128
# pass
123
129
124
130
125
- def ListVariable (key , help , default , names , map = {}) -> Tuple [str , str , str , None , Callable ]:
126
- """Return a tuple describing a list SCons Variable.
127
-
128
- The input parameters describe a 'list' option. Returns
129
- a tuple including the correct converter and validator.
130
- The result is usable for input to :meth:`Add`.
131
-
132
- *help* will have text appended indicating the legal values
133
- (not including any extra names from *map*).
134
-
135
- *map* can be used to map alternative names to the ones in *names* -
136
- that is, a form of alias.
137
-
138
- A 'list' option may either be 'all', 'none' or a list of
139
- names (separated by commas).
131
+ # lint: W0622: Redefining built-in 'help' (redefined-builtin)
132
+ # lint: W0622: Redefining built-in 'map' (redefined-builtin)
133
+ def ListVariable (
134
+ key ,
135
+ help : str ,
136
+ default : Union [str , List [str ]],
137
+ names : List [str ],
138
+ map : Optional [dict ] = None ,
139
+ ) -> Tuple [str , str , str , None , Callable ]:
140
+ """Return a tuple describing a list variable.
141
+
142
+ The input parameters describe a list variable, where the values
143
+ can be one or more from *names* plus the special values ``all``
144
+ and ``none``.
145
+
146
+ Arguments:
147
+ key: the name of the list variable.
148
+ help: the basic help message. Will have text appended indicating
149
+ the allowable values (not including any extra names from *map*).
150
+ default: the default value(s) for the list variable. Can be
151
+ given as string (possibly comma-separated), or as a list of strings.
152
+ ``all`` or ``none`` are allowed as *default*.
153
+ names: the allowable values. Must be a list of strings.
154
+ map: optional dictionary to map alternative names to the ones in
155
+ *names*, providing a form of alias. The converter will make
156
+ the replacement, names from *map* are not stored and will
157
+ not appear in the help message.
158
+
159
+ Returns:
160
+ A tuple including the correct converter and validator. The
161
+ result is usable as input to :meth:`~SCons.Variables.Variables.Add`.
140
162
"""
141
- names_str = 'allowed names: %s' % ' ' .join (names )
163
+ if map is None :
164
+ map = {}
165
+ names_str = f"allowed names: { ' ' .join (names )} "
142
166
if SCons .Util .is_List (default ):
143
167
default = ',' .join (default )
144
168
help = '\n ' .join (
145
169
(help , '(all|none|comma-separated list of names)' , names_str ))
146
- return ( key , help , default , None , lambda val : _converter (val , names , map ) )
170
+ return key , help , default , None , lambda val : _converter (val , names , map )
147
171
148
172
# Local Variables:
149
173
# tab-width:4
0 commit comments