plugin.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import imp
  4. class Plugin(object):
  5. """
  6. Base Class for Plugins.
  7. =======================
  8. All plugins must consist of a single class that inherits from this one.
  9. The class name must be equivalent to the module name.
  10. ie myplugin.py contains the myplugin class (lower case).
  11. Plugins must be installed in the plugins directory
  12. To enable a pugin add it to plugin.json as a key value pair, where
  13. the key is the plugin name and the value is the target.
  14. The target is page, where * is all pages. path/* is also permissible.
  15. Each class must
  16. 1. be capable of taking any set of arbitary key/value pairs via **kwargs
  17. (you are free to ignore/discard any of them)
  18. 2. produce as their sole ouput a dictionary through the output method,
  19. that must have a key called 'class_name'
  20. with the value self.__class__.__name__
  21. This dictionary will be passed back into the Page/Post etc object
  22. in a dictionary called plugins in as the value to a key that is
  23. the same as the module name (i.e. lower class) which will be
  24. added to its vars(self) where it can be used in jinja2 templates.
  25. It will be passed vars(self) as **kwargs
  26. """
  27. pass
  28. class Sampleplugin(Plugin):
  29. """Sample Plugin"""
  30. def __init__(self, **kwargs):
  31. """takes **kwargs"""
  32. self.page = "My name is " + kwargs['page']
  33. def output(self):
  34. """returns dict"""
  35. return {'class_name' : self.__class__.__name__, 'page_name' : self.page}
  36. def load_plugin_conf(plugin_file):
  37. """Get plugin conf from json file, return dict.
  38. JSON file should be name, applies to k/v array"""
  39. try:
  40. with open(plugin_file, 'r') as pfile:
  41. plugin_conf = json.load(pfile)
  42. except IOError:
  43. plugin_conf = {}
  44. return plugin_conf
  45. def load_plugins(plugin_path, plugin_conf):
  46. """Parse plugin conf and checks to see if plugin can be found, and if so
  47. load it.Returns a dict identical in format to
  48. plugin_conf (name, applies_to) with loaded modules"""
  49. plugins={}
  50. for name, applies_to in plugin_conf:
  51. filename, pathname, description = imp.find_module(name, plugin_path)
  52. try:
  53. imp.load_module(filename, pathname, description)
  54. plugins[name] = applies_to
  55. finally:
  56. if filepath:
  57. filepath.close()
  58. return plugins
  59. '''
  60. # importing with indirection using __import__
  61. # import class
  62. X = __import__('datetime')
  63. # assign class
  64. z = vars(X)['datetime']
  65. # or x = X.getattr(module, 'datetime')
  66. z.now()
  67. # with imp
  68. file, fpath, desc = imp.find_module(name[, path]) -- search for module
  69. imp.load_module(name, file, fpath, desc)
  70. '''