Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

fix urllib2 imports #463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions examples/google_random_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
import random
import sys
import transitfeed
import urllib
import urlparse

try: # py3
import urllib.parse as urllibparse
urlparse = urllibparse
except ImportError:
import urllib as urllibparse
import urlparse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we move this into transitfeed.compat.


def Distance(lat0, lng0, lat1, lng1):
"""
Expand Down Expand Up @@ -106,7 +110,7 @@ def LatLngsToGoogleUrl(source, destination, dt):
"ie": "UTF8",
"oe": "UTF8"}
url = urlparse.urlunsplit(("http", "maps.google.com", "/maps",
urllib.urlencode(params), ""))
urllibparse.urlencode(params), ""))
return url


Expand Down
7 changes: 5 additions & 2 deletions examples/shuttle_from_xmlfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import os.path
import re
import transitfeed
import urllib
try: # py3
import urllib.request as urlrequest
except ImportError:
import urllib as urlrequest

try:
import xml.etree.ElementTree as ET # python 2.5
Expand All @@ -45,7 +48,7 @@ def UnusedStop(self, stop_id, stop_name):
pass

def SaveFeed(input, output):
tree = ET.parse(urllib.urlopen(input))
tree = ET.parse(urlrequest.urlopen(input))

schedule = transitfeed.Schedule()
service_period = schedule.GetDefaultServicePeriod()
Expand Down
8 changes: 6 additions & 2 deletions misc/import_ch_zurich.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
import datetime
import optparse
import sys
import urllib
import zipfile

try: # py3
import urllib.parse as urlparse
except ImportError:
import urlparse


# Zurich tram lines
TRAM_LINES = {'2':['FF3300','FFFFFF'],
Expand Down Expand Up @@ -218,7 +222,7 @@ def ImportStations(self, station_file, adv_file):
station.name, station.city = self.DemangleName(name)
station.country = 'CH'
station.url = 'http://fahrplan.zvv.ch/?to.0=' + \
urllib.quote(name.encode('iso-8859-1'))
urlparse.quote(name.encode('iso-8859-1'))
station.advertised_lines = set()
self.stations[id] = station
for station_id, line_id in ReadCSV(adv_file, ['ORT_NR', 'LI_NR']):
Expand Down
8 changes: 5 additions & 3 deletions schedule_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import time
import transitfeed
from transitfeed import util
import urllib

try: # py3
import urllib.parse as urlparse
except ImportError:
import urllib as urlparse

# By default Windows kills Python with Ctrl+Break. Instead make Ctrl+Break
# raise a KeyboardInterrupt.
Expand Down Expand Up @@ -97,7 +99,7 @@ def do_GET(self):
scheme, host, path, x, params, fragment = urlparse.urlparse(self.path)
parsed_params = {}
for k in params.split('&'):
k = urllib.unquote(k)
k = urlparse.unquote(k)
if '=' in k:
k, v = k.split('=', 1)
parsed_params[k] = unicode(v, 'utf8')
Expand Down
8 changes: 6 additions & 2 deletions tests/testexamples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import re
import transitfeed
import unittest
import urllib
try: # py3
import urllib.request as urlrequest
except ImportError:
import urllib as urlrequest

from tests import util

class WikiExample(util.TempDirTestCaseBase):
# Download example from wiki and run it
def runTest(self):
wiki_source = urllib.urlopen(
wiki_source = urlrequest.urlopen(
'https://raw.githubusercontent.com/wiki/google/transitfeed/TransitFeed.md'
).read()
m = re.search(r'```\s*(import transitfeed.*)```', wiki_source, re.DOTALL)
Expand Down
26 changes: 16 additions & 10 deletions tests/transitfeed/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@
from transitfeed import util
from transitfeed import version
import unittest
from urllib2 import HTTPError, URLError
import urllib2
try: # py3
import urllib.request as urlrequest
from urllib.error import HTTPError, URLError
except ImportError:
import urllib2 as urlrequest
from urllib2 import HTTPError, URLError




class ColorLuminanceTestCase(test_util.TestCase):
Expand Down Expand Up @@ -254,14 +260,14 @@ def runTest(self):

class CheckVersionTestCase(test_util.TempDirTestCaseBase):
def setUp(self):
self.orig_urlopen = urllib2.urlopen
self.orig_urlopen = urlrequest.urlopen
self.mock = MockURLOpen()
self.accumulator = test_util.RecordingProblemAccumulator(self)
self.problems = ProblemReporter(self.accumulator)

def tearDown(self):
self.mock = None
urllib2.urlopen = self.orig_urlopen
urlrequest.urlopen = self.orig_urlopen

def testAssignedDifferentVersion(self):
util.CheckVersion(self.problems, '100.100.100')
Expand All @@ -275,40 +281,40 @@ def testAssignedSameVersion(self):
self.accumulator.AssertNoMoreExceptions()

def testGetCorrectReturns(self):
urllib2.urlopen = self.mock.mockedConnectSuccess
urlrequest.urlopen = self.mock.mockedConnectSuccess
util.CheckVersion(self.problems)
self.accumulator.PopException('NewVersionAvailable')

def testPageNotFound(self):
urllib2.urlopen = self.mock.mockedPageNotFound
urlrequest.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))

def testConnectionTimeOut(self):
urllib2.urlopen = self.mock.mockedConnectionTimeOut
urlrequest.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))

def testGetAddrInfoFailed(self):
urllib2.urlopen = self.mock.mockedGetAddrInfoFailed
urlrequest.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))

def testEmptyIsReturned(self):
urllib2.urlopen = self.mock.mockedEmptyIsReturned
urlrequest.urlopen = self.mock.mockedEmptyIsReturned
util.CheckVersion(self.problems)
e = self.accumulator.PopException('OtherProblem')
self.assertTrue(re.search(r'we had trouble parsing', e.description))


class MockURLOpen:
"""Pretend to be a urllib2.urlopen suitable for testing."""
"""Pretend to be a urlrequest.urlopen suitable for testing."""
def mockedConnectSuccess(self, request):
return StringIO.StringIO('latest_version=100.0.1')

Expand Down
15 changes: 10 additions & 5 deletions transitfeed/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
import socket
import sys
import time
import urllib2
try: # py3
import urllib.request as urlrequest
import urllib.error as urlerror
except ImportError:
import urllib2 as urlrequest
urlerror = urlrequest

from . import errors
from .version import __version__
Expand Down Expand Up @@ -190,23 +195,23 @@ def CheckVersion(problems, latest_version=None):
if not latest_version:
timeout = 20
socket.setdefaulttimeout(timeout)
request = urllib2.Request(LATEST_RELEASE_VERSION_URL)
request = urlrequest.Request(LATEST_RELEASE_VERSION_URL)

try:
response = urllib2.urlopen(request)
response = urlrequest.urlopen(request)
content = response.read()
m = re.search(r'version=(\d+\.\d+\.\d+)', content)
if m:
latest_version = m.group(1)

except urllib2.HTTPError as e:
except urlerror.HTTPError as e:
description = ('During the new-version check, we failed to reach '
'transitfeed server: Reason: %s [%s].' %
(e.reason, e.code))
problems.OtherProblem(
description=description, type=errors.TYPE_NOTICE)
return
except urllib2.URLError as e:
except urlerror.URLError as e:
description = ('During the new-version check, we failed to reach '
'transitfeed server. Reason: %s.' % e.reason)
problems.OtherProblem(
Expand Down