From 69570ac7088e78edab2d41809a86be2cd9e6e2fd Mon Sep 17 00:00:00 2001 From: Oz Tiram Date: Tue, 17 Sep 2019 16:45:12 +0200 Subject: [PATCH] Add property returncode to WSClient This will be familiar for Python users as subprocess.Popen has the same attribute. Also, the behavior is such the returncode returns a None value if the process was not run yet. Other than that, when the process exists with a numerical code this will be the value of the return code. If the command executed successfully the return value will be 0. --- stream/ws_client.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/stream/ws_client.py b/stream/ws_client.py index 65f0df17..935c9885 100644 --- a/stream/ws_client.py +++ b/stream/ws_client.py @@ -14,15 +14,19 @@ from kubernetes.client.rest import ApiException -import select import certifi -import time import collections -from websocket import WebSocket, ABNF, enableTrace -import six +import select import ssl +import time + +import six +import yaml + from six.moves.urllib.parse import urlencode, quote_plus, urlparse, urlunparse +from websocket import WebSocket, ABNF, enableTrace + STDIN_CHANNEL = 0 STDOUT_CHANNEL = 1 STDERR_CHANNEL = 2 @@ -203,6 +207,21 @@ def run_forever(self, timeout=None): else: while self.is_open(): self.update(timeout=None) + @property + def returncode(self): + """ + The return code, A None value indicates that the process hasn't + terminated yet. + """ + if self.is_open(): + return None + else: + err = self.read_channel(ERROR_CHANNEL) + err = yaml.safe_load(err) + if err['status'] == "Success": + return 0 + return int(err['details']['causes'][0]['message']) + def close(self, **kwargs): """