PushService: Initial release
[enigma2-plugins.git] / pushservice / src / PluginBase.py
1 from Components.config import ConfigYesNo, NoSave
2
3 # Plugin base class
4 class PluginBase(object):
5         # You only have to overwrite the functions You need
6         # If You don't have to save something, You don't need getOptions / setOptions
7         
8         UniqueCounter = 0
9         
10         ForceSingleInstance = True
11         
12         def __init__(self):
13                 # Is called on instance creation
14                 
15                 # Default configuration
16                 self.enable = NoSave(ConfigYesNo( default = False ))
17                 
18                 PluginBase.UniqueCounter += 1
19                 self.uniqueid = PluginBase.UniqueCounter
20                 
21                 #self.activation = eTimer()
22                 
23                 self.options = {}
24                 # Build a list of key-value string tuples
25                 # [ (key, value, description, config element) , ]
26                 #self.options['enabled'] = ConfigYesNo( default = False )
27
28         ################################################
29         # Base class functions
30         def getName(self):
31                 # Return the Plugin Name
32                 return self.__class__.__name__
33
34         def getNameId(self):
35                 return self.getName() + " (" + str(self.getUniqueID()) + ")"
36
37         def getValue(self, key):
38                 if key in self.options:
39                         return self.options[key][0].value
40                 else:
41                         return None
42
43         def setValue(self, key, value):
44                 self.options[key][0].value = value
45
46         def getOption(self, key):
47                 if key in self.options:
48                         return self.options[key]
49                 else:
50                         return None
51
52         def setOption(self, key, option, description):
53                 self.options[key] = ( option, description )
54
55         def setOptions(self, options):
56                 # Parse a list of key-value string tuples
57                 # [ (key, value) , ]
58                 # If something is missing, the last/default value is used
59                 for key, value in options:
60                         default = self.getValue(key)
61                         if type(default) is str:
62                                 self.setValue(key, value)
63                         elif type(default) is bool:
64                                 self.setValue(key, eval(value))
65                         elif type(default) is int:
66                                 self.setValue(key, int(value))
67
68         def getUniqueID(self):
69                 return self.uniqueid
70
71         def getEnable(self):
72                 return self.enable.value
73
74         def setEnable(self, enable):
75                 self.enable.value = enable
76
77         def getConfigEnable(self):
78                 return self.enable
79
80         def getStringEnable(self):
81                 return str(self.enable.value)
82
83         def getStringOptions(self):
84                 return [ ( str(key), str(option.value), str(description) ) for ( key, ( option, description ) ) in self.options.items() ]
85
86         def getConfigOptions(self):
87                 return [ ( key, option, description) for ( key, ( option, description ) ) in self.options.items() ]
88
89         @classmethod
90         def getPluginClass(cls):
91                 # Return the Plugin Class
92                 return cls
93         
94         @classmethod
95         def forceSingle(cls):
96                 return cls.ForceSingleInstance
97
98         @staticmethod
99         def resetUniqueID():
100                 PluginBase.UniqueCounter = 0
101
102         ################################################
103         # Functions to be implemented in the plugin
104
105         def begin(self):
106                 # Is called after starting PushSerive
107                 pass
108
109         def run(self):
110                 # Return Header, Body, Attachment
111                 # If empty or none is returned, nothing will be sent
112                 return [], [], []
113
114         def end(self):
115                 # Is called after stopping PushSerive
116                 pass
117
118         # Callback functions
119         def success(self):
120                 # Called after successful sending the message
121                 pass
122
123         def error(self):
124                 # Called after message sent has failed
125                 pass