Skip to content

Commit bb3ff94

Browse files
author
Frederick Ross
committed
Added armor plating around running the examples.
Try adding ../ to PYTHONPATH within the example so they will run when run from the examples directory. Add an exception with a useful error message when run in a configuration where they cannot import utils. Removed a few unused imports.
1 parent 498d397 commit bb3ff94

33 files changed

+170
-77
lines changed

examples/analytics/input.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import urllib2, sys
1818
from datetime import datetime
1919
import splunklib.client as client
20-
import utils
20+
21+
try:
22+
import utils
23+
except ImportError:
24+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
25+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
2126

2227
__all__ = [
2328
"AnalyticsTracker",

examples/analytics/output.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
import urllib2, sys
1818
import splunklib.client as client
1919
import splunklib.results as results
20-
import utils
20+
try:
21+
import utils
22+
except ImportError:
23+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
24+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
2125

2226
__all__ = [
2327
"TimeRange",

examples/analytics/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
from input import AnalyticsTracker
2424
from output import AnalyticsRetriever, TimeRange
25-
import utils
25+
try:
26+
import utils
27+
except ImportError:
28+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
29+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
2630

2731
splunk_opts = None
2832
retrievers = {}

examples/async/async.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727

2828
import splunklib.binding as binding
2929
import splunklib.client as client
30-
from utils import parse, error
30+
try:
31+
from utils import parse, error
32+
except ImportError:
33+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
34+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
35+
3136

3237
# Placeholder for a specific implementation of `urllib2`,
3338
# to be defined depending on whether or not we are running

examples/binding1.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424

2525
from splunklib.binding import connect
2626

27-
from utils import parse
27+
try:
28+
from utils import parse
29+
except ImportError:
30+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
31+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
32+
2833

2934
class Service:
3035
def __init__(self, context):

examples/conf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
sys.path.insert(0, '../')
2121

2222
from splunklib.client import connect
23-
from utils import error, parse
23+
24+
try:
25+
from utils import error, parse
26+
except ImportError:
27+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
28+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
2429

2530
class Program:
2631
"""Break up operations into specific methods."""

examples/dashboard/feed.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525

2626
import splunklib.client as client
2727
import splunklib.results as results
28-
from utils import parse, error
28+
try:
29+
from utils import parse
30+
except ImportError:
31+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
32+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
33+
2934

3035
leftronic_access_key = ""
3136

examples/event_types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
from splunklib.client import connect
2323

24-
from utils import parse
24+
try:
25+
from utils import parse
26+
except ImportError:
27+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
28+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
2529

2630
def main():
2731
opts = parse(sys.argv[1:], {}, ".splunkrc")

examples/explorer/explorer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
sys.path.insert(0, '../../') # Use splunklib and utils without installing
2323

24-
import utils
24+
try:
25+
import utils
26+
except ImportError:
27+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
28+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
2529
import urllib
2630

2731
PORT = 8080

examples/export/export.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626

2727
# splunk support files
2828
from splunklib.binding import connect
29-
from utils import error, parse
29+
try:
30+
from utils import error, parse
31+
except ImportError:
32+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
33+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
3034

3135
# hidden file
3236
OUTPUT_FILE = "./export.out"

0 commit comments

Comments
 (0)