From f0543842c1c762176d90939ae86fc08d098ee1b3 Mon Sep 17 00:00:00 2001 From: Jeremy Carbaugh Date: Fri, 3 Jun 2011 12:52:13 -0400 Subject: [PATCH] add RemoteFile object for streaming from URL --- saucebrush/utils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/saucebrush/utils.py b/saucebrush/utils.py index c21bcf3..da0399f 100644 --- a/saucebrush/utils.py +++ b/saucebrush/utils.py @@ -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() \ No newline at end of file