ConfigurationChecker.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import re
  2. from core.Utils import Utils, 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 corresponding to a Synapse
  31. :param settings: The YAML settings file
  32. :type settings: String
  33. :return: the path to store the cache
  34. :rtype: String
  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. - neuron exist
  61. :param neuron_dict:
  62. :return:
  63. """
  64. def check_neuron_exist(neuron_name):
  65. package_name = "neurons"
  66. mod = __import__(package_name, fromlist=[neuron_name])
  67. try:
  68. getattr(mod, neuron_name)
  69. except AttributeError:
  70. raise ModuleNotFoundError("The module %s does not exist in package %s" % (neuron_name, package_name))
  71. return True
  72. if isinstance(neuron_dict, dict):
  73. for neuron_name in neuron_dict:
  74. check_neuron_exist(neuron_name)
  75. else:
  76. check_neuron_exist(neuron_dict)
  77. return True
  78. @staticmethod
  79. def check_signal_dict(signal_dict):
  80. if ('event' not in signal_dict) and ('order' not in signal_dict):
  81. raise NoValidSignal("The signal is not an event or an order %s" % signal_dict)
  82. return True
  83. @staticmethod
  84. def check_event_dict(event_dict):
  85. if event_dict is None:
  86. raise NoEventPeriod("Event must contain a period: %s" % event_dict)
  87. return True
  88. @staticmethod
  89. def check_order_dict(order_dict):
  90. if order_dict is not None:
  91. return True
  92. return False
  93. @staticmethod
  94. def check_synapes(synapses_list):
  95. """
  96. Check the synapse list is ok:
  97. - No double same name
  98. :param synapses_list:
  99. :type synapses_list: list of Synapse
  100. :return:
  101. """
  102. seen = set()
  103. for synapse in synapses_list:
  104. # convert ascii to UTF-8
  105. synapse_name = synapse.name.encode('utf-8')
  106. if synapse_name in seen:
  107. raise MultipleSameSynapseName("Multiple synapse found with the same name: %s" % synapse_name)
  108. seen.add(synapse.name)
  109. return True