Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def topology_import(self, file):
book = open_workbook(file_contents=file.read())
status = "Topology successfully imported."
for obj_type in ("device", "link"):
try:
sheet = book.sheet_by_name(obj_type)
except XLRDError:
continue
properties = sheet.row_values(0)
for row_index in range(1, sheet.nrows):
values = {"dont_update_pools": True}
for index, property in enumerate(properties):
func = field_conversion[property_types[property]]
values[property] = func(sheet.row_values(row_index)[index])
try:
factory(obj_type, **values).serialized
except Exception as e:
info(f"{str(values)} could not be imported ({str(e)})")
status = "Partial import (see logs)."
Session.commit()
for pool in fetch_all("pool"):
pool.compute_pool()
self.log("info", status)
workbook = xlrd.open_workbook(file_pointer.name)
sheets = workbook.sheet_names()
sheet = workbook.sheet_by_name(sheets[0])
if sheet.ncols > MAX_COLS or sheet.nrows > MAX_ROWS:
raise TooBigTableError("Too many rows or columns")
retdic = {}
num_sheets = len(sheets)
if num_sheets > 1:
retdic['message'] = "File contains {0} sheets. Only the first is displayed. Download the file to view all of them.".format(num_sheets)
try:
retdic['dataframe'] = pd.read_excel(file_pointer, sheets[0])
return retdic
except (IndexError, XLRDError):
raise BlankOrCorruptTableError("Is this a valid excel file?")
workbook = xlrd.open_workbook(file_pointer.name)
sheets = workbook.sheet_names()
sheet = workbook.sheet_by_name(sheets[0])
if sheet.ncols > MAX_COLS or sheet.nrows > MAX_ROWS:
raise TooBigTableError
retdic = {}
num_sheets = len(sheets)
if num_sheets > 1:
retdic['message'] = "File contains {0} sheets. Only the first is displayed. Download the file to view all of them.".format(num_sheets)
try:
retdic['dataframe'] = pd.read_excel(file_pointer, sheets[0])
return retdic
except (IndexError, XLRDError):
raise BlankOrCorruptTableError("Is this a valid excel file?")
def _yield_rows(self):
from xlrd import open_workbook
from xlrd.biffh import XLRDError
try:
wb = open_workbook(self.file_name)
except XLRDError:
from zipfile import ZipFile
# Usually b/c the .xls file is XML, but not zipped.
self.file_name.replace('.xls', '.xml')
wb = open_workbook(self.file_name)
self.workbook = wb
s = wb.sheets()[self.segment if self.segment else 0]
for i in range(0, s.nrows):
self.line_number = i
yield self.srow_to_list(i, s)
def make_name_access_maps(bk):
name_and_scope_map = {} # (name.lower(), scope): Name_object
name_map = {} # name.lower() : list of Name_objects (sorted in scope order)
num_names = len(bk.name_obj_list)
for namex in xrange(num_names):
nobj = bk.name_obj_list[namex]
name_lcase = nobj.name.lower()
key = (name_lcase, nobj.scope)
if key in name_and_scope_map:
msg = 'Duplicate entry %r in name_and_scope_map' % (key, )
if 0:
raise XLRDError(msg)
else:
if bk.verbosity:
print(msg, file=bk.logfile)
name_and_scope_map[key] = nobj
if name_lcase in name_map:
name_map[name_lcase].append((nobj.scope, nobj))
else:
name_map[name_lcase] = [(nobj.scope, nobj)]
for key in name_map.keys():
alist = name_map[key]
alist.sort()
name_map[key] = [x[1] for x in alist]
bk.name_and_scope_map = name_and_scope_map
bk.name_map = name_map
return """
<div>Unable to render; download file to view it: </div>
<div>Is this file blank?</div>
"""
except ValueError:
if ext == ".dta":
return """
<div>Unable to render; download file to view it: </div>
<div>Version of given Stata file is not 104, 105, 108, 113 (Stata 8/9), 114 (Stata 10/11) or 115 (Stata 12) </div>
<div>Is this a valid Stata file?</div>
"""
else:
raise Exception
except (RRuntimeError, error, XLRDError):
return """
<div>Unable to render; download file to view it: </div>
<div>Is this a valid {ext} file?</div><br>
""".format(ext=ext)
if dataframe is None:
return """
<div>Unable to render; download file to view it: </div>
<div>Is it a valid {ext} file?</div><br>
<div>Is it empty?</div>
""".format(file_name=file_name, ext=ext)
try:
check_shape(dataframe)
except TooBigError:
return """
def get_sheet(self, names):
if self.wb:
if not isinstance(names, (list, tuple)):
names = [names]
for ni in names:
try:
if isinstance(ni, str):
sheet = self.wb.sheet_by_name(ni)
elif isinstance(ni, int):
sheet = self.wb.sheet_by_index(ni)
else:
sheet = ni
except XLRDError:
continue
return sheet
def make_name_access_maps(bk):
name_and_scope_map = {} # (name.lower(), scope): Name_object
name_map = {} # name.lower() : list of Name_objects (sorted in scope order)
num_names = len(bk.name_obj_list)
for namex in xrange(num_names):
nobj = bk.name_obj_list[namex]
name_lcase = nobj.name.lower()
key = (name_lcase, nobj.scope)
if key in name_and_scope_map:
msg = 'Duplicate entry %r in name_and_scope_map' % (key, )
if 0:
raise XLRDError(msg)
else:
if bk.verbosity:
print(msg, file=bk.logfile)
name_and_scope_map[key] = nobj
sort_data = (nobj.scope, namex, nobj)
if name_lcase in name_map:
name_map[name_lcase].append(sort_data)
else:
name_map[name_lcase] = [sort_data]
for key in name_map.keys():
alist = name_map[key]
alist.sort()
name_map[key] = [x[2] for x in alist]
bk.name_and_scope_map = name_and_scope_map
bk.name_map = name_map
entity.schema = model.get('Workbook')
self.extract_ole_metadata(file_path, entity)
try:
book = xlrd.open_workbook(file_path, formatting_info=False)
except Exception as err:
raise ProcessingException('Invalid Excel file: %s' % err) from err
try:
for sheet in book.sheets():
table = self.manager.make_entity('Table', parent=entity)
table.make_id(entity.id, sheet.name)
table.set('title', sheet.name)
self.emit_row_tuples(table, self.generate_csv(sheet))
if table.has('csvHash'):
self.manager.emit_entity(table)
except XLRDError as err:
raise ProcessingException('Invalid Excel file: %s' % err) from err
finally:
book.release_resources()
def read_xls(xls_file, sheet = '', csv_file = None, return_table = True):
"""
Generic function to read MS Excel XLS file, and convert one sheet
to CSV, or return as a list of lists
"""
try:
if hasattr(xls_file, 'read'):
book = xlrd.open_workbook(
file_contents = xls_file.read(),
on_demand = True,
)
else:
book = xlrd.open_workbook(xls_file, on_demand = True)
try:
sheet = book.sheet_by_name(sheet)
except xlrd.biffh.XLRDError:
sheet = book.sheet_by_index(0)
table = [[unicode(c.value) for c in sheet.row(i)]
for i in xrange(sheet.nrows)]
if csv_file:
with open(csv_file, 'w') as csv:
csv.write('\n'.join(['\t'.join(r) for r in table]))
if not return_table:
table = None
book.release_resources()
return table
except IOError:
sys.stdout.write('No such file: %s\n' % xls_file)
sys.stdout.flush()