PushService: Simplified FreeSpace
[enigma2-plugins.git] / pushservice / src / ModuleBase.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 from Components.config import ConfigYesNo, NoSave
20
21 try:
22         #Python >= 2.7
23         from collections import OrderedDict
24 except:
25         from OrderedDict import OrderedDict
26
27
28 # Module base class
29 class ModuleBase(object):
30
31         def __init__(self):
32                 # Is called on instance creation
33                 
34                 # Default configuration
35                 self.enable = NoSave(ConfigYesNo( default = False ))
36                 
37                 self.options = OrderedDict()
38                 
39                 # Build a list of key-value string tuples
40                 # [ (key, value, description, config element) , ]
41                 #self.options['enabled'] = ConfigYesNo( default = False )
42
43
44         ################################################
45         # Base classmethod functions
46         @classmethod
47         def getClass(cls):
48                 # Return the Class
49                 return cls.__name__
50
51
52         ################################################
53         # Base functions
54         def getName(self):
55                 # Return the Class Name
56                 return self.__class__.__name__
57
58         def getNameId(self):
59                 return self.getName() + " (" + str(self.getUniqueID()) + ")"
60
61         def getValue(self, key):
62                 if key in self.options:
63                         return self.options[key][0].value
64                 else:
65                         return None
66
67         def setValue(self, key, value):
68                 self.options[key][0].value = value
69
70         def getOption(self, key):
71                 if key in self.options:
72                         return self.options[key]
73                 else:
74                         return None
75
76         def setOption(self, key, option, description):
77                 self.options[key] = ( option, description )
78
79         def setOptions(self, options):
80                 # Parse a list of key-value string tuples
81                 # [ (key, value) , ]
82                 # If something is missing, the last/default value is used
83                 for key, value in options:
84                         try:
85                                 default = self.getValue(key)
86                                 if type(default) is str:
87                                         self.setValue(key, value)
88                                 elif type(default) is bool:
89                                         self.setValue(key, eval(value))
90                                 elif type(default) is int:
91                                         self.setValue(key, int(value))
92                         except:
93                                 print _("[PushService] Module %s:\n") % ( self.getName() )
94                                 print _("Skipping config option:") + str(key) + " " + str(value)
95                                 continue
96
97         def getUniqueID(self):
98                 return self.uniqueid
99
100         def getEnable(self):
101                 return self.enable.value
102
103         def setEnable(self, enable):
104                 self.enable.value = enable
105
106         def getConfigEnable(self):
107                 return self.enable
108
109         def getStringEnable(self):
110                 return str(self.enable.value)
111
112         def getStringOptions(self):
113                 return [ ( str(key), str(option.value), str(description) ) for ( key, ( option, description ) ) in self.options.items() ]
114
115         def getConfigOptions(self):
116                 return [ ( key, option, description) for ( key, ( option, description ) ) in self.options.items() ]
117
118
119         ################################################
120         # Functions to be implemented in the plugin
121         def begin(self):
122                 # Is called after starting PushSerive
123                 pass
124
125         def end(self):
126                 # Is called after stopping PushSerive
127                 pass
128
129         def cancel(self):
130                 # Cancel activities
131                 pass