fix overwriting a variable
[vuplus_dvbapp-plugin] / autotimer / src / plugin.py
1 # for localized messages
2 from . import _
3
4 # GUI (Screens)
5 from Screens.MessageBox import MessageBox
6
7 # Config
8 from Components.config import config, ConfigSubsection, ConfigEnableDisable, \
9         ConfigNumber, ConfigSelection, ConfigYesNo
10
11 # Plugin
12 from Components.PluginComponent import plugins
13 from Plugins.Plugin import PluginDescriptor
14
15 # Initialize Configuration
16 config.plugins.autotimer = ConfigSubsection()
17 config.plugins.autotimer.autopoll = ConfigEnableDisable(default = False)
18 config.plugins.autotimer.interval = ConfigNumber(default = 3)
19 config.plugins.autotimer.refresh = ConfigSelection(choices = [
20                 ("none", _("None")),
21                 ("auto", _("Only AutoTimers created during this Session")),
22                 ("all", _("All non-repeating Timers"))
23         ], default = "none"
24 )
25 config.plugins.autotimer.try_guessing = ConfigEnableDisable(default = True)
26 config.plugins.autotimer.editor = ConfigSelection(choices = [
27                 ("plain", _("Classic")),
28                 ("wizard", _("Wizard"))
29         ], default = "wizard"
30 )
31 config.plugins.autotimer.disabled_on_conflict = ConfigEnableDisable(default = False)
32 config.plugins.autotimer.show_in_extensionsmenu = ConfigYesNo(default = False)
33
34 autotimer = None
35 autopoller = None
36
37 # Autostart
38 def autostart(reason, **kwargs):
39         global autotimer
40         global autopoller
41
42         # Startup
43         if config.plugins.autotimer.autopoll.value and reason == 0:
44                 # Initialize AutoTimer
45                 from AutoTimer import AutoTimer
46                 autotimer = AutoTimer()
47
48                 # Start Poller
49                 from AutoPoller import AutoPoller
50                 autopoller = AutoPoller()
51                 autopoller.start()
52         # Shutdown
53         elif reason == 1:
54                 # Stop Poller
55                 if autopoller is not None:
56                         autopoller.stop()
57                         autopoller = None
58
59                 if autotimer is not None:
60                         # We re-read the config so we won't save wrong information
61                         try:
62                                 autotimer.readXml()
63                         except:
64                                 pass
65
66                         # Save xml
67                         autotimer.writeXml()
68
69                         # Remove AutoTimer
70                         autotimer = None
71
72 # Mainfunction
73 def main(session, **kwargs):
74         global autotimer
75         global autopoller
76
77         if autotimer is None:
78                 from AutoTimer import AutoTimer
79                 autotimer = AutoTimer()
80
81         try:
82                 autotimer.readXml()
83         except SyntaxError, se:
84                 session.open(
85                         MessageBox,
86                         _("Your config file is not well-formed:\n%s") % (str(se)),
87                         type = MessageBox.TYPE_ERROR,
88                         timeout = 10
89                 )
90                 return
91
92         # Do not run in background while editing, this might screw things up
93         if autopoller is not None:
94                 autopoller.stop()
95
96         from AutoTimerOverview import AutoTimerOverview
97         session.openWithCallback(
98                 editCallback,
99                 AutoTimerOverview,
100                 autotimer
101         )
102
103 def editCallback(session):
104         global autotimer
105         global autopoller
106
107         # XXX: canceling of GUI (Overview) won't affect config values which might have been changed - is this intended?
108
109         # Don't parse EPG if editing was canceled
110         if session is not None:
111                 # Poll EPGCache
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
120                 # Save xml
121                 autotimer.writeXml()
122
123         # Start autopoller again if wanted
124         if config.plugins.autotimer.autopoll.value:
125                 if autopoller is None:
126                         from AutoPoller import AutoPoller
127                         autopoller = AutoPoller()
128                 autopoller.start(initial = False)
129         # Remove instance if not running in background
130         else:
131                 autopoller = None
132                 autotimer = None
133
134 # Movielist
135 def movielist(session, service, **kwargs):
136         from AutoTimerEditor import addAutotimerFromService
137         addAutotimerFromService(session, service)
138
139 # Event Info
140 def eventinfo(session, servicelist, **kwargs):
141         from AutoTimerEditor import AutoTimerEPGSelection
142         ref = session.nav.getCurrentlyPlayingServiceReference()
143         session.open(AutoTimerEPGSelection, ref)
144
145 # XXX: we need this helper function to identify the descriptor
146 # Extensions menu
147 def extensionsmenu(session, **kwargs):
148         main(session, **kwargs)
149
150 def housekeepingExtensionsmenu(el):
151         if el.value:
152                 plugins.addPlugin(extDescriptor)
153         else:
154                 plugins.removePlugin(extDescriptor)
155
156 config.plugins.autotimer.show_in_extensionsmenu.addNotifier(housekeepingExtensionsmenu, initial_call = False, immediate_feedback = False)
157 extDescriptor = PluginDescriptor(name="AutoTimer", description = _("Edit Timers and scan for new Events"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = extensionsmenu)
158
159 def Plugins(**kwargs):
160         l = [
161                 PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart),
162                 PluginDescriptor(name="AutoTimer", description = _("Edit Timers and scan for new Events"), where = PluginDescriptor.WHERE_PLUGINMENU, icon = "plugin.png", fnc = main),
163                 PluginDescriptor(name="AutoTimer", description= _("Add AutoTimer..."), where = PluginDescriptor.WHERE_MOVIELIST, fnc = movielist),
164                 PluginDescriptor(name="AutoTimer", description= _("Add AutoTimer..."), where = PluginDescriptor.WHERE_EVENTINFO, fnc = eventinfo),
165         ]
166         if config.plugins.autotimer.show_in_extensionsmenu.value:
167                 l.append(extDescriptor)
168         return l
169