3 #from time import datetime
4 from Tools import Directories, Notifications
6 from Components.config import config
10 from Screens.MessageBox import MessageBox
11 from Screens.SubserviceSelection import SubserviceSelection
12 import NavigationInstance
14 from Tools.XMLTools import elementsWithTag, mergeText
15 from ServiceReference import ServiceReference
17 # ok, for descriptions etc we have:
18 # service reference (to get the service name)
20 # description (description)
21 # event data (ONLY for time adjustments etc.)
24 # parses an event, and gives out a (begin, end, name, duration, eit)-tuple.
25 # begin and end will be corrected
27 name = ev.getEventName()
28 description = ev.getShortDescription()
29 begin = ev.getBeginTime()
30 end = begin + ev.getDuration()
32 begin -= config.recording.margin_before.value[0] * 60
33 end += config.recording.margin_after.value[0] * 60
34 return (begin, end, name, description, eit)
36 # please do not translate log messages
37 class RecordTimerEntry(timer.TimerEntry):
38 def __init__(self, serviceref, begin, end, name, description, eit, disabled = False):
39 timer.TimerEntry.__init__(self, int(begin), int(end))
41 assert isinstance(serviceref, ServiceReference)
43 self.service_ref = serviceref
47 self.description = description
48 self.disabled = disabled
50 self.record_service = None
51 self.start_prepare = 0
56 def log(self, code, msg):
57 self.log_entries.append((int(time.time()), code, msg))
61 self.state = self.StateWaiting
62 self.first_try_prepare = True
65 def calculateFilename(self):
66 service_name = self.service_ref.getServiceName()
67 begin_date = time.strftime("%Y%m%d %H%M", time.localtime(self.begin))
69 print "begin_date: ", begin_date
70 print "service_name: ", service_name
71 print "name:", self.name
72 print "description: ", self.description
74 filename = begin_date + " - " + service_name
76 filename += " - " + self.name
78 self.Filename = Directories.getRecordingFilename(filename)
79 self.log(0, "Filename calculated as: '%s'" % self.Filename)
80 #begin_date + " - " + service_name + description)
83 self.calculateFilename()
84 self.record_service = NavigationInstance.instance.recordService(self.service_ref)
85 if self.record_service == None:
86 self.log(1, "'record service' failed")
89 prep_res = self.record_service.prepare(self.Filename + ".ts")
91 self.log(2, "'prepare' failed: error %d" % prep_res)
92 self.record_service = None
95 self.log(3, "prepare ok, writing meta information to %s" % self.Filename)
97 f = open(self.Filename + ".ts.meta", "w")
98 f.write(str(self.service_ref) + "\n")
99 f.write(self.name + "\n")
100 f.write(self.description + "\n")
101 f.write(str(self.begin) + "\n")
104 self.log(4, "failed to write meta information")
107 def do_backoff(self):
108 if self.backoff == 0:
112 if self.backoff > 100:
114 self.log(10, "backoff: retry in %d seconds" % self.backoff)
117 next_state = self.state + 1
118 self.log(5, "activating state %d" % next_state)
120 if next_state == self.StatePrepared:
121 if self.tryPrepare():
122 self.log(6, "prepare ok, waiting for begin")
123 # fine. it worked, resources are allocated.
124 self.next_activation = self.begin
128 self.log(7, "prepare failed")
129 if self.first_try_prepare:
130 self.first_try_prepare = False
131 if config.recording.asktozap.value == 0:
132 self.log(8, "asking user to zap away")
133 Notifications.AddNotificationWithCallback(self.failureCB, MessageBox, _("A timer failed to record!\nDisable TV and try again?\n"))
134 else: # zap without asking
135 self.log(9, "zap without asking")
140 self.start_prepare = time.time() + self.backoff
142 elif next_state == self.StateRunning:
143 self.log(11, "start recording")
144 record_res = self.record_service.start()
147 self.log(13, "start record returned %d" % record_res)
150 self.begin = time.time() + self.backoff
154 elif next_state == self.StateEnded:
155 self.log(12, "stop recording")
156 self.record_service.stop()
157 self.record_service = None
160 def getNextActivation(self):
161 if self.state == self.StateEnded:
164 next_state = self.state + 1
166 return {self.StatePrepared: self.start_prepare,
167 self.StateRunning: self.begin,
168 self.StateEnded: self.end }[next_state]
170 def failureCB(self, answer):
172 self.log(13, "ok, zapped away")
173 #NavigationInstance.instance.stopUserServices()
174 NavigationInstance.instance.playService(self.service_ref.ref)
176 self.log(14, "user didn't want to zap away, record will probably fail")
178 def timeChanged(self):
179 old_prepare = self.start_prepare
180 self.start_prepare = self.begin - self.prepare_time
183 if old_prepare != self.start_prepare:
184 self.log(15, "record time changed, start prepare is now: %s" % time.ctime(self.start_prepare))
186 def createTimer(xml):
187 begin = int(xml.getAttribute("begin"))
188 end = int(xml.getAttribute("end"))
189 serviceref = ServiceReference(str(xml.getAttribute("serviceref")))
190 description = xml.getAttribute("description").encode("utf-8")
191 repeated = xml.getAttribute("repeated").encode("utf-8")
193 disabled = eval(xml.getAttribute("disabled"))
197 eit = long(xml.getAttribute("eit").encode("utf-8"))
200 name = xml.getAttribute("name").encode("utf-8")
201 #filename = xml.getAttribute("filename").encode("utf-8")
202 entry = RecordTimerEntry(serviceref, begin, end, name, description, eit, disabled)
203 entry.repeated = int(repeated)
205 for l in elementsWithTag(xml.childNodes, "log"):
206 time = int(l.getAttribute("time"))
207 code = int(l.getAttribute("code"))
208 msg = mergeText(l.childNodes).strip()
209 entry.log_entries.append((time, code, msg))
213 class RecordTimer(timer.Timer):
215 timer.Timer.__init__(self)
217 self.Filename = Directories.resolveFilename(Directories.SCOPE_CONFIG, "timers.xml")
222 print "unable to load timers from file!"
224 def isRecording(self):
226 for timer in self.timer_list:
227 if timer.isRunning():
233 doc = xml.dom.minidom.parse(self.Filename)
235 root = doc.childNodes[0]
236 for timer in elementsWithTag(root.childNodes, "timer"):
237 self.record(createTimer(timer))
239 def strToXML(self, str):
240 return str.replace('&', '&').replace('<', '<').replace('>', '>').replace("'", '''). replace('"', '&qout;')
243 #doc = xml.dom.minidom.Document()
244 #root_element = doc.createElement('timers')
245 #doc.appendChild(root_element)
246 #root_element.appendChild(doc.createTextNode("\n"))
248 #for timer in self.timer_list + self.processed_timers:
249 # some timers (instant records) don't want to be saved.
253 #t = doc.createTextNode("\t")
254 #root_element.appendChild(t)
255 #t = doc.createElement('timer')
256 #t.setAttribute("begin", str(int(timer.begin)))
257 #t.setAttribute("end", str(int(timer.end)))
258 #t.setAttribute("serviceref", str(timer.service_ref))
259 #t.setAttribute("repeated", str(timer.repeated))
260 #t.setAttribute("name", timer.name)
261 #t.setAttribute("description", timer.description)
262 #t.setAttribute("eit", str(timer.eit))
264 #for time, code, msg in timer.log_entries:
265 #t.appendChild(doc.createTextNode("\t\t"))
266 #l = doc.createElement('log')
267 #l.setAttribute("time", str(time))
268 #l.setAttribute("code", str(code))
269 #l.appendChild(doc.createTextNode(msg))
271 #t.appendChild(doc.createTextNode("\n"))
273 #root_element.appendChild(t)
274 #t = doc.createTextNode("\n")
275 #root_element.appendChild(t)
278 #file = open(self.Filename, "w")
285 list.append('<?xml version="1.0" ?>\n')
286 list.append('<timers>\n')
288 for timer in self.timer_list + self.processed_timers:
292 list.append('<timer')
293 list.append(' begin="' + str(int(timer.begin)) + '"')
294 list.append(' end="' + str(int(timer.end)) + '"')
295 list.append(' serviceref="' + str(timer.service_ref) + '"')
296 list.append(' repeated="' + str(int(timer.repeated)) + '"')
297 list.append(' name="' + str(self.strToXML(timer.name)) + '"')
298 list.append(' description="' + str(self.strToXML(timer.description)) + '"')
299 list.append(' eit="' + str(timer.eit) + '"')
300 list.append(' disabled="' + str(int(timer.disabled)) + '"')
303 for time, code, msg in timer.log_entries:
305 list.append(' code="' + str(code) + '"')
306 list.append(' time="' + str(time) + '"')
308 list.append(str(msg))
309 list.append('</log>\n')
312 list.append('</timer>\n')
314 list.append('</timers>\n')
316 file = open(self.Filename, "w")
321 def record(self, entry):
323 print "[Timer] Record " + str(entry)
325 self.addTimerEntry(entry)
327 def removeEntry(self, entry):
328 print "[Timer] Remove " + str(entry)
331 entry.repeated = False
334 # this sets the end time to current time, so timer will be stopped.
337 if entry.state != entry.StateEnded:
338 self.timeChanged(entry)
340 print "state: ", entry.state
341 print "in processed: ", entry in self.processed_timers
342 print "in running: ", entry in self.timer_list
343 # now the timer should be in the processed_timers list. remove it from there.
344 self.processed_timers.remove(entry)