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