You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have various programs that are generating source files for use in a C project build. Often these files get generated the same each time but since they are written regardless they trigger cascading rebuilds of that file and those that depend on it.
I didn't implement it yet but it seems like it wouldn't be particularly difficult to add a changed check right after self._f.close(). Perhaps right before to avoid reopening the new content? I would not expect this changed check to be atomic with the replace. Maybe there's a way. It not being atomic isn't an issue for me. If two things are trying to dump into the file 'at the same time'... so it goes.
The 'only if changed' option would then be cascaded back up at least to the click.File() interface. Though having only_write_if_changed be always present but only usable with atomic=True isn't great so probably more to talk about there. A separate callable, something else, I haven't thought this through yet.
Does this sound like a feature which would be of interest and is worth me developing a PR to discuss? Or, should I just go implement this for myself on the side without bothering to have so much click integration.
The text was updated successfully, but these errors were encountered:
import click
class FileIfChanged(click.File):
def write(self, value):
# Check if the file exists and read its current content
try:
with open(self.get_filename(), 'r') as f:
existing_content = f.read()
except FileNotFoundError:
existing_content = ""
# Compare the existing content with the new content
if existing_content != value:
# Only write if the content is different
super().write(value)
@click.command()
@click.option('--output', type=FileIfChanged('w'), default='output.txt')
def main(output):
# Your program logic here
output.write('Hello, World!')
if __name__ == '__main__':
main()
I have various programs that are generating source files for use in a C project build. Often these files get generated the same each time but since they are written regardless they trigger cascading rebuilds of that file and those that depend on it.
click/click/_compat.py
Lines 536 to 546 in 38a2712
I didn't implement it yet but it seems like it wouldn't be particularly difficult to add a changed check right after
self._f.close()
. Perhaps right before to avoid reopening the new content? I would not expect this changed check to be atomic with the replace. Maybe there's a way. It not being atomic isn't an issue for me. If two things are trying to dump into the file 'at the same time'... so it goes.The 'only if changed' option would then be cascaded back up at least to the
click.File()
interface. Though havingonly_write_if_changed
be always present but only usable withatomic=True
isn't great so probably more to talk about there. A separate callable, something else, I haven't thought this through yet.Does this sound like a feature which would be of interest and is worth me developing a PR to discuss? Or, should I just go implement this for myself on the side without bothering to have so much click integration.
The text was updated successfully, but these errors were encountered: