def reload(self): """Reload the current S3 directory (prefix) listing.""" self.columns = [] if not ( self.use_glob_matching or self.fs.exists(self.source.given) or self.fs.isdir(self.source.given) ): vd.fail(f"unable to open S3 path: {self.source.given}") for col in ( Column("name", getter=self.object_display_name), Column("type", getter=lambda _, row: row.get("type")), Column("size", type=int, getter=lambda _, row: row.get("size")), Column("modtime", type=date, getter=lambda _, row: row.get("LastModified")), ): self.addColumn(col) if self.version_aware: self.addColumn( Column("latest", type=bool, getter=lambda _, row: row.get("IsLatest")) ) self.addColumn( Column( "version_id", type=str, getter=lambda _, row: row.get("VersionId"), width=0, ) ) super().reload()
def remove(vd, vs): if vs in vd.sheets: vd.sheets.remove(vs) if vs in vd.allSheets: vd.allSheets.remove(vs) vd.allSheets.append(vs) else: vd.fail('sheet not on stack')
def reverse(self): if not self.scr: return for y in range(self.y1, self.y2): for x in range(self.x1, self.x2): try: self.scr.chgat( y, x, 1, screen_contents.get((x, y), (0, 0))[1] | curses.A_REVERSE) except curses.error: vd.fail(y, x)
def getCommand(self, keystrokes_or_longname): longname = vd.bindkeys._get(keystrokes_or_longname) try: if longname: return vd.commands._get(longname) or vd.fail( 'no command "%s"' % longname) else: return vd.commands._get(keystrokes_or_longname) or vd.fail( 'no binding for %s' % keystrokes_or_longname) except Exception as exc: return None
def moveVisibleCol(sheet, fromVisColIdx, toVisColIdx): 'Move visible column to another visible index in sheet.' if 0 <= toVisColIdx < sheet.nVisibleCols: fromVisColIdx = min(max(fromVisColIdx, 0), sheet.nVisibleCols-1) fromColIdx = sheet.columns.index(sheet.visibleCols[fromVisColIdx]) # a regular column cannot move to the left of keycols if toVisColIdx <= 0: toColIdx = sheet.columns.index(sheet.nonKeyVisibleCols[toVisColIdx]) else: toColIdx = sheet.columns.index(sheet.visibleCols[toVisColIdx]) moveListItem(sheet.columns, fromColIdx, toColIdx) return toVisColIdx else: vd.fail('already at edge')
def addRegexColumns(regexMaker, vs, colIndex, origcol, regexstr): regexstr or vd.fail('regex required') regex = re.compile(regexstr, vs.regex_flags()) func = regexMaker(regex, origcol) n = options.default_sample_size if n and n < len(vs.rows): exampleRows = random.sample(vs.rows, max( 0, n - 1)) # -1 to account for included cursorRow else: exampleRows = vs.rows ncols = 0 # number of new columns added already for r in Progress(exampleRows + [vs.cursorRow]): try: m = func(r) if not m: continue except Exception as e: vd.exceptionCaught(e) for _ in range(len(m) - ncols): c = Column( origcol.name + '_re' + str(ncols), getter=lambda col, row, i=ncols, func=func: func(row)[i], origCol=origcol) vs.addColumn(c, index=colIndex + ncols + 1) ncols += 1
def undo(vd, sheet): if not options.undo: vd.fail("options.undo not enabled") # don't allow undo of first command on a sheet, which is always the command that created the sheet. for cmdlogrow in sheet.cmdlog_sheet.rows[:0:-1]: if cmdlogrow.undofuncs: for undofunc, args, kwargs, in cmdlogrow.undofuncs: undofunc(*args, **kwargs) sheet.undone.append(cmdlogrow) sheet.cmdlog_sheet.rows.remove(cmdlogrow) vd.moveToReplayContext(cmdlogrow) vd.clearCaches() vd.status("%s undone" % cmdlogrow.longname) return vd.fail("nothing to undo on current sheet")
def to_entries(col): ''' Convert values from a dict into a list of Key/Value pairs, similar to the to_entries function in jq: Abort if the specified column's value for any row is _not_ a dict. ''' sheet = col.sheet rows = sheet.rows new_idx = sheet.columns.index(col) + 1 new_col = sheet.addColumn(SettableColumn(col.name), index=new_idx) isNull = isNullFunc() for r in rows: val = col.getValue(r) if isNull(val): continue if not isinstance(val, dict): sheet.columns.pop(new_idx) vd.fail('Column "{}" is not a dict'.format(col.name)) new_col.setValue(r, [{'Key': k, 'Value': v} for k, v in val.items()]) col.hide() return new_col
def __init__(self, name='', *, type=anytype, cache=False, **kwargs): self.sheet = None # owning Sheet, set in .recalc() via Sheet.addColumn self.name = name # display visible name self.fmtstr = '' # by default, use str() self._type = type # anytype/str/int/float/date/func self.getter = lambda col, row: row self.setter = lambda col, row, value: vd.fail( col.name + ' column cannot be changed') self.width = None # == 0 if hidden, None if auto-compute next time self.height = 1 # max height, None/0 to auto-compute for each row self.keycol = 0 # keycol index (or 0 if not key column) self.expr = None # Column-type-dependent parameter self.setCache(cache) for k, v in kwargs.items(): setattr( self, k, v ) # instead of __dict__.update(kwargs) to invoke property.setters
def openRow(self, row): vd.fail(f'cannot open {self.rowtype} on this sheet')
def _die(): sheet.columns.pop(new_idx) vd.fail(f'Columns {col.name} is not a list of Key/Value pairs')
def someSelectedRows(self): if self.nSelected == 0: vd.fail('no rows selected') return self.selectedRows
def iterload(self): 'Override this generator for loading.' if False: yield vd.fail('no iterload for this loader yet')
def redo(vd, sheet): sheet.undone or vd.fail("nothing to redo") cmdlogrow = sheet.undone.pop() vd.replayOne(cmdlogrow) vd.status("%s redone" % cmdlogrow.longname)
def putValue(self, row, value): subrow = self.subfunc(row, self.expr) if subrow is None: vd.fail('no source row') self.origcol.setValue(subrow, value)
def __init__(self, frame): frame = normalize_container(frame) if not isinstance(frame, Frame): vd.fail('%s is not a StaticFrame Frame' % type(frame).__name__) self.frame = frame
def column(self, colname): 'Return first column whose Column.name matches colname.' return self.colsByName.get(colname) or vd.fail('no column matching "%s"' % colname)