ConfigurationChecker.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import re
  2. class InvalidSynapeName(Exception):
  3. pass
  4. class NoSynapeName(Exception):
  5. pass
  6. class NoSynapeNeurons(Exception):
  7. pass
  8. class NoSynapeSignals(Exception):
  9. pass
  10. class NoValidSignal(Exception):
  11. pass
  12. class NoEventID(Exception):
  13. pass
  14. class NoEventPeriod(Exception):
  15. pass
  16. class MultipleSameSynapseName(Exception):
  17. pass
  18. class NotValidSynapseName(Exception):
  19. pass
  20. class ConfigurationChecker:
  21. def __init__(self):
  22. pass
  23. @staticmethod
  24. def check_synape_dict(synape_dict):
  25. if 'name' not in synape_dict:
  26. raise NoSynapeName("The Synapse does not have a name: %s" % synape_dict)
  27. # check that the name is conform
  28. # Regex for [a - zA - Z0 - 9\-] with dashes allowed in between but not at the start or end
  29. pattern = r'(?=[a-zA-Z0-9\-]{4,100}$)^[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*$'
  30. prog = re.compile(pattern)
  31. result = prog.match(synape_dict["name"])
  32. if result is None:
  33. raise InvalidSynapeName("Error with synapse name \"%s\".Valid syntax: [a - zA - Z0 - 9\-] with dashes "
  34. "allowed in between but not at the start or end" % synape_dict["name"])
  35. if 'neurons' not in synape_dict:
  36. raise NoSynapeNeurons("The Synapse does not have neurons: %s" % synape_dict)
  37. if 'signals' not in synape_dict:
  38. raise NoSynapeSignals("The Synapse does not have signals: %s" % synape_dict)
  39. return True
  40. @staticmethod
  41. def check_neuron_dict(neuron_dict):
  42. # TODO check that the Neuron plugin exist
  43. return True
  44. @staticmethod
  45. def check_signal_dict(signal_dict):
  46. if ('event' not in signal_dict) and ('order' not in signal_dict):
  47. raise NoValidSignal("The signal is not an event or an order %s" % signal_dict)
  48. return True
  49. @staticmethod
  50. def check_event_dict(event_dict):
  51. if event_dict is None:
  52. raise NoEventPeriod("Event must contain a period: %s" % event_dict)
  53. return True
  54. @staticmethod
  55. def check_order_dict(order_dict):
  56. if order_dict is not None:
  57. return True
  58. return False
  59. @staticmethod
  60. def check_synapes(synapses_list):
  61. """
  62. Check the synapse list is ok:
  63. - No double same name
  64. :param synapses_list:
  65. :type synapses_list: list of Synapse
  66. :return:
  67. """
  68. seen = set()
  69. for synapse in synapses_list:
  70. # convert ascii to UTF-8
  71. synapse_name = synapse.name.encode('utf-8')
  72. if synapse_name in seen:
  73. raise MultipleSameSynapseName("Multiple synapse found with the same name: %s" % synapse_name)
  74. seen.add(synapse.name)
  75. return True