Remove redundant code
[scons.git] / SCons / Variables / ListVariable.py
bloba0640e63407fc26469de24d221deb96631a67cdf
1 # MIT License
3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 """Variable type for list Variables.
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
28 value holds either the named list elements, all list elements or no
29 list elements at all.
31 Usage example::
33 list_of_libs = Split('x11 gl qt ical')
35 opts = Variables()
36 opts.Add(
37 ListVariable(
38 'shared',
39 help='libraries to build as shared libraries',
40 default='all',
41 elems=list_of_libs,
44 ...
45 for lib in list_of_libs:
46 if lib in env['shared']:
47 env.SharedObject(...)
48 else:
49 env.Object(...)
50 """
52 # Known Bug: This should behave like a Set-Type, but does not really,
53 # since elements can occur twice.
55 import collections
56 from typing import Tuple, Callable
58 import SCons.Util
60 __all__ = ['ListVariable',]
63 class _ListVariable(collections.UserList):
64 def __init__(self, initlist=None, allowedElems=None) -> None:
65 if initlist is None:
66 initlist = []
67 if allowedElems is None:
68 allowedElems = []
69 super().__init__([_f for _f in initlist if _f])
70 self.allowedElems = sorted(allowedElems)
72 def __cmp__(self, other):
73 return NotImplemented
75 def __eq__(self, other):
76 return NotImplemented
78 def __ge__(self, other):
79 return NotImplemented
81 def __gt__(self, other):
82 return NotImplemented
84 def __le__(self, other):
85 return NotImplemented
87 def __lt__(self, other):
88 return NotImplemented
90 def __str__(self) -> str:
91 if not len(self):
92 return 'none'
93 self.data.sort()
94 if self.data == self.allowedElems:
95 return 'all'
96 else:
97 return ','.join(self)
99 def prepare_to_store(self):
100 return self.__str__()
102 def _converter(val, allowedElems, mapdict) -> _ListVariable:
103 """ """
104 if val == 'none':
105 val = []
106 elif val == 'all':
107 val = allowedElems
108 else:
109 val = [_f for _f in val.split(',') if _f]
110 val = [mapdict.get(v, v) for v in val]
111 notAllowed = [v for v in val if v not in allowedElems]
112 if notAllowed:
113 raise ValueError(
114 "Invalid value(s) for option: %s" % ','.join(notAllowed)
116 return _ListVariable(val, allowedElems)
119 # def _validator(key, val, env) -> None:
120 # """ """
121 # # TODO: write validator for pgk list
122 # pass
125 def ListVariable(key, help, default, names, map={}) -> Tuple[str, str, str, None, Callable]:
126 """Return a tuple describing a list SCons Variable.
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`.
132 *help* will have text appended indicating the legal values
133 (not including any extra names from *map*).
135 *map* can be used to map alternative names to the ones in *names* -
136 that is, a form of alias.
138 A 'list' option may either be 'all', 'none' or a list of
139 names (separated by commas).
141 names_str = 'allowed names: %s' % ' '.join(names)
142 if SCons.Util.is_List(default):
143 default = ','.join(default)
144 help = '\n '.join(
145 (help, '(all|none|comma-separated list of names)', names_str))
146 return (key, help, default, None, lambda val: _converter(val, names, map))
148 # Local Variables:
149 # tab-width:4
150 # indent-tabs-mode:nil
151 # End:
152 # vim: set expandtab tabstop=4 shiftwidth=4: