Package Gnumed :: Package timelinelib :: Package plugin :: Module factory' |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.plugin.factory'

      1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  import os
     20  import sys
     21  from inspect import isclass
     22
     23
     24  EVENTBOX_DRAWER = "eventboxdrawer"
     25  EXPORTER = "exporter"
     26  TEXT_TRANSFORMER = "texttransformer"
     27  VALID_SERVICES = [EVENTBOX_DRAWER, EXPORTER, TEXT_TRANSFORMER]
     28
     29
     30 -class PluginException(Exception):
     31      pass
     32
     33
     34 -class PluginFactory(object):
     35
     36 -    def __init__(self):
     37          self.plugins = {}
     38
     39 -    def load_plugins(self):
     40          candidates = self._get_candidate_modules()
     41          class_names = []
     42          for candidate in candidates:
     43              classes = [x for x in dir(candidate) if isclass(getattr(candidate, x))]
     44              for cl in classes:
     45                  if cl not in class_names:
     46                      class_names.append(cl)
     47                      self._save_class_instance_for_plugins(candidate, cl)
     48
     49 -    def get_plugins(self, service):
     50          try:
     51              return self.plugins[service]
     52          except:
     53              pass
     54
     55 -    def get_plugin(self, service, name):
     56          try:
     57              return [plugin for plugin in self.get_plugins(service) if plugin.display_name() == _(name)][0]
     58          except:
     59              pass
     60
     61 -    def _save_class_instance_for_plugins(self, candidate, cl):
     62          class_ = getattr(candidate, cl)
     63          try:
     64              instance = class_()
     65              try:
     66                  self._validate_plugin(instance)
     67                  self._save_plugin(instance)
     68              except:
     69                  pass
     70          except:
     71              pass
     72
     73 -    def _get_candidate_modules(self):
     74          modules = self._find_modules("plugins")
     75          return [self._import_module("timelinelib.plugin.%s" % mod) for mod in modules]
     76
     77 -    def _find_modules(self, subdir):
     78          modules = []
     79          for module_file in os.listdir(os.path.join(os.path.dirname(__file__), subdir)):
     80              if os.path.isdir(os.path.join(os.path.dirname(__file__), subdir, module_file)):
     81                  modules.extend(self._find_modules(os.path.join(subdir, module_file)))
     82              elif module_file.endswith(".py") and module_file != "__init__.py":
     83                  module_name = os.path.basename(module_file)[:-3]
     84                  abs_module_name = "%s.%s" % (subdir.replace(os.sep, "."), module_name)
     85                  modules.append(abs_module_name)
     86          return modules
     87
     88 -    def _import_module(self, module_name):
     89          __import__(module_name)
     90          return sys.modules[module_name]
     91
     92 -    def _validate_plugin(self, instance):
     93          self._get_plugin_method(instance, "isplugin")
     94          self._get_plugin_method(instance, "service")
     95          self._get_plugin_method(instance, "display_name")
     96          if not instance.isplugin():
     97              print("NP")
     98              raise PluginException()
     99          if instance.service() not in VALID_SERVICES:
    100              print("NVS")
    101              raise PluginException()
    102
    103 -    def _get_plugin_method(self, obj, method_name):
    104          method = getattr(obj, method_name, None)
    105          if not callable(method):
    106              raise PluginException()
    107
    108 -    def _save_plugin(self, instance):
    109          if instance.service() in self.plugins.keys():
    110              self.plugins[instance.service()].append(instance)
    111          else:
    112              self.plugins[instance.service()] = [instance]
    113