Update watch.py
Changes made in unmarshal_event for not having issues with empty lines.
This commit is contained in:
parent
14a2554c18
commit
f0a73c8824
@ -82,7 +82,7 @@ def iter_resp_lines(resp):
|
|||||||
last_was_empty = False # Reset empty-line flag
|
last_was_empty = False # Reset empty-line flag
|
||||||
else:
|
else:
|
||||||
if not last_was_empty:
|
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
|
last_was_empty = True # Mark that we handled an empty line
|
||||||
next_newline = buffer.find(b'\n')
|
next_newline = buffer.find(b'\n')
|
||||||
|
|
||||||
@ -113,24 +113,29 @@ class Watch(object):
|
|||||||
return 'watch'
|
return 'watch'
|
||||||
|
|
||||||
def unmarshal_event(self, data, return_type):
|
def unmarshal_event(self, data, return_type):
|
||||||
js = json.loads(data)
|
if not data or data.isspace():
|
||||||
js['raw_object'] = js['object']
|
return None
|
||||||
# BOOKMARK event is treated the same as ERROR for a quick fix of
|
try:
|
||||||
# decoding exception
|
js = json.loads(data)
|
||||||
# TODO: make use of the resource_version in BOOKMARK event for more
|
js['raw_object'] = js['object']
|
||||||
# efficient WATCH
|
# BOOKMARK event is treated the same as ERROR for a quick fix of
|
||||||
if return_type and js['type'] != 'ERROR' and js['type'] != 'BOOKMARK':
|
# decoding exception
|
||||||
obj = SimpleNamespace(data=json.dumps(js['raw_object']))
|
# TODO: make use of the resource_version in BOOKMARK event for more
|
||||||
js['object'] = self._api_client.deserialize(obj, return_type)
|
# efficient WATCH
|
||||||
if hasattr(js['object'], 'metadata'):
|
if return_type and js['type'] != 'ERROR' and js['type'] != 'BOOKMARK':
|
||||||
self.resource_version = js['object'].metadata.resource_version
|
obj = SimpleNamespace(data=json.dumps(js['raw_object']))
|
||||||
# For custom objects that we don't have model defined, json
|
js['object'] = self._api_client.deserialize(obj, return_type)
|
||||||
# deserialization results in dictionary
|
if hasattr(js['object'], 'metadata'):
|
||||||
elif (isinstance(js['object'], dict) and 'metadata' in js['object']
|
self.resource_version = js['object'].metadata.resource_version
|
||||||
and 'resourceVersion' in js['object']['metadata']):
|
# For custom objects that we don't have model defined, json
|
||||||
self.resource_version = js['object']['metadata'][
|
# deserialization results in dictionary
|
||||||
'resourceVersion']
|
elif (isinstance(js['object'], dict) and 'metadata' in js['object']
|
||||||
return js
|
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):
|
def stream(self, func, *args, **kwargs):
|
||||||
"""Watch an API resource and stream the result back via a generator.
|
"""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
|
yield line # Normal non-empty line
|
||||||
last_was_empty = False
|
last_was_empty = False
|
||||||
elif not last_was_empty:
|
elif not last_was_empty:
|
||||||
yield '/n' # Only yield one empty line
|
yield '' # Only yield one empty line
|
||||||
last_was_empty = True
|
last_was_empty = True
|
||||||
if self._stop:
|
if self._stop:
|
||||||
break
|
break
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user