Beispiel #1
0
 def testGetAddrInfoFailed(self):
     urllib2.urlopen = self.mock.mockedGetAddrInfoFailed
     util.CheckVersion(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))
Beispiel #2
0
def RunValidation(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.CheckVersion(problems, options.latest_version)

    # TODO: Add tests for this flag in testfeedvalidator.py
    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)
    else:
        extension_module = transitfeed

    gtfs_factory = extension_module.GetGtfsFactory()

    print 'validating %s' % feed
    print 'FeedValidator extension used: %s' % options.extension
    loader = gtfs_factory.Loader(feed, 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.GetAccumulator()
    if accumulator.HasIssues():
        print 'ERROR: %s found' % accumulator.FormatCount()
        return schedule, 1
    else:
        print 'feed validated successfully'
        return schedule, 0
def main(RequestHandlerClass=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=FindDefaultFileDir(),
                        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 = raw_input('Enter Feed Location: ').strip('"')

    default_key_file = GetDefaultKeyFilePath()
    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.CheckVersion(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=RequestHandlerClass)
    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()
Beispiel #4
0
 def testEmptyIsReturned(self):
     urllib2.urlopen = self.mock.mockedEmptyIsReturned
     util.CheckVersion(self.problems)
     e = self.accumulator.PopException('OtherProblem')
     self.assertTrue(re.search(r'we had trouble parsing', e.description))
Beispiel #5
0
 def testConnectionTimeOut(self):
     urllib2.urlopen = self.mock.mockedConnectionTimeOut
     util.CheckVersion(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))
Beispiel #6
0
 def testPageNotFound(self):
     urllib2.urlopen = self.mock.mockedPageNotFound
     util.CheckVersion(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))
Beispiel #7
0
 def testGetCorrectReturns(self):
     urllib2.urlopen = self.mock.mockedConnectSuccess
     util.CheckVersion(self.problems)
     self.accumulator.PopException('NewVersionAvailable')
Beispiel #8
0
 def testAssignedSameVersion(self):
     util.CheckVersion(self.problems, version.__version__)
     self.accumulator.AssertNoMoreExceptions()
Beispiel #9
0
 def testAssignedDifferentVersion(self):
     util.CheckVersion(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.AssertNoMoreExceptions()
Beispiel #10
0
 def testGetCorrectReturns(self, mock_urlopen):
     mock_urlopen.return_value = StringIO('latest_version=100.0.1')
     util.CheckVersion(self.problems)
     self.accumulator.PopException('NewVersionAvailable')
Beispiel #11
0
def main(RequestHandlerClass=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=FindDefaultFileDir(),
        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 = GetDefaultKeyFilePath()
    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.CheckVersion(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(os.path.expanduser(options.feed_filename))

    server = StoppableHTTPServer(
        server_address=("", options.port),
        RequestHandlerClass=RequestHandlerClass,
    )
    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()