From: Dirk M. <di...@us...> - 2004-08-05 17:16:15
|
Update of /cvsroot/freevo/freevo/lib/pyepg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11100 Modified Files: __init__.py epg_types.py epg_xmltv.py Log Message: misc enhancements Index: epg_types.py =================================================================== RCS file: /cvsroot/freevo/freevo/lib/pyepg/epg_types.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** epg_types.py 26 Jul 2004 12:45:56 -0000 1.1 --- epg_types.py 5 Aug 2004 17:16:05 -0000 1.2 *************** *** 11,16 **** # ----------------------------------------------------------------------- # $Log$ ! # Revision 1.1 2004/07/26 12:45:56 dischi ! # first draft of pyepg (not integrated in Freevo) # # Revision 1.20 2004/07/10 12:33:41 dischi --- 11,16 ---- # ----------------------------------------------------------------------- # $Log$ ! # Revision 1.2 2004/08/05 17:16:05 dischi ! # misc enhancements # # Revision 1.20 2004/07/10 12:33:41 dischi *************** *** 79,83 **** self.categories = [] self.date = None ! # Due to problems with Twisted's marmalade this should not be changed # to a boolean type. --- 79,84 ---- self.categories = [] self.date = None ! self.pos = None ! # Due to problems with Twisted's marmalade this should not be changed # to a boolean type. *************** *** 85,98 **** ! def __str__(self): bt = time.localtime(self.start) # Beginning time tuple et = time.localtime(self.stop) # End time tuple begins = '%s-%02d-%02d %02d:%02d' % (bt[0], bt[1], bt[2], bt[3], bt[4]) ends = '%s-%02d-%02d %02d:%02d' % (et[0], et[1], et[2], et[3], et[4]) - s = '%s to %s %3s %s' % (begins, ends, self.channel_id, self.title) - return s def __cmp__(self, other): """ --- 86,102 ---- ! def __unicode__(self): bt = time.localtime(self.start) # Beginning time tuple et = time.localtime(self.stop) # End time tuple begins = '%s-%02d-%02d %02d:%02d' % (bt[0], bt[1], bt[2], bt[3], bt[4]) ends = '%s-%02d-%02d %02d:%02d' % (et[0], et[1], et[2], et[3], et[4]) + return u'%s to %s %3s ' % (begins, ends, self.channel_id) + \ + self.title + u' (%s)' % self.pos + def __str__(self): + return String(self.__unicode__()) + def __cmp__(self, other): """ *************** *** 133,139 **** self.programs = [] self.times = None ! ! def Sort(self): # Sort the programs so that the earliest is first in the list f = lambda a, b: cmp(a.start, b.start) --- 137,145 ---- self.programs = [] self.times = None ! self.index = {} ! self.index_start = 0 ! self.index_end = 0 ! def sort(self): # Sort the programs so that the earliest is first in the list f = lambda a, b: cmp(a.start, b.start) *************** *** 141,157 **** ! def __str__(self): ! s = 'CHANNEL ID %-20s' % self.id if self.programs: ! s += '\n' for program in self.programs: ! s += ' ' + String(program) + '\n' else: ! s += ' NO DATA\n' ! return s class TvGuide: def __init__(self): --- 147,239 ---- ! def __unicode__(self): ! s = u'CHANNEL ID %-20s' % self.id if self.programs: ! s += u'\n' for program in self.programs: ! s += u' ' + unicode(program) + u'\n' else: ! s += u' NO DATA\n' return s + + + def __str__(self): + return String(self.__unicode__()) + + def get_key(self, t): + return int('%04d%02d%02d' % time.localtime(t)[:3]) + + def create_index(self): + """ + create index for faster access + """ + if not self.programs: + return + last = -1 + index = -1 + for p in self.programs: + index += 1 + key = self.get_key(p.start) + if not self.index_start: + self.index_start = key + self.index_end = key + if not self.index.has_key(key): + self.index[key] = [] + pos = time.localtime(p.start)[3:5] + pos = pos[0] * 2 + ((pos[1] + 29) / 30) + p.pos = pos + + if len(self.index[key]) >= pos + 1: + last = index + continue + while len(self.index[key]) < pos: + self.index[key].append(last) + self.index[key].append(index) + last = index + while len(self.index[key]) < 48: + self.index[key].append(index) + + + def get_pos(self, start, stop): + """ + get internal positions for programs between start and stop + """ + key = self.get_key(start) + if key < self.index_start: + key = self.index_start + pos = time.localtime(start)[3:5] + pos = max(pos[0] * 2 + (pos[1] / 30), 0) + start = self.index[key][pos] + + key = self.get_key(stop) + if key > self.index_end: + key = self.index_end + pos = time.localtime(stop)[3:5] + pos = max(pos[0] * 2 + ((pos[1] + 29) / 30), 0) + stop = self.index[key][pos] + 1 + + return start, stop + + + def get(self, start, stop=0): + """ + get programs between start and stop time or if stop=0, get + the program running at 'start' + """ + if stop: + start, stop = self.get_pos(start, stop) + return self.programs[start:stop] + else: + start_p, stop_p = self.get_pos(start, start) + f = lambda p, a=start, b=start: not (p.start > b or p.stop < a) + try: + return filter(f, self.programs[start_p:stop_p])[0] + except Exception, e: + return None + + class TvGuide: def __init__(self): *************** *** 163,168 **** - - def AddChannel(self, channel): if not self.chan_dict.has_key(channel.id): --- 245,248 ---- *************** *** 190,266 **** # Oops, something is broken here self.chan_dict[program.channel_id].programs = p[:-1] ! self.chan_dict[program.channel_id].programs += [program] ! ! ! # Get all programs that occur at least partially between ! # the start and stop timeframe. ! # If start is None, get all programs from the start. ! # If stop is None, get all programs until the end. ! # The chanids can be used to select only certain channel id's, ! # all channels are returned otherwise ! # ! # The return value is a list of channels (TvChannel) ! def GetPrograms(self, start = None, stop = None, chanids = None): ! if start == None: ! start = 0 ! if stop == None: ! stop = 2147483647 # Year 2038 ! ! # Return a cached version? ! global cache_last_start, cache_last_stop, cache_last_chanids ! global cache_last_time, cache_last_result ! if (cache_last_start == start and cache_last_stop == stop and ! cache_last_chanids == chanids and ! time.time() < cache_last_time): ! if config.DEBUG > 1: ! a = cache_last_time - time.time() ! print 'epg: Returning cached results, valid for %1.1f secs.' % a ! return cache_last_result[:] # Return a copy ! ! channels = [] ! for chan in self.chan_list: ! if chanids and (not chan.id in chanids): ! continue ! ! # Copy the channel info ! c = TvChannel() ! c.id = chan.id ! c.displayname = chan.displayname ! c.tunerid = chan.tunerid ! c.logo = chan.logo ! c.times = chan.times ! # Copy the programs that are inside the indicated time bracket ! f = lambda p, a=start, b=stop: not (p.start > b or p.stop < a) ! c.programs = filter(f, chan.programs) - channels.append(c) - # Update cache variables - cache_last_start = start - cache_last_stop = stop - if chanids: - cache_last_chanids = chanids[:] - else: - cache_last_chanids = None - cache_last_timeout = time.time() + 20 - cache_last_result = channels[:] # Make a copy in case the caller modifies it - if config.DEBUG > 1: - print 'epg: Returning new results' - - return channels ! ! def Sort(self): # Sort all channel programs in time order for chan in self.chan_list: ! chan.Sort() ! def __str__(self): ! s = 'XML TV Guide\n' ! for chan in self.chan_list: s += String(chan) - return s ! --- 270,309 ---- # Oops, something is broken here self.chan_dict[program.channel_id].programs = p[:-1] ! program.index = len(self.chan_dict[program.channel_id].programs) ! self.chan_dict[program.channel_id].programs.append(program) ! def sort(self): # Sort all channel programs in time order for chan in self.chan_list: ! chan.sort() ! def __unicode__(self): ! s = u'XML TV Guide\n' for chan in self.chan_list: s += String(chan) return s ! ! ! def __str__(self): ! return String(self.__unicode__()) ! ! ! def create_index(self): ! """ ! create an index for faster access ! """ ! for chan in self.chan_list: ! chan.create_index() ! ! def get(self, id): ! """ ! return channel with given id ! """ ! for chan in self.chan_list: ! if chan.id == id: ! return chan ! return None ! Index: epg_xmltv.py =================================================================== RCS file: /cvsroot/freevo/freevo/lib/pyepg/epg_xmltv.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** epg_xmltv.py 26 Jul 2004 12:45:56 -0000 1.1 --- epg_xmltv.py 5 Aug 2004 17:16:05 -0000 1.2 *************** *** 10,15 **** # ----------------------------------------------------------------------- # $Log$ ! # Revision 1.1 2004/07/26 12:45:56 dischi ! # first draft of pyepg (not integrated in Freevo) # # Revision 1.53 2004/07/11 12:25:44 dischi --- 10,15 ---- # ----------------------------------------------------------------------- # $Log$ ! # Revision 1.2 2004/08/05 17:16:05 dischi ! # misc enhancements # # Revision 1.53 2004/07/11 12:25:44 dischi *************** *** 69,113 **** cached_guide = None - def list_channels(XMLTV_FILE, TV_CHANNELS=None, verbose=True): - if TV_CHANNELS: - return TV_CHANNELS - if not os.path.isfile(XMLTV_FILE): - return [] - - if verbose: - print 'epg_xmltv.py: Adding all channels' - xmltv_channels = None - - f = open(XMLTV_FILE) - xmltv_channels = xmltv.read_channels(f) - f.close() - - # Was the guide read successfully? - if not xmltv_channels: - return [] - - TV_CHANNELS = [] - for chan in xmltv_channels: - - id = chan['id'].encode('latin-1', 'ignore') - c = epg_types.TvChannel() - c.id = id - if ' ' in id: - # Assume the format is "TUNERID CHANNELNAME" - c.displayname = id.split()[1] # XXX Educated guess - c.tunerid = id.split()[0] # XXX Educated guess - else: - displayname = chan['display-name'][0][0] - if ' ' in displayname: - c.displayname = displayname.split()[1] - c.tunerid = displayname.split()[0] - else: - c.displayname = displayname - c.tunerid = _('REPLACE WITH TUNERID FOR %s') % displayname - TV_CHANNELS.append((c.displayname, c.id, c.tunerid)) - return TV_CHANNELS - - - def load_guide(XMLTV_FILE, TV_CHANNELS=None, verbose=True): """ --- 69,72 ---- *************** *** 126,156 **** gotfile = 0 - if not TV_CHANNELS: - # get channel listing - TV_CHANNELS = list_channels(XMLTV_FILE, verbose=True) - - if not TV_CHANNELS: - # still no listing? - return None - # Add the channels that are in the config list, or all if the # list is empty ! if verbose: ! print 'epg_xmltv.py: Only adding channels in list' ! for data in TV_CHANNELS: ! (id, disp, tunerid) = data[:3] ! c = epg_types.TvChannel() ! c.id = id ! c.displayname = disp ! c.tunerid = tunerid ! # Handle the optional time-dependent station info ! c.times = [] ! if len(data) > 3 and len(data[3:4]) == 3: ! for (days, start_time, stop_time) in data[3:4]: ! c.times.append((days, int(start_time), int(stop_time))) ! guide.AddChannel(c) xmltv_programs = None --- 85,140 ---- gotfile = 0 # Add the channels that are in the config list, or all if the # list is empty ! if TV_CHANNELS: ! if verbose: ! print 'epg_xmltv.py: Only adding channels in list' ! for data in TV_CHANNELS: ! (id, disp, tunerid) = data[:3] ! c = epg_types.TvChannel() ! c.id = id ! c.displayname = disp ! c.tunerid = tunerid ! ! # Handle the optional time-dependent station info ! c.times = [] ! if len(data) > 3 and len(data[3:4]) == 3: ! for (days, start_time, stop_time) in data[3:4]: ! c.times.append((days, int(start_time), int(stop_time))) ! guide.AddChannel(c) ! ! ! else: # Add all channels in the XMLTV file ! if verbose: ! print 'epg_xmltv.py: Adding all channels' ! xmltv_channels = None ! if gotfile: ! f = open(XMLTV_FILE) ! xmltv_channels = xmltv.read_channels(f) ! f.close() ! # Was the guide read successfully? ! if not xmltv_channels: ! return None # No ! ! for chan in xmltv_channels: ! id = chan['id'].encode('latin-1', 'ignore') ! c = epg_types.TvChannel() ! c.id = id ! if ' ' in id: ! # Assume the format is "TUNERID CHANNELNAME" ! c.displayname = id.split()[1] # XXX Educated guess ! c.tunerid = id.split()[0] # XXX Educated guess ! else: ! displayname = chan['display-name'][0][0] ! if ' ' in displayname: ! c.displayname = displayname.split()[1] ! c.tunerid = displayname.split()[0] ! else: ! c.displayname = displayname ! c.tunerid = _('REPLACE WITH TUNERID FOR %s') % displayname + guide.AddChannel(c) xmltv_programs = None *************** *** 225,229 **** print 'Error in tv guide, skipping' ! guide.Sort() return guide --- 209,214 ---- print 'Error in tv guide, skipping' ! guide.sort() ! guide.create_index() return guide Index: __init__.py =================================================================== RCS file: /cvsroot/freevo/freevo/lib/pyepg/__init__.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** __init__.py 26 Jul 2004 12:45:56 -0000 1.1 --- __init__.py 5 Aug 2004 17:16:05 -0000 1.2 *************** *** 1,9 **** import compat import epg_xmltv ! def list_channels(data = 'tmp/TV.xml'): ! return epg_xmltv.list_channels(data, verbose=True) ! ! def load(data = 'tmp/TV.xml'): return epg_xmltv.load_guide(data, verbose=True) --- 1,11 ---- import compat import epg_xmltv + import stat + import os ! def load(data = '/tmp/TV.xml'): return epg_xmltv.load_guide(data, verbose=True) + def timestamp(data = '/tmp/TV.xml'): + return 0 + |