PushService: Initial release
[enigma2-plugins.git] / pushservice / src / PushServiceBase.py
1 import os
2
3 # Config
4 from Components.config import *
5
6 # XML
7 from xml.etree.cElementTree import Element, SubElement, Comment
8 from Tools.XMLTools import stringToXML
9
10 # Plugin internal
11 from . import _
12 from PushMail import PushMail
13 from PluginModules import PluginModules
14 from PushServiceConfigFile import PushServiceConfigFile
15 from PluginBase import PluginBase
16
17
18 # Constants
19 PLUGIN = "Plugin"
20 OPTION = "Option"
21
22
23 class PushServiceBase(PushMail, PluginModules, PushServiceConfigFile):
24
25         def __init__(self, path=""):
26                 PushMail.__init__(self)
27                 PluginModules.__init__(self)
28                 PushServiceConfigFile.__init__(self)
29
30
31         ######################################
32         # Plugin handling
33         def begin(self, plugins):
34                 # Loop over all Plugins
35                 if plugins:
36                         for plugin in self.plugins:
37                                 if plugin.getEnable():
38                                         plugin.begin()
39
40         def end(self, plugins):
41                 # Loop over all Plugins
42                 if plugins:
43                         for plugin in self.plugins:
44                                 if plugin.getEnable():
45                                         plugin.end()
46
47         def run(self, plugins, send=True):
48                 response = ""
49                 print _("PushService started: ") + strftime( _("%d.%m.%Y %H:%M"), localtime() )
50                 
51                 # Loop over all Plugins
52                 if plugins:
53                         for plugin in plugins:
54                                 if plugin.getEnable():
55                                         print _("PushService running: ") + str( plugin.getName() )
56                                         subject, text, attachments = "", "", []
57                                         
58                                         try:
59                                                 # Run plugin
60                                                 ret = plugin.run()
61                                         except Exception, e:
62                                                 print _("PushService run exception ") + str(e)
63                                                 if sys.exc_info()[0]:
64                                                         print _("Unexpected error: "), sys.exc_info()[0]
65                                                         traceback.print_exc(file=sys.stdout)
66                                                 ret = None
67                                         
68                                         # Parse return value(s)
69                                         if ret:
70                                                 if len(ret) == 3:
71                                                         subject, text, attachments = ret
72                                                 elif len(ret) == 2:
73                                                         # No attachment given
74                                                         subject, text = ret
75                                                         attachments = []
76                                                 else:
77                                                         # Header and Body will contain the same
78                                                         subject = text = ret
79                                                         attachments = []
80                                                 
81                                                 # Prepare resonse text
82                                                 if subject:     response += "[ " + subject + " ]\n\n"
83                                                 if text:                response += text + "\n\n\n"
84                                                 
85                                                 if send and subject:
86                                                         # Push mail
87                                                         self.push(subject, text, attachments, plugin.success, plugin.error)
88                 return response or "Nothing to send"
89
90
91         ######################################
92         # Config
93         def load(self):
94                 path = config.pushservice.xmlpath.value
95                 # Read xml config file
96                 root = self.readXML( path )
97                 if root:
98                         # Reset the unique id counter
99                         PluginBase.resetUniqueID()
100                         return self.parseConfig( root )
101                 else:
102                         return []
103
104         def save(self, plugins):
105                 path = config.pushservice.xmlpath.value
106                 root = self.buildConfig( plugins )
107                 self.writeXML( root, path )
108
109
110         ######################################
111         # Internal
112         def parseConfig(self, root):
113                 plugins = []
114                 
115                 #if version != CURRENT_CONFIG_VERSION:
116                 #       parseConfigOld(configuration, list, uniqueTimerId)
117                 #       return
118                 
119                 if root:
120                         from xml.etree.cElementTree import tostring
121                         for element in root.findall(PLUGIN):
122                                 name = element.get("name", "")
123                                 enable = element.get("enable", "True")
124                                 if name:
125                                         plugin = self.instantiatePlugin(name)
126                                         if plugin:
127                                                 plugin.setEnable(eval(enable))
128                                                 
129                                                 # Set plugin options
130                                                 options = []
131                                                 for option in element.findall(OPTION):
132                                                         key = option.get("key", "")
133                                                         value = option.text
134                                                         if key and value:
135                                                                 options.append((key, value))
136                                                 
137                                                 if options:
138                                                         plugin.setOptions(options)
139                                                 
140                                                 print "PLUGIN APPEND"
141                                                 
142                                                 # Append to active plugin list
143                                                 plugins.append(plugin)
144                 return plugins
145
146         def buildConfig(self, plugins):
147                 # Generate List in RAM
148                 from plugin import NAME, VERSION
149                 
150                 # Header
151                 root = Element('PushService')
152                 root.set('version', VERSION)
153                 root.append(Comment(_("Don't edit this manually unless you really know what you are doing")))
154                 
155                 # Body
156                 if plugins:
157                         for plugin in plugins:
158                                 # Add plugin
159                                 element = SubElement(root, PLUGIN, name=stringToXML(plugin.getName()), enable=stringToXML(plugin.getStringEnable()))
160                                 
161                                 # Add options
162                                 options = plugin.getStringOptions()
163                                 if options:
164                                         for key, value, description in options:
165                                                 SubElement( element, OPTION, key = stringToXML(key) ).text = stringToXML(value)
166                 
167                 return root