PushService: Initial release
[enigma2-plugins.git] / pushservice / src / PluginModules.py
1 # Plugin framework
2 import os, imp, sys, traceback
3
4 # Path
5 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
6
7 # Plugin internal
8 from . import _
9 from PluginBase import PluginBase
10
11
12 # Constants
13 PLUGIN_PATH = os.path.join( resolveFilename(SCOPE_PLUGINS), "Extensions/PushService/Plugins/" )
14 MODULE_PREFIX = 'PushService'
15
16
17 class PluginModules(object):
18
19         def __init__(self):
20                 self.modules = {}
21                 
22                 self.loadModules()
23
24         #######################################################
25         # Module functions
26         def loadModules(self, path = PLUGIN_PATH):
27                 self.modules = {}
28                 
29                 if not os.path.exists(path):
30                         return
31                 
32                 files = [fname[:-3] for fname in os.listdir(path) if fname.endswith(".py")]
33                 for name in files:
34                         module = None
35                         
36                         try:
37                                 fp, pathname, description = imp.find_module(name, [path])
38                         except Exception, e:
39                                 print _("[PushService] Find: ") + str(e)
40                                 fp = None
41                         
42                         if not fp:
43                                 print _("[PushService] Load: no module")
44                                 continue
45                         
46                         try:
47                                 # Use a prefix to avoid namespace conflicts
48                                 module = imp.load_module(MODULE_PREFIX+name, fp, pathname, description)
49                         except Exception, e:
50                                 print _("[PushService] Load: ") + str(e)
51                         finally:
52                                 # Since we may exit via an exception, close fp explicitly.
53                                 if fp:
54                                         fp.close()
55                         
56                         if not module:
57                                 continue
58                         
59                         # Instantiate only if the class is available
60                         if not hasattr(module, name):
61                                 print _("[PushService] Warning no class definition")
62                                 continue
63                         
64                         # Instantiate only if the class is a subclass of PluginBase
65                         if not issubclass( getattr(module, name), PluginBase):
66                                 print _("[PushService] Warning no subclass of PluginBase")
67                                 continue
68                         
69                         # Add module to the module list
70                         self.modules[name] = getattr(module, name)
71
72         def instantiatePlugin(self, name):
73                 plugin = self.modules.get(name)
74                 if plugin and callable(plugin):
75                         # Create plugin instance
76                         try:
77                                 return plugin()
78                         except Exception, e:
79                                 print _("[PushService] Instantiate: ") + name + "\n" + str(e)
80                                 if sys.exc_info()[0]:
81                                         print _("Unexpected error: "), sys.exc_info()[0]
82                                         traceback.print_exc(file=sys.stdout)
83                 else:
84                         print _("[PushService] Module is not callable")
85                         return None