def parse(self): ''' Parse the agency.txt csv file creating an Agency record per csv record ''' # Delete existing entries agenciesToDelete = Agency.objects.filter(source=self.source) self.logger.debug('Cleaning existing Agencies %s', agenciesToDelete) agenciesToDelete.delete() # Iterate over all entries reader = csv.DictReader(open(self.filename, 'r'), skipinitialspace=True) agency = None for row in reader: #Parse row self.logger.info('Parsing agency row: %s', row) agencyId = csvValueOrNone(row, 'agency_id') agency = Agency() agency.source = self.source agency.agencyId = agencyId agency.agencyName = row['agency_name'] agency.agencyUrl= row['agency_url'] agency.agencyTimezone = row['agency_timezone'] agency.agencyLang = csvValueOrNone(row, 'agency_lang') agency.agencyPhone = csvValueOrNone(row, 'agency_phone') agency.agencyFareUrl = csvValueOrNone(row, 'agency_fare_url') agency.save() return agency
def parse(self): tripsToDelete = Trip.objects.filter(source=self.source) self.logger.info('Cleaning existing Trips %s', tripsToDelete) tripsToDelete.delete() reader = csv.DictReader(open(self.filename, 'r'), skipinitialspace=True) tripIdToTripMapping = {} for row in reader: tripId = row['trip_id'] trip = Trip() self.logger.info('Parsing trip row: %s', row) trip.source = self.source trip.tripId = tripId trip.routeId = row['route_id'] trip.route = trip.routeId in self.routeIdToRouteMapping and self.routeIdToRouteMapping[trip.routeId] or Route.objects.filter(source=self.source).get(routeId=trip.routeId) trip.serviceId = row['service_id'] trip.service = row['service_id'] in self.serviceIdToCalendarMapping and self.serviceIdToCalendarMapping[row['service_id']] or Calendar.objects.filter(source=self.source).get(serviceId=row['service_id']) trip.headSign = csvValueOrNone(row, 'trip_headsign') trip.shortName = csvValueOrNone(row, 'trip_short_name') trip.directionId = 'direction_id' in row and row['direction_id'] or None trip.blockId = csvValueOrNone(row, 'block_id') trip.shapeId = self.shapeIdForRow(row) trip.save() tripIdToTripMapping[trip.tripId] = trip return tripIdToTripMapping
def parse(self): ''' Parse the agency.txt csv file creating an Agency record per csv record ''' # Delete existing entries agenciesToDelete = Agency.objects.filter(source=self.source) self.logger.debug('Cleaning existing Agencies %s', agenciesToDelete) agenciesToDelete.delete() # Iterate over all entries reader = csv.DictReader(open(self.filename, 'r'), skipinitialspace=True) agency = None for row in reader: #Parse row self.logger.info('Parsing agency row: %s', row) agencyId = csvValueOrNone(row, 'agency_id') agency = Agency() agency.source = self.source agency.agencyId = agencyId agency.agencyName = row['agency_name'] agency.agencyUrl = row['agency_url'] agency.agencyTimezone = row['agency_timezone'] agency.agencyLang = csvValueOrNone(row, 'agency_lang') agency.agencyPhone = csvValueOrNone(row, 'agency_phone') agency.agencyFareUrl = csvValueOrNone(row, 'agency_fare_url') agency.save() return agency
def parse(self): tripsToDelete = Trip.objects.filter(source=self.source) self.logger.info('Cleaning existing Trips %s', tripsToDelete) tripsToDelete.delete() reader = csv.DictReader(open(self.filename, 'r'), skipinitialspace=True) tripIdToTripMapping = {} for row in reader: tripId = row['trip_id'] trip = Trip() self.logger.info('Parsing trip row: %s', row) trip.source = self.source trip.tripId = tripId trip.routeId = row['route_id'] trip.route = trip.routeId in self.routeIdToRouteMapping and self.routeIdToRouteMapping[ trip.routeId] or Route.objects.filter(source=self.source).get( routeId=trip.routeId) trip.serviceId = row['service_id'] trip.service = row[ 'service_id'] in self.serviceIdToCalendarMapping and self.serviceIdToCalendarMapping[ row['service_id']] or Calendar.objects.filter( source=self.source).get(serviceId=row['service_id']) trip.headSign = csvValueOrNone(row, 'trip_headsign') trip.shortName = csvValueOrNone(row, 'trip_short_name') trip.directionId = 'direction_id' in row and row[ 'direction_id'] or None trip.blockId = csvValueOrNone(row, 'block_id') trip.shapeId = self.shapeIdForRow(row) trip.save() tripIdToTripMapping[trip.tripId] = trip return tripIdToTripMapping
def shapeIdForRow(self, row): shapeId = csvValueOrNone(row, 'shape_id') return shapeId