def adapter(data, headers, table_format='csv', **kwargs): """Wrap the formatting inside a function for TabularOutputFormatter.""" keys = ('dialect', 'delimiter', 'doublequote', 'escapechar', 'lineterminator', 'quotechar', 'quoting', 'skipinitialspace', 'strict') if table_format == 'csv': delimiter = ',' elif table_format == 'tsv': delimiter = '\t' else: raise ValueError('Invalid table_format specified.') ckwargs = {'delimiter': delimiter} ckwargs.update(filter_dict_by_key(kwargs, keys)) with contextlib.closing(StringIO()) as content: writer = csv.writer(content, **ckwargs) writer.writerow(headers) for row in data: writer.writerow(row) return content.getvalue()
def style_field(token, field): """Get the styled text for a *field* using *token* type.""" s = StringIO() formatter.format(((token, field), ), s) return s.getvalue()
def style_field(token, field, style): """Get the styled text for a *field* using *token* type.""" formatter = Terminal256Formatter(style=style) s = StringIO() formatter.format(((token, field), ), s) return s.getvalue()