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

Automatically create the directory path for a JSONTokenStorage #998

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Fixed
~~~~~

- When a JSONTokenStorage is used, the containing directory will be automatically be
created if it doesn't exist instead of erroring (on Mac).
Copy link
Member

Choose a reason for hiding this comment

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

Since it's not mac-specific, I don't think we should note the platform. Since I was suggesting an edit, I also went ahead and restored the pr link text from the template (:pr:`998` also works).

Suggested change
created if it doesn't exist instead of erroring (on Mac).
created if it doesn't exist instead of erroring. (:pr:`NUMBER`)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if it's mac exclusive or not. I only know it doesn't work on mac.

8 changes: 8 additions & 0 deletions src/globus_sdk/experimental/tokenstorage/json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
import os
import pathlib
import typing as t

Expand Down Expand Up @@ -40,8 +41,15 @@ def __init__(self, filename: pathlib.Path | str, *, namespace: str = "DEFAULT"):
:param namespace: A user-supplied namespace for partitioning token data
"""
self.filename = str(filename)
self._ensure_containing_dir_exists()
Copy link
Member

Choose a reason for hiding this comment

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

If we're adding this, shouldn't we do it on the parent class, not here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that's a fair question. Would you like to open that PR or me?

Copy link
Member

Choose a reason for hiding this comment

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

Sure, I'll work that up!

(Also, no worries about merge/review order. I noticed it, and we'll sort out now. 😄 )

super().__init__(namespace=namespace)

def _ensure_containing_dir_exists(self) -> None:
"""
Ensure that the directory containing the given filename exists.
"""
os.makedirs(os.path.dirname(self.filename), exist_ok=True)

def _invalid(self, msg: str) -> t.NoReturn:
raise ValueError(
f"{msg} while loading from '{self.filename}' for JSON Token Storage"
Expand Down
Loading