36b530a9cacb1363cb56ada802885acc3f903c32
[scons.git] / src / engine / SCons / Variables / ListVariable.py
1 """engine.SCons.Variables.ListVariable
2
3 This file defines the option type for SCons implementing 'lists'.
4
5 A 'list' option may either be 'all', 'none' or a list of names
6 separated by comma. After the option has been processed, the option
7 value holds either the named list elements, all list elemens or no
8 list elements at all.
9
10 Usage example:
11
12   list_of_libs = Split('x11 gl qt ical')
13
14   opts = Variables()
15   opts.Add(ListVariable('shared',
16                       'libraries to build as shared libraries',
17                       'all',
18                       elems = list_of_libs))
19   ...
20   for lib in list_of_libs:
21      if lib in env['shared']:
22          env.SharedObject(...)
23      else:
24          env.Object(...)
25 """
26
27 #
28 # __COPYRIGHT__
29 #
30 # Permission is hereby granted, free of charge, to any person obtaining
31 # a copy of this software and associated documentation files (the
32 # "Software"), to deal in the Software without restriction, including
33 # without limitation the rights to use, copy, modify, merge, publish,
34 # distribute, sublicense, and/or sell copies of the Software, and to
35 # permit persons to whom the Software is furnished to do so, subject to
36 # the following conditions:
37 #
38 # The above copyright notice and this permission notice shall be included
39 # in all copies or substantial portions of the Software.
40 #
41 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
42 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
43 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
44 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
45 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
46 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
47 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 #
49 from __future__ import generators  ### KEEP FOR COMPATIBILITY FIXERS
50
51 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
52
53 # Know Bug: This should behave like a Set-Type, but does not really,
54 # since elements can occur twice.
55
56 __all__ = ['ListVariable',]
57
58 import UserList
59
60 import SCons.Util
61
62
63 class _ListVariable(UserList.UserList):
64     def __init__(self, initlist=[], allowedElems=[]):
65         UserList.UserList.__init__(self, [_f for _f in initlist if _f])
66         self.allowedElems = allowedElems[:]
67         self.allowedElems.sort()
68
69     def __cmp__(self, other):
70         raise NotImplementedError
71     def __eq__(self, other):
72         raise NotImplementedError
73     def __ge__(self, other):
74         raise NotImplementedError
75     def __gt__(self, other):
76         raise NotImplementedError
77     def __le__(self, other):
78         raise NotImplementedError
79     def __lt__(self, other):
80         raise NotImplementedError
81     def __str__(self):
82         if len(self) == 0:
83             return 'none'
84         self.data.sort()
85         if self.data == self.allowedElems:
86             return 'all'
87         else:
88             return ','.join(self)
89     def prepare_to_store(self):
90         return self.__str__()
91
92 def _converter(val, allowedElems, mapdict):
93     """
94     """
95     if val == 'none':
96         val = []
97     elif val == 'all':
98         val = allowedElems
99     else:
100         val = [_f for _f in val.split(',') if _f]
101         val = [mapdict.get(v, v) for v in val]
102         notAllowed = [v for v in val if not v in allowedElems]
103         if notAllowed:
104             raise ValueError("Invalid value(s) for option: %s" %
105                              ','.join(notAllowed))
106     return _ListVariable(val, allowedElems)
107
108
109 ## def _validator(key, val, env):
110 ##     """
111 ##     """
112 ##     # todo: write validater for pgk list
113 ##     return 1
114
115
116 def ListVariable(key, help, default, names, map={}):
117     """
118     The input parameters describe a 'package list' option, thus they
119     are returned with the correct converter and validater appended. The
120     result is usable for input to opts.Add() .
121
122     A 'package list' option may either be 'all', 'none' or a list of
123     package names (separated by space).
124     """
125     names_str = 'allowed names: %s' % ' '.join(names)
126     if SCons.Util.is_List(default):
127         default = ','.join(default)
128     help = '\n    '.join(
129         (help, '(all|none|comma-separated list of names)', names_str))
130     return (key, help, default,
131             None, #_validator,
132             lambda val: _converter(val, names, map))
133
134 # Local Variables:
135 # tab-width:4
136 # indent-tabs-mode:nil
137 # End:
138 # vim: set expandtab tabstop=4 shiftwidth=4: