Added get_json_from_command()
This commit is contained in:
parent
115a462f6e
commit
f27f3024e8
1 changed files with 19 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
"""WizardKit: Execution functions"""
|
"""WizardKit: Execution functions"""
|
||||||
#vim: sts=2 sw=2 ts=2
|
#vim: sts=2 sw=2 ts=2
|
||||||
|
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
@ -92,6 +93,24 @@ def build_cmd_kwargs(cmd, minimized=False, pipe=True, shell=False, **kwargs):
|
||||||
return cmd_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):
|
def get_procs(name, exact=True):
|
||||||
"""Get process object(s) based on name, returns list of proc objects."""
|
"""Get process object(s) based on name, returns list of proc objects."""
|
||||||
LOG.debug('name: %s, exact: %s', name, exact)
|
LOG.debug('name: %s, exact: %s', name, exact)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue