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 sys 
 20  from inspect import isclass 
 21  import pkgutil 
 22  import timelinelib 
 23   
 24   
 25  EVENTBOX_DRAWER = "eventboxdrawer" 
 26  EXPORTER = "exporter" 
 27  TEXT_TRANSFORMER = "texttransformer" 
 28  VALID_SERVICES = [EVENTBOX_DRAWER, EXPORTER, TEXT_TRANSFORMER] 
 29   
 30   
31 -class PluginException(Exception):
32 pass
33 34
35 -class PluginFactory(object):
36
37 - def __init__(self):
38 self.plugins = {}
39
40 - def load_plugins(self):
41 candidates = self._get_candidate_modules() 42 class_names = [] 43 for candidate in candidates: 44 classes = [x for x in dir(candidate) if isclass(getattr(candidate, x))] 45 for cl in classes: 46 if cl not in class_names: 47 class_names.append(cl) 48 self._save_class_instance_for_plugins(candidate, cl)
49
50 - def get_plugins(self, service):
51 try: 52 return self.plugins[service] 53 except: 54 pass
55
56 - def get_plugin(self, service, name):
57 try: 58 return [plugin for plugin in self.get_plugins(service) if plugin.display_name() == _(name)][0] 59 except: 60 pass
61
62 - def _save_class_instance_for_plugins(self, candidate, cl):
63 class_ = getattr(candidate, cl) 64 try: 65 instance = class_() 66 try: 67 self._validate_plugin(instance) 68 self._save_plugin(instance) 69 except: 70 pass 71 except: 72 pass
73
74 - def _get_candidate_modules(self):
75 modules = self._find_modules("plugins") 76 return [self._import_module("timelinelib.plugin.%s" % mod) for mod in modules]
77
78 - def _find_modules(self, subdir):
79 name_offset = len('timelinelib.plugin.') 80 package = timelinelib 81 module_names = [] 82 for importer, modname, ispkg in pkgutil.walk_packages(path=package.__path__, 83 prefix=package.__name__+'.', 84 onerror=lambda x: None): 85 if modname.startswith('timelinelib.plugin.%s' % subdir) and not ispkg: 86 module_names.append(modname[name_offset:]) 87 return module_names
88
89 - def _import_module(self, module_name):
90 __import__(module_name) 91 return sys.modules[module_name]
92
93 - def _validate_plugin(self, instance):
94 self._get_plugin_method(instance, "isplugin") 95 self._get_plugin_method(instance, "service") 96 self._get_plugin_method(instance, "display_name") 97 if not instance.isplugin(): 98 print("NP") 99 raise PluginException() 100 if instance.service() not in VALID_SERVICES: 101 print("NVS") 102 raise PluginException()
103
104 - def _get_plugin_method(self, obj, method_name):
105 method = getattr(obj, method_name, None) 106 if not callable(method): 107 raise PluginException()
108
109 - def _save_plugin(self, instance):
110 if instance.service() in self.plugins.keys(): 111 self.plugins[instance.service()].append(instance) 112 else: 113 self.plugins[instance.service()] = [instance]
114