3636
3737from io import BufferedReader , BytesIO
3838
39+ import deprecation
40+
3941from splunklib import six
42+
4043try :
4144 import xml .etree .cElementTree as et
4245except :
5558 "Message"
5659]
5760
61+
5862class Message (object ):
5963 """This class represents informational messages that Splunk interleaves in the results stream.
6064
@@ -65,6 +69,7 @@ class Message(object):
6569
6670 m = Message("DEBUG", "There's something in that variable...")
6771 """
72+
6873 def __init__ (self , type_ , message ):
6974 self .type = type_
7075 self .message = message
@@ -78,6 +83,7 @@ def __eq__(self, other):
7883 def __hash__ (self ):
7984 return hash ((self .type , self .message ))
8085
86+
8187class _ConcatenatedStream (object ):
8288 """Lazily concatenate zero or more streams into a stream.
8389
@@ -90,6 +96,7 @@ class _ConcatenatedStream(object):
9096 s = _ConcatenatedStream(StringIO("abc"), StringIO("def"))
9197 assert s.read() == "abcdef"
9298 """
99+
93100 def __init__ (self , * streams ):
94101 self .streams = list (streams )
95102
@@ -108,6 +115,7 @@ def read(self, n=None):
108115 del self .streams [0 ]
109116 return response
110117
118+
111119class _XMLDTDFilter (object ):
112120 """Lazily remove all XML DTDs from a stream.
113121
@@ -121,6 +129,7 @@ class _XMLDTDFilter(object):
121129 s = _XMLDTDFilter("<?xml abcd><element><?xml ...></element>")
122130 assert s.read() == "<element></element>"
123131 """
132+
124133 def __init__ (self , stream ):
125134 self .stream = stream
126135
@@ -151,6 +160,8 @@ def read(self, n=None):
151160 n -= 1
152161 return response
153162
163+
164+ @deprecation .deprecated (deprecated_in = "1.16.9" , details = "Use the JSONResultsReader function instead" )
154165class ResultsReader (object ):
155166 """This class returns dictionaries and Splunk messages from an XML results
156167 stream.
@@ -178,6 +189,7 @@ class ResultsReader(object):
178189 print "Message: %s" % result
179190 print "is_preview = %s " % reader.is_preview
180191 """
192+
181193 # Be sure to update the docstrings of client.Jobs.oneshot,
182194 # client.Job.results_preview and client.Job.results to match any
183195 # changes made to ResultsReader.
@@ -258,16 +270,16 @@ def _parse_results(self, stream):
258270 # So we'll define it here
259271
260272 def __itertext (self ):
261- tag = self .tag
262- if not isinstance (tag , six .string_types ) and tag is not None :
263- return
264- if self .text :
265- yield self .text
266- for e in self :
267- for s in __itertext (e ):
268- yield s
269- if e .tail :
270- yield e .tail
273+ tag = self .tag
274+ if not isinstance (tag , six .string_types ) and tag is not None :
275+ return
276+ if self .text :
277+ yield self .text
278+ for e in self :
279+ for s in __itertext (e ):
280+ yield s
281+ if e .tail :
282+ yield e .tail
271283
272284 text = "" .join (__itertext (elem ))
273285 values .append (text )
@@ -288,6 +300,7 @@ def __itertext(self):
288300 else :
289301 raise
290302
303+
291304class JSONResultsReader (object ):
292305 """This class returns dictionaries and Splunk messages from a JSON results
293306 stream.
@@ -310,6 +323,7 @@ class JSONResultsReader(object):
310323 print "Message: %s" % result
311324 print "is_preview = %s " % reader.is_preview
312325 """
326+
313327 # Be sure to update the docstrings of client.Jobs.oneshot,
314328 # client.Job.results_preview and client.Job.results to match any
315329 # changes made to JSONResultsReader.
@@ -339,7 +353,7 @@ def _parse_results(self, stream):
339353 """Parse results and messages out of *stream*."""
340354 for line in stream .readlines ():
341355 strip_line = line .strip ()
342- if strip_line .__len__ () == 0 : continue
356+ if strip_line .__len__ () == 0 : continue
343357 parsed_line = json_loads (strip_line )
344358 if "preview" in parsed_line :
345359 self .is_preview = parsed_line ["preview" ]
@@ -352,4 +366,4 @@ def _parse_results(self, stream):
352366 yield parsed_line ["result" ]
353367 if "results" in parsed_line :
354368 for result in parsed_line ["results" ]:
355- yield result
369+ yield result
0 commit comments