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.
26 name = ev.getEventName()
27 description = ev.getShortDescription()
28 begin = ev.getBeginTime()
29 end = begin + ev.getDuration()
31 return (begin, end, name, description, eit)
33 # please do not translate log messages
34 class RecordTimerEntry(timer.TimerEntry):
35 def __init__(self, serviceref, begin, end, name, description, eit):
36 timer.TimerEntry.__init__(self, int(begin), int(end))
38 assert isinstance(serviceref, ServiceReference)
40 self.service_ref = serviceref
44 self.description = description
46 self.record_service = None
47 self.start_prepare = 0
52 def log(self, code, msg):
53 self.log_entries.append((int(time.time()), code, msg))
57 self.state = self.StateWaiting
58 self.first_try_prepare = True
61 def calculateFilename(self):
62 service_name = self.service_ref.getServiceName()
63 # begin_date = datetime.fromtimestamp(begin).strf...
66 print "begin_date: ", begin_date
67 print "service_name: ", service_name
68 print "name:", self.name
69 print "description: ", self.description
71 self.Filename = Directories.getRecordingFilename(service_name)
72 self.log(0, "Filename calculated as: '%s'" % self.Filename)
73 #begin_date + " - " + service_name + description)
76 self.calculateFilename()
77 self.record_service = NavigationInstance.instance.recordService(self.service_ref)
78 if self.record_service == None:
79 self.log(1, "'record service' failed")
82 prep_res = self.record_service.prepare(self.Filename + ".ts")
84 self.log(2, "'prepare' failed: error %d" % prep_res)
85 self.record_service = None
88 self.log(3, "prepare ok, writing meta information to %s" % self.Filename)
90 f = open(self.Filename + ".ts.meta", "w")
91 f.write(str(self.service_ref) + "\n")
92 f.write(self.name + "\n")
93 f.write(self.description + "\n")
94 f.write(str(self.begin) + "\n")
97 self.log(4, "failed to write meta information")
100 def do_backoff(self):
101 if self.backoff == 0:
105 if self.backoff > 100:
107 self.log(10, "backoff: retry in %d seconds" % self.backoff)
110 next_state = self.state + 1
111 self.log(5, "activating state %d" % next_state)
113 if next_state == self.StatePrepared:
114 if self.tryPrepare():
115 self.log(6, "prepare ok, waiting for begin")
116 # fine. it worked, resources are allocated.
117 self.next_activation = self.begin
121 self.log(7, "prepare failed")
122 if self.first_try_prepare:
123 self.first_try_prepare = False
124 if config.recording.asktozap.value == 0:
125 self.log(8, "asking user to zap away")
126 Notifications.AddNotificationWithCallback(self.failureCB, MessageBox, _("A timer failed to record!\nDisable TV and try again?\n"))
127 else: # zap without asking
128 self.log(9, "zap without asking")
133 self.start_prepare = time.time() + self.backoff
135 elif next_state == self.StateRunning:
136 self.log(11, "start recording")
137 record_res = self.record_service.start()
140 self.log(13, "start record returned %d" % record_res)
143 self.begin = time.time() + self.backoff
147 elif next_state == self.StateEnded:
148 self.log(12, "stop recording")
149 self.record_service.stop()
150 self.record_service = None
153 def getNextActivation(self):
154 if self.state == self.StateEnded:
157 next_state = self.state + 1
159 return {self.StatePrepared: self.start_prepare,
160 self.StateRunning: self.begin,
161 self.StateEnded: self.end }[next_state]
163 def failureCB(self, answer):
165 self.log(13, "ok, zapped away")
166 #NavigationInstance.instance.stopUserServices()
167 NavigationInstance.instance.playService(self.service_ref.ref)
169 self.log(14, "user didn't want to zap away, record will probably fail")
171 def timeChanged(self):
172 old_prepare = self.start_prepare
173 self.start_prepare = self.begin - self.prepare_time
176 if old_prepare != self.start_prepare:
177 self.log(15, "record time changed, start prepare is now: %s" % time.ctime(self.start_prepare))
179 def createTimer(xml):
180 begin = int(xml.getAttribute("begin"))
181 end = int(xml.getAttribute("end"))
182 serviceref = ServiceReference(str(xml.getAttribute("serviceref")))
183 description = xml.getAttribute("description").encode("utf-8")
184 repeated = xml.getAttribute("repeated").encode("utf-8")
186 eit = long(xml.getAttribute("eit").encode("utf-8"))
189 name = xml.getAttribute("name").encode("utf-8")
190 #filename = xml.getAttribute("filename").encode("utf-8")
191 entry = RecordTimerEntry(serviceref, begin, end, name, description, eit)
192 entry.repeated = int(repeated)
194 for l in elementsWithTag(xml.childNodes, "log"):
195 time = int(l.getAttribute("time"))
196 code = int(l.getAttribute("code"))
197 msg = mergeText(l.childNodes).strip()
198 entry.log_entries.append((time, code, msg))
202 class RecordTimer(timer.Timer):
204 timer.Timer.__init__(self)
206 self.Filename = Directories.resolveFilename(Directories.SCOPE_CONFIG, "timers.xml")
211 print "unable to load timers from file!"
213 def isRecording(self):
215 for timer in self.timer_list:
216 if timer.isRunning():
222 doc = xml.dom.minidom.parse(self.Filename)
224 root = doc.childNodes[0]
225 for timer in elementsWithTag(root.childNodes, "timer"):
226 self.record(createTimer(timer))
229 #doc = xml.dom.minidom.Document()
230 #root_element = doc.createElement('timers')
231 #doc.appendChild(root_element)
232 #root_element.appendChild(doc.createTextNode("\n"))
234 #for timer in self.timer_list + self.processed_timers:
235 # some timers (instant records) don't want to be saved.
239 #t = doc.createTextNode("\t")
240 #root_element.appendChild(t)
241 #t = doc.createElement('timer')
242 #t.setAttribute("begin", str(int(timer.begin)))
243 #t.setAttribute("end", str(int(timer.end)))
244 #t.setAttribute("serviceref", str(timer.service_ref))
245 #t.setAttribute("repeated", str(timer.repeated))
246 #t.setAttribute("name", timer.name)
247 #t.setAttribute("description", timer.description)
248 #t.setAttribute("eit", str(timer.eit))
250 #for time, code, msg in timer.log_entries:
251 #t.appendChild(doc.createTextNode("\t\t"))
252 #l = doc.createElement('log')
253 #l.setAttribute("time", str(time))
254 #l.setAttribute("code", str(code))
255 #l.appendChild(doc.createTextNode(msg))
257 #t.appendChild(doc.createTextNode("\n"))
259 #root_element.appendChild(t)
260 #t = doc.createTextNode("\n")
261 #root_element.appendChild(t)
264 #file = open(self.Filename, "w")
271 list.append('<?xml version="1.0" ?>\n')
272 list.append('<timers>\n')
274 for timer in self.timer_list + self.processed_timers:
275 list.append('<timer')
276 list.append(' begin="' + str(int(timer.begin)) + '"')
277 list.append(' end="' + str(int(timer.end)) + '"')
278 list.append(' serviceref="' + str(timer.service_ref) + '"')
279 list.append(' repeated="' + str(int(timer.repeated)) + '"')
280 list.append(' name="' + str(timer.name) + '"')
281 list.append(' description="' + str(timer.description) + '"')
282 list.append(' eit="' + str(timer.eit) + '"')
285 for time, code, msg in timer.log_entries:
287 list.append(' code="' + str(code) + '"')
288 list.append(' time="' + str(time) + '"')
290 list.append(str(msg))
291 list.append('</log>\n')
294 list.append('</timer>\n')
296 list.append('</timers>\n')
298 file = open(self.Filename, "w")
303 def record(self, entry):
305 print "[Timer] Record " + str(entry)
307 self.addTimerEntry(entry)
309 def removeEntry(self, entry):
310 print "[Timer] Remove " + str(entry)
313 entry.repeated = False
316 # this sets the end time to current time, so timer will be stopped.
319 if entry.state != entry.StateEnded:
320 self.timeChanged(entry)
322 print "state: ", entry.state
323 print "in processed: ", entry in self.processed_timers
324 print "in running: ", entry in self.timer_list
325 # now the timer should be in the processed_timers list. remove it from there.
326 self.processed_timers.remove(entry)