Update watch.py

Changes made in unmarshal_event for not having issues with empty lines.
This commit is contained in:
Raj Bhargav 2025-03-19 14:15:44 +05:30 committed by GitHub
parent 14a2554c18
commit f0a73c8824
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -82,7 +82,7 @@ def iter_resp_lines(resp):
last_was_empty = False # Reset empty-line flag
else:
if not last_was_empty:
yield '\n' # Only print one empty line
yield '' # Only print one empty line
last_was_empty = True # Mark that we handled an empty line
next_newline = buffer.find(b'\n')
@ -113,24 +113,29 @@ class Watch(object):
return 'watch'
def unmarshal_event(self, data, return_type):
js = json.loads(data)
js['raw_object'] = js['object']
# BOOKMARK event is treated the same as ERROR for a quick fix of
# decoding exception
# TODO: make use of the resource_version in BOOKMARK event for more
# efficient WATCH
if return_type and js['type'] != 'ERROR' and js['type'] != 'BOOKMARK':
obj = SimpleNamespace(data=json.dumps(js['raw_object']))
js['object'] = self._api_client.deserialize(obj, return_type)
if hasattr(js['object'], 'metadata'):
self.resource_version = js['object'].metadata.resource_version
# For custom objects that we don't have model defined, json
# deserialization results in dictionary
elif (isinstance(js['object'], dict) and 'metadata' in js['object']
and 'resourceVersion' in js['object']['metadata']):
self.resource_version = js['object']['metadata'][
'resourceVersion']
return js
if not data or data.isspace():
return None
try:
js = json.loads(data)
js['raw_object'] = js['object']
# BOOKMARK event is treated the same as ERROR for a quick fix of
# decoding exception
# TODO: make use of the resource_version in BOOKMARK event for more
# efficient WATCH
if return_type and js['type'] != 'ERROR' and js['type'] != 'BOOKMARK':
obj = SimpleNamespace(data=json.dumps(js['raw_object']))
js['object'] = self._api_client.deserialize(obj, return_type)
if hasattr(js['object'], 'metadata'):
self.resource_version = js['object'].metadata.resource_version
# For custom objects that we don't have model defined, json
# deserialization results in dictionary
elif (isinstance(js['object'], dict) and 'metadata' in js['object']
and 'resourceVersion' in js['object']['metadata']):
self.resource_version = js['object']['metadata'][
'resourceVersion']
return js
except json.JSONDecodeError:
return None
def stream(self, func, *args, **kwargs):
"""Watch an API resource and stream the result back via a generator.
@ -209,7 +214,7 @@ class Watch(object):
yield line # Normal non-empty line
last_was_empty = False
elif not last_was_empty:
yield '/n' # Only yield one empty line
yield '' # Only yield one empty line
last_was_empty = True
if self._stop:
break