Skip to content

Commit

Permalink
Correct passthrough to ignore shape - passing through multiexporters …
Browse files Browse the repository at this point in the history
…should work through it
  • Loading branch information
AngryLawyer committed Dec 22, 2016
1 parent b302fe6 commit 21b2260
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
30 changes: 10 additions & 20 deletions csv_wrangler/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class BaseExporter(metaclass=ABCMeta):
def to_list(self) -> List[List[str]]: # pragma: no cover
pass

def as_response(self, filename: str='export') -> HttpResponse:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename)
writer = csv.writer(response)
[writer.writerow(row) for row in self.to_list()]
return response


class Exporter(Generic[T], BaseExporter, metaclass=ABCMeta):

Expand Down Expand Up @@ -65,13 +72,6 @@ def to_list(self) -> List[List[str]]:
]
return [self.get_header_labels()] + lines

def as_response(self, filename: str='export') -> HttpResponse:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename)
writer = csv.writer(response)
[writer.writerow(row) for row in self.to_list()]
return response


class MultiExporter(BaseExporter):

Expand Down Expand Up @@ -119,22 +119,12 @@ def to_list(self) -> List[List[str]]:
return [self.get_csv_header_labels()] + lines


class PassthroughExporter(Exporter):
class PassthroughExporter(BaseExporter):

data = [] # type: List[List[str]]

def __init__(self, data: List[List[str]]) -> None:
self.data = data

def make_header(self, field_name: str, idx: int) -> Header[List[str]]:
return Header(label=field_name, callback=lambda record: record[idx])

def fetch_records(self) -> List[List[str]]:
return self.data[1:]

def get_headers(self) -> List[Header[List[str]]]:
return [
self.make_header(field_name, idx)
for idx, field_name in enumerate(self.data[0])
]

def to_list(self) -> List[List[str]]:
return self.data
11 changes: 11 additions & 0 deletions csv_wrangler/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,14 @@ def test_passthrough_to_list(self) -> None:
self.assertEqual(results[0], ['a', 'b', 'c'])
self.assertEqual(results[1], ['1', '2', '3'])
self.assertEqual(results[2], ['2', '3', '4'])

def test_malformed_passthrough(self) -> None:
exporter = PassthroughExporter([
['a', 'b', 'c'],
[],
['d', 'e', 'f'],
])
results = exporter.to_list()
self.assertEqual(results[0], ['a', 'b', 'c'])
self.assertEqual(results[1], [])
self.assertEqual(results[2], ['d', 'e', 'f'])

0 comments on commit 21b2260

Please sign in to comment.