def import_trips(cls, directory): from Route import Route from Calendar import Calendar from TripRoute import TripRoute from Path import Path from Stop import Stop try: f = open(os.path.join(directory, 'trips.txt'), 'rb') reader = csv.reader(f) mappings = {'route_id': ('route', lambda x: Route.get_by_gtfs_id(x)), 'service_id': ('calendar', lambda x: Calendar.get_by_gtfs_id(x)), 'trip_id': ('name', lambda x: x), 'trip_headsign': ('headsign', lambda x: x), 'direction_id': ('direction', lambda x: int(x) if x else 0), 'shape_id': ('path', lambda x: Path.get_by_gtfs_id(x)), } # create a headers with an index headers = reader.next() r_headers = dict([(x, i) for i, x in enumerate(headers)]) for l2 in reader: if len(l2) != len(headers): print >> sys.stderr, 'Invalid line', l2, headers continue kw = {} for i, a in enumerate(l2): key = headers[i] if key in mappings: kw[mappings[key][0]] = mappings[key][1](BaseObject.unquote(a)) # create the trip route trip_route = TripRoute(**kw) # set the id trip_route.gtfs_id = BaseObject.unquote(l2[r_headers['trip_id']]) # create a trip trip = trip_route.add_trip() trip.gtfs_id = BaseObject.unquote(l2[r_headers['trip_id']]) # go through the list again and set block ids #!mwd - I'm not sure how to do this. We link # blocks by trip ids, but block ids are # random in gtfs, so we have no way to link # them back except IOError, e: print >> sys.stderr, 'Unable to open trips.txt:', e