- add function to add autotimer based on an event,
[vuplus_dvbapp-plugin] / autotimer / src / plugin.py
1 # GUI (Screens)
2 from Screens.MessageBox import MessageBox
3
4 # Plugin definition
5 from Plugins.Plugin import PluginDescriptor
6
7 # ExpatError
8 from xml.parsers.expat import ExpatError
9
10 # Config
11 from Components.config import config, ConfigSubsection, ConfigEnableDisable, \
12         ConfigInteger, ConfigSelection
13
14 # Initialize Configuration
15 config.plugins.autotimer = ConfigSubsection()
16 config.plugins.autotimer.autopoll = ConfigEnableDisable(default = False)
17 config.plugins.autotimer.interval = ConfigInteger(default = 3, limits=(1, 24))
18 config.plugins.autotimer.refresh = ConfigSelection(choices = [("none", _("None")), ("auto", _("Only AutoTimers created during this Session")), ("all", _("All non-repeating Timers"))], default = "none")
19 config.plugins.autotimer.try_guessing = ConfigEnableDisable(default = True)
20
21 autotimer = None
22 autopoller = None
23
24 # Autostart
25 def autostart(reason, **kwargs):
26         global autotimer
27         global autopoller
28
29         # Startup
30         if config.plugins.autotimer.autopoll.value and reason == 0:
31                 # Initialize AutoTimer
32                 from AutoTimer import AutoTimer
33                 autotimer = AutoTimer()
34
35                 # Start Poller
36                 from AutoPoller import AutoPoller
37                 autopoller = AutoPoller()
38                 autopoller.start()
39         # Shutdown
40         elif reason == 1:
41                 # Stop Poller
42                 if autopoller is not None:
43                         autopoller.stop()
44
45                         autopoller = None
46
47                 if autotimer is not None:
48                         # Save xml
49                         autotimer.writeXml()
50
51                         # Remove AutoTimer
52                         autotimer = None
53
54 # Mainfunction
55 def main(session, **kwargs):
56         global autotimer
57         global autopoller
58
59         if autotimer is None:
60                 from AutoTimer import AutoTimer
61                 autotimer = AutoTimer()
62
63         try:
64                 autotimer.readXml()
65         except ExpatError, ee:
66                 session.open(
67                         MessageBox,
68                         "Your config file is not well-formed.\nError parsing in line: %s" % (ee.lineno),
69                         type = MessageBox.TYPE_ERROR,
70                         timeout = 10
71                 )
72                 return
73         except Exception, e:
74                 # Don't crash during development
75                 import traceback, sys
76                 traceback.print_exc(file=sys.stdout)
77                 session.open(
78                         MessageBox,
79                         "An unexpected error occured: %s" % (e),
80                         type = MessageBox.TYPE_ERROR,
81                         timeout = 15
82                 )
83                 return
84
85         # Do not run in background while editing, this might screw things up
86         if autopoller is not None:
87                 autopoller.stop()
88
89         from AutoTimerOverview import AutoTimerOverview
90         session.openWithCallback(
91                 editCallback,
92                 AutoTimerOverview,
93                 autotimer
94         )
95
96 def editCallback(session):
97         global autotimer
98         global autopoller
99
100         # Start autopoller again if wanted
101         if config.plugins.autotimer.autopoll.value:
102                 if autopoller is None:
103                         from AutoPoller import AutoPoller
104                         autopoller = AutoPoller()
105                 autopoller.start(initial = False)
106
107         # Don't do anything when editing was canceled
108         if session is None:
109                 return
110
111         # We might re-parse Xml so catch parsing error
112         try:
113                 ret = autotimer.parseEPG()
114                 session.open(
115                         MessageBox,
116                         "Found a total of %d matching Events.\n%d Timer were added and %d modified.." % (ret[0], ret[1], ret[2]),
117                         type = MessageBox.TYPE_INFO,
118                         timeout = 10
119                 )
120         except Exception, e:
121                 # Don't crash during development
122                 import traceback, sys
123                 traceback.print_exc(file=sys.stdout)
124                 session.open(
125                         MessageBox,
126                         "An unexpected error occured: %s" % (e),
127                         type = MessageBox.TYPE_ERROR,
128                         timeout = 15
129                 )
130
131         # Remove instance if not running in background
132         if not config.plugins.autotimer.autopoll.value:
133                 autopoller = None
134
135                 # Save xml
136                 autotimer.writeXml()
137                 autotimer = None
138
139 def Plugins(**kwargs):
140         return [
141                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart),
142                 PluginDescriptor(name="AutoTimer", description = "Edit Timers and scan for new Events", where = PluginDescriptor.WHERE_PLUGINMENU, icon = "plugin.png", fnc = main),
143                 PluginDescriptor(name="AutoTimer", description = "Edit Timers and scan for new Events", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main)
144         ]