parameters.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  11. # implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Container set for groups and parameters
  16. """
  17. from ..utils.datastructures import SortedDict
  18. class Parameter(object):
  19. allowed_keys = ('CONF_NAME', 'CMD_OPTION', 'USAGE', 'PROMPT',
  20. 'PROCESSORS', 'VALIDATORS', 'LOOSE_VALIDATION',
  21. 'DEFAULT_VALUE', 'USE_DEFAULT', 'OPTION_LIST',
  22. 'MASK_INPUT', 'NEED_CONFIRM', 'CONDITION', 'DEPRECATES',
  23. 'MESSAGE', 'MESSAGE_VALUES')
  24. def __init__(self, attributes=None):
  25. attributes = attributes or {}
  26. defaults = {}.fromkeys(self.allowed_keys)
  27. defaults.update(attributes)
  28. for key, value in defaults.iteritems():
  29. if key not in self.allowed_keys:
  30. raise KeyError('Given attribute %s is not allowed' % key)
  31. self.__dict__[key] = value
  32. class Group(Parameter):
  33. allowed_keys = ('GROUP_NAME', 'DESCRIPTION', 'PRE_CONDITION',
  34. 'PRE_CONDITION_MATCH', 'POST_CONDITION',
  35. 'POST_CONDITION_MATCH')
  36. def __init__(self, attributes=None, parameters=None):
  37. super(Group, self).__init__(attributes)
  38. self.parameters = SortedDict()
  39. for param in parameters or []:
  40. self.parameters[param['CONF_NAME']] = Parameter(attributes=param)
  41. def search(self, attr, value):
  42. """
  43. Returns list of parameters which have given attribute of given
  44. value.
  45. """
  46. result = []
  47. for param in self.parameters.itervalues():
  48. if getattr(param, attr) == value:
  49. result.append(param)
  50. return result