vistir.misc module

vistir.misc.shell_escape(cmd: Union[str, List[str]]) → str[source]

Escape strings for use in Popen() and run().

This is a passthrough method for instantiating a Script object which can be used to escape commands to output as a single string.

vistir.misc.unnest(elem)[source]

Flatten an arbitrarily nested iterable.

Parameters:elem (Iterable) – An iterable to flatten
>>> nested_iterable = (
        1234, (3456, 4398345, (234234)), (
            2396, (
                23895750, 9283798, 29384, (
                    289375983275, 293759, 2347, (
                        2098, 7987, 27599
                    )
                )
            )
        )
    )
>>> list(vistir.misc.unnest(nested_iterable))
[1234, 3456, 4398345, 234234, 2396, 23895750, 9283798, 29384, 289375983275, 293759,
 2347, 2098, 7987, 27599]
vistir.misc.run(cmd, env=None, return_object=False, block=True, cwd=None, verbose=False, nospin=False, spinner_name=None, combine_stderr=True, display_limit=200, write_to_stdout=True, encoding='utf-8')[source]

Use subprocess.Popen to get the output of a command and decode it.

Parameters:
  • cmd (list) – A list representing the command you want to run.
  • env (dict) – Additional environment settings to pass through to the subprocess.
  • return_object (bool) – When True, returns the whole subprocess instance
  • block (bool) – When False, returns a potentially still-running subprocess.Popen instance
  • cwd (str) – Current working directory context to use for spawning the subprocess.
  • verbose (bool) – Whether to print stdout in real time when non-blocking.
  • nospin (bool) – Whether to disable the cli spinner.
  • spinner_name (str) – The name of the spinner to use if enabled, defaults to bouncingBar
  • combine_stderr (bool) – Optionally merge stdout and stderr in the subprocess, false if nonblocking.
  • dispay_limit (int) – The max width of output lines to display when using a spinner.
  • write_to_stdout (bool) – Whether to write to stdout when using a spinner, defaults to True.
Returns:

A 2-tuple of (output, error) or a subprocess.Popen object.

Warning

Merging standard out and standard error in a nonblocking subprocess can cause errors in some cases and may not be ideal. Consider disabling this functionality.

vistir.misc.load_path(python)[source]

Load the sys.path from the given python executable’s environment as json.

Parameters:python (str) – Path to a valid python executable
Returns:A python representation of the sys.path value of the given python executable.
Return type:list
>>> load_path("/home/user/.virtualenvs/requirementslib-5MhGuG3C/bin/python")
['', '/home/user/.virtualenvs/requirementslib-5MhGuG3C/lib/python37.zip',
 '/home/user/.virtualenvs/requirementslib-5MhGuG3C/lib/python3.7',
 '/home/user/.virtualenvs/requirementslib-5MhGuG3C/lib/python3.7/lib-dynload',
 '/home/user/.pyenv/versions/3.7.0/lib/python3.7',
 '/home/user/.virtualenvs/requirementslib-5MhGuG3C/lib/python3.7/site-packages',
 '/home/user/git/requirementslib/src']
vistir.misc.partialclass(cls, *args, **kwargs)[source]

Returns a partially instantiated class.

Returns:A partial class instance
Return type:cls
>>> source = partialclass(Source, url="https://pypi.org/simple")
>>> source
<class '__main__.Source'>
>>> source(name="pypi")
>>> source.__dict__
mappingproxy({
    '__module__': '__main__',
    '__dict__': <attribute '__dict__' of 'Source' objects>,
    '__weakref__': <attribute '__weakref__' of 'Source' objects>,
    '__doc__': None,
    '__init__': functools.partialmethod(
        <function Source.__init__ at 0x7f23af429bf8>, , url='https://pypi.org/simple'
    )
})
>>> new_source = source(name="pypi")
>>> new_source
<__main__.Source object at 0x7f23af189b38>
>>> new_source.__dict__
{'url': 'https://pypi.org/simple', 'verify_ssl': True, 'name': 'pypi'}
vistir.misc.to_text(string, encoding='utf-8', errors=None)[source]

Force a value to a text-type.

Parameters:
  • string (str or bytes unicode) – Some input that can be converted to a unicode representation.
  • encoding – The encoding to use for conversions, defaults to “utf-8”
  • encoding – str, optional
Returns:

The unicode representation of the string

Return type:

str

vistir.misc.to_bytes(string, encoding='utf-8', errors=None)[source]

Force a value to bytes.

Parameters:
  • string (str or bytes unicode or a memoryview subclass) – Some input that can be converted to a bytes.
  • encoding – The encoding to use for conversions, defaults to “utf-8”
  • encoding – str, optional
Returns:

Corresponding byte representation (for use in filesystem operations)

Return type:

bytes

vistir.misc.getpreferredencoding()[source]

Determine the proper output encoding for terminal rendering.

vistir.misc.decode_for_output(output, target_stream=None, translation_map=None)[source]

Given a string, decode it for output to a terminal.

Parameters:
  • output (str) – A string to print to a terminal
  • target_stream – A stream to write to, we will encode to target this stream if possible.
  • translation_map (dict) – A mapping of unicode character ordinals to replacement strings.
Returns:

A re-encoded string using the preferred encoding

Return type:

str

vistir.misc.get_canonical_encoding_name(name)[source]

Given an encoding name, get the canonical name from a codec lookup.

Parameters:name (str) – The name of the codec to lookup
Returns:The canonical version of the codec name
Return type:str
vistir.misc.get_wrapped_stream(stream, encoding=None, errors='replace')[source]

Given a stream, wrap it in a StreamWrapper instance and return the wrapped stream.

Parameters:
  • stream – A stream instance to wrap
  • encoding (str) – The encoding to use for the stream
  • errors (str) – The error handler to use, default “replace”
Returns:

A new, wrapped stream

Return type:

StreamWrapper

class vistir.misc.StreamWrapper(stream, encoding, errors, line_buffering=True, **kwargs)[source]

Bases: _io.TextIOWrapper

This wrapper class will wrap a provided stream and supply an interface for compatibility.

isatty()[source]

Return whether this is an ‘interactive’ stream.

Return False if it can’t be determined.

write(x)[source]

Write string to stream. Returns the number of characters written (which is always equal to the length of the string).

writelines(lines)[source]

Write a list of lines to stream.

Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.