ConfigurationChecker.py 5.7 KB

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