Skip to content

Commit e372a05

Browse files
author
David Noble
committed
Resolves DVPL-3352
splunklib.searchcommands | Eliminate field name existence checking CHANGE Code elimination verified against unit tests Signed-off-by: David Noble <[email protected]>
1 parent 60d974e commit e372a05

File tree

4 files changed

+9
-32
lines changed

4 files changed

+9
-32
lines changed

examples/searchcommands_app/bin/countmatches.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from splunklib.searchcommands import \
2020
dispatch, StreamingCommand, Configuration, Option, validators
2121

22+
2223
@Configuration()
2324
class CountMatchesCommand(StreamingCommand):
2425
""" Counts the number of non-overlapping matches to a regular expression in

splunklib/searchcommands/search_command.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def process(self, args=argv, input_file=stdin, output_file=stdout):
154154
ConfigurationSettings, operation, args, reader = self._prepare(
155155
args, input_file=None)
156156
try:
157-
self.parser.parse(args, self, 'ANY')
157+
self.parser.parse(args, self)
158158
except (SyntaxError, ValueError) as e:
159159
writer = csv.DictWriter(output_file, self, fieldnames=['ERROR'])
160160
writer.writerow({'ERROR': e})
@@ -174,7 +174,7 @@ def process(self, args=argv, input_file=stdin, output_file=stdout):
174174
args, input_file)
175175

176176
try:
177-
self.parser.parse(args, self, 'ANY')
177+
self.parser.parse(args, self)
178178
except (SyntaxError, ValueError) as e:
179179
self.messages.append("error_message", e)
180180
self.messages.write(output_file)

splunklib/searchcommands/search_command_internals.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class SearchCommandParser(object):
209209
setting the built-in `log_level` immediately changes the `log_level`.
210210
211211
"""
212-
def parse(self, argv, command, fieldnames='ANY'):
212+
def parse(self, argv, command):
213213
""" Splits an argument list into an options dictionary and a fieldname
214214
list.
215215
@@ -266,33 +266,8 @@ def parse(self, argv, command, fieldnames='ANY'):
266266

267267
# Parse field names
268268

269-
selected_fields = command_args.group('fieldnames').split()
270-
271-
if isinstance(fieldnames, str):
272-
if fieldnames != 'ANY':
273-
raise ValueError(
274-
'Illegal argument to %s.parse method: fieldnames=%s' %
275-
(type(self).__name__, fieldnames))
276-
command.fieldnames = selected_fields
277-
278-
elif fieldnames:
279-
undefined_fields = []
280-
for name in selected_fields:
281-
if not name in fieldnames:
282-
undefined_fields += [name]
283-
if len(undefined_fields) > 0:
284-
raise ValueError(
285-
'Unrecognized field(s): %s' % ', '.join(undefined_fields))
286-
command.fieldnames = selected_fields
287-
288-
elif len(selected_fields) > 0:
289-
raise ValueError(
290-
'Command does not accept field names, but %s found: %s' % (
291-
'one was' if len(selected_fields) == 1 else 'some were',
292-
', '.join(selected_fields)))
293-
294-
command.logger.debug(
295-
'Parsed %s: %s' % (type(command).__name__, command))
269+
command.fieldnames = command_args.group('fieldnames').split()
270+
command.logger.debug('%s: %s' % (type(command).__name__, command))
296271
return
297272

298273
@classmethod

tests/test_searchcommands_app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,16 @@ def test_command_parser(self):
224224
'integer=10',
225225
'optionname=foo_bar',
226226
'regularexpression="\\\\w+"',
227-
'set=foo']
227+
'set=foo',
228+
'show_configuration=true']
228229
fields = ['field_1', 'field_2', 'field_3']
229230

230231
command = StubbedStreamingCommand() # All options are required
231232
parser.parse(options + fields, command)
232233
command_line = str(command)
233234

234235
self.assertEqual(
235-
'stubbedstreaming boolean=true duration=10 fieldname="word_count" file=%s integer=10 optionname="foo_bar" regularexpression="\\\\w+" set="foo" field_1 field_2 field_3' % encoder.encode(file_path),
236+
'stubbedstreaming boolean=true duration=10 fieldname="word_count" file=%s integer=10 optionname="foo_bar" regularexpression="\\\\w+" set="foo" show_configuration=true field_1 field_2 field_3' % encoder.encode(file_path),
236237
command_line)
237238

238239
for option in options:

0 commit comments

Comments
 (0)