api.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. '''
  2. Created on 25/07/2012
  3. @author: konstaa
  4. '''
  5. class Plugin(object):
  6. def initialize(self):
  7. pass
  8. def terminate(self):
  9. pass
  10. def set_name(self, name):
  11. self.name = name
  12. def get_name(self):
  13. if hasattr(self, 'name') == False:
  14. return None
  15. return self.name
  16. def set_plugin_loader(self, loader):
  17. self.plugin_loader = loader
  18. def get_plugin_loader(self):
  19. if hasattr(self, 'plugin_loader') == False:
  20. return None
  21. return self.plugin_loader
  22. class InterfaceNotImplemented(Exception):
  23. def __init__(self, obj):
  24. import sys
  25. Exception.__init__(self, "Method '"
  26. + sys._getframe(1).f_code.co_name
  27. + "' has not been implemented for "
  28. + str(obj.__class__))
  29. class IConfigurable(object):
  30. def configure(self, options):
  31. raise InterfaceNotImplemented(self)
  32. def declare_configuration(self, optparser):
  33. raise InterfaceNotImplemented(self)
  34. class IRunable(object):
  35. def run(self, args):
  36. raise InterfaceNotImplemented(self)
  37. class CallbackNotImplemented(Exception):
  38. def __init__(self, obj, callback_name):
  39. Exception.__init__(self, "Callback '"
  40. + callback_name
  41. + "' has not been implemented for "
  42. + str(obj.__class__))
  43. class Child(object):
  44. def notify(self, parent, callback_name, *args):
  45. if hasattr(self, callback_name) == False:
  46. raise CallbackNotImplemented(self, callback_name)
  47. self.__getattribute__(callback_name)(parent, *args)
  48. class Parent(object):
  49. def init_Parent(self):
  50. if hasattr(self, 'children') == False:
  51. self.children = []
  52. def subscribe(self, obj, callback_name):
  53. self.init_Parent()
  54. if (isinstance(obj, Child) == False):
  55. raise TypeError()
  56. self.children.append((obj,callback_name))
  57. def unsubscribe(self, obj, callback_name):
  58. self.init_Parent()
  59. self.children.remove((obj, callback_name))
  60. def notify_children(self, *args):
  61. self.init_Parent()
  62. for child in self.children:
  63. child[0].notify(self, child[1], *args)
  64. def iterate_children(self):
  65. self.init_Parent()
  66. for child in self.children:
  67. yield child
  68. class ExitError(Exception):
  69. def __init__(self, plugin, reason):
  70. Exception.__init__(self, "Plugin '"
  71. + plugin.get_name()
  72. + "' requested abnormal termination: "
  73. + reason)
  74. def subscribe_by_parents_name(parent_name, child, callback_name='callback'):
  75. child.get_plugin_loader().get_plugin(parent_name).subscribe(child, callback_name)
  76. # interfaces for subscription
  77. class ICode(object):
  78. pass
  79. def subscribe_by_parents_interface(interface, child, callback_name='callback'):
  80. for plugin in child.get_plugin_loader().iterate_plugins():
  81. if isinstance(plugin, interface):
  82. plugin.subscribe(child, callback_name)