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

Add async wrapper for commonly used filesystem functions #3584

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Apr 8, 2024

Add async wrapper for commonly used filesystem functions os.path.exists and os.remove

This PR simply wraps file system accesses which currently happen from the event loop, separate follow-up PRs will handle optimizing the number of file system calls as needed.

custom_components/hacs/__init__.py Outdated Show resolved Hide resolved
custom_components/hacs/utils/file_system.py Outdated Show resolved Hide resolved
@emontnemery emontnemery closed this Apr 9, 2024
@emontnemery emontnemery reopened this Apr 9, 2024
@emontnemery emontnemery marked this pull request as draft April 9, 2024 09:52
@emontnemery emontnemery marked this pull request as ready for review April 10, 2024 09:03
Comment on lines 800 to 801
if await async_exists(self.hacs.hass, path):
await async_remove(self.hacs.hass, path)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's bad to go into the executor twice. Can we add a feature to async_remove to not fail if a file doesn't exist ?

Copy link
Member

Choose a reason for hiding this comment

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

If it's changed to use Path(filepath).unlink(missing_ok=True) it would be a single one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could give async_remove a missing_ok flag

@@ -467,7 +468,7 @@ def _write_file():
self.log.error("Could not write data to %s - %s", file_path, error)
return False

return os.path.exists(file_path)
return await async_exists(self.hass, file_path)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check should be part of the _write_file function, that should be fixed in a separate PR though.

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

Successfully merging this pull request may close these issues.

None yet

4 participants