Skip to content

Commit

Permalink
Merge pull request #538 from onyxfish/csvlook
Browse files Browse the repository at this point in the history
Use agate.Table.print_table() in csvlook, closes #515
  • Loading branch information
James McKinney committed Jan 31, 2016
2 parents 16afab3 + ca9afcd commit 9486792
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 75 deletions.
56 changes: 1 addition & 55 deletions csvkit/utilities/csvlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import itertools

import agate
import six

from csvkit.cli import CSVKitUtility
from csvkit.table import make_default_headers
Expand All @@ -16,60 +15,7 @@ def add_arguments(self):
pass

def main(self):
rows = agate.reader(self.input_file, **self.reader_kwargs)

# Make a default header row if none exists
if self.args.no_header_row:
row = next(rows)

column_names = make_default_headers(len(row))

# Put the row back on top
rows = itertools.chain([row], rows)
else:
column_names = next(rows)

column_names = list(column_names)

# prepend 'line_number' column with line numbers if --linenumbers option
if self.args.line_numbers:
column_names.insert(0, 'line_number')
rows = [list(itertools.chain([str(i + 1)], row)) for i, row in enumerate(rows)]

# Convert to normal list of rows
rows = list(rows)

# Insert the column names at the top
rows.insert(0, column_names)

widths = []

for row in rows:
for i, v in enumerate(row):
try:
if len(v) > widths[i]:
widths[i] = len(v)
except IndexError:
widths.append(len(v))

# Dashes span each width with '+' character at intersection of
# horizontal and vertical dividers.
divider = '|--' + '-+-'.join('-' * w for w in widths) + '--|'

self.output_file.write('%s\n' % divider)

for i, row in enumerate(rows):
output = []

for j, d in enumerate(row):
if d is None:
d = ''
output.append(' %s ' % six.text_type(d).ljust(widths[j]))

self.output_file.write('| %s |\n' % ('|'.join(output)))

if (i == 0 or i == len(rows) - 1):
self.output_file.write('%s\n' % divider)
agate.Table.from_csv(self.input_file, header=not self.args.no_header_row, **self.reader_kwargs).print_table(output=self.output_file, **self.writer_kwargs)


def launch_new_instance():
Expand Down
2 changes: 1 addition & 1 deletion examples/test_utf8.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
a,b,c
foo,bar,baz
1,2,3
4,5,ʤ
2 changes: 1 addition & 1 deletion tests/test_utilities/test_csvcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_unicode(self):
input_file = six.StringIO(output_file.getvalue())
reader = agate.reader(input_file)

self.assertEqual(next(reader), ['a', 'c'])
self.assertEqual(next(reader), ['foo', 'baz'])
self.assertEqual(next(reader), ['1', '3'])
self.assertEqual(next(reader), ['4', u'ʤ'])

Expand Down
36 changes: 18 additions & 18 deletions tests/test_utilities/test_csvlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def test_simple(self):

input_file = six.StringIO(output_file.getvalue())

self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| a | b | c |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), '| 1 | 4 | 5 |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '|-------+---+----|\n')
self.assertEqual(next(input_file), '| a | b | c |\n')
self.assertEqual(next(input_file), '|-------+---+----|\n')
self.assertEqual(next(input_file), '| True | 2 | 3 |\n')
self.assertEqual(next(input_file), '| True | 4 | 5 |\n')
self.assertEqual(next(input_file), '|-------+---+----|\n')

def test_no_header(self):
args = ['--no-header-row', 'examples/no_header_row3.csv']
Expand All @@ -46,12 +46,12 @@ def test_no_header(self):

input_file = six.StringIO(output_file.getvalue())

self.assertEqual(next(input_file), '|----------+---------+----------|\n')
self.assertEqual(next(input_file), '| column1 | column2 | column3 |\n')
self.assertEqual(next(input_file), '|----------+---------+----------|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), '| 4 | 5 | 6 |\n')
self.assertEqual(next(input_file), '|----------+---------+----------|\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| A | B | C |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), '| 4 | 5 | 6 |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')

def test_unicode(self):
args = ['examples/test_utf8.csv']
Expand All @@ -63,9 +63,9 @@ def test_unicode(self):

input_file = six.StringIO(output_file.getvalue())

self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| a | b | c |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), u'| 4 | 5 | ʤ |\n')
self.assertEqual(next(input_file), '|----+---+----|\n')
self.assertEqual(next(input_file), '|------+-----+------|\n')
self.assertEqual(next(input_file), '| foo | bar | baz |\n')
self.assertEqual(next(input_file), '|------+-----+------|\n')
self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n')
self.assertEqual(next(input_file), u'| 4 | 5 | ʤ |\n')
self.assertEqual(next(input_file), '|------+-----+------|\n')

0 comments on commit 9486792

Please sign in to comment.