Skip to content

Commit 78b3dd5

Browse files
author
Shakeel Mohamed
committed
Merge branch 'fix/searchcommands' into feature/dnoble-bug_fixes
2 parents 7b71977 + 5384f4a commit 78b3dd5

File tree

13 files changed

+1109
-1564
lines changed

13 files changed

+1109
-1564
lines changed

examples/searchcommands_app/bin/simulate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class SimulateCommand(GeneratingCommand):
8686

8787
def generate(self):
8888
""" Yields one random record at a time for the duration of `duration` """
89-
self.logger.debug('SimulateCommand: %s' % self) # log command line
89+
self.logger.debug('SimulateCommand: %s', self) # log command line
9090
if not self.records:
9191
if self.seed is not None:
9292
random.seed(self.seed)

splunklib/searchcommands/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def __repr__(self):
222222
return str(self)
223223

224224
def __str__(self):
225-
value = self.validator.format(self.value) if self.validator is not None else str(self.value)
225+
value = str(self.value) if self.validator is None else self.validator.format(self.value)
226226
encoder = Option.Encoder(self)
227227
text = '='.join([self.name, encoder.encode(value)])
228228
return text

splunklib/searchcommands/logging.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import os
2020
import sys
2121

22+
def get_app_directory(probing_path):
23+
return os.path.dirname(os.path.abspath(os.path.dirname(probing_path)))
2224

23-
def configure(name, path=None):
25+
def configure(name, probing_path=None, app_root=None):
2426
""" Configure logging and return a logger and the location of its logging
2527
configuration file.
2628
@@ -57,63 +59,67 @@ def configure(name, path=None):
5759
return.
5860
5961
You may short circuit the search for a logging configuration file by
60-
providing an alternative file location in `path`. Logging configuration
62+
providing an alternative file location in `probing_path`. Logging configuration
6163
files must be in `ConfigParser format`_.
6264
6365
#Arguments:
6466
6567
:param name: Logger name
6668
:type name: str
67-
:param path: Location of an alternative logging configuration file or `None`
68-
:type path: str or NoneType
69+
:param probing_path: Location of an alternative logging configuration file or `None`
70+
:type probing_path: str or NoneType
6971
:returns: A logger and the location of its logging configuration file
72+
:param app_root: The root of the application directory, used primarily by tests.
73+
:type app_root: str or NoneType
7074
7175
.. _ConfigParser format: http://goo.gl/K6edZ8
7276
7377
"""
74-
app_directory = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))
78+
if app_root:
79+
print "app_root is " + app_root
80+
app_directory = get_app_directory(sys.argv[0]) if app_root is None else app_root
7581

76-
if path is None:
77-
probing_path = [
82+
if probing_path is None:
83+
probing_paths = [
7884
'local/%s.logging.conf' % name,
7985
'default/%s.logging.conf' % name,
8086
'local/logging.conf',
8187
'default/logging.conf']
82-
for relative_path in probing_path:
88+
for relative_path in probing_paths:
8389
configuration_file = os.path.join(app_directory, relative_path)
8490
if os.path.exists(configuration_file):
85-
path = configuration_file
91+
probing_path = configuration_file
8692
break
87-
elif not os.path.isabs(path):
93+
elif not os.path.isabs(probing_path):
8894
found = False
8995
for conf in 'local', 'default':
90-
configuration_file = os.path.join(app_directory, conf, path)
96+
configuration_file = os.path.join(app_directory, conf, probing_path)
9197
if os.path.exists(configuration_file):
92-
path = configuration_file
98+
probing_path = configuration_file
9399
found = True
94100
break
95101
if not found:
96102
raise ValueError(
97103
'Logging configuration file "%s" not found in local or default '
98-
'directory' % path)
99-
elif not os.path.exists(path):
104+
'directory' % probing_path)
105+
elif not os.path.exists(probing_path):
100106
raise ValueError('Logging configuration file "%s" not found')
101107

102-
if path is not None:
108+
if probing_path is not None:
103109
working_directory = os.getcwd()
104110
os.chdir(app_directory)
105111
try:
106112
splunk_home = os.path.normpath(os.path.join(working_directory, os.environ['SPLUNK_HOME']))
107113
except KeyError:
108114
splunk_home = working_directory # reasonable in debug scenarios
109115
try:
110-
path = os.path.abspath(path)
111-
fileConfig(path, {'SPLUNK_HOME': splunk_home})
116+
probing_path = os.path.abspath(probing_path)
117+
fileConfig(probing_path, {'SPLUNK_HOME': splunk_home})
112118
finally:
113119
os.chdir(working_directory)
114120

115121
if len(root.handlers) == 0:
116122
root.addHandler(StreamHandler())
117123

118124
logger = getLogger(name)
119-
return logger, path
125+
return logger, probing_path

splunklib/searchcommands/search_command.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ class SearchCommand(object):
4343
4444
"""
4545

46-
def __init__(self):
46+
def __init__(self, app_root=None):
47+
"""
48+
:param app_root: The root of the application directory, used primarily by tests.
49+
:type app_root: str or NoneType
50+
"""
4751

4852
# Variables that may be used, but not altered by derived classes
4953

50-
self.logger, self._logging_configuration = logging.configure(type(self).__name__)
54+
self.logger, self._logging_configuration = logging.configure(type(self).__name__, app_root=app_root)
5155
self.input_header = InputHeader()
5256
self.messages = MessagesHeader()
5357

@@ -61,6 +65,7 @@ def __init__(self):
6165

6266
# Variables backing option/property values
6367

68+
self._app_root = app_root
6469
self._default_logging_level = self.logger.level
6570
self._configuration = None
6671
self._fieldnames = None
@@ -95,7 +100,7 @@ def logging_configuration(self):
95100
@logging_configuration.setter
96101
def logging_configuration(self, value):
97102
self.logger, self._logging_configuration = logging.configure(
98-
type(self).__name__, value)
103+
type(self).__name__, value, app_root=self._app_root)
99104
return
100105

101106
@Option

splunklib/searchcommands/validators.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import re
2323
import sys
2424

25-
2625
class Validator(object):
2726
""" Base class for validators that check and format search command options.
2827
@@ -123,12 +122,12 @@ def check_range(value):
123122
elif minimum is not None:
124123
def check_range(value):
125124
if value < minimum:
126-
raise ValueError('Expected integer in the range [-∞,%d]: %d' % (maximum, value))
125+
raise ValueError('Expected integer in the range [%d,+∞]: %d' % (minimum, value))
127126
return
128127
elif maximum is not None:
129128
def check_range(value):
130129
if value > maximum:
131-
raise ValueError('Expected integer in the range [%d,+∞]: %d' % (minimum, value))
130+
raise ValueError('Expected integer in the range [-∞,%d]: %d' % (maximum, value))
132131
return
133132
else:
134133
def check_range(value):
@@ -144,7 +143,7 @@ def __call__(self, value):
144143
return value
145144

146145
def format(self, value):
147-
return str(value)
146+
return str(int(value))
148147

149148

150149
class Duration(Validator):
@@ -244,6 +243,9 @@ def __call__(self, value):
244243
raise ValueError('Illegal characters in option name: %s' % value)
245244
return value
246245

246+
def format(self, value):
247+
return self.__call__(value)
248+
247249

248250
class RegularExpression(Validator):
249251
""" Validates regular expression option values.
@@ -274,3 +276,6 @@ def __call__(self, value):
274276
if value not in self.membership:
275277
raise ValueError('Unrecognized value: %s' % value)
276278
return value
279+
280+
def format(self, value):
281+
return self.__call__(value)

0 commit comments

Comments
 (0)