add RemoteFile object for streaming from URL

This commit is contained in:
Jeremy Carbaugh 2011-06-03 12:52:13 -04:00
parent b47cc70d92
commit f0543842c1

View File

@ -1,3 +1,4 @@
import urllib2
"""
General utilities used within saucebrush that may be useful elsewhere.
"""
@ -72,6 +73,12 @@ 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
"""
def __init__(self, *args):
self.paths = []
@ -95,3 +102,18 @@ class Files(object):
for line in f:
yield line
f.close()
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)
for line in resp:
yield line.rstrip()
resp.close()