예제 #1
0
    def _load_calendar(self, rdr):
        """Fetch the calendar table, creating :class:`Service` objects.

        Given a calendar row, create the details for each date.

        Mapping from day number to day "name".
        Must match the keys in GTF data.
        """
        Service.set_db( settings.db )
        services_by_date= defaultdict(set)
        for row in rdr:
            start_date= datetime.datetime.strptime( row['start_date'], '%Y%m%d' ).date()
            end_date= datetime.datetime.strptime( row['end_date'], '%Y%m%d' ).date()
            for d in xrange((end_date-start_date).days+1):
                date= start_date + datetime.timedelta(days=d)
                day_name= self.weekday_name[date.weekday()]
                if row[day_name] == '1':
                    services_by_date[date].add(row['service_id'])
        for date in sorted(services_by_date):
            if date.day == 1:
                time.sleep(0.25)
            previous= list( Service.view( 'service/bydate', key=date.strftime("%Y-%m-%d") ) )
            if previous:
                settings.db.delete_docs( previous )
            serv= Service(
                date= date,
                day_of_week= self.weekday_name[date.weekday()].title(),
                services= list( sorted(services_by_date[date]) ),
            )
            #print( serv, settings.db, Service.get_db() )
            settings.db.save_doc(serv)
예제 #2
0
def get_services_today( today=None ):
    """Returns the Service ID applicable today.
    Examines calendar and calendar dates.

    :param today: A :class:`datetime.date` object.
        If omitted, the current date is used.
    :returns: A sequence of service id strings that apply to this date.
    """
    Service.set_db(settings.db)
    if not today:
        today= datetime.datetime.today().date()
    svc= list( Service.view( 'service/bydate', key=today.strftime("%Y-%m-%d")) )
    if not svc: return []
    return svc[0].services
예제 #3
0
 def _load_calendar_dates(self, rdr):
     """Fetch the calendar_dates table, updating the :class:`Service` objects.
     Can only be done successfully **after** the core calendar has been loaded.
     These are add/remove changes to the calendar.
     """
     Service.set_db( settings.db )
     for row in rdr:
         date= datetime.datetime.strptime( row['date'], '%Y%m%d' ).date()
         docs= list( Service.view( 'service/bydate', key=date.strftime("%Y-%m-%d") ) )
         if docs:
             serv= docs[0]
             if row['exception_type'] == '1': # Add
                 serv.services.append( row['service_id'])
             elif row['exception_type'] == '2' and row['service_id'] in serv.services: # Remove
                 serv.services.remove( row['service_id'])
             elif row['exception_type'] == '2' and row['service_id'] not in serv.services:
                 # Wasn't there to begin with?  Weird.
                 pass
             else:
                 raise GTFException( "Unknown Calendar Date {0!r}".format(row) )
         else:
             # Missing date?  Weird.
             pass
         serv.save()