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

[SYNPY-1551] Tables refactor #1151

Draft
wants to merge 19 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 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
4 changes: 4 additions & 0 deletions docs/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ div.doc-contents {
.md-typeset table tbody {
font-size: .7rem
}

.md-grid {
max-width: 1700px;
}
34 changes: 34 additions & 0 deletions docs/reference/oop/table_refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Contained within this file are proposed changes for interacting with Tables via this
client.



::: synapseclient.models.Table
options:
inherited_members: true
members:
- get
- store
- delete
- query
- store_rows
- delete_rows
- snapshot
- delete_column
- add_column
- reorder_column
- set_columns
- get_permissions
- get_acl
- set_permissions

::: synapseclient.models.table.SchemaStorageStrategy
::: synapseclient.models.table.ColumnExpansionStrategy

::: synapseclient.models.FacetType
::: synapseclient.models.ColumnType
::: synapseclient.models.table.JsonSubColumn

::: synapseclient.models.Column
options:
members:
7 changes: 5 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ nav:
- Core: reference/core.md
- REST Apis: reference/rest_apis.md
- Experimental:
- Object-Orientated Models: reference/oop/models.md
- Async Object-Orientated Models: reference/oop/models_async.md
- Object-Orientated Models: reference/oop/models.md
- Async Object-Orientated Models: reference/oop/models_async.md
- Table refactor: reference/oop/table_refactor.md
- Further Reading:
- Home: explanations/home.md
- Domain Models of Synapse: explanations/domain_models_of_synapse.md
Expand Down Expand Up @@ -120,6 +121,8 @@ theme:
- toc.follow
- navigation.tabs
- navigation.tabs.sticky
- navigation.instant
- navigation.instant.progress

extra_css:
- css/custom.css
Expand Down
3 changes: 3 additions & 0 deletions synapseclient/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
put_file_multipart_add,
put_file_multipart_complete,
)
from .table_services import get_columns

__all__ = [
# annotations
Expand Down Expand Up @@ -78,4 +79,6 @@
"get_transfer_config",
# entity_factory
"get_from_entity_factory",
# columns
"get_columns",
]
93 changes: 93 additions & 0 deletions synapseclient/api/table_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
The purpose of this module is to provide any functions that are needed to interact with
columns in the Synapse REST API.
"""

from typing import TYPE_CHECKING, List, Optional

if TYPE_CHECKING:
from synapseclient import Synapse
from synapseclient.models import Column


async def get_columns(
table_id: str,
*,
synapse_client: Optional["Synapse"] = None,
) -> List["Column"]:
"""Call to synapse and set the annotations for the given input.

Arguments:
table_id: The ID of the Table to get the columns for.
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.

Returns: The annotations set in Synapse.
"""
from synapseclient import Synapse
from synapseclient.models import Column

result = await Synapse.get_client(synapse_client=synapse_client).rest_get_async(
f"/entity/{table_id}/column",
)

columns = []

for column in result.get("results", []):
columns.append(Column().fill_from_dict(synapse_column=column))

return columns


# TODO: Finish this function, this was copied out of the Synapse class and will be used to implement this API: https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/table/TableSchemaChangeRequest.html
# async def table_updates(
# self,
# table_id: str,
# changes: List[dict] = [],
# create_snapshot: bool = False,
# comment: str = None,
# label: str = None,
# activity: str = None,
# wait: bool = True,
# ) -> dict:
# """
# Creates view updates and snapshots

# Arguments:
# table: The schema of the EntityView or its ID.
# changes: Array of Table changes
# create_snapshot: Create snapshot
# comment: Optional snapshot comment.
# label: Optional snapshot label.
# activity: Optional activity ID applied to snapshot version.
# wait: True to wait for async table update to complete

# Returns:
# A Snapshot Response
# """
# snapshot_options = {
# "snapshotComment": comment,
# "snapshotLabel": label,
# "snapshotActivityId": activity,
# }
# new_snapshot = {
# key: value for key, value in snapshot_options.items() if value is not None
# }
# table_update_body = {
# "changes": changes,
# "createSnapshot": create_snapshot,
# "snapshotOptions": new_snapshot,
# }

