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

Allow write_file to sanitize spaces and newlines #165

Closed
wants to merge 1 commit into from

Conversation

ssbarnea
Copy link
Member

@ssbarnea ssbarnea commented Aug 20, 2024

@ssbarnea ssbarnea added the enhancement New feature or request label Aug 20, 2024
@ssbarnea ssbarnea requested a review from felixfontein August 20, 2024 09:30
@ssbarnea ssbarnea marked this pull request as ready for review August 20, 2024 09:43
@ssbarnea ssbarnea requested a review from gotmax23 August 20, 2024 09:43
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the wrong place for such functionality. This should be done by the caller before calling write_file().

Also the implementation is very inefficient. Check for example with " "*100000 + "x" + " "*100000. Compare for example to content = '\n'.join([l.rstrip(' \t') for l in content.splitlines()]), which is so much faster for this example.

@@ -71,10 +72,17 @@ async def copy_file(
flog.debug("Leave")


async def write_file(filename: StrOrBytesPath, content: str) -> None:
async def write_file(
filename: StrOrBytesPath, content: str, /, sanitize: bool = False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically speaking, making filename and content positional-only arguments is a breaking change.

Comment on lines +82 to +84
if sanitize:
content = re.sub(r"[ \t]+$", "", content, flags=re.MULTILINE)
content = content.rstrip("\n\r") + "\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Felix that perhaps this isn't the best place to do this, but maybe a separate sanitize_trailing_spaces() function in antsibull_core would make sense

@@ -0,0 +1,2 @@
minor_changes:
- Allow write_file to sanitize spaces and newlines. (https://github.com/ansible-community/antsibull-docs/issues/305)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Allow write_file to sanitize spaces and newlines. (https://github.com/ansible-community/antsibull-docs/issues/305)
- Allow write_file to sanitize spaces and newlines (https://github.com/ansible-community/antsibull-docs/issues/305).

is the format we generally follow.

@gotmax23
Copy link
Collaborator

Is this still relevant? I think @ssbarnea's original issue was solved differently in antsibull-docs?

@gotmax23
Copy link
Collaborator

Either way, thank you for the contribution.

@felixfontein
Copy link
Collaborator

Closing since this is apparently no longer needed. Thanks for your contribution!

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

Successfully merging this pull request may close these issues.

3 participants