Skip to content
This repository has been archived by the owner on Jan 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #243 from cbmi/issue-242
Browse files Browse the repository at this point in the history
Refactor formatter for model-derived typing
  • Loading branch information
naegelyd committed Oct 15, 2014
2 parents e368dc2 + 8ceb4ac commit b277e5b
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 76 deletions.
2 changes: 1 addition & 1 deletion avocado/export/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BaseExporter(object):
"Base class for all exporters"
file_extension = 'txt'
content_type = 'text/plain'
preferred_formats = []
preferred_formats = ()

def __init__(self, concepts=None):
if concepts is None:
Expand Down
7 changes: 6 additions & 1 deletion avocado/export/_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CSVExporter(BaseExporter):
file_extension = 'csv'
content_type = 'text/csv'

preferred_formats = ('csv', 'number', 'string')
preferred_formats = ('csv', 'string')

def write(self, iterable, buff=None, *args, **kwargs):
header = []
Expand All @@ -39,11 +39,16 @@ def write(self, iterable, buff=None, *args, **kwargs):

for i, row_gen in enumerate(self.read(iterable, *args, **kwargs)):
row = []

for data in row_gen:
if i == 0:
header.extend(data.keys())

row.extend(data.values())

if i == 0:
writer.writerow(header)

writer.writerow(row)

return buff
27 changes: 22 additions & 5 deletions avocado/export/_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ExcelExporter(BaseExporter):
file_extension = 'xlsx'
content_type = 'application/vnd.ms-excel'

preferred_formats = ('excel', 'boolean', 'number', 'string')
preferred_formats = ('excel', 'string')

def write(self, iterable, buff=None, *args, **kwargs):
buff = self.get_file_obj(buff)
Expand All @@ -27,33 +27,49 @@ def write(self, iterable, buff=None, *args, **kwargs):
ws_data.title = 'Data'

header = []

# Create the data worksheet
for i, row_gen in enumerate(self.read(iterable, *args, **kwargs)):
row = []

for data in row_gen:
if i == 0:
# Build up header row
header.extend(data.keys())

# Add formatted section to the row
row.extend(data.values())

# Write headers on first iteration
if i == 0:
ws_data.append(header)

ws_data.append(row)

ws_dict = wb.create_sheet()
ws_dict.title = 'Data Dictionary'

# Create the Data Dictionary Worksheet
ws_dict.append(('Field Name', 'Data Type', 'Description',
'Concept Name', 'Concept Discription'))
ws_dict.append((
'Field Name',
'Data Type',
'Description',
'Concept Name',
'Concept Description',
))

for c in self.concepts:
cfields = c.concept_fields.select_related('field')

for cfield in cfields:
field = cfield.field
ws_dict.append((field.field_name, field.simple_type,
field.description, c.name, c.description))
ws_dict.append((
field.field_name,
field.simple_type,
field.description,
c.name,
c.description,
))

# This hacked up implementation is due to `save_virtual_workbook`
# not behaving correctly. This function should handle the work
Expand All @@ -66,4 +82,5 @@ def write(self, iterable, buff=None, *args, **kwargs):
_buff.close()
else:
wb.save(buff)

return buff
1 change: 1 addition & 0 deletions avocado/export/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ def write(self, iterable, template, buff=None, *args, **kwargs):

context = Context({'rows': self.read(iterable, *args, **kwargs)})
buff.write(template.render(context))

return buff
4 changes: 3 additions & 1 deletion avocado/export/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ class JSONExporter(BaseExporter):
file_extension = 'json'
content_type = 'application/json'

preferred_formats = ('json', 'number', 'string')
preferred_formats = ('json',)

def write(self, iterable, buff=None, *args, **kwargs):
buff = self.get_file_obj(buff)

encoder = JSONGeneratorEncoder()

for chunk in encoder.iterencode(self.read(iterable, *args, **kwargs)):
buff.write(chunk)

return buff
10 changes: 7 additions & 3 deletions avocado/export/_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ class RExporter(BaseExporter):
file_extension = 'zip'
content_type = 'application/zip'

preferred_formats = ('r', 'coded', 'number', 'string')
preferred_formats = ('r', 'coded')

def _format_name(self, name):
punc = punctuation.replace('_', '')
name = str(name).translate(None, punc)
name = name.replace(' ', '_')
words = name.split('_')

for i, w in enumerate(words):
if i == 0:
name = w.lower()
continue

name += w.capitalize()

if name[0].isdigit():
name = '_' + name

Expand Down Expand Up @@ -56,6 +59,7 @@ def _code_values(self, name, field, coded_labels):

def write(self, iterable, buff=None, template_name='export/script.R',
*args, **kwargs):

zip_file = ZipFile(self.get_file_obj(buff), 'w')

factors = [] # field names
Expand All @@ -68,8 +72,8 @@ def write(self, iterable, buff=None, template_name='export/script.R',
for cfield in cfields:
field = cfield.field
name = self._format_name(field.field_name)
labels.append(u'attr(data${0}, "label") = "{1}"'.format(
name, unicode(cfield)))
labels.append(u'attr(data${0}, "label") = "{1}"'
.format(name, unicode(cfield)))

coded_labels = field.coded_labels()

Expand Down
2 changes: 1 addition & 1 deletion avocado/export/_sas.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SASExporter(BaseExporter):
file_extension = 'zip'
content_type = 'application/zip'

preferred_formats = ('sas', 'coded', 'number', 'string')
preferred_formats = ('sas', 'coded')

num_lg_names = 0

Expand Down
Loading

0 comments on commit b277e5b

Please sign in to comment.