-
Notifications
You must be signed in to change notification settings - Fork 355
Add a parameter create
to resolve()
to create the directory/file if it doesn't exist.
#2751
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
Open
JackAshwell11
wants to merge
1
commit into
pythonarcade:development
Choose a base branch
from
JackAshwell11:resolve_create
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,19 @@ def resolve_resource_path(path: str | Path) -> Path: | |
return resolve(path) | ||
|
||
|
||
def resolve(path: str | Path) -> Path: | ||
def create_path(path: Path) -> None: | ||
""" | ||
Create a file or directory at the given path. | ||
If the path has a suffix, it's treated as a file, otherwise, as a directory. | ||
""" | ||
if path.suffix: | ||
path.parent.mkdir(parents=True, exist_ok=True) | ||
path.touch(exist_ok=True) | ||
else: | ||
path.mkdir(parents=True, exist_ok=True) | ||
|
||
|
||
def resolve(path: str | Path, *, create: bool = False) -> Path: | ||
""" | ||
Attempts to resolve a path to a resource including resource handles. | ||
|
||
|
@@ -67,6 +79,7 @@ def resolve(path: str | Path) -> Path: | |
|
||
Args: | ||
path: A Path or string | ||
create: If True, create the path if it doesn't exist. | ||
""" | ||
# Convert to a Path object and resolve resource handle | ||
if isinstance(path, str): | ||
|
@@ -87,22 +100,30 @@ def resolve(path: str | Path) -> Path: | |
# match. This allows for overriding of resources. | ||
paths = get_resource_handle_paths(handle) | ||
for handle_path in reversed(paths): | ||
path = handle_path / resource | ||
if path.exists(): | ||
candidate_path = handle_path / resource | ||
if candidate_path.exists(): | ||
path = candidate_path | ||
break | ||
else: | ||
searched_paths = "\n".join(f"-> {p}" for p in reversed(paths)) | ||
raise FileNotFoundError( | ||
f"Cannot locate resource '{resource}' using handle " | ||
f"'{handle}' in any of the following paths:\n" | ||
f"{searched_paths}" | ||
) | ||
if create: | ||
path = paths[-1] / resource | ||
create_path(path) | ||
else: | ||
searched_paths = "\n".join(f"-> {p}" for p in reversed(paths)) | ||
raise FileNotFoundError( | ||
f"Cannot locate resource '{resource}' using handle " | ||
f"'{handle}' in any of the following paths:\n" | ||
f"{searched_paths}" | ||
) | ||
|
||
# Always convert into a Path object | ||
path = Path(handle_path / resource) | ||
path = Path(path) | ||
else: | ||
path = Path(path) | ||
|
||
if create: | ||
create_path(path) | ||
Comment on lines
+124
to
+125
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. Should this not also check if the path exists? |
||
|
||
try: | ||
path = Path(path.resolve(strict=True)) | ||
except AttributeError: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path objects have an
is_dir
method, which is probably a safer check.