graphtage.printer¶
A module for abstracting printing.
There are several reasons for using this abstraction when printing in Graphtage:
to globally toggle ANSI color output without having to implement that logic in every print function;
to provide an API for automatically handling indentation and pretty-printing;
to handle concurrency when using a status printer like tqdm; and
to permit extensibility (e.g., the
HTMLPrinterextendsPrinterto output in HTML rather than to the command line).
printer classes¶
ANSIContext¶
-
class
graphtage.printer.ANSIContext(stream: Union[graphtage.printer.RawWriter, ANSIContext], fore: Optional[colorama.ansi.AnsiFore] = None, back: Optional[colorama.ansi.AnsiBack] = None, style: Optional[colorama.ansi.AnsiStyle] = None)¶ Bases:
objectA context for printing to the terminal with ANSI color escapes.
-
__init__(stream: Union[graphtage.printer.RawWriter, ANSIContext], fore: Optional[colorama.ansi.AnsiFore] = None, back: Optional[colorama.ansi.AnsiBack] = None, style: Optional[colorama.ansi.AnsiStyle] = None)¶ Initializes a context.
- Parameters
stream – The writer or parent context that this context will wrap.
fore – An optional foreground color.
back – An optional background color.
style – An optional style.
If any of the color or style options are omitted, they will default to the parent context’s options.
-
property
back¶ The computed background color of this context.
-
background(bg_color: colorama.ansi.AnsiBack) → graphtage.printer.ANSIContext¶ Returns a new context with the given background color.
-
bright() → graphtage.printer.ANSIContext¶ Returns a new context with the bright style enabled.
-
color(foreground_color: colorama.ansi.AnsiFore) → graphtage.printer.ANSIContext¶ Returns a new context with the given foreground color.
-
dim() → graphtage.printer.ANSIContext¶ Returns a new context with the dim style enabled.
-
property
end_code¶ Returns the ANSI end code for this context.
-
property
fore¶ The computed foreground color of this context.
-
is_applied: bool = None¶ Keeps track of whether this context’s options have already been applied to the underlying stream.
-
property
parent¶ This context’s parent context.
-
property
root¶ The root context.
-
property
start_code¶ Returns the ANSI start code for this context.
-
property
style¶ The computed style of this context.
-
CombiningMarkContext¶
-
class
graphtage.printer.CombiningMarkContext(writer: graphtage.printer.CombiningMarkWriter, *combining_marks: str)¶ Bases:
objectA context returned by
CombiningMarkWriter.context().-
__init__(writer: graphtage.printer.CombiningMarkWriter, *combining_marks: str)¶ Initialize self. See help(type(self)) for accurate signature.
-
CombiningMarkWriter¶
-
class
graphtage.printer.CombiningMarkWriter(parent: graphtage.printer.RawWriter)¶ Bases:
graphtage.printer.RawWriterA writer that automatically adds combining marks to the bytes that are written.
-
__init__(parent: graphtage.printer.RawWriter)¶ Initializes the writer.
- Parameters
parent – The parent writer to wrap.
-
context(*combining_marks: str) → graphtage.printer.CombiningMarkContext¶ Returns an
__enter__-able context for interacting with this writer, with the given combining marks.
-
enabled: bool = None¶ Whether or not combining marks will be added.
-
flush()¶ Flushes any buffered bytes, if necessary.
-
property
marks¶ Returns the set of combining marks in this writer.
-
property
marks_str¶ Returns a string representation of this writer’s combining marks.
-
parent: RawWriter = None¶ This writer’s parent.
-
write(s: str) → int¶ Writes a string to
self.parent.If
self.enabled,self.marks_strwill be appended to each character ins.- Parameters
s – The string to write.
- Returns
The number of bytes written.
- Return type
-
HTMLANSIContext¶
-
class
graphtage.printer.HTMLANSIContext(stream: Union[graphtage.printer.RawWriter, ANSIContext], fore: Optional[colorama.ansi.AnsiFore] = None, back: Optional[colorama.ansi.AnsiBack] = None, style: Optional[colorama.ansi.AnsiStyle] = None)¶ Bases:
graphtage.printer.ANSIContextA context that sets HTML colors instead of ANSI terminal escapes.
-
__init__(stream: Union[graphtage.printer.RawWriter, ANSIContext], fore: Optional[colorama.ansi.AnsiFore] = None, back: Optional[colorama.ansi.AnsiBack] = None, style: Optional[colorama.ansi.AnsiStyle] = None)¶ Initializes a context.
- Parameters
stream – The writer or parent context that this context will wrap.
fore – An optional foreground color.
back – An optional background color.
style – An optional style.
If any of the color or style options are omitted, they will default to the parent context’s options.
-
property
back¶ The computed background color of this context.
-
background(bg_color: colorama.ansi.AnsiBack) → graphtage.printer.ANSIContext¶ Returns a new context with the given background color.
-
bright() → graphtage.printer.ANSIContext¶ Returns a new context with the bright style enabled.
-
color(foreground_color: colorama.ansi.AnsiFore) → graphtage.printer.ANSIContext¶ Returns a new context with the given foreground color.
-
dim() → graphtage.printer.ANSIContext¶ Returns a new context with the dim style enabled.
-
property
end_code¶ Returns the ANSI end code for this context.
-
property
fore¶ The computed foreground color of this context.
-
property
parent¶ This context’s parent context.
-
property
root¶ The root context.
-
property
start_code¶ Returns the ANSI start code for this context.
-
property
style¶ The computed style of this context.
-
HTMLPrinter¶
-
class
graphtage.printer.HTMLPrinter(*args, title: Optional[str] = None, **kwargs)¶ Bases:
graphtage.printer.PrinterA Printer that outputs in HTML.
-
__init__(*args, title: Optional[str] = None, **kwargs)¶ Initializes a Printer.
- Parameters
out_stream – The stream to which to print. If omitted, it defaults to
sys.stdout.ansi_color – Whether or not color should be enabled in the output. If omitted, it defaults to
out_stream.isatty.quiet – If
True, progress and status messages will be suppressed.options – An optional dict, the keys of which will be set as attributes of this class. This is used to provide additional formatting options to functions that use this printer.
-
property
ansi_color¶ Returns whether this printer has color enabled.
-
background(bg_color: colorama.ansi.AnsiBack) → graphtage.printer.ANSIContext¶ Returns a new context for this printer with the given background color.
-
bright() → graphtage.printer.ANSIContext¶ Returns a new context for this printer with the bright style enabled.
-
close()¶
-
property
closed¶
-
color(foreground_color: colorama.ansi.AnsiFore) → graphtage.printer.ANSIContext¶ Returns a new context for this printer with the given foreground color.
-
context() → graphtage.printer.ANSIContext¶ Returns the context for this printer.
-
dim() → graphtage.printer.ANSIContext¶ Returns a new context for this printer with the dim style enabled.
-
flush(final=False)¶ Flushes this writer.
If
finalisTrue, any extra bytes will be flushed along with a final newline.
-
html_element(element_name, inline=False, **kwargs) → graphtage.printer.HTMLPrinter¶ A convenience function for printing an element.
-
indent() → graphtage.printer.Printer¶ Returns a new context that is indented one step.
-
property
mode¶
-
property
name¶
-
newline()¶
-
strike()¶ Returns a new context for this printer where printed strings will be striked out.
-
tqdm(*args, **kwargs) → tqdm.std.tqdm¶ Returns a
tqdm.tqdmobject.
-
trange(*args, **kwargs) → tqdm.std.trange¶ Returns a
tqdm.trangeobject.
-
under_plus()¶ Returns a new context for this printer where printed strings will have a plus underneath.
-
NullANSIContext¶
-
class
graphtage.printer.NullANSIContext(printer: graphtage.printer.Printer)¶ Bases:
objectA “fake”
ANSIContextthat has the same functions but does not actually emit any colors.-
__init__(printer: graphtage.printer.Printer)¶ Initialize self. See help(type(self)) for accurate signature.
-
Printer¶
-
class
graphtage.printer.Printer(out_stream: Optional[graphtage.printer.Writer] = None, ansi_color: Optional[bool] = None, quiet: bool = False, options: Optional[Dict[str, Any]] = None)¶ Bases:
graphtage.progress.StatusWriter,graphtage.printer.RawWriterAn ANSI color and status printer.
-
__init__(out_stream: Optional[graphtage.printer.Writer] = None, ansi_color: Optional[bool] = None, quiet: bool = False, options: Optional[Dict[str, Any]] = None)¶ Initializes a Printer.
- Parameters
out_stream – The stream to which to print. If omitted, it defaults to
sys.stdout.ansi_color – Whether or not color should be enabled in the output. If omitted, it defaults to
out_stream.isatty.quiet – If
True, progress and status messages will be suppressed.options – An optional dict, the keys of which will be set as attributes of this class. This is used to provide additional formatting options to functions that use this printer.
-
property
ansi_color¶ Returns whether this printer has color enabled.
-
background(bg_color: colorama.ansi.AnsiBack) → graphtage.printer.ANSIContext¶ Returns a new context for this printer with the given background color.
-
bright() → graphtage.printer.ANSIContext¶ Returns a new context for this printer with the bright style enabled.
-
property
closed¶
-
color(foreground_color: colorama.ansi.AnsiFore) → graphtage.printer.ANSIContext¶ Returns a new context for this printer with the given foreground color.
-
context() → graphtage.printer.ANSIContext¶ Returns the context for this printer.
-
dim() → graphtage.printer.ANSIContext¶ Returns a new context for this printer with the dim style enabled.
-
flush(final=False)¶ Flushes this writer.
If
finalisTrue, any extra bytes will be flushed along with a final newline.
-
indent() → graphtage.printer.Printer¶ Returns a new context that is indented one step.
-
indent_str: str = None¶ The string used for each indent step (default is four spaces).
-
indents: int = None¶ The number of indent steps.
-
property
mode¶
-
property
name¶
-
newline()¶
-
out_stream: CombiningMarkWriter = None¶ The stream wrapped by this printer.
-
strike() → graphtage.printer.CombiningMarkContext¶ Returns a new context for this printer where printed strings will be striked out.
-
tqdm(*args, **kwargs) → tqdm.std.tqdm¶ Returns a
tqdm.tqdmobject.
-
trange(*args, **kwargs) → tqdm.std.trange¶ Returns a
tqdm.trangeobject.
-
under_plus()¶ Returns a new context for this printer where printed strings will have a plus underneath.
-
RawWriter¶
-
class
graphtage.printer.RawWriter(*args, **kwargs)¶ Bases:
graphtage.printer.Writer,typing.ProtocolA writer that provides a pass-through for printing bytes to the underlying stream without modification.
-
__init__(*args, **kwargs)¶
-
abstract
flush() → Any¶ Flushes any buffered bytes, if necessary.
-
printer functions¶
only_ansi¶
-
graphtage.printer.only_ansi(func)¶ A decorator for
Printermethods that specifies it should only be called when outputting in color.If the
Printerimplementing the decorated method hasPrinter.ansi_colorset toFalse, then this decorator will have the method automatically return aNullANSIContext.