Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_guess_json(self):
self.assertEqual('json', convert.guess_format('testdata.json'))
def test_guess_csv(self):
self.assertEqual('csv', convert.guess_format('testdata.csv'))
def test_guess_invalid(self):
self.assertEqual(None, convert.guess_format('testdata.invalid'))
def test_xlsx(self):
format = convert.guess_format('testdata.xlsx')
self.assertEqual(format, 'xlsx')
def test_guess_fixed(self):
self.assertEqual('fixed', convert.guess_format('testdata'))
def test_fixed(self):
format = convert.guess_format('testdata')
self.assertEqual(format, 'fixed')
def test_guess_xlsx(self):
self.assertEqual('xlsx', convert.guess_format('testdata.xlsx'))
def upload():
context = {}
if request.method == 'POST':
f = request.files['input_file']
if f:
if allowed_file(f.filename):
inp = StringIO(f.read())
if sys.getsizeof(inp.getvalue()) <= MAX_CONTENT_LENGTH:
inp.seek(0)
file_format = convert.guess_format(f.filename)
try:
converted = convert.convert(inp, file_format)
except UnicodeDecodeError:
context['errors'] = ['We had a problem with reading your file. \
This could have to do with the file encoding or format']
converted = None
f.seek(0)
if converted:
outp = StringIO(converted)
reader = UnicodeCSVReader(outp)
session['header_row'] = reader.next()
rows = []
columns = [[] for c in session['header_row']]
column_ids = range(len(session['header_row']))
for row in range(100):
try:
def main(self):
path = self.args.input_path
# Determine the file type.
if self.args.filetype:
filetype = self.args.filetype
if filetype not in SUPPORTED_FORMATS:
self.argparser.error('"%s" is not a supported format' % self.args.filetype)
elif self.args.schema:
filetype = 'fixed'
elif self.args.key:
filetype = 'json'
else:
if not path or path == '-':
self.argparser.error('You must specify a format when providing input as piped data via STDIN.')
filetype = convert.guess_format(path)
if not filetype:
self.argparser.error('Unable to automatically determine the format of the input file. Try specifying a format with --format.')
if self.args.names_only:
if filetype in ('xls', 'xlsx'):
sheets = self.sheet_names(path, filetype)
for sheet in sheets:
self.output_file.write('%s\n' % sheet)
else:
self.argparser.error('You cannot use the -n or --names options with non-Excel files.')
return
# Set the input file.
if filetype in ('xls', 'xlsx'):
self.input_file = self.open_excel_input_file(path)
else: