baangt-Plugin.rst 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. What is a baangt-plugin
  2. =======================
  3. Simply speaking, one baangt-plugin correspond to one class, and the
  4. methods in the class correspond to the implements in the plugin.
  5. how to make a baangt-plugin
  6. ===========================
  7. first of all , we need to create a implement class, like this:
  8. ::
  9. import baangt
  10. from baangt.base.Timing.Timing import Timing
  11. class TimingHookImpl:
  12. @baangt.hook_impl
  13. def timing_init(self):
  14. return Timing()
  15. @baangt.hook_impl
  16. def timing_takeTime(self, timingObject, timingName, forceNew=False):
  17. return timingObject.takeTime(timingName, forceNew)
  18. @baangt.hook_impl
  19. def timing_addAttribute(self, timingObject, attribute, value, timingSection=None):
  20. return timingObject.addAttribute(attribute, value, timingSection)
  21. @baangt.hook_impl
  22. def timing_takeTimeSumOutput(self, timingObject):
  23. return timingObject.takeTimeSumOutput()
  24. @baangt.hook_impl
  25. def timing_returnTime(self, timingObject):
  26. return timingObject.returnTime()
  27. @baangt.hook_impl
  28. def timing_returnTimeSegment(self, timingObject, segment):
  29. return timingObject.returnTimeSegment(segment)
  30. @baangt.hook_impl
  31. def timing_resetTime(self, timingObject):
  32. return timingObject.resetTime()
  33. and then register this implement class in /baangt/\_*init\_*.py:
  34. ::
  35. from baangt.base.Timing.hookImpls import TimingHookImpl
  36. plugin_manager.register(plugin=TimingHookImpl())
  37. how the baangt-plugin work
  38. ==========================
  39. for example, after transfer TestRun to a plugin, we can replace the
  40. code:
  41. ::
  42. from xxx import TestRun
  43. TestRun()
  44. by
  45. ::
  46. from xxx import plugin_manager
  47. plugin_manager.hook.testRun_init()
  48. this replacement does not change anything of the result of programme's
  49. execution.
  50. how to replace the existing plugin by your own one
  51. ==================================================
  52. for example, if you want to replace the default TestRun plugin,
  53. you can easily change the implement of TestRun by just unregister the
  54. default plugin and register your own one:
  55. ::
  56. plugin_manager.unregister(plugin=default_plugin)
  57. plugin_manager.register(plugin=my_plugin)
  58. notice that if you don't unregister the old one, two same implements
  59. (with same function name) in two plugins may both execute if you call
  60. the function:
  61. ::
  62. plugin_manager.hook.i_got_two_implements()
  63. the order of the execution follows the FILO (first-in-last-out) rule.
  64. Author: Yuyi Shao