Skip to content

Parameter parser helper to replace eval of env variable (#89) #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/hunter/paramparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python

testdata = """
stdlib=False
~Q(kind="line"),~Q(module_in=["six","pkg_resources"]),~Q(filename=""),stdlib=False
stdlib=False,Q(filename_contains='venv')
"""


class ParamParser:
"""
Splits a param string into list of strings.

The logic is basically to split by commas, ignoring commas inside parens.
"""
def parse(self, string):
# make object variables for debugging
self.raw = string
self.parsed = []
if not self.raw:
pass # print("empty param string")
return self.parsed

self.parsed = []
self.current = ''
self.embrace = False # True if parsing inside parens
# scan for either comma or brace
for i in self.raw:
if self.embrace:
self.current += i
if i == ')':
self.embrace = False
continue

# from here we are not in parens (yet)
if i == '(':
self.current += i
self.embrace = True
elif i == ',':
if not self.current:
pass # print("hey, that's a missing param between commas")
else:
self.parsed.append(self.current)
self.current = ''
else: # i is not ( or ,
self.current += i

else:
if not self.current:
pass # print("empty param after last comma")
else:
self.parsed.append(self.current)
self.current = ''
return self.parsed


if __name__ == '__main__':
for line in testdata.splitlines():
print(line)
params = ParamParser().parse(line)
if not params:
print(" %s" % params)
else:
for param in params:
print(" %s" % param)