From fad10e5a9840ba7c30f99f8b5cbf02d0cda9ab74 Mon Sep 17 00:00:00 2001 From: Jeremy Carbaugh Date: Sun, 11 Mar 2012 22:18:12 -0700 Subject: [PATCH] use either urllib2 or urllib.request as necessary --- saucebrush/utils.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/saucebrush/utils.py b/saucebrush/utils.py index 2139137..afa67dc 100644 --- a/saucebrush/utils.py +++ b/saucebrush/utils.py @@ -1,5 +1,10 @@ import os -import urllib2 + +try: + from urllib.request import urlopen # attemp py3 first +except ImportError: + from urllib2 import urlopen # fallback to py2 + """ General utilities used within saucebrush that may be useful elsewhere. """ @@ -75,7 +80,7 @@ def str_or_list(obj): class Files(object): """ Iterate over multiple files as a single file. Pass the paths of the files as arguments to the class constructor: - + for line in Files('/path/to/file/a', '/path/to/file/b'): pass """ @@ -104,15 +109,15 @@ class Files(object): class RemoteFile(object): """ Stream data from a remote file. - + :param url: URL to remote file """ - + def __init__(self, url): self._url = url - + def __iter__(self): - resp = urllib2.urlopen(self._url) + resp = urlopen(self._url) for line in resp: yield line.rstrip() resp.close() \ No newline at end of file