[WebIf]: Fix some bugs in timer handling
[enigma2-plugins.git] / pushservice / src / Modules.py
1 #######################################################################
2 #
3 #    Push Service for Enigma-2
4 #    Coded by betonme (c) 2012 <glaserfrank(at)gmail.com>
5 #    Support: http://www.i-have-a-dreambox.com/wbb2/thread.php?threadid=167779
6 #
7 #    This program is free software; you can redistribute it and/or
8 #    modify it under the terms of the GNU General Public License
9 #    as published by the Free Software Foundation; either version 2
10 #    of the License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #######################################################################
18
19 import os, sys, traceback
20
21 # Plugin framework
22 import imp, inspect
23
24 # Plugin internal
25 from . import _
26 from ModuleBase import ModuleBase
27 from ServiceBase import ServiceBase
28 from ControllerBase import ControllerBase
29
30
31 class Modules(object):
32
33         def __init__(self):
34                 pass
35
36         #######################################################
37         # Module functions
38         def loadModules(self, path, base):
39                 modules = {}
40                 
41                 if not os.path.exists(path):
42                         return
43                 
44                 # Import all subfolders to allow relative imports
45                 for root, dirs, files in os.walk(path):
46                         if root not in sys.path:
47                                 sys.path.append(root)
48                 
49                 # Import PushService modules
50                 files = [fname[:-3] for fname in os.listdir(path) if fname.endswith(".py")]
51                 for name in files:
52                         module = None
53                         
54                         if name == "__init__":
55                                 continue
56                         
57                         try:
58                                 fp, pathname, description = imp.find_module(name, [path])
59                         except Exception, e:
60                                 print _("[PushService] Find module exception: ") + str(e)
61                                 fp = None
62                         
63                         if not fp:
64                                 print _("[PushService] No module found: ") + str(name)
65                                 continue
66                         
67                         try:
68                                 module = imp.load_module( name, fp, pathname, description)
69                         except Exception, e:
70                                 print _("[PushService] Load exception: ") + str(e)
71                         finally:
72                                 # Since we may exit via an exception, close fp explicitly.
73                                 if fp: fp.close()
74                         
75                         if not module:
76                                 print _("[PushService] No module available: ") + str(name)
77                                 continue
78                         
79                         # Continue only if the attribute is available
80                         if not hasattr(module, name):
81                                 print _("[PushService] Warning attribute not available: ") + str(name)
82                                 continue
83                         
84                         # Continue only if attr is a class
85                         attr = getattr(module, name)
86                         if not inspect.isclass(attr):
87                                 print _("[PushService] Warning no class definition: ") + str(name)
88                                 continue
89                         
90                         # Continue only if the class is a subclass of the corresponding base class
91                         if not issubclass( attr, base):
92                                 print _("[PushService] Warning no subclass of base: ") + str(name)
93                                 continue
94                         
95                         # Add module to the module list
96                         modules[name] = attr
97                 return modules
98
99         def instantiateModule(self, module):
100                 if module and callable(module):
101                         # Create instance
102                         try:
103                                 return module()
104                         except Exception, e:
105                                 print _("[PushService] Instantiate exception: ") + str(module) + "\n" + str(e)
106                                 if sys.exc_info()[0]:
107                                         print _("Unexpected error: "), sys.exc_info()[0]
108                                         traceback.print_exc(file=sys.stdout)
109                 else:
110                         print _("[PushService] Module is not callable")
111                         return None