def test_no_header(self): args = ['--no-header-row', 'examples/no_header_row3.csv'] output_file = StringIO.StringIO() utility = CSVLook(args, output_file) utility.main() input_file = StringIO.StringIO(output_file.getvalue()) #self.assertEqual(input_file.next(), '|----+---+----|\n') #self.assertEqual(input_file.next(), '| 1 | 2 | 3 |\n') #self.assertEqual(input_file.next(), '| 4 | 5 | 6 |\n') #self.assertEqual(input_file.next(), '|----+---+----|\n') self.assertEqual(input_file.next(), '|----------+---------+----------|\n') self.assertEqual(input_file.next(), '| column1 | column2 | column3 |\n') self.assertEqual(input_file.next(), '|----------+---------+----------|\n') self.assertEqual(input_file.next(), '| 1 | 2 | 3 |\n') self.assertEqual(input_file.next(), '| 4 | 5 | 6 |\n') self.assertEqual(input_file.next(), '|----------+---------+----------|\n')
def test_no_header(self): args = ['--no-header-row', 'examples/no_header_row3.csv'] output_file = StringIO.StringIO() utility = CSVLook(args, output_file) utility.main() input_file = StringIO.StringIO(output_file.getvalue()) self.assertEqual(input_file.next(), '|----+---+----|\n') self.assertEqual(input_file.next(), '| 1 | 2 | 3 |\n') self.assertEqual(input_file.next(), '| 4 | 5 | 6 |\n') self.assertEqual(input_file.next(), '|----+---+----|\n')
def test_simple(self): args = ['examples/dummy3.csv'] output_file = six.StringIO() utility = CSVLook(args, output_file) utility.main() 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')
def test_phabricator(self): args = ['examples/dummy3.csv', '--phabricator'] output_file = six.StringIO() utility = CSVLook(args, output_file) utility.main() 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.assertRaises(StopIteration, next, input_file)
def test_no_header(self): args = ["--no-header-row", "examples/no_header_row3.csv"] output_file = six.BytesIO() utility = CSVLook(args, output_file) utility.main() input_file = six.BytesIO(output_file.getvalue()) self.assertEqual(next(input_file), b"|----------+---------+----------|\n") self.assertEqual(next(input_file), b"| column1 | column2 | column3 |\n") self.assertEqual(next(input_file), b"|----------+---------+----------|\n") self.assertEqual(next(input_file), b"| 1 | 2 | 3 |\n") self.assertEqual(next(input_file), b"| 4 | 5 | 6 |\n") self.assertEqual(next(input_file), b"|----------+---------+----------|\n")
def test_simple(self): args = ["examples/dummy3.csv"] output_file = six.BytesIO() utility = CSVLook(args, output_file) utility.main() input_file = six.BytesIO(output_file.getvalue()) self.assertEqual(next(input_file), b"|----+---+----|\n") self.assertEqual(next(input_file), b"| a | b | c |\n") self.assertEqual(next(input_file), b"|----+---+----|\n") self.assertEqual(next(input_file), b"| 1 | 2 | 3 |\n") self.assertEqual(next(input_file), b"| 1 | 4 | 5 |\n") self.assertEqual(next(input_file), b"|----+---+----|\n")
def test_my_unicode(self): args = ["examples/customer_sample.csv"] output_file = six.StringIO() utility = CSVLook(args, output_file) utility.main() input_file = six.StringIO(output_file.getvalue()) self.assertEqual(next(input_file), "|-----+------------+-----------+----------|\n") self.assertEqual(next(input_file), "| id | first_name | last_name | company |\n") self.assertEqual(next(input_file), "|-----+------------+-----------+----------|\n") self.assertEqual(next(input_file), "| 1 | Ramon | Jašek | |\n") self.assertEqual(next(input_file), "|-----+------------+-----------+----------|\n")
def test_no_header(self): args = ['--no-header-row', 'examples/no_header_row3.csv'] output_file = six.StringIO() utility = CSVLook(args, output_file) utility.main() 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.assertRaises(StopIteration, next, input_file)
def test_unicode(self): args = ["examples/test_utf8.csv"] output_file = six.StringIO() utility = CSVLook(args, output_file) utility.main() 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")
def test_unicode(self): args = ['examples/test_utf8.csv'] output_file = six.StringIO() utility = CSVLook(args, output_file) utility.main() 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.assertRaises(StopIteration, next, input_file)
def test_truncate(self): args = ['examples/dummy4.csv', '--truncate', '3'] output_file = six.StringIO() utility = CSVLook(args, output_file) utility.main() 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), '| 6 | 7 | 8 |\n') self.assertEqual(next(input_file), '|----+---+----|\n') self.assertRaises(StopIteration, next, input_file)
def convert_attachment(attachment): outfile_path = '/tmp/' + attachment.file.name.split('/')[-1] outfile = open(outfile_path, 'w') ext = outfile_path.split('.')[-1].lower() if ext not in ('xls', 'xlsx', 'csv', 'pdf'): return infile_path = attachment.file.path if ext == 'pdf': infile_path = tabula_csv(attachment, listify=False) # in2csv ... can't say the python module utlities are working 100% here #convert = In2CSV(args=[attachment.file.path], output_file=outfile) #convert.main() import subprocess print 'attachment_id = ', attachment.id print 'agency = ', get_attachment_agency(attachment) print 'infile_path = ', infile_path if ext == 'csv': # already csv outfile_path = infile_path else: subprocess.call( [ 'in2csv', #'--format',outfile_path.split('.')[-1].lower(), infile_path ], #attachment.file.path], stdout=outfile) # label column headers by zero-index headers = get_headers(outfile_path) if headers: write_header_indices(outfile_path, headers) # csvlook file | head output = StringIO.StringIO() look = CSVLook(args=[outfile_path, '-l'], output_file=output) look.main() input = StringIO.StringIO(output.getvalue()) return input else: print 'something wrong with headers. attachment id', attachment.id