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 optlines(self, it, optname): 'Generate next options.<optname> elements from iterator with exceptions wrapped.' for i in range(options.get(optname, self)): try: yield next(it) except StopIteration: break
def editOption(self, row): currentValue = options.get(row.name, self.source) vd.addUndo(options.set, row.name, currentValue, self.source) if isinstance(row.value, bool): options.set(row.name, not currentValue, self.source) else: options.set(row.name, self.editCell(1, value=currentValue), self.source)
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]) self.recalc() self.rows = [] with Progress(total=self.source.filesize) as prog: for L in itertools.chain(lines, getlines(fp)): row = L.split(delim) ncols = self._rowtype.length() # current number of cols if len(row) > ncols: # add unnamed columns to the type not found in the header newcols = [ColumnItem('', len(row)+i, width=8) for i in range(len(row)-ncols)] self._rowtype = namedlist(self._rowtype.__name__, list(self._rowtype._fields) + ['_' for c in newcols]) for c in newcols: self.addColumn(c) elif len(row) < ncols: # extend rows that are missing entries row.extend([None]*(ncols-len(row))) self.addRow(self._rowtype(row)) prog.addProgress(len(L))
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)
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', width=40, 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 '' def editOption(self, row): currentValue = options.get(row.name, self.source) vd.addUndo(options.set, row.name, currentValue, self.source) if isinstance(row.value, bool): options.set(row.name, not currentValue, self.source) else: options.set(row.name, self.editCell(1, value=currentValue), self.source) def reload(self): self.rows = [] for k in options.keys(): opt = options._get(k) self.addRow(opt) self.columns[ 1].name = 'global_value' if self.source == 'override' else 'sheet_value'
def diffOption(self, optname): val = options.get(optname, self.source) default = options.get(optname, 'global') return val if val != default else ''
def editOption(self, row): if isinstance(row.value, bool): options.set(row.name, not options.get(row.name, self.source), self.source) else: options.set(row.name, self.editCell(1), self.source)