-
Notifications
You must be signed in to change notification settings - Fork 1
Add public OneDep session metadata API #8
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
Draft
KingAlejandro
wants to merge
2
commits into
develop
Choose a base branch
from
onedep-session-metadata-api
base: develop
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| from onedep_lib.apis.deposit.client import HttpApiClient | ||
| from onedep_lib.apis.deposit.models import DepositError, DepositStatus, Experiment | ||
| from onedep_lib.apis.deposit.types import ApiClient | ||
| from onedep_lib.auths.token import TokenStore | ||
| from onedep_lib.checks.report import CheckReport | ||
| from onedep_lib.checks.runner import CheckRunner | ||
| from onedep_lib.checks.types import CheckRunner as CheckRunnerProtocol | ||
|
|
@@ -16,9 +17,8 @@ | |
| from onedep_lib.schemas.local import LocalSchemaProvider | ||
| from onedep_lib.schemas.remote import RemoteSchemaProvider | ||
| from onedep_lib.session.json_store import JsonSessionStore | ||
| from onedep_lib.session.models import LocalFile, LocalSession | ||
| from onedep_lib.session.models import LocalFile, LocalSession, SessionSummary | ||
| from onedep_lib.session.types import SessionStore | ||
| from onedep_lib.auths.token import TokenStore | ||
|
|
||
|
|
||
| def _md5_of_file(path: Path, chunk_size: int = 1 << 20) -> str: | ||
|
|
@@ -29,6 +29,21 @@ def _md5_of_file(path: Path, chunk_size: int = 1 << 20) -> str: | |
| return h.hexdigest() | ||
|
|
||
|
|
||
| def _session_base_dir(config: DepositConfig, base_dir: Path | None = None) -> Path: | ||
| return Path(base_dir or config.session_dir) | ||
|
|
||
|
|
||
| def _summarize_session(session: LocalSession, files: list[LocalFile]) -> SessionSummary: | ||
| return SessionSummary( | ||
| session_id=session.session_id, | ||
| remote_dep_id=session.remote_dep_id, | ||
| experiment_type=session.experiment_type, | ||
| created_at=session.created_at, | ||
| updated_at=session.updated_at or session.created_at, | ||
| file_count=len(files), | ||
| ) | ||
|
|
||
|
|
||
| def list_sessions(base_dir: Path | None = None) -> list[tuple[LocalSession, list[LocalFile]]]: | ||
| """Return all local sessions with their registered files, newest first.""" | ||
| config = DepositConfig.load() | ||
|
|
@@ -53,6 +68,41 @@ def list_sessions(base_dir: Path | None = None) -> list[tuple[LocalSession, list | |
| return results | ||
|
|
||
|
|
||
| def list_session_metadata(base_dir: Path | None = None) -> list[SessionSummary]: | ||
| """Return metadata summaries for all local sessions, newest first.""" | ||
| return [_summarize_session(session, files) for session, files in list_sessions(base_dir=base_dir)] | ||
|
|
||
|
|
||
| def get_session(session_id: str, base_dir: Path | None = None) -> tuple[LocalSession, list[LocalFile]]: | ||
| """Return one local session and its registered files. | ||
|
|
||
| Args: | ||
| session_id: Local session UUID. | ||
| base_dir: Optional session storage directory. Defaults to the configured | ||
| OneDep Lib session directory. | ||
|
|
||
| Raises: | ||
| KeyError: If no local session with the given session_id exists. | ||
| """ | ||
| config = DepositConfig.load() | ||
| _base = base_dir or config.session_dir | ||
| json_path = _base / session_id / "session.json" | ||
| if not json_path.exists(): | ||
| raise KeyError(f"No session found for {session_id!r}") | ||
|
|
||
| store = JsonSessionStore(session_id, base_dir=_base) | ||
| try: | ||
| return store.get_session(), store.get_all_files() | ||
| finally: | ||
| store.close() | ||
|
|
||
|
|
||
| def get_session_metadata(session_id: str, base_dir: Path | None = None) -> SessionSummary: | ||
| """Return a metadata summary for one local session.""" | ||
| session, files = get_session(session_id, base_dir=base_dir) | ||
| return _summarize_session(session, files) | ||
|
|
||
|
|
||
| def deposit_init( | ||
| email: str, | ||
| users: list[str], | ||
|
|
@@ -61,7 +111,7 @@ def deposit_init( | |
| em_subtype: EMSubType | None = None, | ||
| coordinates: bool | None = None, | ||
| config: DepositConfig | None = None, | ||
| _base_dir: Path | None = None, | ||
| base_dir: Path | None = None, | ||
| _api_client: ApiClient | None = None, | ||
| _check_runner: CheckRunnerProtocol | None = None, | ||
| ) -> Deposition: | ||
|
|
@@ -75,7 +125,8 @@ def deposit_init( | |
| em_subtype: EM experiment subtype (can be set later via set_em_params). | ||
| coordinates: Whether coordinates are being deposited (can be set later). | ||
| config: Optional pre-built DepositConfig; loaded from default sources if None. | ||
| _base_dir: Override session storage directory (for testing only). | ||
| base_dir: Optional session storage directory. Defaults to the configured | ||
| OneDep Lib session directory. | ||
| _api_client: Override API client (for testing only). | ||
| _check_runner: Override check runner (for testing only). | ||
|
|
||
|
|
@@ -84,8 +135,8 @@ def deposit_init( | |
| """ | ||
| config = config or DepositConfig.load() | ||
| session_id = str(uuid.uuid4()) | ||
| base_dir = _base_dir or config.session_dir | ||
| store: SessionStore = JsonSessionStore(session_id, base_dir=base_dir) | ||
| session_base_dir = _session_base_dir(config, base_dir=base_dir) | ||
| store: SessionStore = JsonSessionStore(session_id, base_dir=session_base_dir) | ||
| api_client: ApiClient = _api_client or HttpApiClient(config, auth_provider=TokenStore(config)) | ||
| check_runner: CheckRunnerProtocol = _check_runner or CheckRunner( | ||
| LocalSchemaProvider(config.local_schema_cache_dir) | ||
|
|
@@ -99,6 +150,7 @@ def deposit_init( | |
| country=country, | ||
| experiment_type=experiment_type, | ||
| created_at=datetime.now(), | ||
| updated_at=datetime.now(), | ||
| em_subtype=em_subtype, | ||
| coordinates=coordinates, | ||
| ) | ||
|
|
@@ -109,7 +161,7 @@ def deposit_init( | |
| def deposit_resume( | ||
| session_id: str, | ||
| config: DepositConfig | None = None, | ||
| _base_dir: Path | None = None, | ||
| base_dir: Path | None = None, | ||
| _api_client: ApiClient | None = None, | ||
| _check_runner: CheckRunnerProtocol | None = None, | ||
| ) -> Deposition: | ||
|
|
@@ -118,7 +170,8 @@ def deposit_resume( | |
| Args: | ||
| session_id: The session_id returned by a previous deposit_init() call. | ||
| config: Optional pre-built DepositConfig; loaded from default sources if None. | ||
| _base_dir: Override session storage directory (for testing only). | ||
| base_dir: Optional session storage directory. Defaults to the configured | ||
| OneDep Lib session directory. | ||
| _api_client: Override API client (for testing only). | ||
| _check_runner: Override check runner (for testing only). | ||
|
|
||
|
|
@@ -129,16 +182,20 @@ def deposit_resume( | |
| KeyError: If no session with the given session_id exists. | ||
| """ | ||
| config = config or DepositConfig.load() | ||
| base_dir = _base_dir or config.session_dir | ||
| store: SessionStore = JsonSessionStore(session_id, base_dir=base_dir) | ||
| session_base_dir = _session_base_dir(config, base_dir=base_dir) | ||
| store: SessionStore = JsonSessionStore(session_id, base_dir=session_base_dir) | ||
| store.get_session() # raises KeyError if not found | ||
|
Comment on lines
184
to
187
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. Hi Alex, try the following after line 194 (session_base_dir = ...) |
||
| api_client: ApiClient = _api_client or HttpApiClient(config, auth_provider=TokenStore(config)) | ||
| check_runner: CheckRunnerProtocol = _check_runner or CheckRunner( | ||
| LocalSchemaProvider(config.local_schema_cache_dir) | ||
| if config.fetch_local_schema | ||
| else RemoteSchemaProvider(config.schema_base_url, config.schema_cache_dir) | ||
| ) | ||
| return Deposition(store=store, api_client=api_client, check_runner=check_runner) | ||
| return Deposition( | ||
| store=store, | ||
| api_client=api_client, | ||
| check_runner=check_runner, | ||
| ) | ||
|
|
||
|
|
||
| class Deposition: | ||
|
|
||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.