-
Notifications
You must be signed in to change notification settings - Fork 4
save_file now cleans the filename, expands user and returns the full file path
#84
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import os | ||
| import re | ||
| from typing import Any, List, Union, Callable | ||
| from pathlib import Path | ||
| from datetime import datetime | ||
|
|
@@ -151,7 +153,7 @@ def create_events(events: List[Union[EventType, str, tuple]]) -> str: | |
| return constructed_events[:-1] | ||
|
|
||
|
|
||
| def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = False) -> None: | ||
| def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = False) -> Path: | ||
| """ | ||
| Saves a file to the specified path. | ||
|
|
||
|
|
@@ -164,9 +166,18 @@ def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = Fal | |
| overwrite | ||
| Whether to overwrite the file if it already exists. | ||
| Default is `False`. | ||
|
|
||
| Returns | ||
| ------- | ||
| `~pathlib.Path` | ||
| The path to the saved file. | ||
| """ | ||
| if isinstance(filename, str): | ||
| filename = Path(filename) | ||
| filepath, filename = os.path.split(filename) | ||
| filename = re.sub(r"[^\w\-_\. ]", "_", filename) | ||
| filename = Path(filepath) / Path(filename) | ||
| filename = Path(filename).expanduser().resolve().absolute() | ||
| # Sanitize the filename - Only works for strings | ||
| if filename.exists() and not overwrite: | ||
| raise FileExistsError(f"{filename} already exists. Use overwrite=True to overwrite.") | ||
| filename.write_bytes(data) | ||
| return filename | ||
|
Member
Author
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. Was there a reason to not do this? It is quite useful to get the return and so I can pass that filepath into another function.
Collaborator
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. Actually, I don't recall. It is better now. |
||
Uh oh!
There was an error while loading. Please reload this page.