Repository navigation
search_pypi_top: add --multiline option
#24
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -170,22 +170,45 @@ def grep(args, archive_filename, regex): | |||||
| logger.info(f"ignore: {filename}") | ||||||
| continue | ||||||
|
|
||||||
| matches = [] | ||||||
| ignore = False | ||||||
| lineno = 1 | ||||||
| # Split at Unix newline b'\n' byte | ||||||
| for line in fp: | ||||||
| if lineno == 1 and IGNORE_CYTHON and CYTHON_REGEX.match(line): | ||||||
| logger.info(f"ignore Cython file: {archive_filename}: {filename}") | ||||||
| ignore = True | ||||||
| break | ||||||
| match = regex.search(line) | ||||||
| if match: | ||||||
| matches.append((filename, line, match.span())) | ||||||
| lineno += 1 | ||||||
|
|
||||||
| if matches and not ignore: | ||||||
| yield from matches | ||||||
| if args.multiline: | ||||||
| yield from _grep_multiline(archive_filename, filename, fp, regex) | ||||||
| else: | ||||||
| yield from _grep_lines(archive_filename, filename, fp, regex) | ||||||
|
|
||||||
|
|
||||||
| def _grep_lines(archive_filename, filename, fp, regex): | ||||||
| matches = [] | ||||||
| lineno = 1 | ||||||
| # Split at Unix newline b'\n' byte | ||||||
| for line in fp: | ||||||
| if lineno == 1 and IGNORE_CYTHON and CYTHON_REGEX.match(line): | ||||||
| logger.info(f"ignore Cython file: {archive_filename}: {filename}") | ||||||
| return | ||||||
| match = regex.search(line) | ||||||
| if match: | ||||||
| matches.append((filename, line, match.span())) | ||||||
| lineno += 1 | ||||||
|
|
||||||
| yield from matches | ||||||
|
|
||||||
|
|
||||||
| def _grep_multiline(archive_filename, filename, fp, regex): | ||||||
| # Search the whole file at once, so the regex can match across lines. | ||||||
| data = fp.read() | ||||||
| if IGNORE_CYTHON and CYTHON_REGEX.match(data): | ||||||
| logger.info(f"ignore Cython file: {archive_filename}: {filename}") | ||||||
| return | ||||||
| last_start = -1 | ||||||
|
Owner
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.
Suggested change
|
||||||
| for match in regex.finditer(data): | ||||||
| start = data.rfind(b'\n', 0, match.start()) + 1 | ||||||
| if start == last_start: | ||||||
| continue | ||||||
| last_start = start | ||||||
| end = data.find(b'\n', match.start()) | ||||||
| if end == -1: | ||||||
| end = len(data) | ||||||
| line = data[start:end] | ||||||
| yield filename, line, (match.start() - start, min(match.end(), end) - start) | ||||||
|
|
||||||
|
|
||||||
| def search_file(filename, index, len_filenames, args, pypi_dir, regex): | ||||||
|
|
@@ -219,7 +242,10 @@ def search_dir(args, pypi_dir, pattern): | |||||
| lines = 0 | ||||||
| projects = set() | ||||||
| all_results = [] | ||||||
| regex = re.compile(pattern) | ||||||
| flags = 0 | ||||||
| if args.multiline: | ||||||
| flags |= re.MULTILINE | ||||||
| regex = re.compile(pattern, flags) | ||||||
| filenames = (filename for filename in os.listdir(pypi_dir) if filename != DS_STORE) | ||||||
| filenames = sorted(filenames, key=str.lower) | ||||||
|
|
||||||
|
|
@@ -262,6 +288,10 @@ def parse_args(): | |||||
| help="Quiet mode (ex: don't log proceed files)") | ||||||
| parser.add_argument('--cython', action='store_true', | ||||||
| help="Search also in code generated by Cython") | ||||||
| parser.add_argument('--multiline', action='store_true', | ||||||
| help="Search the whole file at once instead of " | ||||||
| "line by line, so the regex can match " | ||||||
| "across lines") | ||||||
|
|
||||||
| return parser.parse_args() | ||||||
|
|
||||||
|
|
||||||
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.
If possible, I would prefer to not have to maintain two implementatons. Can't we just use _grep_multiline() in all cases (multiline or not)? Just inline _grep_multiline() again in grep().
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.
It would slow down (by a noticeable amount) searches that could be done on a line by line basis, as such I opted to make it optional.