api.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.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. self.is_updated = False
  22. db_loader = self.get_plugin_loader().get_database_loader()
  23. prev_version = db_loader.set_property(self.get_name() + ":version", self.get_version())
  24. if prev_version != self.get_version():
  25. self.is_updated = True
  26. def terminate(self):
  27. pass
  28. def set_name(self, name):
  29. self.name = name
  30. def get_name(self):
  31. if hasattr(self, 'name') == False:
  32. return None
  33. return self.name
  34. def set_version(self, version):
  35. self.version = version
  36. def get_version(self):
  37. if hasattr(self, 'version') == False:
  38. return None
  39. return self.version
  40. def set_plugin_loader(self, loader):
  41. self.plugin_loader = loader
  42. def get_plugin_loader(self):
  43. if hasattr(self, 'plugin_loader') == False:
  44. return None
  45. return self.plugin_loader
  46. class InterfaceNotImplemented(Exception):
  47. def __init__(self, obj):
  48. import sys
  49. Exception.__init__(self, "Method '"
  50. + sys._getframe(1).f_code.co_name
  51. + "' has not been implemented for "
  52. + str(obj.__class__))
  53. class IConfigurable(object):
  54. def configure(self, options):
  55. raise InterfaceNotImplemented(self)
  56. def declare_configuration(self, optparser):
  57. raise InterfaceNotImplemented(self)
  58. class IRunable(object):
  59. def run(self, args):
  60. raise InterfaceNotImplemented(self)
  61. class IParser(object):
  62. def process(self, parent, data, is_updated):
  63. raise InterfaceNotImplemented(self)
  64. class CallbackNotImplemented(Exception):
  65. def __init__(self, obj, callback_name):
  66. Exception.__init__(self, "Callback '"
  67. + callback_name
  68. + "' has not been implemented for "
  69. + str(obj.__class__))
  70. class Child(object):
  71. def notify(self, parent, callback_name, *args):
  72. if hasattr(self, callback_name) == False:
  73. raise CallbackNotImplemented(self, callback_name)
  74. self.__getattribute__(callback_name)(parent, *args)
  75. class Parent(object):
  76. def init_Parent(self):
  77. if hasattr(self, 'children') == False:
  78. self.children = []
  79. def subscribe(self, obj, callback_name):
  80. self.init_Parent()
  81. if (isinstance(obj, Child) == False):
  82. raise TypeError()
  83. self.children.append((obj,callback_name))
  84. def unsubscribe(self, obj, callback_name):
  85. self.init_Parent()
  86. self.children.remove((obj, callback_name))
  87. def notify_children(self, *args):
  88. self.init_Parent()
  89. for child in self.children:
  90. child[0].notify(self, child[1], *args)
  91. def iterate_children(self):
  92. self.init_Parent()
  93. for child in self.children:
  94. yield child
  95. class ExitError(Exception):
  96. def __init__(self, plugin, reason):
  97. if plugin != None:
  98. Exception.__init__(self, "Plugin '"
  99. + plugin.get_name()
  100. + "' requested abnormal termination: "
  101. + reason)
  102. else:
  103. Exception.__init__(self, "'Abnormal termination requested: "
  104. + reason)
  105. def subscribe_by_parents_name(parent_name, child, callback_name='callback'):
  106. child.get_plugin_loader().get_plugin(parent_name).subscribe(child, callback_name)
  107. # interfaces for subscription
  108. class ICode(object):
  109. pass
  110. def subscribe_by_parents_interface(interface, child, callback_name='callback'):
  111. for plugin in child.get_plugin_loader().iterate_plugins():
  112. if isinstance(plugin, interface):
  113. plugin.subscribe(child, callback_name)
  114. class ITool(object):
  115. def run(self, tool_args):
  116. raise InterfaceNotImplemented(self)