3 #from time import datetime
4 from Tools import Directories
9 import NavigationInstance
11 from Tools.XMLTools import elementsWithTag
12 from ServiceReference import ServiceReference
14 class RecordTimerEntry(timer.TimerEntry):
15 def __init__(self, begin, end, serviceref, epg, description):
16 timer.TimerEntry.__init__(self, int(begin), int(end))
18 assert isinstance(serviceref, ServiceReference)
20 self.service_ref = serviceref
24 #str(epg.m_event_name)
29 self.description = description
31 self.record_service = None
33 def calculateFilename(self):
34 service_name = self.service_ref.getServiceName()
35 # begin_date = datetime.fromtimestamp(begin).strf...
37 if self.epg_data is not None:
38 description = " - " + self.epg_data
42 print "begin_date: " + str(begin_date)
43 print "service_name: " + str(service_name)
44 print "description: " + str(description)
45 self.Filename = Directories.getRecordingFilename(service_name)
46 #begin_date + " - " + service_name + description)
48 # build filename from epg
50 # pff das geht noch nicht...
52 # self.Filename = "recording.ts"
54 # self.Filename = "record_" + str(epg.m_event_name) + ".ts"
56 # print "------------ record filename: %s" % (self.Filename)
59 def activate(self, event):
60 if event == self.EventPrepare:
61 self.calculateFilename()
62 self.record_service = NavigationInstance.instance.recordService(self.service_ref)
63 if self.record_service == None:
64 print "timer record failed."
66 self.record_service.prepare(self.Filename + ".ts")
67 f = open(self.Filename + ".ts.meta", "w")
68 f.write(str(self.service_ref) + "\n")
69 f.write(self.epg_data + "\n")
72 elif self.record_service == None:
73 if event != self.EventAbort:
74 print "timer record start failed, can't finish recording."
75 elif event == self.EventStart:
76 self.record_service.start()
77 print "timer started!"
78 elif event == self.EventEnd or event == self.EventAbort:
79 self.record_service.stop()
80 self.record_service = None
81 print "Timer successfully ended"
85 begin = int(xml.getAttribute("begin"))
86 end = int(xml.getAttribute("end"))
87 serviceref = ServiceReference(str(xml.getAttribute("serviceref")))
88 description = xml.getAttribute("description")
89 repeated = xml.getAttribute("repeated")
90 epgdata = xml.getAttribute("epgdata")
91 #filename = xml.getAttribute("filename")
92 entry = RecordTimerEntry(begin, end, serviceref, epgdata, description)
93 entry.repeated = int(repeated)
96 class RecordTimer(timer.Timer):
98 timer.Timer.__init__(self)
100 self.Filename = Directories.resolveFilename(Directories.SCOPE_USERETC, "timers.xml")
105 print "unable to load timers from file!"
109 doc = xml.dom.minidom.parse(self.Filename)
111 root = doc.childNodes[0]
112 for timer in elementsWithTag(root.childNodes, "timer"):
113 self.record(createTimer(timer))
116 doc = xml.dom.minidom.Document()
117 root_element = doc.createElement('timers')
118 doc.appendChild(root_element)
119 root_element.appendChild(doc.createTextNode("\n"))
121 for timer in self.timer_list + self.processed_timers:
122 # some timers (instant records) don't want to be saved.
126 t = doc.createTextNode("\t")
127 root_element.appendChild(t)
128 t = doc.createElement('timer')
129 t.setAttribute("begin", str(timer.begin))
130 t.setAttribute("end", str(timer.end))
131 t.setAttribute("serviceref", str(timer.service_ref))
132 t.setAttribute("repeated", str(timer.repeated))
133 #t.setAttribute("epgdata", timer.)
134 t.setAttribute("description", timer.description)
135 root_element.appendChild(t)
136 t = doc.createTextNode("\n")
137 root_element.appendChild(t)
139 file = open(self.Filename, "w")
140 doc.writexml(codecs.getwriter('UTF-8')(file))
144 def record(self, entry):
145 print "[Timer] Record " + str(entry)
147 self.addTimerEntry(entry)
149 def removeEntry(self, entry):
150 print "[Timer] Remove " + str(entry)
152 if entry.state == timer.TimerEntry.StateRunning:
153 print "remove running timer."
154 entry.end = time.time()
155 self.timeChanged(entry)
156 elif entry.state != timer.TimerEntry.StateEnded:
157 entry.activate(timer.TimerEntry.EventAbort)
158 self.timer_list.remove(entry)
159 self.calcNextActivation()
160 print "timer did not yet start - removing"
162 print "timer did already end - doing nothing."
164 print "state: ", entry.state
165 print "in processed: ", entry in self.processed_timers
166 print "in running: ", entry in self.timer_list
167 # now the timer should be in the processed_timers list. remove it from there.
168 self.processed_timers.remove(entry)