Skip to content

Commit af44b5e

Browse files
committed
added deprecated function annotation
1 parent debd64c commit af44b5e

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

examples/results.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@
1717
"""A script that reads XML search results from stdin and pretty-prints them
1818
back to stdout. The script is designed to be used with the search.py
1919
example, eg: './search.py "search 404" | ./results.py'"""
20-
20+
2121
from __future__ import absolute_import
2222
from pprint import pprint
2323
import sys, os
24+
2425
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
2526

2627
import splunklib.results as results
2728

29+
2830
def pretty():
29-
reader = results.ResultsReader(sys.stdin)
31+
reader = results.JSONResultsReader(sys.stdin)
3032
for event in reader:
3133
pprint(event)
3234

35+
3336
if __name__ == "__main__":
3437
pretty()

examples/search_modes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def modes(argv):
3333
while not job.is_ready():
3434
time.sleep(0.5)
3535
pass
36-
reader = results.ResultsReader(job.events(output_mode='json'))
36+
reader = results.JSONResultsReader(job.events(output_mode='json'))
3737
# Events found: 10
3838
print('Events found with adhoc_search_level="verbose": %s' % len([e for e in reader]))
3939

splunklib/client.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,9 +2767,8 @@ def pause(self):
27672767
return self
27682768

27692769
def results(self, **query_params):
2770-
"""Returns a streaming handle to this job's search results. To get a
2771-
nice, Pythonic iterator, pass the handle to :class:`splunklib.results.ResultsReader`,
2772-
as in::
2770+
"""Returns a streaming handle to this job's search results. To get a nice, Pythonic iterator, pass the handle
2771+
to :class:`splunklib.results.JSONResultsReader` along with the query param "output_mode='json'", as in::
27732772
27742773
import splunklib.client as client
27752774
import splunklib.results as results
@@ -2778,7 +2777,7 @@ def results(self, **query_params):
27782777
job = service.jobs.create("search * | head 5")
27792778
while not job.is_done():
27802779
sleep(.2)
2781-
rr = results.ResultsReader(job.results())
2780+
rr = results.JSONResultsReader(job.results(output_mode='json'))
27822781
for result in rr:
27832782
if isinstance(result, results.Message):
27842783
# Diagnostic messages may be returned in the results
@@ -2808,19 +2807,17 @@ def results(self, **query_params):
28082807
def preview(self, **query_params):
28092808
"""Returns a streaming handle to this job's preview search results.
28102809
2811-
Unlike :class:`splunklib.results.ResultsReader`, which requires a job to
2812-
be finished to
2813-
return any results, the ``preview`` method returns any results that have
2814-
been generated so far, whether the job is running or not. The
2815-
returned search results are the raw data from the server. Pass
2816-
the handle returned to :class:`splunklib.results.ResultsReader` to get a
2817-
nice, Pythonic iterator over objects, as in::
2810+
Unlike :class:`splunklib.results.JSONResultsReader`along with the query param "output_mode='json'",
2811+
which requires a job to be finished to return any results, the ``preview`` method returns any results that
2812+
have been generated so far, whether the job is running or not. The returned search results are the raw data
2813+
from the server. Pass the handle returned to :class:`splunklib.results.JSONResultsReader` to get a nice,
2814+
Pythonic iterator over objects, as in::
28182815
28192816
import splunklib.client as client
28202817
import splunklib.results as results
28212818
service = client.connect(...)
28222819
job = service.jobs.create("search * | head 5")
2823-
rr = results.ResultsReader(job.preview())
2820+
rr = results.JSONResultsReader(job.preview(output_mode='json'))
28242821
for result in rr:
28252822
if isinstance(result, results.Message):
28262823
# Diagnostic messages may be returned in the results
@@ -2975,15 +2972,15 @@ def create(self, query, **kwargs):
29752972
return Job(self.service, sid)
29762973

29772974
def export(self, query, **params):
2978-
"""Runs a search and immediately starts streaming preview events.
2979-
This method returns a streaming handle to this job's events as an XML
2980-
document from the server. To parse this stream into usable Python objects,
2981-
pass the handle to :class:`splunklib.results.ResultsReader`::
2975+
"""Runs a search and immediately starts streaming preview events. This method returns a streaming handle to
2976+
this job's events as an XML document from the server. To parse this stream into usable Python objects,
2977+
pass the handle to :class:`splunklib.results.JSONResultsReader` along with the query param
2978+
"output_mode='json'"::
29822979
29832980
import splunklib.client as client
29842981
import splunklib.results as results
29852982
service = client.connect(...)
2986-
rr = results.ResultsReader(service.jobs.export("search * | head 5"))
2983+
rr = results.JSONResultsReader(service.jobs.export("search * | head 5",output_mode='json'))
29872984
for result in rr:
29882985
if isinstance(result, results.Message):
29892986
# Diagnostic messages may be returned in the results
@@ -3032,14 +3029,14 @@ def itemmeta(self):
30323029
def oneshot(self, query, **params):
30333030
"""Run a oneshot search and returns a streaming handle to the results.
30343031
3035-
The ``InputStream`` object streams XML fragments from the server. To
3036-
parse this stream into usable Python objects,
3037-
pass the handle to :class:`splunklib.results.ResultsReader`::
3032+
The ``InputStream`` object streams XML fragments from the server. To parse this stream into usable Python
3033+
objects, pass the handle to :class:`splunklib.results.JSONResultsReader` along with the query param
3034+
"output_mode='json'" ::
30383035
30393036
import splunklib.client as client
30403037
import splunklib.results as results
30413038
service = client.connect(...)
3042-
rr = results.ResultsReader(service.jobs.oneshot("search * | head 5"))
3039+
rr = results.JSONResultsReader(service.jobs.oneshot("search * | head 5",output_mode='json'))
30433040
for result in rr:
30443041
if isinstance(result, results.Message):
30453042
# Diagnostic messages may be returned in the results

splunklib/results.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636

3737
from io import BufferedReader, BytesIO
3838

39-
import deprecation
40-
4139
from splunklib import six
4240

41+
from splunklib.six import deprecated
42+
4343
try:
4444
import xml.etree.cElementTree as et
4545
except:
@@ -161,7 +161,7 @@ def read(self, n=None):
161161
return response
162162

163163

164-
@deprecation.deprecated(deprecated_in="1.16.9", details="Use the JSONResultsReader function instead")
164+
@deprecated("Use the JSONResultsReader function instead in conjuction with the 'output_mode' query param set to 'json'")
165165
class ResultsReader(object):
166166
"""This class returns dictionaries and Splunk messages from an XML results
167167
stream.

splunklib/six.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,3 +978,16 @@ def python_2_unicode_compatible(klass):
978978
del i, importer
979979
# Finally, add the importer to the meta path import hook.
980980
sys.meta_path.append(_importer)
981+
982+
import warnings
983+
984+
def deprecated(message):
985+
def deprecated_decorator(func):
986+
def deprecated_func(*args, **kwargs):
987+
warnings.warn("{} is a deprecated function. {}".format(func.__name__, message),
988+
category=DeprecationWarning,
989+
stacklevel=2)
990+
warnings.simplefilter('default', DeprecationWarning)
991+
return func(*args, **kwargs)
992+
return deprecated_func
993+
return deprecated_decorator

0 commit comments

Comments
 (0)