Skip to content

Commit 8727204

Browse files
committed
deprecated project_id parameter
1 parent 4568357 commit 8727204

3 files changed

Lines changed: 132 additions & 38 deletions

File tree

synapseclient/extensions/curator/record_based_metadata_task.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
CurationTask,
1616
Grid,
1717
JSONSchema,
18+
Project,
1819
RecordBasedMetadataTaskProperties,
1920
RecordSet,
2021
)
22+
from synapseclient.operations import get
2123

2224

2325
def extract_property_titles(schema_data: Dict[str, Any]) -> List[str]:
@@ -99,7 +101,6 @@ def extract_schema_properties_from_web(
99101

100102

101103
def create_record_based_metadata_task(
102-
project_id: str,
103104
folder_id: str,
104105
record_set_name: str,
105106
record_set_description: str,
@@ -112,6 +113,7 @@ def create_record_based_metadata_task(
112113
assignee_principal_id: Optional[Union[str, int]] = None,
113114
*,
114115
synapse_client: Optional[Synapse] = None,
116+
project_id: Optional[str] = None, # Deprecated, will be removed in v5.0.0
115117
) -> Tuple[RecordSet, CurationTask, Grid]:
116118
"""
117119
Generate and upload CSV templates as a RecordSet for record-based metadata,
@@ -142,7 +144,6 @@ def create_record_based_metadata_task(
142144
143145
record_set, task, grid = create_record_based_metadata_task(
144146
synapse_client=syn,
145-
project_id="syn12345678",
146147
folder_id="syn87654321",
147148
record_set_name="BiospecimenMetadata_RecordSet",
148149
record_set_description="RecordSet for biospecimen metadata curation",
@@ -155,9 +156,10 @@ def create_record_based_metadata_task(
155156
```
156157
157158
Arguments:
158-
project_id: The Synapse ID of the project where the folder exists.
159159
folder_id: The Synapse ID of the folder to upload RecordSet to.
160-
record_set_name: Name for the RecordSet.
160+
record_set_name: Name for the RecordSet entity that will be created.
161+
A RecordSet entity captures record-based metadata as a special type of CSV and stores contributor
162+
provided metadata collected via Curator enabling sharing and download of validated metadata in Synapse.
161163
record_set_description: Description for the RecordSet.
162164
curation_task_name: Name for the CurationTask (used as data_type field).
163165
Must be unique within the project, otherwise if it matches an existing
@@ -177,6 +179,7 @@ def create_record_based_metadata_task(
177179
synapse_client: If not passed in and caching was not disabled by
178180
`Synapse.allow_client_caching(False)` this will use the last created
179181
instance from the Synapse class constructor.
182+
project_id: Deprecated, will be removed in v5.0.0
180183
181184
Returns:
182185
Tuple containing the created RecordSet, CurationTask, and Grid objects
@@ -186,8 +189,6 @@ def create_record_based_metadata_task(
186189
SynapseError: If there are issues with Synapse operations.
187190
"""
188191
# Validate required parameters
189-
if not project_id:
190-
raise ValueError("project_id is required")
191192
if not folder_id:
192193
raise ValueError("folder_id is required")
193194
if not record_set_name:
@@ -203,8 +204,18 @@ def create_record_based_metadata_task(
203204
if not schema_uri:
204205
raise ValueError("schema_uri is required")
205206

207+
if project_id:
208+
synapse_client.logger.warning(
209+
"The 'project_id' parameter is deprecated and will be removed in v5.0.0. "
210+
"The project ID will be inferred from the folder ID provided."
211+
)
212+
206213
synapse_client = Synapse.get_client(synapse_client=synapse_client)
207214

215+
project_id = project_id_from_entity_id(
216+
entity_id=folder_id, synapse_client=synapse_client
217+
)
218+
208219
template_df = extract_schema_properties_from_web(
209220
syn=synapse_client, schema_uri=schema_uri
210221
)
@@ -283,3 +294,23 @@ def create_record_based_metadata_task(
283294
raise e
284295

285296
return record_set_with_data, curation_task, curation_grid
297+
298+
299+
def project_id_from_entity_id(entity_id: str, synapse_client: Synapse) -> str:
300+
"""
301+
Helper function to retrieve the project ID from a given entity ID by traversing up the hierarchy.
302+
303+
Args:
304+
entity_id: The Synapse ID of the entity (e.g., folder, file) to start from.
305+
synapse_client: Authenticated Synapse client instance
306+
"""
307+
308+
# Get the project ID from the folder ID
309+
current_obj = get(entity_id, synapse_client=synapse_client)
310+
iter = 0
311+
while not isinstance(current_obj, Project):
312+
current_obj = get(current_obj.parent_id, synapse_client=synapse_client)
313+
iter += 1
314+
if iter > 1000:
315+
raise ValueError("Could not find project ID in folder hierarchy")
316+
return current_obj.id
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
3+
from synapseclient import Synapse
4+
from synapseclient.extensions.curator.record_based_metadata_task import (
5+
project_id_from_entity_id,
6+
)
7+
from synapseclient.models import Folder, Project
8+
9+
10+
class TestProjectIDFromEntityID:
11+
@pytest.fixture(scope="module")
12+
def temp_hierarchy(self, syn: Synapse, request) -> tuple[str, str, str]:
13+
"""Creates a Project -> Folder -> Folder hierarchy for testing."""
14+
project = Project(name="IntegrationTest_Root_Project").store(synapse_client=syn)
15+
folder1 = Folder(name="TestFolder1", parent_id=project.id).store(
16+
synapse_client=syn
17+
)
18+
folder2 = Folder(name="TestFolder2", parent_id=folder1.id).store(
19+
synapse_client=syn
20+
)
21+
22+
def delete_project():
23+
project.delete(synapse_client=syn)
24+
25+
request.addfinalizer(delete_project)
26+
return project.id, folder1.id, folder2.id
27+
28+
def test_project_id_from_folder(self, syn, temp_hierarchy):
29+
# Test finding project from a nested file
30+
folder_id = temp_hierarchy[2]
31+
expected_project_id = temp_hierarchy[0]
32+
33+
result = project_id_from_entity_id(folder_id, syn)
34+
assert result == expected_project_id
35+
36+
def test_project_id_from_project(self, syn, temp_hierarchy):
37+
# Test finding project from a nested file
38+
project_id = temp_hierarchy[0]
39+
40+
result = project_id_from_entity_id(project_id, syn)
41+
assert result == project_id

0 commit comments

Comments
 (0)