Esempio n. 1
0
    def buildSections(self, tableIds, version, onid, tsid, serviceId,
                      serviceDescriptors, schedule):

        if schedule:
            events = self.events
        else:
            events = self.findPresentFollowing()

        # now build the sections
        self.serialiser = SerialiseEITSection()
        sectionedEvents = self.groupEventsBySection(events)
        return self.compileSections(
            tableIds,
            version,
            onid,
            tsid,
            serviceId,
            sectionedEvents,
        )
Esempio n. 2
0
 def buildSections(self,tableIds,version,onid,tsid,serviceId,serviceDescriptors,schedule):
     
     if schedule:
         events = self.events
     else:
         events = self.findPresentFollowing()
     
     # now build the sections
     self.serialiser = SerialiseEITSection()
     sectionedEvents = self.groupEventsBySection(events)
     return self.compileSections(tableIds,
             version,
             onid,
             tsid,
             serviceId,
             sectionedEvents,
             )
Esempio n. 3
0
class Schedule(object):
    def __init__(self, infile, timenow):
        super(Schedule, self).__init__()
        self.events = []
        self.timenow = timenow

        COMMENT = re.compile(r"^\s*[#].*$")
        EMPTY = re.compile(r"^\s*$")
        EVENT = re.compile(
            r"^\s*event\s+(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d)\s+(\d\d):(\d\d):(\d\d)\s+(\d+)\s+(.*)\s*$",
            re.I)
        for line in infile:
            if re.match(COMMENT, line):
                pass
            elif re.match(EMPTY, line):
                pass
            else:
                match = re.match(EVENT, line)
                if match:
                    e = ScheduleEvent()
                    e.setStartISO(match.group(1))
                    e.setDuration(
                        hour=int(match.group(2)),
                        minute=int(match.group(3)),
                        second=int(match.group(4)),
                    )
                    e.event_id = int(match.group(5))
                    e.programme_info_file = match.group(6)

                    # resolve running status
                    if timenow >= e.starttime and timenow < e.starttime + e.duration:
                        e.setRunningStatus("running")
                    else:
                        e.setRunningStatus("no")

                    self.events.append(e)

        # sort into chronological order
        self.events.sort()

    def buildDescriptors(self, serviceDescriptors, programmes):
        for event in self.events:
            event.buildDescriptors(serviceDescriptors, programmes)

    def findPresentFollowing(self):
        """\
        Reduces down the set of events to only those needed for Present-Following table data
        """
        # find the one marked as running (the "present") then find the one after it (the "following")
        presentEvent = None
        for event in self.events:
            if presentEvent is None:
                if event.running_status == 4:
                    presentEvent = event
            else:
                return [presentEvent, event]

        # nothing is present, so lets do a more exhaustive search for following
        # (find first event in the future, going through them in ascending order)
        for event in self.events:
            if event.starttime > self.timenow:
                return [event]

        return []  # really can't find anything

    def buildScheduleSections(self, tableIds, version, onid, tsid, serviceId,
                              serviceDescriptors):
        return self.buildSections(tableIds, version, onid, tsid, serviceId,
                                  serviceDescriptors, True)

    def buildPfSections(self, tableIds, version, onid, tsid, serviceId,
                        serviceDescriptors):
        return self.buildSections(tableIds, version, onid, tsid, serviceId,
                                  serviceDescriptors, False)

    def buildSections(self, tableIds, version, onid, tsid, serviceId,
                      serviceDescriptors, schedule):

        if schedule:
            events = self.events
        else:
            events = self.findPresentFollowing()

        # now build the sections
        self.serialiser = SerialiseEITSection()
        sectionedEvents = self.groupEventsBySection(events)
        return self.compileSections(
            tableIds,
            version,
            onid,
            tsid,
            serviceId,
            sectionedEvents,
        )

    def groupEventsBySection(self, events):
        eventGroups = []

        # first pass, go through compiling just event sections and getting them grouped
        # then we'll know how many table sections there actually are, and we can then
        # build the full tables
        remainingEvents = [ \
            { \
                "event_id"       : e.event_id,
                "starttime"      : e.starttime.timetuple()[0:6],
                "duration"       : e.duration_tuple,
                "running_status" : e.running_status,
                "free_CA_mode"   : False,
                "descriptors"    : e.descriptors,
            } \
            for e in events ]

        while len(remainingEvents) > 0:
            (serialisedEvents,
             count) = self.serialiser.consumeEvents(remainingEvents)
            eventGroups.append(serialisedEvents)
            for _ in range(count):
                remainingEvents.pop(0)

        return eventGroups

    def compileSections(self, tableIds, version, onid, tsid, serviceId,
                        eventGroups):
        # now we know how it is going to spread across sections and table ids, we can build
        # the full sections
        numTables = len(eventGroups) / 256
        if len(eventGroups) % 256 > 0:
            numTables = numTables + 1

        section = {
            #            "table_id" : -1,
            "current": True,
            "service_id": serviceId,
            "version": version % 32,
            #            "section" : -1,
            #            "last_section" : -1,
            "last_table_id": tableIds[numTables - 1],
            "original_network_id": onid,
            "transport_stream_id": tsid,
        }

        sections = []
        tid = 0
        sectionNum = 0
        remainingSections = len(eventGroups)
        for eg in eventGroups:
            assert (remainingSections > 0)
            section["table_id"] = tableIds[tid]
            section["section"] = sectionNum
            section["last_section"] = min(remainingSections - 1, 255)
            sectionNum += 1
            remainingSections -= 1
            if sectionNum > 255:
                sectionNum = 0
                tid += 1

            serialisedSection = self.serialiser.serialise(section,
                                                          prebuiltEvents=eg)
            sections.append(serialisedSection)

        assert (remainingSections == 0)

        return sections
