return e2simplexmlresult rather than plain text and save autotimer.xml before removin...
[enigma2-plugins.git] / autotimer / src / AutoTimerResource.py
1 from twisted.web2 import http, http_headers, resource, responsecode
2 from AutoTimer import AutoTimer
3 from . import _
4
5 # pretty basic resource which is just present to have a way to start a
6 # forced run through the webif
7 class AutoTimerResource(resource.Resource):
8         def __init__(self):
9                 resource.Resource.__init__(self)
10
11         def render(self, req):
12                 from plugin import autotimer
13
14                 remove = False
15                 res = False
16                 if autotimer is None:
17                         autotimer = AutoTimer()
18                         remove = True
19
20                 if req.args.has_key("parse"):
21                         ret = autotimer.parseEPG()
22                         output = _("Found a total of %d matching Events.\n%d Timer were added and %d modified.") % (ret[0], ret[1], ret[2])
23                         res = True
24                 else:
25                         output = "unknown command"
26
27                 if remove:
28                         autotimer.writeXml()
29                         autotimer = None
30
31                 result = """<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n
32                         <e2simplexmlresult>\n
33                                 <e2state>%s</e2state>\n
34                                 <e2statetext>%s</e2statetext>\n
35                         </e2simplexmlresult>
36                         """ % ('true' if res else 'false', output)
37
38                 XML_HEADER = {'Content-type': http_headers.MimeType('application', 'xhtml+xml', (('charset', 'UTF-8'),))}
39                 return http.Response(responsecode.OK, XML_HEADER, stream = result)
40