-
Notifications
You must be signed in to change notification settings - Fork 467
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
small improvements to existing FileParsers #107
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,16 @@ | |
|
||
class IniFileParser(object): | ||
|
||
_comment_regex = re.compile(r'\s*[;#]') | ||
|
||
def __init__(self, file): | ||
self.parser = configparser.ConfigParser() | ||
self.parser.optionxform = str | ||
self.parser.read_file(file) | ||
|
||
# Hacky way to keep track of line location | ||
file.seek(0) | ||
self.lines = list(map(lambda x: x.strip(), file.readlines())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ |
||
self.lines = [line.strip() for line in file.readlines()] | ||
self.line_offset = 0 | ||
|
||
def iterator(self): | ||
|
@@ -56,42 +58,31 @@ def _get_value_and_line_offset(self, key, values): | |
output = [] | ||
lines_modified = False | ||
|
||
first_line_regex = re.compile(r'^\s*{}[ :=]+{}'.format( | ||
re.escape(key), | ||
re.escape(values_list[current_value_list_index]), | ||
)) | ||
comment_regex = re.compile(r'\s*[;#]') | ||
for index, line in enumerate(self.lines): | ||
# Check ignored lines before checking values, because | ||
# you can write comments *after* the value. | ||
if not line.strip() or self._comment_regex.match(line): | ||
continue | ||
|
||
if current_value_list_index == 0: | ||
first_line_regex = re.compile(r'^\s*{}[ :=]+{}'.format( | ||
re.escape(key), | ||
re.escape(values_list[current_value_list_index]), | ||
)) | ||
if first_line_regex.match(line): | ||
output.append(( | ||
values_list[current_value_list_index], | ||
self.line_offset + index + 1, | ||
)) | ||
|
||
current_value_list_index += 1 | ||
|
||
continue | ||
|
||
# Check ignored lines before checking values, because | ||
# you can write comments *after* the value. | ||
|
||
# Ignore blank lines | ||
if not line.strip(): | ||
continue | ||
|
||
# Ignore comments | ||
if comment_regex.match(line): | ||
continue | ||
|
||
if current_value_list_index == len(values_list): | ||
if index == 0: | ||
index = 1 # don't want to count the same line again | ||
|
||
index = 1 # don't want to count the same line again | ||
self.line_offset += index | ||
self.lines = self.lines[index:] | ||
lines_modified = True | ||
|
||
break | ||
else: | ||
output.append(( | ||
|
@@ -132,10 +123,7 @@ def _construct_values_list(values): | |
2. For all other values, ignore blank lines. | ||
Then, we can parse through, and look for values only. | ||
""" | ||
values_list = values.splitlines() | ||
return values_list[:1] + list( | ||
filter( | ||
lambda x: x, | ||
values_list[1:], | ||
), | ||
) | ||
lines = values.splitlines() | ||
values_list = lines[:1] | ||
values_list.extend(filter(None, lines[1:])) | ||
return values_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super Nit: Maybe add to the regex comment
# e.g. python
-># e.g. python or yaml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that'll be necessary if a python file parser is implemented. Then we can
'py': WHITELIST_REGEXES[0]
. More language/specification specific file parsers is what I'm pushing for, or at least suggesting, architecturally. See #105.