<s> import transitfeed <EOL> from optparse import OptionParser <EOL> import re <EOL> stops = { } <EOL> def AddRouteToSchedule ( schedule , table ) : <EOL> if len ( table ) >= <NUM_LIT:2> : <EOL> r = schedule . AddRoute ( short_name = table [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , long_name = table [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , route_type = '<STR_LIT>' ) <EOL> for trip in table [ <NUM_LIT:2> : ] : <EOL> if len ( trip ) > len ( table [ <NUM_LIT:1> ] ) : <EOL> print "<STR_LIT>" % trip [ len ( table [ <NUM_LIT:1> ] ) : ] <EOL> trip = trip [ <NUM_LIT:0> : len ( table [ <NUM_LIT:1> ] ) ] <EOL> t = r . AddTrip ( schedule , headsign = '<STR_LIT>' ) <EOL> trip_stops = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( trip ) ) : <EOL> if re . search ( r'<STR_LIT>' , trip [ i ] ) : <EOL> trip_stops . append ( ( transitfeed . TimeToSecondsSinceMidnight ( trip [ i ] ) , table [ <NUM_LIT:1> ] [ i ] ) ) <EOL> trip_stops . sort ( ) <EOL> for ( time , stopname ) in trip_stops : <EOL> t . AddStopTime ( stop = stops [ stopname . lower ( ) ] , arrival_secs = time , <EOL> departure_secs = time ) <EOL> def TransposeTable ( table ) : <EOL> """<STR_LIT>""" <EOL> transposed = [ ] <EOL> rows = len ( table ) <EOL> cols = max ( len ( row ) for row in table ) <EOL> for x in range ( cols ) : <EOL> transposed . append ( [ ] ) <EOL> for y in range ( rows ) : <EOL> if x < len ( table [ y ] ) : <EOL> transposed [ x ] . append ( table [ y ] [ x ] ) <EOL> else : <EOL> transposed [ x ] . append ( None ) <EOL> return transposed <EOL> def ProcessOptions ( schedule , table ) : <EOL> service_period = schedule . GetDefaultServicePeriod ( ) <EOL> agency_name , agency_url , agency_timezone = ( None , None , None ) <EOL> for row in table [ <NUM_LIT:1> : ] : <EOL> command = row [ <NUM_LIT:0> ] . lower ( ) <EOL> if command == '<STR_LIT>' : <EOL> service_period . SetWeekdayService ( ) <EOL> elif command == '<STR_LIT>' : <EOL> service_period . SetStartDate ( row [ <NUM_LIT:1> ] ) <EOL> elif command == '<STR_LIT>' : <EOL> service_period . SetEndDate ( row [ <NUM_LIT:1> ] ) <EOL> elif command == '<STR_LIT>' : <EOL> service_period . SetDateHasService ( date = row [ <NUM_LIT:1> ] ) <EOL> elif command == '<STR_LIT>' : <EOL> service_period . SetDateHasService ( date = row [ <NUM_LIT:1> ] , has_service = False ) <EOL> elif command == '<STR_LIT>' : <EOL> agency_name = row [ <NUM_LIT:1> ] <EOL> elif command == '<STR_LIT>' : <EOL> agency_url = row [ <NUM_LIT:1> ] <EOL> elif command == '<STR_LIT>' : <EOL> agency_timezone = row [ <NUM_LIT:1> ] <EOL> if not ( agency_name and agency_url and agency_timezone ) : <EOL> print "<STR_LIT>" <EOL> schedule . NewDefaultAgency ( agency_name = agency_name , agency_url = agency_url , <EOL> agency_timezone = agency_timezone ) <EOL> def AddStops ( schedule , table ) : <EOL> for name , lat_str , lng_str in table [ <NUM_LIT:1> : ] : <EOL> stop = schedule . AddStop ( lat = float ( lat_str ) , lng = float ( lng_str ) , name = name ) <EOL> stops [ name . lower ( ) ] = stop <EOL> def ProcessTable ( schedule , table ) : <EOL> if table [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' : <EOL> ProcessOptions ( schedule , table ) <EOL> elif table [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' : <EOL> AddStops ( schedule , table ) <EOL> else : <EOL> transposed = [ table [ <NUM_LIT:0> ] ] <EOL> transposed . extend ( TransposeTable ( table [ <NUM_LIT:1> : ] ) ) <EOL> AddRouteToSchedule ( schedule , transposed ) <EOL> def main ( ) : <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT:input>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . set_defaults ( output = '<STR_LIT>' ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> schedule = transitfeed . Schedule ( ) <EOL> table = [ ] <EOL> for line in open ( options . input ) : <EOL> line = line . rstrip ( ) <EOL> if not line : <EOL> ProcessTable ( schedule , table ) <EOL> table = [ ] <EOL> else : <EOL> table . append ( line . split ( '<STR_LIT:\t>' ) ) <EOL> ProcessTable ( schedule , table ) <EOL> schedule . WriteGoogleTransitFeed ( options . output ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
