You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.3 KiB

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