shell_command: add "log_complete" argument to run methods

This brings consistency to the "run" methods.  It also removes potential confusion between the previous "quiet" argument and the "verbose" argument in "run()".

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-03-06 19:25:14 -05:00
parent 44f1147f71
commit 5d095125d3
2 changed files with 8 additions and 7 deletions

View File

@ -103,7 +103,7 @@ class ProcStats:
async def _check_throttled_state(self): async def _check_throttled_state(self):
try: try:
resp = await self.vcgencmd.run_with_response( resp = await self.vcgencmd.run_with_response(
timeout=.5, quiet=True) timeout=.5, log_complete=False)
ts = int(resp.strip().split("=")[-1], 16) ts = int(resp.strip().split("=")[-1], 16)
except Exception: except Exception:
return {'bits': 0, 'flags': ["?"]} return {'bits': 0, 'flags': ["?"]}

View File

@ -102,7 +102,7 @@ class ShellCommand:
def get_return_code(self): def get_return_code(self):
return self.return_code return self.return_code
async def run(self, timeout=2., verbose=True): async def run(self, timeout=2., verbose=True, log_complete=True):
self.return_code = self.proc = None self.return_code = self.proc = None
self.cancelled = False self.cancelled = False
if not timeout: if not timeout:
@ -124,9 +124,10 @@ class ShellCommand:
await self.proc.cancel() await self.proc.cancel()
else: else:
complete = not self.cancelled complete = not self.cancelled
return self._check_proc_success(complete) return self._check_proc_success(complete, log_complete)
async def run_with_response(self, timeout=2., retries=1, quiet=False): async def run_with_response(self, timeout=2., retries=1,
log_complete=True):
self.return_code = self.proc = None self.return_code = self.proc = None
self.cancelled = False self.cancelled = False
while retries > 0: while retries > 0:
@ -143,7 +144,7 @@ class ShellCommand:
complete = not self.cancelled complete = not self.cancelled
if self.log_stderr and stderr: if self.log_stderr and stderr:
logging.info(f"{self.command[0]}: {stderr.decode()}") logging.info(f"{self.command[0]}: {stderr.decode()}")
if self._check_proc_success(complete, quiet): if self._check_proc_success(complete, log_complete):
return stdout.decode().rstrip("\n") return stdout.decode().rstrip("\n")
elif stdout: elif stdout:
logging.debug( logging.debug(
@ -175,7 +176,7 @@ class ShellCommand:
return False return False
return True return True
def _check_proc_success(self, complete, quiet=False): def _check_proc_success(self, complete, log_complete):
self.return_code = self.proc.returncode self.return_code = self.proc.returncode
success = self.return_code == 0 and complete success = self.return_code == 0 and complete
if success: if success:
@ -187,7 +188,7 @@ class ShellCommand:
else: else:
msg = f"Command ({self.name}) exited with return code" \ msg = f"Command ({self.name}) exited with return code" \
f" {self.return_code}" f" {self.return_code}"
if not quiet: if log_complete:
logging.info(msg) logging.info(msg)
return success return success