Skip to content

Commit e7baa56

Browse files
committed
more comments, docstrings, and strictures on use of get_json_encoded_struct
1 parent f2d11e9 commit e7baa56

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

irods/message/__init__.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,38 @@ def __init__(self, msg_type=b'', msg=None, error=b'', bs=b'', int_info=0):
284284
self.bs = bs
285285
self.int_info = int_info
286286

287+
class InappropriateXMLTarget(Exception): pass
288+
287289
def get_json_encoded_struct (self):
290+
"""For messages having STR_PI and *BytesBuf_PI in the highest level XML tag.
288291
292+
Invoke this method to recover a (usually JSON-formatted) server message
293+
returned by a server API.
294+
"""
289295
Xml = ET().fromstring(self.msg.replace(b'\0',b''))
290296

291-
STR_PI_element = Xml.find('myStr')
292-
if STR_PI_element is not None:
293-
return json.loads( STR_PI_element.text )
297+
# Handle STR_PI case, which corresponds to server APIs with a 'char**' output parameter.
298+
if Xml.tag == 'STR_PI':
299+
STR_PI_element = Xml.find('myStr')
300+
if STR_PI_element is not None:
301+
return json.loads( STR_PI_element.text )
302+
303+
# Handle remaining cases, i.e. BinBytesBuf_PI and BytesBuf_PI.
304+
json_str = getattr(Xml.find('buf'), 'text', None)
305+
if json_str is None:
306+
error_text = "Message does not have a suitable 'buf' tag from which to extract text or binary content."
307+
raise self.InappropriateXMLTarget(error_text)
294308

295-
json_str = Xml.find('buf').text
296309
if Xml.tag == 'BinBytesBuf_PI':
297310
mybin = JSON_Binary_Response()
298311
mybin.unpack(Xml)
299312
json_str = mybin.buf.replace(b'\0',b'').decode()
300-
return json.loads( json_str )
313+
314+
if Xml.tag in ('BinBytesBuf_PI', 'BytesBuf_PI'):
315+
return json.loads( json_str )
316+
317+
error_text = "Inappropriate top-level tag '{Xml.tag}' used in iRODSMessage.get_json_encoded_struct".format(**locals())
318+
raise self.InappropriateXMLTarget(error_text)
301319

302320
@staticmethod
303321
def recv(sock):

0 commit comments

Comments
 (0)