Esempio n. 4
0
class Schedule(object):
    def __init__(self, infile, timenow):
        super(Schedule,self).__init__()
        self.events = []
        self.timenow = timenow

        COMMENT = re.compile(r"^\s*[#].*$")
        EMPTY = re.compile(r"^\s*$")
        EVENT = re.compile(r"^\s*event\s+(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d)\s+(\d\d):(\d\d):(\d\d)\s+(\d+)\s+(.*)\s*$", re.I)
        for line in infile:
            if re.match(COMMENT, line):
                pass
            elif re.match(EMPTY, line):
                pass
            else:
                match = re.match(EVENT, line)
                if match:
                    e = ScheduleEvent()
                    e.setStartISO(match.group(1))
                    e.setDuration(hour   = int(match.group(2)),
                                  minute = int(match.group(3)),
                                  second = int(match.group(4)),
                              )
                    e.event_id   = int(match.group(5))
                    e.programme_info_file = match.group(6)
                    
                    # resolve running status
                    if timenow >= e.starttime and timenow < e.starttime + e.duration:
                        e.setRunningStatus("running")
                    else:
                        e.setRunningStatus("no")
                    
                    self.events.append(e)
                    
        # sort into chronological order
        self.events.sort()
            
        
    def buildDescriptors(self, serviceDescriptors, programmes):
        for event in self.events:
            event.buildDescriptors(serviceDescriptors,programmes)
            
            
    def findPresentFollowing(self):
        """\
        Reduces down the set of events to only those needed for Present-Following table data
        """
        # find the one marked as running (the "present") then find the one after it (the "following")
        presentEvent = None
        for event in self.events:
            if presentEvent is None:
                if event.running_status == 4:
                    presentEvent = event
            else:
                return [ presentEvent, event ]
    
        # nothing is present, so lets do a more exhaustive search for following
        # (find first event in the future, going through them in ascending order)
        for event in self.events:
            if event.starttime > self.timenow:
                return [ event ]
        
        return [] # really can't find anything
        
    def buildScheduleSections(self,tableIds,version,onid,tsid,serviceId,serviceDescriptors):
        return self.buildSections(tableIds,version,onid,tsid,serviceId,serviceDescriptors,True)
    
    def buildPfSections(self,tableIds,version,onid,tsid,serviceId,serviceDescriptors):
        return self.buildSections(tableIds,version,onid,tsid,serviceId,serviceDescriptors,False)
    
    def buildSections(self,tableIds,version,onid,tsid,serviceId,serviceDescriptors,schedule):
        
        if schedule:
            events = self.events
        else:
            events = self.findPresentFollowing()
        
        # now build the sections
        self.serialiser = SerialiseEITSection()
        sectionedEvents = self.groupEventsBySection(events)
        return self.compileSections(tableIds,
                version,
                onid,
                tsid,
                serviceId,
                sectionedEvents,
                )
        
        
    def groupEventsBySection(self, events):
        eventGroups = []
        
        # first pass, go through compiling just event sections and getting them grouped
        # then we'll know how many table sections there actually are, and we can then
        # build the full tables
        remainingEvents = [ \
            { \
                "event_id"       : e.event_id,
                "starttime"      : e.starttime.timetuple()[0:6],
                "duration"       : e.duration_tuple,
                "running_status" : e.running_status,
                "free_CA_mode"   : False,
                "descriptors"    : e.descriptors,
            } \
            for e in events ]
        
        while len(remainingEvents) > 0:
            (serialisedEvents, count) = self.serialiser.consumeEvents(remainingEvents)
            eventGroups.append(serialisedEvents)
            for _ in range(count):
                remainingEvents.pop(0)
    
        return eventGroups
    
    def compileSections(self,tableIds,version,onid,tsid,serviceId,eventGroups):
        # now we know how it is going to spread across sections and table ids, we can build
        # the full sections
        numTables = len(eventGroups) / 256
        if  len(eventGroups) % 256 > 0:
            numTables = numTables + 1
            
        section = {
#            "table_id" : -1,
            "current"  : True,
            "service_id" : serviceId,
            "version" : version % 32,
#            "section" : -1,
#            "last_section" : -1,
            "last_table_id" : tableIds[numTables-1],
            "original_network_id" : onid,
            "transport_stream_id" : tsid,
        }
        
        sections = []
        tid = 0
        sectionNum = 0
        remainingSections = len(eventGroups)
        for eg in eventGroups:
            assert(remainingSections > 0)
            section["table_id"] = tableIds[tid]
            section["section"] = sectionNum
            section["last_section"] = min(remainingSections-1, 255)
            sectionNum += 1
            remainingSections -= 1
            if sectionNum > 255:
                sectionNum = 0
                tid += 1
                
            serialisedSection = self.serialiser.serialise(section, prebuiltEvents=eg)
            sections.append(serialisedSection)
            
        assert(remainingSections == 0)

        return sections