ConfigurationChecker.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import re
  2. from core.Utils import ModuleNotFoundError
  3. class InvalidSynapeName(Exception):
  4. """
  5. The name of the synapse is not correct. It should only contains alphanumerics at the beginning and the end of
  6. its name. It can also contains dash in beetween alphanumerics.
  7. """
  8. pass
  9. class NoSynapeName(Exception):
  10. """
  11. A synapse needs a name
  12. """
  13. pass
  14. class NoSynapeNeurons(Exception):
  15. """
  16. A synapse must contains at least one neuron
  17. .. seealso:: Synapse, Neuron
  18. """
  19. pass
  20. class NoSynapeSignals(Exception):
  21. """
  22. A synapse must contains at least an Event or an Order
  23. .. seealso:: Event, Order
  24. """
  25. pass
  26. class NoValidSignal(Exception):
  27. """
  28. A synapse must contains at least a valid Event or an Order
  29. .. seealso:: Event, Order
  30. """
  31. pass
  32. class NoEventPeriod(Exception):
  33. """
  34. An Event must contains a period corresponding to its execution
  35. .. seealso:: Event
  36. """
  37. pass
  38. class MultipleSameSynapseName(Exception):
  39. """
  40. A synapse name must be unique
  41. """
  42. pass
  43. class ConfigurationChecker:
  44. """
  45. This Class provides all method to Check the configuration files are properly set up.
  46. """
  47. def __init__(self):
  48. pass
  49. @staticmethod
  50. def check_synape_dict(synape_dict):
  51. """
  52. Return True if the provided dict is well corresponding to a Synapse
  53. :param synape_dict: The synapse Dictionary
  54. :type synape_dict: Dict
  55. :return: True if synapse are ok
  56. :rtype: Boolean
  57. :Example:
  58. ConfigurationChecker().check_synape_dict(synapses_dict):
  59. .. seealso:: Synapse
  60. .. raises:: NoSynapeName, InvalidSynapeName, NoSynapeNeurons, NoSynapeSignals
  61. .. warnings:: Static and Public
  62. """
  63. if 'name' not in synape_dict:
  64. raise NoSynapeName("The Synapse does not have a name: %s" % synape_dict)
  65. # check that the name is conform
  66. # Regex for [a - zA - Z0 - 9\-] with dashes allowed in between but not at the start or end
  67. pattern = r'(?=[a-zA-Z0-9\-]{4,100}$)^[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*$'
  68. prog = re.compile(pattern)
  69. result = prog.match(synape_dict["name"])
  70. if result is None:
  71. raise InvalidSynapeName("Error with synapse name \"%s\".Valid syntax: [a - zA - Z0 - 9\-] with dashes "
  72. "allowed in between but not at the start or end" % synape_dict["name"])
  73. if 'neurons' not in synape_dict:
  74. raise NoSynapeNeurons("The Synapse does not have neurons: %s" % synape_dict)
  75. if 'signals' not in synape_dict:
  76. raise NoSynapeSignals("The Synapse does not have signals: %s" % synape_dict)
  77. return True
  78. @staticmethod
  79. def check_neuron_dict(neuron_dict):
  80. """
  81. Check received neuron dict is valid:
  82. :param neuron_dict: The neuron Dictionary
  83. :type neuron_dict: Dict
  84. :return: True if neuron is ok
  85. :rtype: Boolean
  86. :Example:
  87. ConfigurationChecker().check_neuron_dict(neurons_dict):
  88. .. seealso:: Synapse
  89. .. raises:: ModuleNotFoundError
  90. .. warnings:: Static and Public
  91. """
  92. def check_neuron_exist(neuron_module_name):
  93. """
  94. Return True if the neuron_name python Class exist in neurons package
  95. :param neuron_module_name: Name of the neuron module to check
  96. :type neuron_module_name: str
  97. :return:
  98. """
  99. package_name = "neurons"
  100. mod = __import__(package_name, fromlist=[neuron_module_name])
  101. try:
  102. getattr(mod, neuron_module_name)
  103. except AttributeError:
  104. raise ModuleNotFoundError("The module %s does not exist in package %s" % (neuron_module_name,
  105. package_name))
  106. return True
  107. if isinstance(neuron_dict, dict):
  108. for neuron_name in neuron_dict:
  109. check_neuron_exist(neuron_name)
  110. else:
  111. check_neuron_exist(neuron_dict)
  112. return True
  113. @staticmethod
  114. def check_signal_dict(signal_dict):
  115. """
  116. Check received signal dictionary is valid:
  117. :param signal_dict: The signal Dictionary
  118. :type signal_dict: Dict
  119. :return: True if signal are ok
  120. :rtype: Boolean
  121. :Example:
  122. ConfigurationChecker().check_signal_dict(signal_dict):
  123. .. seealso:: Order, Event
  124. .. raises:: NoValidSignal
  125. .. warnings:: Static and Public
  126. """
  127. if ('event' not in signal_dict) and ('order' not in signal_dict):
  128. raise NoValidSignal("The signal is not an event or an order %s" % signal_dict)
  129. return True
  130. @staticmethod
  131. def check_event_dict(event_dict):
  132. """
  133. Check received event dictionary is valid:
  134. :param event_dict: The event Dictionary
  135. :type event_dict: Dict
  136. :return: True if event are ok
  137. :rtype: Boolean
  138. :Example:
  139. ConfigurationChecker().check_event_dict(event_dict):
  140. .. seealso:: Event
  141. .. raises:: NoEventPeriod
  142. .. warnings:: Static and Public
  143. """
  144. if event_dict is None:
  145. raise NoEventPeriod("Event must contain a period: %s" % event_dict)
  146. return True
  147. @staticmethod
  148. def check_order_dict(order_dict):
  149. """
  150. Check received order dictionary is valid:
  151. :param order_dict: The Order Dict
  152. :type order_dict: Dict
  153. :return: True if event are ok
  154. :rtype: Boolean
  155. :Example:
  156. ConfigurationChecker().check_order_dict(order_dict):
  157. .. seealso:: Order
  158. .. warnings:: Static and Public
  159. """
  160. if order_dict is not None:
  161. return True
  162. return False
  163. @staticmethod
  164. def check_synapes(synapses_list):
  165. """
  166. Check the synapse list is ok:
  167. - No double same name
  168. :param synapses_list: The Synapse List
  169. :type synapses_list: List
  170. :return: list of Synapse
  171. :rtype: List
  172. :Example:
  173. ConfigurationChecker().check_synapes(order_dict):
  174. .. seealso:: Synapse
  175. .. raises:: MultipleSameSynapseName
  176. .. warnings:: Static and Public
  177. """
  178. seen = set()
  179. for synapse in synapses_list:
  180. # convert ascii to UTF-8
  181. synapse_name = synapse.name.encode('utf-8')
  182. if synapse_name in seen:
  183. raise MultipleSameSynapseName("Multiple synapse found with the same name: %s" % synapse_name)
  184. seen.add(synapse.name)
  185. return True