diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2023-02-23 11:18:54 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-23 11:18:54 -0800 |
commit | 6b27aebeb429d1f7c2bb450c49cd43270d1d007e (patch) | |
tree | 57384d3bef7ba94defe6f1e881e90cd85a0d4ccc /lib | |
parent | 5c7cccd052e0c899aab3c2a7918c5e3b554abe4a (diff) | |
download | spack-6b27aebeb429d1f7c2bb450c49cd43270d1d007e.tar.gz spack-6b27aebeb429d1f7c2bb450c49cd43270d1d007e.tar.bz2 spack-6b27aebeb429d1f7c2bb450c49cd43270d1d007e.tar.xz spack-6b27aebeb429d1f7c2bb450c49cd43270d1d007e.zip |
colify.py: get rid of **kwargs (#35641)
`colify` is an old module in Spack that still uses `**kwargs` liberally.
We should be more explicit. Doing this eliminates the need for many
checks (can't pass the wrong arg if it isn't allowed) and makes the
function documentation more clear.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/llnl/util/tty/colify.py | 99 |
1 files changed, 62 insertions, 37 deletions
diff --git a/lib/spack/llnl/util/tty/colify.py b/lib/spack/llnl/util/tty/colify.py index 3ab7d9978a..d22be2a2e1 100644 --- a/lib/spack/llnl/util/tty/colify.py +++ b/lib/spack/llnl/util/tty/colify.py @@ -11,6 +11,7 @@ from __future__ import division, unicode_literals import io import os import sys +from typing import IO, Any, List, Optional from llnl.util.tty import terminal_size from llnl.util.tty.color import cextra, clen @@ -97,7 +98,16 @@ def config_uniform_cols(elts, console_width, padding, cols=0): return config -def colify(elts, **options): +def colify( + elts: List[Any], + cols: int = 0, + output: Optional[IO] = None, + indent: int = 0, + padding: int = 2, + tty: Optional[bool] = None, + method: str = "variable", + console_cols: Optional[int] = None, +): """Takes a list of elements as input and finds a good columnization of them, similar to how gnu ls does. This supports both uniform-width and variable-width (tighter) columns. @@ -106,31 +116,21 @@ def colify(elts, **options): using ``str()``. Keyword Arguments: - output (typing.IO): A file object to write to. Default is ``sys.stdout`` - indent (int): Optionally indent all columns by some number of spaces - padding (int): Spaces between columns. Default is 2 - width (int): Width of the output. Default is 80 if tty not detected - cols (int): Force number of columns. Default is to size to terminal, or + output: A file object to write to. Default is ``sys.stdout`` + indent: Optionally indent all columns by some number of spaces + padding: Spaces between columns. Default is 2 + width: Width of the output. Default is 80 if tty not detected + cols: Force number of columns. Default is to size to terminal, or single-column if no tty - tty (bool): Whether to attempt to write to a tty. Default is to autodetect a + tty: Whether to attempt to write to a tty. Default is to autodetect a tty. Set to False to force single-column output - method (str): Method to use to fit columns. Options are variable or uniform. + method: Method to use to fit columns. Options are variable or uniform. Variable-width columns are tighter, uniform columns are all the same width and fit less data on the screen + console_cols: number of columns on this console (default: autodetect) """ - # Get keyword arguments or set defaults - cols = options.pop("cols", 0) - output = options.pop("output", sys.stdout) - indent = options.pop("indent", 0) - padding = options.pop("padding", 2) - tty = options.pop("tty", None) - method = options.pop("method", "variable") - console_cols = options.pop("width", None) - - if options: - raise TypeError( - "'%s' is an invalid keyword argument for this function." % next(options.iterkeys()) - ) + if output is None: + output = sys.stdout # elts needs to be an array of strings so we can count the elements elts = [str(elt) for elt in elts] @@ -153,10 +153,11 @@ def colify(elts, **options): cols = 1 # Specify the number of character columns to use. - if not console_cols: + if console_cols is None: console_rows, console_cols = terminal_size() - elif type(console_cols) != int: + elif not isinstance(console_cols, int): raise ValueError("Number of columns must be an int") + console_cols = max(1, console_cols - indent) # Choose a method. Variable-width colums vs uniform-width. @@ -192,7 +193,13 @@ def colify(elts, **options): return (config.cols, tuple(config.widths)) -def colify_table(table, **options): +def colify_table( + table: List[List[Any]], + output: Optional[IO] = None, + indent: int = 0, + padding: int = 2, + console_cols: Optional[int] = None, +): """Version of ``colify()`` for data expressed in rows, (list of lists). Same as regular colify but: @@ -218,20 +225,38 @@ def colify_table(table, **options): for row in table: yield row[i] - if "cols" in options: - raise ValueError("Cannot override columsn in colify_table.") - options["cols"] = columns - - # don't reduce to 1 column for non-tty - options["tty"] = True - - colify(transpose(), **options) - - -def colified(elts, **options): + colify( + transpose(), + cols=columns, # this is always the number of cols in the table + tty=True, # don't reduce to 1 column for non-tty + output=output, + indent=indent, + padding=padding, + console_cols=console_cols, + ) + + +def colified( + elts: List[Any], + cols: int = 0, + output: Optional[IO] = None, + indent: int = 0, + padding: int = 2, + tty: Optional[bool] = None, + method: str = "variable", + console_cols: Optional[int] = None, +): """Invokes the ``colify()`` function but returns the result as a string instead of writing it to an output string.""" sio = io.StringIO() - options["output"] = sio - colify(elts, **options) + colify( + elts, + cols=cols, + output=sio, + indent=indent, + padding=padding, + tty=tty, + method=method, + console_cols=console_cols, + ) return sio.getvalue() |