# uri = "/entity/{}/table/transaction/async".format(id_of(table))

# if wait:
# result = self._waitForAsync(uri, table_update_body)

# else:
# result = self.restPOST(
# "{}/start".format(uri), body=json.dumps(table_update_body)
# )

# return result
5 changes: 5 additions & 0 deletions synapseclient/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
as handling error cases for HTTP requests."""

import logging
import re
from typing import Union

import httpx
import requests

from synapseclient.core import utils

BEARER_TOKEN_PATTERN = re.compile(r"\'Bearer \S+\'")


class SynapseError(Exception):
"""Generic exception thrown by the client."""
Expand Down Expand Up @@ -175,6 +178,7 @@ def _raise_for_status(response, verbose=False):
# Append the request sent
message += f"\n\n{REQUEST_PREFIX}\n{response.request.url} {response.request.method}"
message += f"\n{HEADERS_PREFIX}{response.request.headers}"
message = re.sub(BEARER_TOKEN_PATTERN, "'Bearer <redacted>'", message)
message += f"\n{BODY_PREFIX}{response.request.body}"
except: # noqa
message += f"\n{UNABLE_TO_APPEND_REQUEST}"
Expand Down Expand Up @@ -273,6 +277,7 @@ def _raise_for_status_httpx(
# Append the request sent
message += f"\n\n{REQUEST_PREFIX}\n{response.request.url} {response.request.method}"
message += f"\n{HEADERS_PREFIX}{response.request.headers}"
message = re.sub(BEARER_TOKEN_PATTERN, "'Bearer <redacted>'", message)
message += f"\n{BODY_PREFIX}{response.request.content}"
except Exception: # noqa
logger.exception(UNABLE_TO_APPEND_REQUEST)
Expand Down
8 changes: 4 additions & 4 deletions synapseclient/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from synapseclient.core.logging_setup import DEFAULT_LOGGER_NAME

if TYPE_CHECKING:
from synapseclient.models import File, Folder, Project
from synapseclient.models import File, Folder, Project, Table

R = TypeVar("R")

Expand Down Expand Up @@ -1376,10 +1376,10 @@ def delete_none_keys(incoming_object: typing.Dict) -> None:


def merge_dataclass_entities(
source: typing.Union["Project", "Folder", "File"],
destination: typing.Union["Project", "Folder", "File"],
source: typing.Union["Project", "Folder", "File", "Table"],
destination: typing.Union["Project", "Folder", "File", "Table"],
fields_to_ignore: typing.List[str] = None,
) -> typing.Union["Project", "Folder", "File"]:
) -> typing.Union["Project", "Folder", "File", "Table"]:
"""
Utility function to merge two dataclass entities together. This is used when we are
upserting an entity from the Synapse service with the requested changes.
Expand Down
3 changes: 0 additions & 3 deletions synapseclient/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from synapseclient.models.table import (
Column,
ColumnType,
CsvResultFormat,
FacetType,
Row,
RowsetResultFormat,
Table,
)
Expand All @@ -33,7 +31,6 @@
"FacetType",
"CsvResultFormat",
"RowsetResultFormat",
"Row",
"Team",
"TeamMember",
"UserProfile",
Expand Down
4 changes: 2 additions & 2 deletions synapseclient/models/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ async def store_async(
),
)
self.fill_from_dict(synapse_activity=saved_activity)
Synapse.get_client(synapse_client=synapse_client).logger.debug(
f"Stored activity {self.id}"
Synapse.get_client(synapse_client=synapse_client).logger.info(
f"[{parent.id}]: Stored activity"
)

return self
Expand Down
2 changes: 1 addition & 1 deletion synapseclient/models/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def store_async(
self.annotations = Annotations.from_dict(result)
self.etag = result["etag"]
Synapse.get_client(synapse_client=synapse_client).logger.debug(
f"Annotations stored for {self.id}"
f"[{self.id}]: Stored annotations"
)
return self

Expand Down
Loading
Loading