def close(self): self._is_open = False self._dbname = None # clear everything Agency.clear() Calendar.clear() Stop.clear() Path.clear() Route.clear() TripRoute.clear() Trip.clear() Frequency.clear() Picture.clear()
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
trip_id = BaseObject.unquote(l2[r_headers['trip_id']]) trip = Trip.get_by_gtfs_id(trip_id) if trip is None: print >> sys.stderr, 'no trip for id', trip_id # add the trip stop stop_id = BaseObject.unquote(l2[r_headers['stop_id']]) stop = Stop.get_by_gtfs_id(stop_id) trip.trip_route.add_stop(stop) trip_stop = trip.stops[-1] trip_stop.arrival = BaseObject.unquote(l2[r_headers['arrival_time']]) trip_stop.departure = BaseObject.unquote(l2[r_headers['departure_time']]) except IOError, e: print >> sys.stderr, 'Unable to open stop_times.txt:', e # merge trip routes TripRoute.merge() class TripStop(BaseObject): def __init__(self, stop, arrival = None, departure = None): BaseObject.__init__(self) self.arrival = arrival self.departure = departure or arrival self._stop = weakref.ref(stop) stop = property(lambda x: x._stop(), None)
description = route_node.findtext('description') route_type = route_node.findtext('route_node') url = route_node.findtext('url') color = route_node.findtext('color') text_color = route_node.findtext('text_color') agency_id = int(agency_id) r = Route(agency = Agency.get(agency_id), short_name = short_name, long_name = long_name, description = description, route_type = route_type, url = url, color = color, text_color = text_color) r.route_id = int(route_id) r.gtfs_id = gtfs_id for trip_route_node in tree.getroot().findall('TripRoute'): trip_route_id = trip_route_node.get('id', TripRoute.new_id()) gtfs_id = trip_route_node.get('gtfs_id', None) name = trip_route_node.findtext('name') route_id = trip_route_node.findtext('route_id') calendar_id = trip_route_node.findtext('calendar_id') headsign = trip_route_node.findtext('headsign') direction = trip_route_node.findtext('direction') path_id = trip_route_node.findtext('path_id') route = Route.get(int(route_id)) calendar = Calendar.get(int(calendar_id)) path = None if path_id != '': path = Path.get(int(path_id)) tr = TripRoute(name, route, calendar, headsign, int(direction), path)