Added get_json_from_command()

This commit is contained in:
2Shirt 2019-09-18 22:37:18 -07:00
parent 115a462f6e
commit f27f3024e8
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -1,6 +1,7 @@
"""WizardKit: Execution functions"""
#vim: sts=2 sw=2 ts=2
import json
import logging
import re
import subprocess
@ -92,6 +93,24 @@ def build_cmd_kwargs(cmd, minimized=False, pipe=True, shell=False, **kwargs):
return cmd_kwargs
def get_json_from_command(cmd, check=True, encoding='utf-8', errors='ignore'):
"""Capture JSON content from cmd output, returns dict.
If the data can't be decoded then either an exception is raised
or an empty dict is returned depending on ignore_errors.
"""
json_data = {}
try:
result = run_program(cmd, check=check, encoding=encoding, errors=errors)
json_data = json.loads(result.stdout)
except (subprocess.CalledProcessError, json.decoder.JSONDecodeError):
if errors != 'ignore':
raise
return json_data
def get_procs(name, exact=True):
"""Get process object(s) based on name, returns list of proc objects."""
LOG.debug('name: %s, exact: %s', name, exact)