Exemple #1
0
 def testGetAddrInfoFailed(self):
     urllib.request.urlopen = self.mock.mocked_get_addr_info_failed
     util.check_version(self.problems)
     e = self.accumulator.PopException('OtherProblem')
     self.assertTrue(re.search(r'we failed to reach', e.description))
     self.assertTrue(re.search(r'Reason: Getaddrinfo failed',
                               e.description))
def run_validation(feed, options, problems):
    """Validate feed, returning the loaded Schedule and exit code.

    Args:
      feed: GTFS file, either path of the file as a string or a file object
      options: options object returned by optparse
      problems: transitfeed.ProblemReporter instance

    Returns:
      a transitfeed.Schedule object, exit code and plain text string of other
      problems
      Exit code is 2 if an extension is provided but can't be loaded, 1 if
      problems are found and 0 if the Schedule is problem free.
      plain text string is '' if no other problems are found.
    """
    util.check_version(problems, options.latest_version)

    # TODO: Add tests for this flag in testfeedvalidator.py
    extension_module = transitfeed

    if options.extension:
        try:
            __import__(options.extension)
            extension_module = sys.modules[options.extension]
        except ImportError:
            # TODO: Document extensions in a wiki page, place link here
            print(
                "Could not import extension %s! Please ensure it is a proper "
                "Python module." % options.extension)
            exit(2)

    gtfs_factory = extension_module.get_gtfs_factory()

    print('validating %s' % feed)
    print('FeedValidator extension used: %s' % options.extension)
    loader = gtfs_factory.Loader(
        feed,
        loader_problems=problems,
        extra_validation=False,
        memory_db=options.memory_db,
        check_duplicate_trips=options.check_duplicate_trips,
        gtfs_factory=gtfs_factory)
    schedule = loader.load()
    # Start validation: children are already validated by the loader.
    schedule.validate(service_gap_interval=options.service_gap_interval,
                      validate_children=False)

    if feed == 'IWantMyvalidation-crash.txt':
        # See tests/testfeedvalidator.py
        raise Exception('For testing the feed validator crash handler.')

    accumulator = problems.get_accumulator()
    if accumulator.has_issues():
        print('ERROR: %s found' % accumulator.format_count())
        return schedule, 1
    else:
        print('feed validated successfully')
        return schedule, 0
Exemple #3
0
 def testEmptyIsReturned(self):
     urllib.request.urlopen = self.mock.mocked_empty_is_returned
     util.check_version(self.problems)
     e = self.accumulator.PopException('OtherProblem')
     self.assertTrue(re.search(r'we had trouble parsing', e.description))
Exemple #4
0
 def testConnectionTimeOut(self):
     urllib.request.urlopen = self.mock.mocked_connection_time_out
     util.check_version(self.problems)
     e = self.accumulator.PopException('OtherProblem')
     self.assertTrue(re.search(r'we failed to reach', e.description))
     self.assertTrue(re.search(r'Reason: Connection timed', e.description))
Exemple #5
0
 def testPageNotFound(self):
     urllib.request.urlopen = self.mock.mocked_page_not_found
     util.check_version(self.problems)
     e = self.accumulator.PopException('OtherProblem')
     self.assertTrue(re.search(r'we failed to reach', e.description))
     self.assertTrue(re.search(r'Reason: Not Found \[404\]', e.description))
Exemple #6
0
 def testGetCorrectReturns(self):
     urllib.request.urlopen = self.mock.mocked_connect_success
     util.check_version(self.problems)
     self.accumulator.PopException('NewVersionAvailable')
Exemple #7
0
 def testAssignedSameVersion(self):
     util.check_version(self.problems, version.__version__)
     self.accumulator.assert_no_more_exceptions()
Exemple #8
0
 def testAssignedDifferentVersion(self):
     util.check_version(self.problems, '100.100.100')
     e = self.accumulator.PopException('NewVersionAvailable')
     self.assertEqual(e.version, '100.100.100')
     self.assertEqual(e.url, 'https://github.com/google/transitfeed')
     self.accumulator.assert_no_more_exceptions()
def main(request_handler_class=ScheduleRequestHandler):
    usage = \
        '''%prog [options] [<input GTFS.zip>]
        
        Runs a webserver that lets you explore a <input GTFS.zip> in your browser.
        
        If <input GTFS.zip> is omited the filename is read from the console. Dragging
        a file into the console may enter the filename.
        
        For more information see
        https://github.com/google/transitfeed/wiki/ScheduleViewer
        '''
    parser = util.OptionParserLongError(usage=usage,
                                        version='%prog ' +
                                        transitfeed.__version__)
    parser.add_option('--feed_filename',
                      '--feed',
                      dest='feed_filename',
                      help='file name of feed to load')
    parser.add_option('--key',
                      dest='key',
                      help='Google Maps API key or the name '
                      'of a text file that contains an API key')
    parser.add_option('--host', dest='host', help='Host name of Google Maps')
    parser.add_option('--port',
                      dest='port',
                      type='int',
                      help='port on which to listen')
    parser.add_option('--file_dir',
                      dest='file_dir',
                      help='directory containing static files')
    parser.add_option('-n',
                      '--noprompt',
                      action='store_false',
                      dest='manual_entry',
                      help='disable interactive prompts')
    parser.set_defaults(port=8765,
                        host='maps.google.com',
                        file_dir=find_default_file_dir(),
                        manual_entry=True)
    options, args = parser.parse_args()

    if not os.path.isfile(os.path.join(options.file_dir, 'index.html')):
        print("Can't find index.html with --file_dir=%s" % options.file_dir)
        exit(1)

    if not options.feed_filename and len(args) == 1:
        options.feed_filename = args[0]

    if not options.feed_filename and options.manual_entry:
        options.feed_filename = input('Enter Feed Location: ').strip('"')

    default_key_file = get_default_key_file_path()
    if not options.key and os.path.isfile(default_key_file):
        options.key = open(default_key_file).read().strip()

    if options.key and os.path.isfile(options.key):
        options.key = open(options.key).read().strip()

    # This key is registered to [email protected]
    if not options.key:
        options.key = 'AIzaSyAZTTRO6RC6LQyKCD3JODhxbClsZl95P9U'

    util.check_version(transitfeed.ProblemReporter())

    schedule = transitfeed.Schedule(
        problem_reporter=transitfeed.ProblemReporter())
    print('Loading data from feed "%s"...' % options.feed_filename)
    print('(this may take a few minutes for larger cities)')
    schedule.load(options.feed_filename)

    server = StoppableHTTPServer(server_address=('', options.port),
                                 RequestHandlerClass=request_handler_class)
    server.key = options.key
    server.schedule = schedule
    server.file_dir = options.file_dir
    server.host = options.host
    server.feed_path = options.feed_filename

    print("To view, point your browser at http://localhost:%d/" %
          server.server_port)
    server.serve_forever()