api.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://swi.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. class Plugin(object):
  20. def initialize(self):
  21. pass
  22. def terminate(self):
  23. pass
  24. def set_name(self, name):
  25. self.name = name
  26. def get_name(self):
  27. if hasattr(self, 'name') == False:
  28. return None
  29. return self.name
  30. def set_plugin_loader(self, loader):
  31. self.plugin_loader = loader
  32. def get_plugin_loader(self):
  33. if hasattr(self, 'plugin_loader') == False:
  34. return None
  35. return self.plugin_loader
  36. class InterfaceNotImplemented(Exception):
  37. def __init__(self, obj):
  38. import sys
  39. Exception.__init__(self, "Method '"
  40. + sys._getframe(1).f_code.co_name
  41. + "' has not been implemented for "
  42. + str(obj.__class__))
  43. class IConfigurable(object):
  44. def configure(self, options):
  45. raise InterfaceNotImplemented(self)
  46. def declare_configuration(self, optparser):
  47. raise InterfaceNotImplemented(self)
  48. class IRunable(object):
  49. def run(self, args):
  50. raise InterfaceNotImplemented(self)
  51. class CallbackNotImplemented(Exception):
  52. def __init__(self, obj, callback_name):
  53. Exception.__init__(self, "Callback '"
  54. + callback_name
  55. + "' has not been implemented for "
  56. + str(obj.__class__))
  57. class Child(object):
  58. def notify(self, parent, callback_name, *args):
  59. if hasattr(self, callback_name) == False:
  60. raise CallbackNotImplemented(self, callback_name)
  61. self.__getattribute__(callback_name)(parent, *args)
  62. class Parent(object):
  63. def init_Parent(self):
  64. if hasattr(self, 'children') == False:
  65. self.children = []
  66. def subscribe(self, obj, callback_name):
  67. self.init_Parent()
  68. if (isinstance(obj, Child) == False):
  69. raise TypeError()
  70. self.children.append((obj,callback_name))
  71. def unsubscribe(self, obj, callback_name):
  72. self.init_Parent()
  73. self.children.remove((obj, callback_name))
  74. def notify_children(self, *args):
  75. self.init_Parent()
  76. for child in self.children:
  77. child[0].notify(self, child[1], *args)
  78. def iterate_children(self):
  79. self.init_Parent()
  80. for child in self.children:
  81. yield child
  82. class ExitError(Exception):
  83. def __init__(self, plugin, reason):
  84. if plugin != None:
  85. Exception.__init__(self, "Plugin '"
  86. + plugin.get_name()
  87. + "' requested abnormal termination: "
  88. + reason)
  89. else:
  90. Exception.__init__(self, "'Abnormal termination requested: "
  91. + reason)
  92. def subscribe_by_parents_name(parent_name, child, callback_name='callback'):
  93. child.get_plugin_loader().get_plugin(parent_name).subscribe(child, callback_name)
  94. # interfaces for subscription
  95. class ICode(object):
  96. pass
  97. def subscribe_by_parents_interface(interface, child, callback_name='callback'):
  98. for plugin in child.get_plugin_loader().iterate_plugins():
  99. if isinstance(plugin, interface):
  100. plugin.subscribe(child, callback_name)