Skip to content
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

Files are read twice even if no patterns are given #160

Closed
fabianegli opened this issue Dec 20, 2022 · 2 comments · Fixed by #163
Closed

Files are read twice even if no patterns are given #160

fabianegli opened this issue Dec 20, 2022 · 2 comments · Fixed by #163

Comments

@fabianegli
Copy link

The below code will open files and loop through them regardless of the presence of patterns to match against.

I propose to make the file opening conditional based on the presence of patterns.

# Use 'rt' here explicitly as opposed to 'rb'
with file_open(mode='rt') as file_handler: # type: ignore # mypy goes crazy here otherwise # noqa: E501
self.found_strings = check_content(
strings=strings_to_check,
text_lines=file_handler)
# Read the file again for the regex
with file_open(mode='rt') as file_handler: # type: ignore # mypy goes crazy here otherwise # noqa: E501
self.found_patterns = check_regex_content(
patterns=patterns_to_check,
text_lines=file_handler)

e.g:

            if strings_to_check:
                with file_open(mode='rt') as file_handler:  # type: ignore  # mypy goes crazy here otherwise  # noqa: E501
                    self.found_strings = check_content(
                        strings=strings_to_check,
                        text_lines=file_handler)
            if patterns_to_check:
                with file_open(mode='rt') as file_handler:  # type: ignore  # mypy goes crazy here otherwise  # noqa: E501
                    self.found_patterns = check_regex_content(
                        patterns=patterns_to_check,
                        text_lines=file_handler)
@rhpvorderman
Copy link
Member

Good find. I will see if I can fix this before the next release.

@rhpvorderman
Copy link
Member

I did a double check. While the files are opened, due to the nature of the iterator the file is not read when there are no patterns.
Even so it is a shame to read the file twice when there are both regexes and strings so that is also fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants