This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added support for remote tables in zipfiles.

  • Loading branch information...
jacobian committed Oct 20, 2011
1 parent 1aab7a6 commit 5bcc4fb933834dc9b2ed3d617279f846fc0304b2
Showing with 28 additions and 2 deletions.
  1. +20 −2 remotetable/api.py
  2. +8 −0 tests/__init__.py
View
@@ -6,6 +6,11 @@
import urllib
import inspect
import itertools
+import zipfile
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
from . import parsers
def open(url, **kwargs):
@@ -20,7 +25,10 @@ def open(url, **kwargs):
if callable(parser):
pass
elif parser is None:
- parser = parsers.guess_parser(url)
+ if 'filename' in kwargs:
+ parser = parsers.guess_parser(kwargs['filename'])
+ else:
+ parser = parsers.guess_parser(url)
else:
parser = parsers.get_parser(parser)
@@ -51,7 +59,17 @@ def open(url, **kwargs):
# Grab the data.
with requests.settings(accept_gzip=False):
response = requests.request(**request_kwargs)
- results = parser(response.raw, **kwargs).read()
+
+ # If we got a `filename` argument then assume this is a zipfile.
+ # XXX: would be nice to support gzip, etc.
+ if 'filename' in kwargs:
+ zf = zipfile.ZipFile(StringIO(response.content))
+ stream = zf.open(kwargs.pop('filename'))
+ else:
+ stream = response.raw
+
+ # Now parse the stream.
+ results = parser(stream, **kwargs).read()
# Process select/omit.
if select_func:
View
@@ -93,3 +93,11 @@ def test_invalid_named_parser(self):
def test_xls(self):
t = remotetable.open('http://cloud.github.com/downloads/seamusabshere/remote_table/remote_table_row_hash_test.alternate_order.xls')
self.assertEqual(t[0]['header2'], 'value2')
+
+ def test_open_csv_inside_zipfile(self):
+ t = remotetable.open('http://www.epa.gov/climatechange/emissions/downloads10/2010-Inventory-Annex-Tables.zip',
+ filename = 'Annex Tables/Annex 3/Table A-93.csv',
+ skip = 1,
+ select = lambda row: row['Vehicle Age'].strip().isdigit()
+ )
+ self.assertEqual(t[0]['LDGV'], '9.09%')

0 comments on commit 5bcc4fb

Please sign in to comment.