Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def reload_sync(self):
'Perform synchronous loading of TSV file, discarding header lines.'
header_lines = options.get('header', self)
delim = options.get('delimiter', self)
with self.source.open_text() as fp:
# get one line anyway to determine number of columns
lines = list(getlines(fp, int(header_lines) or 1))
headers = [L.split(delim) for L in lines]
if header_lines <= 0:
self.columns = [ColumnItem('', i) for i in range(len(headers[0]))]
else:
self.columns = [
ColumnItem('\\n'.join(x), i)
for i, x in enumerate(zip(*headers[:header_lines]))
]
lines = lines[header_lines:] # in case of header_lines == 0
def reload_sync(self):
'Perform synchronous loading of TSV file, discarding header lines.'
header_lines = options.get('header', self)
delim = options.get('delimiter', self)
with self.source.open_text() as fp:
# get one line anyway to determine number of columns
lines = list(getlines(fp, int(header_lines) or 1))
headers = [L.split(delim) for L in lines]
if header_lines <= 0:
self.columns = [ColumnItem('', i) for i in range(len(headers[0]))]
else:
self.columns = [
ColumnItem('\\n'.join(x), i)
for i, x in enumerate(zip(*headers[:header_lines]))
]
lines = lines[header_lines:] # in case of header_lines == 0
self._rowtype = namedlist('tsvobj', [c.name for c in self.columns])
def optlines(self, it, optname):
'Generate next options. elements from iterator with exceptions wrapped.'
for i in range(options.get(optname, self)):
try:
yield next(it)
except StopIteration:
break
def save_tsv(p, vs):
'Write sheet to file `fn` as TSV.'
delim = options.get('delimiter', vs)
trdict = tsv_trdict(vs)
save_tsv_header(p, vs)
with p.open_text(mode='a') as fp:
for dispvals in genAllValues(vs.rows, vs.visibleCols, trdict, format=True):
fp.write(delim.join(dispvals))
fp.write('\n')
status('%s save finished' % p)
def tsv_trdict(vs):
'returns string.translate dictionary for replacing tabs and newlines'
if options.safety_first:
delim = options.get('delimiter', vs)
return {ord(delim): options.get('tsv_safe_tab', vs), # \t
10: options.get('tsv_safe_newline', vs), # \n
13: options.get('tsv_safe_newline', vs), # \r
}
return {}
def tsv_trdict(vs):
'returns string.translate dictionary for replacing tabs and newlines'
if options.safety_first:
delim = options.get('delimiter', vs)
return {ord(delim): options.get('tsv_safe_tab', vs), # \t
10: options.get('tsv_safe_newline', vs), # \n
13: options.get('tsv_safe_newline', vs), # \r
}
return {}
def tsv_trdict(vs):
'returns string.translate dictionary for replacing tabs and newlines'
if options.safety_first:
delim = options.get('delimiter', vs)
return {ord(delim): options.get('tsv_safe_tab', vs), # \t
10: options.get('tsv_safe_newline', vs), # \n
13: options.get('tsv_safe_newline', vs), # \r
}
return {}
widestoptwidth, widestopt = sorted((len(opt.name)+len(str(opt.value)), opt.name) for opt in optvalues)[-1]
print('widest option+default is "%s", width %d' % (widestopt, widestoptwidth))
widestoptwidth = 35
menuOut.write('.Bl -tag -width %s -compact\n' % ('X'*(widestoptwidth+3)))
# cliwidth = max(padding+len(str(opt.value)) for opt in optvalues)
cliwidth = 43
print('using width for cli options of %d' % cliwidth)
cliOut.write('.Bl -tag -width %s -compact\n' % ('X'*(cliwidth+3)))
for opt in optvalues:
if opt.name[:5] in ['color', 'disp_']:
options_menu = options_menu_skel.format(optname=opt.name,
type=type(opt.value).__name__,
default=visidata.options.get(opt.name, 'global'),
description=opt.helpstr)
menuOut.write(options_menu)
else:
cli_optname=opt.name.replace('_', '-')
cli_type=type(opt.value).__name__
optlen = len(cli_optname)+len(cli_type)+1
cliOut.write(options_cli_skel.format(cli_optname=cli_optname,
optname = opt.name,
type=cli_type+" "*(padding-optlen),
default=visidata.options.get(opt.name, 'global'),
description=opt.helpstr))
menuOut.write('.El')
cliOut.write('.El')