<s> import bisect <EOL> import cStringIO as StringIO <EOL> import datetime <EOL> import itertools <EOL> import os <EOL> try : <EOL> import sqlite3 as sqlite <EOL> native_sqlite = True <EOL> except ImportError : <EOL> try : <EOL> from pysqlite2 import dbapi2 as sqlite <EOL> native_sqlite = True <EOL> except ImportError : <EOL> from com . ziclix . python . sql import zxJDBC as sqlite <EOL> native_sqlite = False <EOL> import tempfile <EOL> import time <EOL> import warnings <EOL> import weakref <EOL> import zipfile <EOL> import gtfsfactory <EOL> import problems as problems_module <EOL> from transitfeed . util import defaultdict <EOL> import util <EOL> class Schedule ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , problem_reporter = None , <EOL> memory_db = True , check_duplicate_trips = False , <EOL> gtfs_factory = None ) : <EOL> if gtfs_factory is None : <EOL> gtfs_factory = gtfsfactory . GetGtfsFactory ( ) <EOL> self . _gtfs_factory = gtfs_factory <EOL> self . _table_columns = { } <EOL> self . _agencies = { } <EOL> self . stops = { } <EOL> self . routes = { } <EOL> self . trips = { } <EOL> self . service_periods = { } <EOL> self . fares = { } <EOL> self . fare_zones = { } <EOL> self . feed_info = None <EOL> self . _shapes = { } <EOL> self . _transfers = defaultdict ( lambda : [ ] ) <EOL> self . _default_service_period = None <EOL> self . _default_agency = None <EOL> if problem_reporter is None : <EOL> self . problem_reporter = problems_module . default_problem_reporter <EOL> else : <EOL> self . problem_reporter = problem_reporter <EOL> self . _check_duplicate_trips = check_duplicate_trips <EOL> self . ConnectDb ( memory_db ) <EOL> def AddTableColumn ( self , table , column ) : <EOL> """<STR_LIT>""" <EOL> if column not in self . _table_columns [ table ] : <EOL> self . _table_columns [ table ] . append ( column ) <EOL> def AddTableColumns ( self , table , columns ) : <EOL> """<STR_LIT>""" <EOL> table_columns = self . _table_columns . setdefault ( table , [ ] ) <EOL> for attr in columns : <EOL> if attr not in table_columns : <EOL> table_columns . append ( attr ) <EOL> def GetTableColumns ( self , table ) : <EOL> """<STR_LIT>""" <EOL> return self . _table_columns [ table ] <EOL> def __del__ ( self ) : <EOL> self . _connection . cursor ( ) . close ( ) <EOL> self . _connection . close ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> os . remove ( self . _temp_db_filename ) <EOL> def ConnectDb ( self , memory_db ) : <EOL> def connector ( db_file ) : <EOL> if native_sqlite : <EOL> return sqlite . connect ( db_file ) <EOL> else : <EOL> return sqlite . connect ( "<STR_LIT>" % db_file , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if memory_db : <EOL> self . _connection = connector ( "<STR_LIT>" ) <EOL> else : <EOL> try : <EOL> self . _temp_db_file = tempfile . NamedTemporaryFile ( ) <EOL> self . _connection = connector ( self . _temp_db_file . name ) <EOL> except sqlite . OperationalError : <EOL> self . _temp_db_file = None <EOL> ( fd , self . _temp_db_filename ) = tempfile . mkstemp ( "<STR_LIT>" ) <EOL> os . close ( fd ) <EOL> self . _connection = connector ( self . _temp_db_filename ) <EOL> cursor = self . _connection . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> def GetStopBoundingBox ( self ) : <EOL> return ( min ( s . stop_lat for s in self . stops . values ( ) ) , <EOL> min ( s . stop_lon for s in self . stops . values ( ) ) , <EOL> max ( s . stop_lat for s in self . stops . values ( ) ) , <EOL> max ( s . stop_lon for s in self . stops . values ( ) ) , <EOL> ) <EOL> def AddAgency ( self , name , url , timezone , agency_id = None ) : <EOL> """<STR_LIT>""" <EOL> agency = self . _gtfs_factory . Agency ( name , url , timezone , agency_id ) <EOL> self . AddAgencyObject ( agency ) <EOL> return agency <EOL> def AddAgencyObject ( self , agency , problem_reporter = None , validate = False ) : <EOL> assert agency . _schedule is None <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> if agency . agency_id in self . _agencies : <EOL> problem_reporter . DuplicateID ( '<STR_LIT>' , agency . agency_id ) <EOL> return <EOL> self . AddTableColumns ( '<STR_LIT>' , agency . _ColumnNames ( ) ) <EOL> agency . _schedule = weakref . proxy ( self ) <EOL> if validate : <EOL> agency . Validate ( problem_reporter ) <EOL> self . _agencies [ agency . agency_id ] = agency <EOL> def GetAgency ( self , agency_id ) : <EOL> """<STR_LIT>""" <EOL> return self . _agencies [ agency_id ] <EOL> def GetDefaultAgency ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _default_agency : <EOL> if len ( self . _agencies ) == <NUM_LIT:0> : <EOL> self . NewDefaultAgency ( ) <EOL> elif len ( self . _agencies ) == <NUM_LIT:1> : <EOL> self . _default_agency = self . _agencies . values ( ) [ <NUM_LIT:0> ] <EOL> return self . _default_agency <EOL> def NewDefaultAgency ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> agency = self . _gtfs_factory . Agency ( ** kwargs ) <EOL> if not agency . agency_id : <EOL> agency . agency_id = util . FindUniqueId ( self . _agencies ) <EOL> self . _default_agency = agency <EOL> self . SetDefaultAgency ( agency , validate = False ) <EOL> return agency <EOL> def SetDefaultAgency ( self , agency , validate = True ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( agency , self . _gtfs_factory . Agency ) <EOL> self . _default_agency = agency <EOL> if agency . agency_id not in self . _agencies : <EOL> self . AddAgencyObject ( agency , validate = validate ) <EOL> def GetAgencyList ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _agencies . values ( ) <EOL> def GetServicePeriod ( self , service_id ) : <EOL> """<STR_LIT>""" <EOL> return self . service_periods [ service_id ] <EOL> def GetDefaultServicePeriod ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _default_service_period : <EOL> if len ( self . service_periods ) == <NUM_LIT:0> : <EOL> self . NewDefaultServicePeriod ( ) <EOL> elif len ( self . service_periods ) == <NUM_LIT:1> : <EOL> self . _default_service_period = self . service_periods . values ( ) [ <NUM_LIT:0> ] <EOL> return self . _default_service_period <EOL> def NewDefaultServicePeriod ( self ) : <EOL> """<STR_LIT>""" <EOL> service_period = self . _gtfs_factory . ServicePeriod ( ) <EOL> service_period . service_id = util . FindUniqueId ( self . service_periods ) <EOL> self . SetDefaultServicePeriod ( service_period , validate = False ) <EOL> return service_period <EOL> def SetDefaultServicePeriod ( self , service_period , validate = True ) : <EOL> assert isinstance ( service_period , self . _gtfs_factory . ServicePeriod ) <EOL> self . _default_service_period = service_period <EOL> if service_period . service_id not in self . service_periods : <EOL> self . AddServicePeriodObject ( service_period , validate = validate ) <EOL> def AddServicePeriodObject ( self , service_period , problem_reporter = None , <EOL> validate = True ) : <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> if service_period . service_id in self . service_periods : <EOL> problem_reporter . DuplicateID ( '<STR_LIT>' , service_period . service_id ) <EOL> return <EOL> if validate : <EOL> service_period . Validate ( problem_reporter ) <EOL> self . service_periods [ service_period . service_id ] = service_period <EOL> def GetServicePeriodList ( self ) : <EOL> return self . service_periods . values ( ) <EOL> def GetDateRange ( self ) : <EOL> """<STR_LIT>""" <EOL> ( minvalue , maxvalue , minorigin , maxorigin ) = self . GetDateRangeWithOrigins ( ) <EOL> return ( minvalue , maxvalue ) <EOL> def GetDateRangeWithOrigins ( self ) : <EOL> """<STR_LIT>""" <EOL> period_list = self . GetServicePeriodList ( ) <EOL> ranges = [ period . GetDateRange ( ) for period in period_list ] <EOL> starts = filter ( lambda x : x , [ item [ <NUM_LIT:0> ] for item in ranges ] ) <EOL> ends = filter ( lambda x : x , [ item [ <NUM_LIT:1> ] for item in ranges ] ) <EOL> if not starts or not ends : <EOL> return ( None , None , None , None ) <EOL> minvalue , minindex = min ( itertools . izip ( starts , itertools . count ( ) ) ) <EOL> maxvalue , maxindex = max ( itertools . izip ( ends , itertools . count ( ) ) ) <EOL> minreason = ( period_list [ minindex ] . HasDateExceptionOn ( minvalue ) and <EOL> "<STR_LIT>" or <EOL> "<STR_LIT>" ) <EOL> maxreason = ( period_list [ maxindex ] . HasDateExceptionOn ( maxvalue ) and <EOL> "<STR_LIT>" or <EOL> "<STR_LIT>" ) <EOL> if self . feed_info and self . feed_info . feed_start_date : <EOL> minvalue = self . feed_info . feed_start_date <EOL> minreason = "<STR_LIT>" <EOL> if self . feed_info and self . feed_info . feed_end_date : <EOL> maxvalue = self . feed_info . feed_end_date <EOL> maxreason = "<STR_LIT>" <EOL> return ( minvalue , maxvalue , minreason , maxreason ) <EOL> def GetServicePeriodsActiveEachDate ( self , date_start , date_end ) : <EOL> """<STR_LIT>""" <EOL> date_it = date_start <EOL> one_day = datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> date_service_period_list = [ ] <EOL> while date_it < date_end : <EOL> periods_today = [ ] <EOL> date_it_string = date_it . strftime ( "<STR_LIT>" ) <EOL> for service in self . GetServicePeriodList ( ) : <EOL> if service . IsActiveOn ( date_it_string , date_it ) : <EOL> periods_today . append ( service ) <EOL> date_service_period_list . append ( ( date_it , periods_today ) ) <EOL> date_it += one_day <EOL> return date_service_period_list <EOL> def AddStop ( self , lat , lng , name , stop_id = None ) : <EOL> """<STR_LIT>""" <EOL> if stop_id is None : <EOL> stop_id = util . FindUniqueId ( self . stops ) <EOL> stop = self . _gtfs_factory . Stop ( stop_id = stop_id , lat = lat , lng = lng , name = name ) <EOL> self . AddStopObject ( stop ) <EOL> return stop <EOL> def AddStopObject ( self , stop , problem_reporter = None ) : <EOL> """<STR_LIT>""" <EOL> assert stop . _schedule is None <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> if not stop . stop_id : <EOL> return <EOL> if stop . stop_id in self . stops : <EOL> problem_reporter . DuplicateID ( '<STR_LIT>' , stop . stop_id ) <EOL> return <EOL> stop . _schedule = weakref . proxy ( self ) <EOL> self . AddTableColumns ( '<STR_LIT>' , stop . _ColumnNames ( ) ) <EOL> self . stops [ stop . stop_id ] = stop <EOL> if hasattr ( stop , '<STR_LIT>' ) and stop . zone_id : <EOL> self . fare_zones [ stop . zone_id ] = True <EOL> def GetStopList ( self ) : <EOL> return self . stops . values ( ) <EOL> def AddRoute ( self , short_name , long_name , route_type , route_id = None ) : <EOL> """<STR_LIT>""" <EOL> if route_id is None : <EOL> route_id = util . FindUniqueId ( self . routes ) <EOL> route = self . _gtfs_factory . Route ( short_name = short_name , long_name = long_name , <EOL> route_type = route_type , route_id = route_id ) <EOL> route . agency_id = self . GetDefaultAgency ( ) . agency_id <EOL> self . AddRouteObject ( route ) <EOL> return route <EOL> def AddRouteObject ( self , route , problem_reporter = None ) : <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> if route . route_id in self . routes : <EOL> problem_reporter . DuplicateID ( '<STR_LIT>' , route . route_id ) <EOL> return <EOL> if route . agency_id not in self . _agencies : <EOL> if not route . agency_id and len ( self . _agencies ) == <NUM_LIT:1> : <EOL> pass <EOL> else : <EOL> problem_reporter . InvalidValue ( '<STR_LIT>' , route . agency_id , <EOL> '<STR_LIT>' ) <EOL> return <EOL> self . AddTableColumns ( '<STR_LIT>' , route . _ColumnNames ( ) ) <EOL> route . _schedule = weakref . proxy ( self ) <EOL> self . routes [ route . route_id ] = route <EOL> def GetRouteList ( self ) : <EOL> return self . routes . values ( ) <EOL> def GetRoute ( self , route_id ) : <EOL> return self . routes [ route_id ] <EOL> def AddShapeObject ( self , shape , problem_reporter = None ) : <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> shape . Validate ( problem_reporter ) <EOL> if shape . shape_id in self . _shapes : <EOL> problem_reporter . DuplicateID ( '<STR_LIT>' , shape . shape_id ) <EOL> return <EOL> self . _shapes [ shape . shape_id ] = shape <EOL> def GetShapeList ( self ) : <EOL> return self . _shapes . values ( ) <EOL> def GetShape ( self , shape_id ) : <EOL> return self . _shapes [ shape_id ] <EOL> def AddTripObject ( self , trip , problem_reporter = None , validate = False ) : <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> if trip . trip_id in self . trips : <EOL> problem_reporter . DuplicateID ( '<STR_LIT>' , trip . trip_id ) <EOL> return <EOL> self . AddTableColumns ( '<STR_LIT>' , trip . _ColumnNames ( ) ) <EOL> trip . _schedule = weakref . proxy ( self ) <EOL> self . trips [ trip . trip_id ] = trip <EOL> if validate : <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> trip . Validate ( problem_reporter , validate_children = False ) <EOL> try : <EOL> self . routes [ trip . route_id ] . _AddTripObject ( trip ) <EOL> except KeyError : <EOL> pass <EOL> def GetTripList ( self ) : <EOL> return self . trips . values ( ) <EOL> def GetTrip ( self , trip_id ) : <EOL> return self . trips [ trip_id ] <EOL> def AddFareObject ( self , fare , problem_reporter = None ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) <EOL> self . AddFareAttributeObject ( fare , problem_reporter ) <EOL> def AddFareAttributeObject ( self , fare , problem_reporter = None ) : <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> fare . Validate ( problem_reporter ) <EOL> if fare . fare_id in self . fares : <EOL> problem_reporter . DuplicateID ( '<STR_LIT>' , fare . fare_id ) <EOL> return <EOL> self . fares [ fare . fare_id ] = fare <EOL> def GetFareList ( self ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) <EOL> return self . GetFareAttributeList ( ) <EOL> def GetFareAttributeList ( self ) : <EOL> return self . fares . values ( ) <EOL> def GetFare ( self , fare_id ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) <EOL> return self . GetFareAttribute ( fare_id ) <EOL> def GetFareAttribute ( self , fare_id ) : <EOL> return self . fares [ fare_id ] <EOL> def AddFareRuleObject ( self , rule , problem_reporter = None ) : <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> if util . IsEmpty ( rule . fare_id ) : <EOL> problem_reporter . MissingValue ( '<STR_LIT>' ) <EOL> return <EOL> if rule . route_id and rule . route_id not in self . routes : <EOL> problem_reporter . InvalidValue ( '<STR_LIT>' , rule . route_id ) <EOL> if rule . origin_id and rule . origin_id not in self . fare_zones : <EOL> problem_reporter . InvalidValue ( '<STR_LIT>' , rule . origin_id ) <EOL> if rule . destination_id and rule . destination_id not in self . fare_zones : <EOL> problem_reporter . InvalidValue ( '<STR_LIT>' , rule . destination_id ) <EOL> if rule . contains_id and rule . contains_id not in self . fare_zones : <EOL> problem_reporter . InvalidValue ( '<STR_LIT>' , rule . contains_id ) <EOL> if rule . fare_id in self . fares : <EOL> self . GetFareAttribute ( rule . fare_id ) . rules . append ( rule ) <EOL> else : <EOL> problem_reporter . InvalidValue ( '<STR_LIT>' , rule . fare_id , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def AddFeedInfoObject ( self , feed_info , problem_reporter = None , validate = False ) : <EOL> assert feed_info . _schedule is None <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> feed_info . _schedule = weakref . proxy ( self ) <EOL> if validate : <EOL> feed_info . Validate ( problem_reporter ) <EOL> self . feed_info = feed_info <EOL> def AddTransferObject ( self , transfer , problem_reporter = None ) : <EOL> assert transfer . _schedule is None , "<STR_LIT>" <EOL> if not problem_reporter : <EOL> problem_reporter = self . problem_reporter <EOL> transfer_id = transfer . _ID ( ) <EOL> if transfer_id in self . _transfers : <EOL> self . problem_reporter . DuplicateID ( self . _gtfs_factory . Transfer . _ID_COLUMNS , <EOL> transfer_id , <EOL> type = problems_module . TYPE_WARNING ) <EOL> transfer . _schedule = weakref . proxy ( self ) <EOL> self . AddTableColumns ( '<STR_LIT>' , transfer . _ColumnNames ( ) ) <EOL> self . _transfers [ transfer_id ] . append ( transfer ) <EOL> def GetTransferIter ( self ) : <EOL> """<STR_LIT>""" <EOL> return itertools . chain ( * self . _transfers . values ( ) ) <EOL> def GetTransferList ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . GetTransferIter ( ) ) <EOL> def GetStop ( self , id ) : <EOL> return self . stops [ id ] <EOL> def GetFareZones ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . fare_zones . keys ( ) <EOL> def GetNearestStops ( self , lat , lon , n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> dist_stop_list = [ ] <EOL> for s in self . stops . values ( ) : <EOL> dist = ( s . stop_lat - lat ) ** <NUM_LIT:2> + ( s . stop_lon - lon ) ** <NUM_LIT:2> <EOL> if len ( dist_stop_list ) < n : <EOL> bisect . insort ( dist_stop_list , ( dist , s ) ) <EOL> elif dist < dist_stop_list [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] : <EOL> bisect . insort ( dist_stop_list , ( dist , s ) ) <EOL> dist_stop_list . pop ( ) <EOL> return [ stop for dist , stop in dist_stop_list ] <EOL> def GetStopsInBoundingBox ( self , north , east , south , west , n ) : <EOL> """<STR_LIT>""" <EOL> stop_list = [ ] <EOL> for s in self . stops . values ( ) : <EOL> if ( s . stop_lat <= north and s . stop_lat >= south and <EOL> s . stop_lon <= east and s . stop_lon >= west ) : <EOL> stop_list . append ( s ) <EOL> if len ( stop_list ) == n : <EOL> break <EOL> return stop_list <EOL> def Load ( self , feed_path , extra_validation = False ) : <EOL> loader = self . _gtfs_factory . Loader ( feed_path , <EOL> self , problems = self . problem_reporter , <EOL> extra_validation = extra_validation ) <EOL> loader . Load ( ) <EOL> def _WriteArchiveString ( self , archive , filename , stringio ) : <EOL> zi = zipfile . ZipInfo ( filename ) <EOL> zi . external_attr = <NUM_LIT:0> <NUM_LIT> << <NUM_LIT:16> L <EOL> zi . compress_type = zipfile . ZIP_DEFLATED <EOL> archive . writestr ( zi , stringio . getvalue ( ) ) <EOL> def WriteGoogleTransitFeed ( self , file ) : <EOL> """<STR_LIT>""" <EOL> archive = zipfile . ZipFile ( file , '<STR_LIT:w>' ) <EOL> if '<STR_LIT>' in self . _table_columns : <EOL> agency_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( agency_string ) <EOL> columns = self . GetTableColumns ( '<STR_LIT>' ) <EOL> writer . writerow ( columns ) <EOL> for a in self . _agencies . values ( ) : <EOL> writer . writerow ( [ util . EncodeUnicode ( a [ c ] ) for c in columns ] ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , agency_string ) <EOL> calendar_dates_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( calendar_dates_string ) <EOL> writer . writerow ( <EOL> self . _gtfs_factory . ServicePeriod . _FIELD_NAMES_CALENDAR_DATES ) <EOL> has_data = False <EOL> for period in self . service_periods . values ( ) : <EOL> for row in period . GenerateCalendarDatesFieldValuesTuples ( ) : <EOL> has_data = True <EOL> writer . writerow ( row ) <EOL> wrote_calendar_dates = False <EOL> if has_data : <EOL> wrote_calendar_dates = True <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , <EOL> calendar_dates_string ) <EOL> calendar_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( calendar_string ) <EOL> writer . writerow ( self . _gtfs_factory . ServicePeriod . _FIELD_NAMES ) <EOL> has_data = False <EOL> for s in self . service_periods . values ( ) : <EOL> row = s . GetCalendarFieldValuesTuple ( ) <EOL> if row : <EOL> has_data = True <EOL> writer . writerow ( row ) <EOL> if has_data or not wrote_calendar_dates : <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , calendar_string ) <EOL> if '<STR_LIT>' in self . _table_columns : <EOL> stop_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( stop_string ) <EOL> columns = self . GetTableColumns ( '<STR_LIT>' ) <EOL> writer . writerow ( columns ) <EOL> for s in self . stops . values ( ) : <EOL> writer . writerow ( [ util . EncodeUnicode ( s [ c ] ) for c in columns ] ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , stop_string ) <EOL> if '<STR_LIT>' in self . _table_columns : <EOL> route_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( route_string ) <EOL> columns = self . GetTableColumns ( '<STR_LIT>' ) <EOL> writer . writerow ( columns ) <EOL> for r in self . routes . values ( ) : <EOL> writer . writerow ( [ util . EncodeUnicode ( r [ c ] ) for c in columns ] ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , route_string ) <EOL> if '<STR_LIT>' in self . _table_columns : <EOL> trips_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( trips_string ) <EOL> columns = self . GetTableColumns ( '<STR_LIT>' ) <EOL> writer . writerow ( columns ) <EOL> for t in self . trips . values ( ) : <EOL> writer . writerow ( [ util . EncodeUnicode ( t [ c ] ) for c in columns ] ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , trips_string ) <EOL> headway_rows = [ ] <EOL> for trip in self . GetTripList ( ) : <EOL> headway_rows += trip . GetFrequencyOutputTuples ( ) <EOL> if headway_rows : <EOL> headway_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( headway_string ) <EOL> writer . writerow ( self . _gtfs_factory . Frequency . _FIELD_NAMES ) <EOL> writer . writerows ( headway_rows ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , headway_string ) <EOL> if self . GetFareAttributeList ( ) : <EOL> fare_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( fare_string ) <EOL> writer . writerow ( self . _gtfs_factory . FareAttribute . _FIELD_NAMES ) <EOL> writer . writerows ( <EOL> f . GetFieldValuesTuple ( ) for f in self . GetFareAttributeList ( ) ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , fare_string ) <EOL> rule_rows = [ ] <EOL> for fare in self . GetFareAttributeList ( ) : <EOL> for rule in fare . GetFareRuleList ( ) : <EOL> rule_rows . append ( rule . GetFieldValuesTuple ( ) ) <EOL> if rule_rows : <EOL> rule_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( rule_string ) <EOL> writer . writerow ( self . _gtfs_factory . FareRule . _FIELD_NAMES ) <EOL> writer . writerows ( rule_rows ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , rule_string ) <EOL> stop_times_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( stop_times_string ) <EOL> writer . writerow ( self . _gtfs_factory . StopTime . _FIELD_NAMES ) <EOL> for t in self . trips . values ( ) : <EOL> writer . writerows ( t . _GenerateStopTimesTuples ( ) ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , stop_times_string ) <EOL> shape_rows = [ ] <EOL> for shape in self . GetShapeList ( ) : <EOL> seq = <NUM_LIT:1> <EOL> for ( lat , lon , dist ) in shape . points : <EOL> shape_rows . append ( ( shape . shape_id , lat , lon , seq , dist ) ) <EOL> seq += <NUM_LIT:1> <EOL> if shape_rows : <EOL> shape_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( shape_string ) <EOL> writer . writerow ( self . _gtfs_factory . Shape . _FIELD_NAMES ) <EOL> writer . writerows ( shape_rows ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , shape_string ) <EOL> if '<STR_LIT>' in self . _table_columns : <EOL> transfer_string = StringIO . StringIO ( ) <EOL> writer = util . CsvUnicodeWriter ( transfer_string ) <EOL> columns = self . GetTableColumns ( '<STR_LIT>' ) <EOL> writer . writerow ( columns ) <EOL> for t in self . GetTransferIter ( ) : <EOL> writer . writerow ( [ util . EncodeUnicode ( t [ c ] ) for c in columns ] ) <EOL> self . _WriteArchiveString ( archive , '<STR_LIT>' , transfer_string ) <EOL> archive . close ( ) <EOL> def GenerateDateTripsDeparturesList ( self , date_start , date_end ) : <EOL> """<STR_LIT>""" <EOL> service_id_to_trips = defaultdict ( lambda : <NUM_LIT:0> ) <EOL> service_id_to_departures = defaultdict ( lambda : <NUM_LIT:0> ) <EOL> for trip in self . GetTripList ( ) : <EOL> headway_start_times = trip . GetFrequencyStartTimes ( ) <EOL> if headway_start_times : <EOL> trip_runs = len ( headway_start_times ) <EOL> else : <EOL> trip_runs = <NUM_LIT:1> <EOL> service_id_to_trips [ trip . service_id ] += trip_runs <EOL> service_id_to_departures [ trip . service_id ] += ( <EOL> ( trip . GetCountStopTimes ( ) - <NUM_LIT:1> ) * trip_runs ) <EOL> date_services = self . GetServicePeriodsActiveEachDate ( date_start , date_end ) <EOL> date_trips = [ ] <EOL> for date , services in date_services : <EOL> day_trips = sum ( service_id_to_trips [ s . service_id ] for s in services ) <EOL> day_departures = sum ( <EOL> service_id_to_departures [ s . service_id ] for s in services ) <EOL> date_trips . append ( ( date , day_trips , day_departures ) ) <EOL> return date_trips <EOL> def ValidateAgenciesHaveSameAgencyTimezone ( self , problems ) : <EOL> timezones_set = set ( map ( lambda agency : agency . agency_timezone , <EOL> self . GetAgencyList ( ) ) ) <EOL> if len ( timezones_set ) > <NUM_LIT:1> : <EOL> timezones_str = '<STR_LIT>' % ( '<STR_LIT>' . join ( timezones_set ) ) <EOL> problems . InvalidValue ( '<STR_LIT>' , timezones_str , <EOL> '<STR_LIT>' '<STR_LIT>' ) <EOL> def ValidateFeedInfoLangMatchesAgencyLang ( self , problems ) : <EOL> if self . feed_info is None : <EOL> return <EOL> if self . feed_info . feed_lang is None : <EOL> return <EOL> agencies = self . GetAgencyList ( ) <EOL> for agency in agencies : <EOL> if not util . IsEmpty ( agency . agency_lang ) and ( <EOL> not self . feed_info . feed_lang == agency . agency_lang ) : <EOL> problems . InvalidValue ( "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> agency . agency_id ) <EOL> def ValidateFeedStartAndExpirationDates ( self , problems , first_date , last_date , <EOL> first_date_origin , last_date_origin , <EOL> today ) : <EOL> """<STR_LIT>""" <EOL> warning_cutoff = today + datetime . timedelta ( days = <NUM_LIT> ) <EOL> if last_date < warning_cutoff : <EOL> problems . ExpirationDate ( time . mktime ( last_date . timetuple ( ) ) , <EOL> last_date_origin ) <EOL> if first_date > today : <EOL> problems . FutureService ( time . mktime ( first_date . timetuple ( ) ) , <EOL> first_date_origin ) <EOL> def ValidateServiceGaps ( self , <EOL> problems , <EOL> validation_start_date , <EOL> validation_end_date , <EOL> service_gap_interval ) : <EOL> """<STR_LIT>""" <EOL> if service_gap_interval is None : <EOL> return <EOL> departures = self . GenerateDateTripsDeparturesList ( validation_start_date , <EOL> validation_end_date ) <EOL> first_day_without_service = validation_start_date <EOL> last_day_without_service = validation_start_date <EOL> consecutive_days_without_service = <NUM_LIT:0> <EOL> for day_date , day_trips , _ in departures : <EOL> if day_trips == <NUM_LIT:0> : <EOL> if consecutive_days_without_service == <NUM_LIT:0> : <EOL> first_day_without_service = day_date <EOL> consecutive_days_without_service += <NUM_LIT:1> <EOL> last_day_without_service = day_date <EOL> else : <EOL> if consecutive_days_without_service >= service_gap_interval : <EOL> problems . TooManyDaysWithoutService ( first_day_without_service , <EOL> last_day_without_service , <EOL> consecutive_days_without_service ) <EOL> consecutive_days_without_service = <NUM_LIT:0> <EOL> if consecutive_days_without_service >= service_gap_interval : <EOL> problems . TooManyDaysWithoutService ( first_day_without_service , <EOL> last_day_without_service , <EOL> consecutive_days_without_service ) <EOL> def ValidateServiceExceptions ( self , <EOL> problems , <EOL> first_service_day , <EOL> last_service_day ) : <EOL> six_months = datetime . timedelta ( days = <NUM_LIT> ) <EOL> service_span = last_service_day - first_service_day <EOL> if service_span < six_months : <EOL> return <EOL> for period in self . GetServicePeriodList ( ) : <EOL> if period . HasExceptions ( ) : <EOL> return <EOL> problems . NoServiceExceptions ( start = first_service_day , <EOL> end = last_service_day ) <EOL> def ValidateServiceRangeAndExceptions ( self , problems , today , <EOL> service_gap_interval ) : <EOL> if today is None : <EOL> today = datetime . date . today ( ) <EOL> ( start_date , end_date , <EOL> start_date_origin , end_date_origin ) = self . GetDateRangeWithOrigins ( ) <EOL> if not end_date or not start_date : <EOL> problems . OtherProblem ( '<STR_LIT>' , <EOL> type = problems_module . TYPE_WARNING ) <EOL> else : <EOL> try : <EOL> last_service_day = datetime . datetime ( <EOL> * ( time . strptime ( end_date , "<STR_LIT>" ) [ <NUM_LIT:0> : <NUM_LIT:6> ] ) ) . date ( ) <EOL> first_service_day = datetime . datetime ( <EOL> * ( time . strptime ( start_date , "<STR_LIT>" ) [ <NUM_LIT:0> : <NUM_LIT:6> ] ) ) . date ( ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . ValidateServiceExceptions ( problems , <EOL> first_service_day , <EOL> last_service_day ) <EOL> self . ValidateFeedStartAndExpirationDates ( problems , <EOL> first_service_day , <EOL> last_service_day , <EOL> start_date_origin , <EOL> end_date_origin , <EOL> today ) <EOL> if service_gap_interval is not None : <EOL> service_gap_timedelta = datetime . timedelta ( <EOL> days = service_gap_interval - <NUM_LIT:1> ) <EOL> one_year = datetime . timedelta ( days = <NUM_LIT> ) <EOL> self . ValidateServiceGaps ( <EOL> problems , <EOL> max ( first_service_day , <EOL> today - service_gap_timedelta ) , <EOL> min ( last_service_day , <EOL> today + one_year ) , <EOL> service_gap_interval ) <EOL> def ValidateStops ( self , problems , validate_children ) : <EOL> for stop in self . stops . values ( ) : <EOL> if validate_children : <EOL> stop . Validate ( problems ) <EOL> cursor = self . _connection . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( stop . stop_id , ) ) <EOL> count = cursor . fetchone ( ) [ <NUM_LIT:0> ] <EOL> if stop . location_type == <NUM_LIT:0> and count == <NUM_LIT:0> : <EOL> problems . UnusedStop ( stop . stop_id , stop . stop_name ) <EOL> elif stop . location_type == <NUM_LIT:1> and count != <NUM_LIT:0> : <EOL> problems . UsedStation ( stop . stop_id , stop . stop_name ) <EOL> if stop . location_type != <NUM_LIT:1> and stop . parent_station : <EOL> if stop . parent_station not in self . stops : <EOL> problems . InvalidValue ( "<STR_LIT>" , <EOL> util . EncodeUnicode ( stop . parent_station ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( util . EncodeUnicode ( stop . parent_station ) , <EOL> util . EncodeUnicode ( stop . stop_id ) ) ) <EOL> elif self . stops [ stop . parent_station ] . location_type != <NUM_LIT:1> : <EOL> problems . InvalidValue ( "<STR_LIT>" , <EOL> util . EncodeUnicode ( stop . parent_station ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( util . EncodeUnicode ( stop . parent_station ) , <EOL> util . EncodeUnicode ( stop . stop_id ) ) ) <EOL> else : <EOL> parent_station = self . stops [ stop . parent_station ] <EOL> distance = util . ApproximateDistanceBetweenStops ( stop , parent_station ) <EOL> if distance is not None : <EOL> if distance > problems_module . MAX_DISTANCE_BETWEEN_STOP_AND_PARENT_STATION_ERROR : <EOL> problems . StopTooFarFromParentStation ( <EOL> stop . stop_id , stop . stop_name , parent_station . stop_id , <EOL> parent_station . stop_name , distance , problems_module . TYPE_ERROR ) <EOL> elif distance > problems_module . MAX_DISTANCE_BETWEEN_STOP_AND_PARENT_STATION_WARNING : <EOL> problems . StopTooFarFromParentStation ( <EOL> stop . stop_id , stop . stop_name , parent_station . stop_id , <EOL> parent_station . stop_name , distance , <EOL> problems_module . TYPE_WARNING ) <EOL> def ValidateNearbyStops ( self , problems ) : <EOL> sorted_stops = filter ( lambda s : s . stop_lat and s . stop_lon , <EOL> self . GetStopList ( ) ) <EOL> sorted_stops . sort ( <EOL> key = ( lambda x : [ x . stop_lat , x . stop_lon , getattr ( x , '<STR_LIT>' , None ) ] ) ) <EOL> TWO_METERS_LAT = <NUM_LIT> <EOL> for index , stop in enumerate ( sorted_stops [ : - <NUM_LIT:1> ] ) : <EOL> index += <NUM_LIT:1> <EOL> while ( ( index < len ( sorted_stops ) ) and <EOL> ( ( sorted_stops [ index ] . stop_lat - stop . stop_lat ) < TWO_METERS_LAT ) ) : <EOL> distance = util . ApproximateDistanceBetweenStops ( stop , <EOL> sorted_stops [ index ] ) <EOL> if distance is not None and distance < <NUM_LIT:2> : <EOL> other_stop = sorted_stops [ index ] <EOL> if stop . location_type == <NUM_LIT:0> and other_stop . location_type == <NUM_LIT:0> : <EOL> problems . StopsTooClose ( <EOL> util . EncodeUnicode ( stop . stop_name ) , <EOL> util . EncodeUnicode ( stop . stop_id ) , <EOL> util . EncodeUnicode ( other_stop . stop_name ) , <EOL> util . EncodeUnicode ( other_stop . stop_id ) , distance ) <EOL> elif stop . location_type == <NUM_LIT:1> and other_stop . location_type == <NUM_LIT:1> : <EOL> problems . StationsTooClose ( <EOL> util . EncodeUnicode ( stop . stop_name ) , <EOL> util . EncodeUnicode ( stop . stop_id ) , <EOL> util . EncodeUnicode ( other_stop . stop_name ) , <EOL> util . EncodeUnicode ( other_stop . stop_id ) , distance ) <EOL> elif ( stop . location_type in ( <NUM_LIT:0> , <NUM_LIT:1> ) and <EOL> other_stop . location_type in ( <NUM_LIT:0> , <NUM_LIT:1> ) ) : <EOL> if stop . location_type == <NUM_LIT:0> and other_stop . location_type == <NUM_LIT:1> : <EOL> this_stop = stop <EOL> this_station = other_stop <EOL> elif stop . location_type == <NUM_LIT:1> and other_stop . location_type == <NUM_LIT:0> : <EOL> this_stop = other_stop <EOL> this_station = stop <EOL> if this_stop . parent_station != this_station . stop_id : <EOL> problems . DifferentStationTooClose ( <EOL> util . EncodeUnicode ( this_stop . stop_name ) , <EOL> util . EncodeUnicode ( this_stop . stop_id ) , <EOL> util . EncodeUnicode ( this_station . stop_name ) , <EOL> util . EncodeUnicode ( this_station . stop_id ) , distance ) <EOL> index += <NUM_LIT:1> <EOL> def ValidateRouteNames ( self , problems , validate_children ) : <EOL> route_names = { } <EOL> for route in self . routes . values ( ) : <EOL> if validate_children : <EOL> route . Validate ( problems ) <EOL> short_name = '<STR_LIT>' <EOL> if not util . IsEmpty ( route . route_short_name ) : <EOL> short_name = route . route_short_name . lower ( ) . strip ( ) <EOL> long_name = '<STR_LIT>' <EOL> if not util . IsEmpty ( route . route_long_name ) : <EOL> long_name = route . route_long_name . lower ( ) . strip ( ) <EOL> name = ( short_name , long_name ) <EOL> if name in route_names : <EOL> problems . InvalidValue ( '<STR_LIT>' , <EOL> long_name , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( route . route_id , route_names [ name ] . route_id ) , <EOL> type = problems_module . TYPE_WARNING ) <EOL> else : <EOL> route_names [ name ] = route <EOL> def ValidateTrips ( self , problems ) : <EOL> stop_types = { } <EOL> trips = { } <EOL> trip_intervals_by_block_id = defaultdict ( lambda : [ ] ) <EOL> for trip in sorted ( self . trips . values ( ) ) : <EOL> if trip . route_id not in self . routes : <EOL> continue <EOL> route_type = self . GetRoute ( trip . route_id ) . route_type <EOL> stop_ids = [ ] <EOL> stop_times = trip . GetStopTimes ( problems ) <EOL> self . ValidateStopTimesForTrip ( problems , trip , stop_times ) <EOL> for index , st in enumerate ( stop_times ) : <EOL> stop_id = st . stop . stop_id <EOL> stop_ids . append ( stop_id ) <EOL> if ( route_type == self . _gtfs_factory . Route . _ROUTE_TYPE_NAMES [ '<STR_LIT>' ] or <EOL> route_type == self . _gtfs_factory . Route . _ROUTE_TYPE_NAMES [ '<STR_LIT>' ] ) : <EOL> if stop_id not in stop_types : <EOL> stop_types [ stop_id ] = [ trip . route_id , route_type , <NUM_LIT:0> ] <EOL> elif ( stop_types [ stop_id ] [ <NUM_LIT:1> ] != route_type and <EOL> stop_types [ stop_id ] [ <NUM_LIT:2> ] == <NUM_LIT:0> ) : <EOL> stop_types [ stop_id ] [ <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> if stop_types [ stop_id ] [ <NUM_LIT:1> ] == self . _gtfs_factory . Route . _ROUTE_TYPE_NAMES [ '<STR_LIT>' ] : <EOL> subway_route_id = stop_types [ stop_id ] [ <NUM_LIT:0> ] <EOL> bus_route_id = trip . route_id <EOL> else : <EOL> subway_route_id = trip . route_id <EOL> bus_route_id = stop_types [ stop_id ] [ <NUM_LIT:0> ] <EOL> problems . StopWithMultipleRouteTypes ( st . stop . stop_name , stop_id , <EOL> subway_route_id , bus_route_id ) <EOL> if not util . IsEmpty ( trip . block_id ) and stop_times : <EOL> first_arrival_secs = stop_times [ <NUM_LIT:0> ] . arrival_secs <EOL> last_departure_secs = stop_times [ - <NUM_LIT:1> ] . departure_secs <EOL> if first_arrival_secs is not None and last_departure_secs is not None : <EOL> key = trip . block_id <EOL> trip_intervals = trip_intervals_by_block_id [ key ] <EOL> trip_interval = ( trip , first_arrival_secs , last_departure_secs ) <EOL> trip_intervals . append ( trip_interval ) <EOL> if self . _check_duplicate_trips : <EOL> if not stop_ids or not stop_times : <EOL> continue <EOL> key = ( trip . service_id , stop_times [ <NUM_LIT:0> ] . arrival_time , str ( stop_ids ) ) <EOL> if key not in trips : <EOL> trips [ key ] = ( trip . route_id , trip . trip_id ) <EOL> else : <EOL> problems . DuplicateTrip ( trips [ key ] [ <NUM_LIT:1> ] , trips [ key ] [ <NUM_LIT:0> ] , trip . trip_id , <EOL> trip . route_id ) <EOL> self . ValidateBlocks ( problems , trip_intervals_by_block_id ) <EOL> def ValidateStopTimesForTrip ( self , problems , trip , stop_times ) : <EOL> """<STR_LIT>""" <EOL> prev_departure_secs = - <NUM_LIT:1> <EOL> consecutive_stop_times_with_potentially_same_time = <NUM_LIT:0> <EOL> consecutive_stop_times_with_fully_specified_same_time = <NUM_LIT:0> <EOL> def CheckSameTimeCount ( ) : <EOL> if ( prev_departure_secs != - <NUM_LIT:1> and <EOL> consecutive_stop_times_with_fully_specified_same_time > <NUM_LIT:5> ) : <EOL> problems . TooManyConsecutiveStopTimesWithSameTime ( trip . trip_id , <EOL> consecutive_stop_times_with_fully_specified_same_time , <EOL> prev_departure_secs ) <EOL> for index , st in enumerate ( stop_times ) : <EOL> if st . arrival_secs is None or st . departure_secs is None : <EOL> consecutive_stop_times_with_potentially_same_time += <NUM_LIT:1> <EOL> continue <EOL> if ( prev_departure_secs == st . arrival_secs and <EOL> st . arrival_secs == st . departure_secs ) : <EOL> consecutive_stop_times_with_potentially_same_time += <NUM_LIT:1> <EOL> consecutive_stop_times_with_fully_specified_same_time = ( <EOL> consecutive_stop_times_with_potentially_same_time ) <EOL> else : <EOL> CheckSameTimeCount ( ) <EOL> consecutive_stop_times_with_potentially_same_time = <NUM_LIT:1> <EOL> consecutive_stop_times_with_fully_specified_same_time = <NUM_LIT:1> <EOL> prev_departure_secs = st . departure_secs <EOL> CheckSameTimeCount ( ) <EOL> def ValidateBlocks ( self , problems , trip_intervals_by_block_id ) : <EOL> service_period_overlap_cache = { } <EOL> for ( block_id , trip_intervals ) in trip_intervals_by_block_id . items ( ) : <EOL> trip_intervals . sort ( key = ( lambda x : x [ <NUM_LIT:1> ] ) ) <EOL> for xi in range ( len ( trip_intervals ) ) : <EOL> trip_interval_a = trip_intervals [ xi ] <EOL> trip_a = trip_interval_a [ <NUM_LIT:0> ] <EOL> for xj in range ( xi + <NUM_LIT:1> , len ( trip_intervals ) ) : <EOL> trip_interval_b = trip_intervals [ xj ] <EOL> trip_b = trip_interval_b [ <NUM_LIT:0> ] <EOL> if trip_interval_a [ <NUM_LIT:2> ] <= trip_interval_b [ <NUM_LIT:1> ] : <EOL> break <EOL> if trip_a . service_id == trip_b . service_id : <EOL> problems . OverlappingTripsInSameBlock ( trip_a . trip_id , <EOL> trip_b . trip_id , block_id ) <EOL> else : <EOL> service_id_pair_key = tuple ( sorted ( [ trip_a . service_id , <EOL> trip_b . service_id ] ) ) <EOL> if service_id_pair_key not in service_period_overlap_cache : <EOL> if trip_a . service_id not in self . service_periods : <EOL> return <EOL> if trip_b . service_id not in self . service_periods : <EOL> return <EOL> service_period_a = self . GetServicePeriod ( trip_a . service_id ) <EOL> service_period_b = self . GetServicePeriod ( trip_b . service_id ) <EOL> dates_a = service_period_a . ActiveDates ( ) <EOL> dates_b = service_period_b . ActiveDates ( ) <EOL> overlap = False <EOL> for date in dates_a : <EOL> if date in dates_b : <EOL> overlap = True <EOL> break <EOL> service_period_overlap_cache [ service_id_pair_key ] = overlap <EOL> if service_period_overlap_cache [ service_id_pair_key ] : <EOL> problems . OverlappingTripsInSameBlock ( trip_a . trip_id , <EOL> trip_b . trip_id , <EOL> block_id ) <EOL> def ValidateRouteAgencyId ( self , problems ) : <EOL> for route in self . routes . values ( ) : <EOL> if ( not util . IsEmpty ( route . agency_id ) and <EOL> not route . agency_id in self . _agencies ) : <EOL> problems . InvalidAgencyID ( '<STR_LIT>' , route . agency_id , <EOL> '<STR_LIT>' , route . route_id ) <EOL> def ValidateTripStopTimes ( self , problems ) : <EOL> for trip in self . trips . values ( ) : <EOL> trip . ValidateChildren ( problems ) <EOL> count_stop_times = trip . GetCountStopTimes ( ) <EOL> if not count_stop_times : <EOL> problems . OtherProblem ( '<STR_LIT>' <EOL> '<STR_LIT>' % trip . trip_id , <EOL> type = problems_module . TYPE_WARNING ) <EOL> if len ( trip . _headways ) > <NUM_LIT:0> : <EOL> problems . OtherProblem ( '<STR_LIT>' <EOL> '<STR_LIT>' % trip . trip_id , <EOL> type = problems_module . TYPE_ERROR ) <EOL> elif count_stop_times == <NUM_LIT:1> : <EOL> problems . OtherProblem ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> trip . trip_id , type = problems_module . TYPE_WARNING ) <EOL> else : <EOL> trip . GetStartTime ( problems = problems ) <EOL> trip . GetEndTime ( problems = problems ) <EOL> def ValidateUnusedShapes ( self , problems ) : <EOL> known_shape_ids = set ( self . _shapes . keys ( ) ) <EOL> used_shape_ids = set ( ) <EOL> for trip in self . GetTripList ( ) : <EOL> used_shape_ids . add ( trip . shape_id ) <EOL> unused_shape_ids = known_shape_ids - used_shape_ids <EOL> if unused_shape_ids : <EOL> problems . OtherProblem ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( unused_shape_ids ) , <EOL> type = problems_module . TYPE_WARNING ) <EOL> def Validate ( self , <EOL> problems = None , <EOL> validate_children = True , <EOL> today = None , <EOL> service_gap_interval = None ) : <EOL> """<STR_LIT>""" <EOL> if not problems : <EOL> problems = self . problem_reporter <EOL> self . ValidateAgenciesHaveSameAgencyTimezone ( problems ) <EOL> self . ValidateFeedInfoLangMatchesAgencyLang ( problems ) <EOL> self . ValidateServiceRangeAndExceptions ( problems , today , <EOL> service_gap_interval ) <EOL> self . ValidateStops ( problems , validate_children ) <EOL> self . ValidateNearbyStops ( problems ) <EOL> self . ValidateRouteNames ( problems , validate_children ) <EOL> self . ValidateTrips ( problems ) <EOL> self . ValidateRouteAgencyId ( problems ) <EOL> self . ValidateTripStopTimes ( problems ) <EOL> self . ValidateUnusedShapes ( problems ) </s>
