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_stream_xlsx_sheet_by_index_not_existent():
source = 'data/special/sheet2.xlsx'
with pytest.raises(exceptions.SourceError) as excinfo:
Stream(source, sheet=3).open()
assert 'sheet "3"' in str(excinfo.value)
def test_inline_iterator():
# Get table
def generator():
yield ['id', 'name']
yield ['1', 'english']
yield ['2', '中国人']
with pytest.raises(exceptions.SourceError) as excinfo:
iterator = generator()
topen(iterator)
assert 'callable' in str(excinfo.value)
self.close()
self.__encoding = encoding
self.__bytes = self.__loader.load(source, mode='b', encoding=encoding)
# Get book
self.__book = ezodf.opendoc(BytesIO(self.__bytes.read()))
# Get sheet
try:
if isinstance(self.__sheet_pointer, six.string_types):
self.__sheet = self.__book.sheets[self.__sheet_pointer]
else:
self.__sheet = self.__book.sheets[self.__sheet_pointer - 1]
except (KeyError, IndexError):
message = 'OpenOffice document "%s" doesn\'t have a sheet "%s"'
raise exceptions.SourceError(message % (source, self.__sheet_pointer))
# Rest parser
self.reset()
def __iter_extended_rows(self):
rows = jsonlines.Reader(self.__chars)
for row_number, row in enumerate(rows, start=1):
if isinstance(row, (tuple, list)):
yield row_number, None, list(row)
elif isinstance(row, dict):
keys, values = zip(*sorted(row.items()))
yield (row_number, list(keys), list(values))
else:
if not self.__force_parse:
raise exceptions.SourceError('JSON item has to be list or dict')
yield (row_number, None, [])
self.__book = xlrd.open_workbook(
file_contents=self.__bytes.read(),
encoding_override=encoding,
formatting_info=True,
logfile=sys.stderr
)
# Get sheet
try:
if isinstance(self.__sheet_pointer, six.string_types):
self.__sheet = self.__book.sheet_by_name(self.__sheet_pointer)
else:
self.__sheet = self.__book.sheet_by_index(self.__sheet_pointer - 1)
except (xlrd.XLRDError, IndexError):
message = 'Excel document "%s" doesn\'t have a sheet "%s"'
raise exceptions.SourceError(message % (source, self.__sheet_pointer))
self.__fragment = self.__sheet.name
# Reset parser
self.reset()
path = '%s.item' % self.__property
items = ijson.items(self.__bytes, path)
for row_number, item in enumerate(items, start=1):
if isinstance(item, (tuple, list)):
yield (row_number, None, list(item))
elif isinstance(item, dict):
keys = []
values = []
for key in sorted(item.keys()):
keys.append(key)
values.append(item[key])
yield (row_number, list(keys), list(values))
else:
if not self.__force_parse:
message = 'JSON item has to be list or dict'
raise exceptions.SourceError(message)
yield (row_number, None, [])
if not hasattr(items, '__iter__'):
items = items()
for row_number, item in enumerate(items, start=1):
if isinstance(item, (tuple, list)):
yield (row_number, None, list(item))
elif isinstance(item, dict):
keys = []
values = []
for key in sorted(item.keys()):
keys.append(key)
values.append(item[key])
yield (row_number, list(keys), list(values))
else:
if not self.__force_parse:
message = 'Inline data item has to be tuple, list or dict'
raise exceptions.SourceError(message)
yield (row_number, None, [])