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