setup_controller.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. # implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. """
  14. Controller class is a SINGLETON which handles all groups, params, sequences,
  15. steps and replaces the CONF dictionary.
  16. """
  17. from .core.parameters import Group
  18. from .core.sequences import Sequence
  19. def steps_new_format(steplist):
  20. # we have to duplicate title to name parameter and also only sigle
  21. # function is allowed in new step
  22. return [{'name': i['title'], 'title': i['title'],
  23. 'function': i['functions'][0]} for i in steplist]
  24. class Controller(object):
  25. __GROUPS = []
  26. __SEQUENCES = []
  27. __PLUGINS = []
  28. MESSAGES = []
  29. CONF = {}
  30. __single = None # the one, true Singleton ... for god's sake why ??? :)
  31. def __new__(self, *args, **kwargs):
  32. """
  33. Singleton implementation.
  34. Will return __single if self is the same class as the class of __single
  35. which means that we will not invoke this singleton if someone tries to create a new
  36. instance from a class which inherit Controller.
  37. did not use isinstance because inheritence makes it behave erratically.
  38. """
  39. if self != type(self.__single):
  40. self.__single = object.__new__(self, *args, **kwargs)
  41. return self.__single
  42. # PLugins
  43. def addPlugin(self, plugObj):
  44. self.__PLUGINS.append(plugObj)
  45. def getPluginByName(self, pluginName):
  46. for plugin in self.__PLUGINS:
  47. if plugin.__name__ == pluginName:
  48. return plugin
  49. return None
  50. def getAllPlugins(self):
  51. return self.__PLUGINS
  52. # Sequences and steps
  53. def addSequence(self, desc, cond, cond_match, steps):
  54. self.__SEQUENCES.append(Sequence(desc, steps_new_format(steps),
  55. condition=cond,
  56. cond_match=cond_match))
  57. def insertSequence(self, desc, cond, cond_match, steps, index=0):
  58. self.__SEQUENCES.insert(index, Sequence(desc,
  59. steps_new_format(steps),
  60. condition=cond,
  61. cond_match=cond_match))
  62. def getAllSequences(self):
  63. return self.__SEQUENCES
  64. def runAllSequences(self):
  65. for sequence in self.__SEQUENCES:
  66. sequence.run(config=self.CONF, messages=self.MESSAGES)
  67. def getSequenceByDesc(self, desc):
  68. for sequence in self.getAllSequences():
  69. if sequence.name == desc:
  70. return sequence
  71. return None
  72. def __getSequenceIndexByDesc(self, desc):
  73. for sequence in self.getAllSequences():
  74. if sequence.name == desc:
  75. return self.__SEQUENCES.index(sequence)
  76. return None
  77. def insertSequenceBeforeSequence(self, sequenceName, desc, cond, cond_match, steps):
  78. """
  79. Insert a sequence before a named sequence.
  80. i.e. if the specified sequence name is "update x", the new
  81. sequence will be inserted BEFORE "update x"
  82. """
  83. index = self.__getSequenceIndexByDesc(sequenceName)
  84. if index is None:
  85. index = len(self.getAllSequences())
  86. self.__SEQUENCES.insert(index, Sequence(desc,
  87. steps_new_format(steps),
  88. condition=cond,
  89. cond_match=cond_match))
  90. # Groups and params
  91. def addGroup(self, group, params):
  92. self.__GROUPS.append(Group(group, params))
  93. def getGroupByName(self, groupName):
  94. for group in self.getAllGroups():
  95. if group.GROUP_NAME == groupName:
  96. return group
  97. return None
  98. def getAllGroups(self):
  99. return self.__GROUPS
  100. def __getGroupIndexByDesc(self, name):
  101. for group in self.getAllGroups():
  102. if group.GROUP_NAME == name:
  103. return self.__GROUPS.index(group)
  104. return None
  105. def insertGroupBeforeGroup(self, groupName, group, params):
  106. """
  107. Insert a group before a named group.
  108. i.e. if the specified group name is "update x", the new
  109. group will be inserted BEFORE "update x"
  110. """
  111. index = self.__getGroupIndexByDesc(groupName)
  112. if index is None:
  113. index = len(self.getAllGroups())
  114. self.__GROUPS.insert(index, Group(group, params))
  115. def getParamByName(self, paramName):
  116. for group in self.getAllGroups():
  117. if paramName in group.parameters:
  118. return group.parameters[paramName]
  119. return None
  120. def getParamKeyValue(self, paramName, keyName):
  121. param = self.getParamByName(paramName)
  122. if param:
  123. return getattr(param, keyName)
  124. else:
  125. return None