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