Added run_program()

This commit is contained in:
2Shirt 2019-09-15 15:10:13 -07:00
parent 6f60006c9a
commit 40413151c8
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -1,4 +1,5 @@
'''WizardKit: Standard Functions'''
# pylint: disable=too-many-lines
# vim: sts=2 sw=2 ts=2
import itertools
@ -874,6 +875,31 @@ def print_warning(msg, **kwargs):
print_colored([msg], ['YELLOW'], **kwargs)
def run_program(cmd, check=True, pipe=True, shell=False, **kwargs):
"""Run program and return a subprocess.CompletedProcess object."""
cmd_kwargs = {
'args': cmd,
'check': check,
'shell': shell,
}
# Add additional kwargs if applicable
for key in ('cwd', 'encoding', 'errors', 'stderr', 'stdout'):
if key in kwargs:
cmd_kwargs[key] = kwargs[key]
# Finalize cmd_kwargs
if pipe:
cmd_kwargs['stderr'] = subprocess.PIPE
cmd_kwargs['stdout'] = subprocess.PIPE
if not ('encoding' in cmd_kwargs or 'errors' in cmd_kwargs):
cmd_kwargs['encoding'] = 'utf-8'
cmd_kwargs['errors'] = 'ignore'
# Ready to run program
return subprocess.run(**cmd_kwargs)
def set_title(title):
"""Set window title."""
if os.name == 'nt':