3232 print "Results are a preview: %s" % reader.is_preview
3333"""
3434
35- from __future__ import absolute_import
36-
3735from io import BufferedReader , BytesIO
3836
39- from splunklib import six
40-
4137from splunklib .six import deprecated
4238
43- try :
44- import xml .etree .cElementTree as et
45- except :
46- import xml .etree .ElementTree as et
39+ import xml .etree .ElementTree as et
4740
4841from collections import OrderedDict
4942from json import loads as json_loads
5043
51- try :
52- from splunklib .six .moves import cStringIO as StringIO
53- except :
54- from splunklib .six import StringIO
55-
5644__all__ = [
5745 "ResultsReader" ,
5846 "Message" ,
@@ -76,7 +64,7 @@ def __init__(self, type_, message):
7664 self .message = message
7765
7866 def __repr__ (self ):
79- return "%s: %s" % ( self .type , self .message )
67+ return f" { self .type } : { self .message } "
8068
8169 def __eq__ (self , other ):
8270 return (self .type , self .message ) == (other .type , other .message )
@@ -264,25 +252,7 @@ def _parse_results(self, stream):
264252 elem .clear ()
265253
266254 elif elem .tag in ('text' , 'v' ) and event == 'end' :
267- try :
268- text = "" .join (elem .itertext ())
269- except AttributeError :
270- # Assume we're running in Python < 2.7, before itertext() was added
271- # So we'll define it here
272-
273- def __itertext (self ):
274- tag = self .tag
275- if not isinstance (tag , six .string_types ) and tag is not None :
276- return
277- if self .text :
278- yield self .text
279- for e in self :
280- for s in __itertext (e ):
281- yield s
282- if e .tail :
283- yield e .tail
284-
285- text = "" .join (__itertext (elem ))
255+ text = "" .join (elem .itertext ())
286256 values .append (text )
287257 elem .clear ()
288258
@@ -298,11 +268,10 @@ def __itertext(self):
298268 # splunk that is described in __init__.
299269 if 'no element found' in pe .msg :
300270 return
301- else :
302- raise
271+ raise
303272
304273
305- class JSONResultsReader ( object ) :
274+ class JSONResultsReader :
306275 """This class returns dictionaries and Splunk messages from a JSON results
307276 stream.
308277 ``JSONResultsReader`` is iterable, and returns a ``dict`` for results, or a
@@ -355,6 +324,8 @@ def next(self):
355324
356325 def _parse_results (self , stream ):
357326 """Parse results and messages out of *stream*."""
327+ msg_type = None
328+ text = None
358329 for line in stream .readlines ():
359330 strip_line = line .strip ()
360331 if strip_line .__len__ () == 0 : continue
0 commit comments