Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if self.thread.profile:
self.thread.profile.disable()
self.thread.profile.dump_stats(options.profile + str(self.profileNumber))
if exc_val:
self.thread.exception = exc_val
else:
# remove very-short-lived async actions
if elapsed_s(self.thread) < min_thread_time_s:
vd().threads.remove(self.thread)
class ProfileSheet(Sheet):
columns = [
Column('funcname', getter=lambda col,row: codestr(row.code)),
Column('filename', getter=lambda col,row: os.path.split(row.code.co_filename)[-1] if not isinstance(row.code, str) else ''),
Column('linenum', type=int, getter=lambda col,row: row.code.co_firstlineno if not isinstance(row.code, str) else None),
Column('inlinetime_us', type=int, getter=lambda col,row: row.inlinetime*1000000),
Column('totaltime_us', type=int, getter=lambda col,row: row.totaltime*1000000),
ColumnAttr('callcount', type=int),
Column('avg_inline_us', type=int, getter=lambda col,row: row.inlinetime*1000000/row.callcount),
Column('avg_total_us', type=int, getter=lambda col,row: row.totaltime*1000000/row.callcount),
ColumnAttr('reccallcount', type=int),
ColumnAttr('calls'),
Column('callers', getter=lambda col,row: col.sheet.callers[row.code]),
]
nKeys=3
def reload(self):
self.rows = self.source
self.orderBy(self.column('inlinetime_us'), reverse=True)
precious = False
class ValueColumn(Column):
'passthrough to the value on the source cursorRow'
def calcValue(self, srcCol):
return srcCol.getDisplayValue(srcCol.sheet.cursorRow)
def setValue(self, srcCol, val):
srcCol.setValue(srcCol.sheet.cursorRow, val)
columns = [
ColumnAttr('sheet', type=str),
ColumnAttr('name', width=options.default_width),
ColumnAttr('width', type=int),
ColumnEnum('type', getGlobals(), default=anytype),
ColumnAttr('fmtstr'),
ValueColumn('value', width=options.default_width),
Column('expr', getter=lambda col,row: getattr(row, 'expr', ''),
setter=lambda col,row,val: setattr(row, 'expr', val)),
]
nKeys = 2
colorizers = [
RowColorizer(7, 'color_key_col', lambda s,c,r,v: r and r.keycol),
RowColorizer(8, 'color_hidden_col', lambda s,c,r,v: r and r.hidden),
]
def reload(self):
if len(self.source) == 1:
self.rows = self.source[0].columns
self.cursorRowIndex = self.source[0].cursorColIndex
self.columns[0].hide() # hide 'sheet' column if only one sheet
else:
self.rows = [col for vs in self.source for col in vs.visibleCols if vs is not self]
def newRow(self):
except Exception as e:
vd.exceptionCaught(e)
if scr:
curses.doupdate()
return ret
class StatusSheet(Sheet):
precious = False
rowtype = 'statuses' # rowdef: (priority, args, nrepeats)
columns = [
ColumnItem('priority', 0, type=int, width=0),
ColumnItem('nrepeats', 2, type=int, width=0),
ColumnItem('args', 1, width=0),
Column('message', getter=lambda col,row: composeStatus(row[1], row[2])),
]
colorizers = [
RowColorizer(1, 'color_error', lambda s,c,r,v: r and r[0] == 3),
RowColorizer(1, 'color_warning', lambda s,c,r,v: r and r[0] in [1,2]),
]
def reload(self):
self.rows = self.source
@VisiData.property
def statusHistorySheet(vd):
return StatusSheet("status_history", source=vd.statusHistory[::-1]) # in reverse order
BaseSheet.addCommand('^P', 'statuses', 'vd.push(vd.statusHistorySheet)')
for (keystrokes, _), longname in bindkeys.iter(self.source)
if keystrokes not in self.revbinds
}
self.rows = []
for (k, o), v in commands.iter(self.source):
self.addRow(v)
v.sheet = o
class OptionsSheet(Sheet):
_rowtype = Option # rowdef: Option
rowtype = 'options'
precious = False
columns = (
ColumnAttr('option', 'name'),
Column('value',
getter=lambda col,row: col.sheet.diffOption(row.name),
setter=lambda col,row,val: options.set(row.name, val, col.sheet.source)),
Column('default', getter=lambda col,row: options.get(row.name, 'global')),
Column('description', getter=lambda col,row: options._get(row.name, 'global').helpstr),
ColumnAttr('replayable'),
)
colorizers = [
CellColorizer(3, None, lambda s,c,r,v: v.value if r and c in s.columns[1:3] and r.name.startswith('color_') else None),
]
nKeys = 1
def diffOption(self, optname):
val = options.get(optname, self.source)
default = options.get(optname, 'global')
return val if val != default else ''
fail('no diffs')
if confirm_overwrite and sheet.defermods:
confirm('really %s? ' % cstr)
sheet.putChanges(path, adds, mods, deletes)
VisiData._dm_newSheet = VisiData.newSheet
@VisiData.api
def newSheet(vd, ncols, name='', **kwargs):
newsheet = VisiData._dm_newSheet(vd, ncols, name, **kwargs)
newsheet.trackmods = False
return newsheet
Column._dm_getValue = Column.getValue
@Column.api
def getValue(col, row):
try:
row, rowmods = col.sheet._deferredMods[col.sheet.rowid(row)]
return rowmods[col]
except KeyError:
return col._dm_getValue(row)
@Column.api
def getSavedValue(col, row):
return Column.calcValue(col, row)
Column._dm_setValue = Column.setValue
@Column.api
def setValue(col, row, val):
def __init__(self, name='', **kwargs):
super().__init__(name=name, **kwargs)
self.rows = UNLOADED # list of opaque row objects (UNLOADED before first reload)
self.cursorRowIndex = 0 # absolute index of cursor into self.rows
self.cursorVisibleColIndex = 0 # index of cursor into self.visibleCols
self._topRowIndex = 0 # cursorRowIndex of topmost row
self.leftVisibleColIndex = 0 # cursorVisibleColIndex of leftmost column
self.rightVisibleColIndex = 0
# as computed during draw()
self.rowLayout = {} # [rowidx] -> (y, w)
self.visibleColLayout = {} # [vcolidx] -> (x, w)
# list of all columns in display order
self.columns = kwargs.get('columns') or [copy(c) for c in self.columns] or [Column('')]
self.recalc() # set .sheet on columns and start caches
self.setKeys(self.columns[:self.nKeys]) # initial list of key columns
self.__dict__.update(kwargs) # also done earlier in BaseSheet.__init__
@Column.api
def toggleWidth(self, width):
'Change column width to either given `width` or default value.'
if self.width != width:
self.width = width
else:
self.width = int(options.default_width)
if all(combinedRow):
self.addRow(combinedRow)
elif self.jointype == 'outer': # all rows from first sheet
for combinedRow in combinedRows:
if combinedRow[1]:
self.addRow(combinedRow)
elif self.jointype == 'diff': # only rows without matching key on all sheets
for combinedRow in combinedRows:
if not all(combinedRow):
self.addRow(combinedRow)
## for ExtendedSheet_reload below
class ExtendedColumn(Column):
def calcValue(self, row):
key = joinkey(self.sheet.joinSources[0], row)
srcsheet = self.sheet.joinSources[self.sheetnum]
srcrow = self.sheet.rowsBySheetKey[srcsheet][key]
if srcrow[0]:
return self.sourceCol.calcValue(srcrow[0])
@asyncthread
def ExtendedSheet_reload(self, sheets):
self.joinSources = sheets
# first item in joined row is the key tuple from the first sheet.
# first columns are the key columns from the first sheet, using its row (0)
self.columns = []
for i, c in enumerate(sheets[0].keyCols):
@Column.api # expr.setter
def expr(self, expr):
try:
self.compiledExpr = compile(expr, '', 'eval') if expr else None
self._expr = expr
except SyntaxError as e:
self._expr = None
@Column.api
@asyncthread
def setValuesFromExpr(self, rows, expr):
compiledExpr = compile(expr, '', 'eval')
vd.addUndoSetValues([self], rows)
for row in Progress(rows, 'setting'):
self.setValueSafe(row, self.sheet.evalexpr(compiledExpr, row))
self.recalc()
status('set %d values = %s' % (len(rows), expr))