ConfigurationChecker.py 6.8 KB

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