/apps/wiki/yaki/plugins.py

https://github.com/myles/comfy · Python · 84 lines · 59 code · 7 blank · 18 comment · 18 complexity · 9430dcee587e4a0b9e569ab299d7bab1 MD5 · raw file

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. Plugins.py
  5. Plugin registration and invocation
  6. Created by Rui Carmo on 2006-11-12.
  7. Published under the MIT license.
  8. """
  9. import os, re
  10. from django.conf import settings
  11. from comfy.apps.wiki.settings import YAKI_PLUGINS_DIR
  12. from comfy.apps.wiki.yaki.utils import *
  13. class PluginRegistry:
  14. plugins = {'markup': {}}
  15. serial = 0
  16. def __init__(self):
  17. print "Loading Wiki plugins..."
  18. # Get plugin directory
  19. plugindir = os.path.join(settings.PROJECT_ROOT, YAKI_PLUGINS_DIR)
  20. for f in locate('*.py', plugindir):
  21. relpath = f.replace(settings.PROJECT_ROOT + '/', '')
  22. (modname, ext) = rsplit(relpath, '.', 1)
  23. modname = '.'.join(modname.split('/'))
  24. try:
  25. _module = __import__(modname, globals(), locals(), [''])
  26. # Load each python file
  27. for x in dir(_module):
  28. if 'WikiPlugin' in x:
  29. _class = getattr(_module, x)
  30. _class() # plugins will register themselves
  31. except ImportError:
  32. pass
  33. def register(self, category, instance, tag, name):
  34. print "Plugin %s registered in category %s for tag %s" % (name,category,tag)
  35. if tag not in self.plugins[category].keys():
  36. self.plugins[category][tag] = {}
  37. self.plugins[category][tag][name.lower()] = instance
  38. def runForAllTags(self, pagename, soup, request=None, response=None):
  39. """Runs all markup plugins that process specific tags (except the plugin one)"""
  40. for tagname in self.plugins['markup'].keys():
  41. if tagname != 'plugin':
  42. for i in self.plugins['markup'][tagname]:
  43. plugin = self.plugins['markup'][tagname][i]
  44. # Go through all tags in document
  45. for tag in soup(tagname):
  46. result = plugin.run(self.serial, tag, tagname, pagename, soup, request, response)
  47. self.serial = self.serial + 1
  48. if result == True:
  49. continue
  50. def run(self, tag, tagname, pagename=None, soup=None, request=None, response=None):
  51. if tagname == 'plugin':
  52. try:
  53. name = tag['name'].lower() # get the attribute
  54. except KeyError:
  55. return
  56. if name in self.plugins['markup']['plugin']:
  57. plugin = self.plugins['markup']['plugin'][name]
  58. result = plugin.run(self.serial, tag, tagname, pagename, soup, request, response)
  59. self.serial = self.serial + 1
  60. # ignore the result for plugin tags
  61. elif tagname in self.plugins['markup']:
  62. for i in self.plugins['markup'][tagname]:
  63. plugin = self.plugins['markup'][tagname][i]
  64. result = plugin.run(self.serial, tag, tagname, pagename, soup, request, response)
  65. self.serial = self.serial + 1
  66. # if plugin returns False, then the tag does not need to be processed any further
  67. if result == False:
  68. return
  69. class WikiPlugin:
  70. """Base class for all Wiki plugins"""
  71. def __init__(self, registry, webapp):
  72. # Register this (override in child classes)
  73. registry.register('markup', self, 'plugin', 'base')
  74. def run(self, serial, tag, tagname, pagename, soup, request=None, response=None):
  75. pass