c959f5032a723938e0da410609a326be4f974abf
[vuplus_dvbapp-plugin] / autotimer / src / AutoTimerResource.py
1 from twisted.web import http, resource
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\" ?>
32                         <e2simplexmlresult>
33                                 <e2state>%s</e2state>
34                                 <e2statetext>%s</e2statetext>
35                         </e2simplexmlresult>
36                         """ % ('true' if res else 'false', output)
37         
38                 req.setResponseCode(http.OK)
39                 req.setHeader('Content-type', 'application; xhtml+xml')
40                 req.setHeader('charset', 'UTF-8')
41                 
42                 return result
43