Folder¶
Contained within this file are experimental interfaces for working with the Synapse Python Client. Unless otherwise noted these interfaces are subject to change at any time. Use at your own risk.
Example Script¶
Working with folders
"""
Expects that ~/temp exists and is a directory.
The purpose of this script is to demonstrate how to use the new OOP interface for folders.
The following actions are shown in this script:
1. Creating a folder
2. Storing a folder to a project
3. Storing several files to a folder
4. Storing several folders in a folder
5. Getting metadata about a folder and it's immediate children
6. Updating the annotations in bulk for a number of folders and files
7. Deleting a folder
8. Copying a folder
9. Moving a folder
10. Using sync_from_synapse to download the files and folders
"""
import os
from datetime import date, datetime, timedelta, timezone
import synapseclient
from synapseclient.models import File, Folder
PROJECT_ID = "syn52948289"
syn = synapseclient.Synapse(debug=True)
syn.login()
def create_random_file(
path: str,
) -> None:
"""Create a random file with random data.
:param path: The path to create the file at.
"""
with open(path, "wb") as f:
f.write(os.urandom(1))
def try_delete_folder(folder_name: str, parent_id: str) -> None:
"""Simple try catch to delete a folder."""
try:
Folder(name=folder_name, parent_id=parent_id).get().delete()
except Exception:
pass
def store_folder():
# Clean up synapse for previous runs:
try_delete_folder("my_new_folder_for_this_project", PROJECT_ID)
try_delete_folder("destination_for_copy", PROJECT_ID)
try_delete_folder("my_new_folder_for_this_project_I_want_to_delete", PROJECT_ID)
# Creating annotations for my folder ==================================================
annotations_for_my_folder = {
"my_single_key_string": "a",
"my_key_string": ["b", "a", "c"],
"my_key_bool": [False, False, False],
"my_key_double": [1.2, 3.4, 5.6],
"my_key_long": [1, 2, 3],
"my_key_date": [date.today(), date.today() - timedelta(days=1)],
"my_key_datetime": [
datetime.today(),
datetime.today() - timedelta(days=1),
datetime.now(tz=timezone(timedelta(hours=-5))),
datetime(2023, 12, 7, 13, 0, 0, tzinfo=timezone(timedelta(hours=0))),
datetime(2023, 12, 7, 13, 0, 0, tzinfo=timezone(timedelta(hours=-7))),
],
}
# 1) Creating a folder ===============================================================
root_folder_for_my_project = Folder(
name="my_new_folder_for_this_project",
annotations=annotations_for_my_folder,
parent_id=PROJECT_ID,
description="This is a folder with random data.",
)
root_folder_for_my_project = root_folder_for_my_project.store()
print(
f"Folder created: {root_folder_for_my_project.name} with id: {root_folder_for_my_project.id}"
)
# 2) Updating and storing an annotation ==============================================
new_folder_instance = Folder(id=root_folder_for_my_project.id).get()
new_folder_instance.annotations["my_key_string"] = ["new", "values", "here"]
stored_folder = new_folder_instance.store()
print(f"Folder {stored_folder.name} updated with new annotations:")
print(stored_folder.annotations)
# 3) Storing several files to a folder ===============================================
files_to_store = []
for loop in range(1, 10):
name_of_file = f"my_file_with_random_data_{loop}.txt"
path_to_file = os.path.join(os.path.expanduser("~/temp"), name_of_file)
create_random_file(path_to_file)
file = File(
path=path_to_file,
name=name_of_file,
)
files_to_store.append(file)
root_folder_for_my_project.files = files_to_store
root_folder_for_my_project = root_folder_for_my_project.store()
# 4) Storing several folders in a folder =============================================
folders_to_store = []
for loop in range(1, 10):
folder_to_store = Folder(
name=f"my_new_folder_for_this_project_{loop}",
)
folders_to_store.append(folder_to_store)
root_folder_for_my_project.folders = folders_to_store
root_folder_for_my_project = root_folder_for_my_project.store()
# 5) Getting metadata about a folder and it's immediate children =====================
new_folder_instance = Folder(id=root_folder_for_my_project.id).sync_from_synapse(
download_file=False, recursive=False
)
print(f"Synced folder {new_folder_instance.name} from synapse")
for file in new_folder_instance.files:
print(f"Found File in Synapse at: {new_folder_instance.name}/{file.name}")
for folder in new_folder_instance.folders:
print(f"Found Folder in Synapse at: {new_folder_instance.name}/{folder.name}")
# 6) Updating the annotations in bulk for a number of folders and files ==============
new_annotations = {
"my_new_key_string": ["b", "a", "c"],
}
for file in new_folder_instance.files:
file.annotations = new_annotations
for folder in new_folder_instance.folders:
folder.annotations = new_annotations
new_folder_instance.store()
# 7) Deleting a folder ===============================================================
folder_to_delete = Folder(
name="my_new_folder_for_this_project_I_want_to_delete",
parent_id=PROJECT_ID,
).store()
folder_to_delete.delete()
# 8) Copying a folder ===============================================================
destination_folder_to_copy_to = Folder(
name="destination_for_copy", parent_id=PROJECT_ID
).store()
coped_folder = root_folder_for_my_project.copy(
parent_id=destination_folder_to_copy_to.id
)
print(
f"Copied folder from {root_folder_for_my_project.id} to {coped_folder.id} in synapse"
)
# You'll also see all the files/folders were copied too
for file in coped_folder.files:
print(f"Found (copied) File in Synapse at: {coped_folder.name}/{file.name}")
for folder in coped_folder.folders:
print(f"Found (copied) Folder in Synapse at: {coped_folder.name}/{folder.name}")
# 9) Moving a folder ===============================================================
folder_i_am_going_to_move = Folder(
name="folder_i_am_going_to_move", parent_id=PROJECT_ID
).store()
current_parent_id = folder_i_am_going_to_move.parent_id
folder_i_am_going_to_move.parent_id = destination_folder_to_copy_to.id
folder_i_am_going_to_move.store()
print(
f"Moved folder from {current_parent_id} to {folder_i_am_going_to_move.parent_id}"
)
# 10) Using sync_from_synapse to download the files and folders ======================
# This will download all the files and folders in the folder to the local file system
path_to_download = os.path.expanduser("~/temp/recursiveDownload")
if not os.path.exists(path_to_download):
os.mkdir(path_to_download)
root_folder_for_my_project.sync_from_synapse(path=path_to_download)
store_folder()
API Reference¶
synapseclient.models.Folder
dataclass
¶
Bases: FolderSynchronousProtocol
, AccessControllable
, StorableContainer
Folder is a hierarchical container for organizing data in Synapse.
ATTRIBUTE | DESCRIPTION |
---|---|
id |
The unique immutable ID for this folder. A new ID will be generated for new Folders. Once issued, this ID is guaranteed to never change or be re-issued. |
name |
The name of this folder. Must be 256 characters or less. Names may only contain: letters, numbers, spaces, underscores, hyphens, periods, plus signs, apostrophes, and parentheses. |
parent_id |
The ID of the Project or Folder that is the parent of this Folder. |
description |
The description of this entity. Must be 1000 characters or less. |
etag |
(Read Only) Synapse employs an Optimistic Concurrency Control (OCC) scheme to handle concurrent updates. Since the E-Tag changes every time an entity is updated it is used to detect when a client's current representation of an entity is out-of-date. |
created_on |
(Read Only) The date this entity was created. |
modified_on |
(Read Only) The date this entity was last modified. |
created_by |
(Read Only) The ID of the user that created this entity. |
modified_by |
(Read Only) The ID of the user that last modified this entity. |
files |
Files that exist within this folder. |
folders |
Folders that exist within this folder. |
annotations |
Additional metadata associated with the folder. The key is the name
of your desired annotations. The value is an object containing a list of
values (use empty list to represent no values for key) and the value type
associated with all values in the list. To remove all annotations set this
to an empty dict
TYPE:
|
create_or_update |
(Store only) Indicates whether the method should automatically perform an update if the resource conflicts with an existing Synapse object. When True this means that any changes to the resource will be non-destructive. This boolean is ignored if you've already stored or retrieved the resource
from Synapse for this instance at least once. Any changes to the resource
will be destructive in this case. For example if you want to delete the
content for a field you will need to call
TYPE:
|
Source code in synapseclient/models/folder.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
|
Functions¶
get
¶
get(parent: Optional[Union[Folder, Project]] = None, *, synapse_client: Optional[Synapse] = None) -> Folder
Get the folder metadata from Synapse. You are able to find a folder by either the id or the name and parent_id.
PARAMETER | DESCRIPTION |
---|---|
parent
|
The parent folder or project this folder exists under. |
synapse_client
|
If not passed in and caching was not disabled by
|
RETURNS | DESCRIPTION |
---|---|
Folder
|
The folder object. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the folder does not have an id or a
(name and ( |
Source code in synapseclient/models/protocols/folder_protocol.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
store
¶
store(parent: Optional[Union[Folder, Project]] = None, failure_strategy: FailureStrategy = LOG_EXCEPTION, *, synapse_client: Optional[Synapse] = None) -> Folder
Store folders and files to synapse. If you have any files or folders attached
to this folder they will be stored as well. You may attach files and folders
to this folder by setting the files
and folders
attributes.
By default the store operation will non-destructively update the folder if
you have not already retrieved the folder from Synapse. If you have already
retrieved the folder from Synapse then the store operation will be destructive
and will overwrite the folder with the current state of this object. See the
create_or_update
attribute for more information.
PARAMETER | DESCRIPTION |
---|---|
parent
|
The parent folder or project to store the folder in. |
failure_strategy
|
Determines how to handle failures when storing attached Files and Folders under this Folder and an exception occurs.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
RETURNS | DESCRIPTION |
---|---|
Folder
|
The folder object. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the folder does not have an id or a
(name and ( |
Source code in synapseclient/models/protocols/folder_protocol.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
delete
¶
Delete the folder from Synapse by its id.
PARAMETER | DESCRIPTION |
---|---|
synapse_client
|
If not passed in and caching was not disabled by
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the folder does not have an id set. |
Source code in synapseclient/models/protocols/folder_protocol.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
copy
¶
copy(parent_id: str, copy_annotations: bool = True, exclude_types: Optional[List[str]] = None, file_update_existing: bool = False, file_copy_activity: Union[str, None] = 'traceback', *, synapse_client: Optional[Synapse] = None) -> Folder
Copy the folder to another Synapse location. This will recursively copy all Tables, Links, Files, and Folders within the folder.
PARAMETER | DESCRIPTION |
---|---|
parent_id
|
Synapse ID of a folder/project that the copied entity is being copied to
TYPE:
|
copy_annotations
|
True to copy the annotations.
TYPE:
|
exclude_types
|
A list of entity types ['file', 'table', 'link'] which determines which entity types to not copy. Defaults to an empty list. |
file_update_existing
|
When the destination has a file that has the same name, users can choose to update that file.
TYPE:
|
file_copy_activity
|
Has three options to set the activity of the copied file:
|
synapse_client
|
If not passed in and caching was not disabled by
|
RETURNS | DESCRIPTION |
---|---|
Folder
|
The copied folder object. |
Using this function
Assuming you have a folder with the ID "syn123" and you want to copy it to a project with the ID "syn456":
new_folder_instance = await Folder(id="syn123").copy(parent_id="syn456")
Copy the folder but do not persist annotations:
new_folder_instance = await Folder(id="syn123").copy(parent_id="syn456", copy_annotations=False)
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the folder does not have an ID and parent_id to copy. |
Source code in synapseclient/models/protocols/folder_protocol.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
sync_from_synapse
¶
sync_from_synapse(path: Optional[str] = None, recursive: bool = True, download_file: bool = True, if_collision: str = COLLISION_OVERWRITE_LOCAL, failure_strategy: FailureStrategy = LOG_EXCEPTION, *, synapse_client: Optional[Synapse] = None) -> Self
Sync this container and all possible sub-folders from Synapse. By default this
will download the files that are found and it will populate the
files
and folders
attributes with the found files and folders. If you only
want to retrieve the full tree of metadata about your container specify
download_file
as False.
This works similar to synapseutils.syncFromSynapse, however, this does not currently support the writing of data to a manifest TSV file. This will be a future enhancement.
Only Files and Folders are supported at this time to be synced from synapse.
PARAMETER | DESCRIPTION |
---|---|
path
|
An optional path where the file hierarchy will be reproduced. If not specified the files will by default be placed in the synapseCache. |
recursive
|
Whether or not to recursively get the entire hierarchy of the folder and sub-folders.
TYPE:
|
download_file
|
Whether to download the files found or not.
TYPE:
|
if_collision
|
Determines how to handle file collisions. May be
TYPE:
|
failure_strategy
|
Determines how to handle failures when retrieving children under this Folder and an exception occurs.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
RETURNS | DESCRIPTION |
---|---|
Self
|
The object that was called on. This will be the same object that was called on to start the sync. |
Using this function
Suppose I want to walk the immediate children of a folder without downloading the files:
from synapseclient import Synapse
from synapseclient.models import Folder
syn = Synapse()
syn.login()
my_folder = Folder(id="syn12345")
my_folder.sync_from_synapse(download_file=False, recursive=False)
for folder in my_folder.folders:
print(folder.name)
for file in my_folder.files:
print(file.name)
Suppose I want to download the immediate children of a folder:
from synapseclient import Synapse
from synapseclient.models import Folder
syn = Synapse()
syn.login()
my_folder = Folder(id="syn12345")
my_folder.sync_from_synapse(path="/path/to/folder", recursive=False)
for folder in my_folder.folders:
print(folder.name)
for file in my_folder.files:
print(file.name)
Suppose I want to download the immediate all children of a Project and all sub-folders and files:
from synapseclient import Synapse
from synapseclient.models import Project
syn = Synapse()
syn.login()
my_project = Project(id="syn12345")
my_project.sync_from_synapse(path="/path/to/folder")
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the folder does not have an id set. |
A sequence diagram for this method is as follows:
sequenceDiagram
autonumber
participant project_or_folder
activate project_or_folder
project_or_folder->>sync_from_synapse: Recursive search and download files
activate sync_from_synapse
opt Current instance not retrieved from Synapse
sync_from_synapse->>project_or_folder: Call `.get()` method
project_or_folder-->>sync_from_synapse: .
end
loop For each return of the generator
sync_from_synapse->>client: call `.getChildren()` method
client-->>sync_from_synapse: .
note over sync_from_synapse: Append to a running list
end
loop For each child
note over sync_from_synapse: Create all `pending_tasks` at current depth
alt Child is File
note over sync_from_synapse: Append `file.get()` method
else Child is Folder
note over sync_from_synapse: Append `folder.get()` method
alt Recursive is True
note over sync_from_synapse: Append `folder.sync_from_synapse()` method
end
end
end
loop For each task in pending_tasks
par `file.get()`
sync_from_synapse->>File: Retrieve File metadata and Optionally download
File->>client: `.get()`
client-->>File: .
File-->>sync_from_synapse: .
and `folder.get()`
sync_from_synapse->>Folder: Retrieve Folder metadataa
Folder->>client: `.get()`
client-->>Folder: .
Folder-->>sync_from_synapse: .
and `folder.sync_from_synapse()`
note over sync_from_synapse: This is a recursive call to `sync_from_synapse`
sync_from_synapse->>sync_from_synapse: Recursive call to `.sync_from_synapse()`
end
end
deactivate sync_from_synapse
deactivate project_or_folder
Source code in synapseclient/models/protocols/storable_container_protocol.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
get_permissions
¶
get_permissions(*, synapse_client: Optional[Synapse] = None) -> Permissions
Get the permissions that the caller has on an Entity.
PARAMETER | DESCRIPTION |
---|---|
synapse_client
|
If not passed in and caching was not disabled by
|
RETURNS | DESCRIPTION |
---|---|
Permissions
|
A Permissions object |
Using this function:
Getting permissions for a Synapse Entity
permissions = File(id="syn123").get_permissions()
Getting access types list from the Permissions object
permissions.access_types
Source code in synapseclient/models/protocols/access_control_protocol.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
get_acl
¶
Get the ACL that a user or group has on an Entity.
PARAMETER | DESCRIPTION |
---|---|
principal_id
|
Identifier of a user or group (defaults to PUBLIC users)
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
RETURNS | DESCRIPTION |
---|---|
List[str]
|
An array containing some combination of ['READ', 'UPDATE', 'CREATE', 'DELETE', 'DOWNLOAD', 'MODERATE', 'CHANGE_PERMISSIONS', 'CHANGE_SETTINGS'] or an empty array |
Source code in synapseclient/models/protocols/access_control_protocol.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
set_permissions
¶
set_permissions(principal_id: int = None, access_type: List[str] = None, modify_benefactor: bool = False, warn_if_inherits: bool = True, overwrite: bool = True, *, synapse_client: Optional[Synapse] = None) -> Dict[str, Union[str, list]]
Sets permission that a user or group has on an Entity. An Entity may have its own ACL or inherit its ACL from a benefactor.
PARAMETER | DESCRIPTION |
---|---|
principal_id
|
Identifier of a user or group.
TYPE:
|
access_type
|
Type of permission to be granted. One or more of CREATE, READ, DOWNLOAD, UPDATE, DELETE, CHANGE_PERMISSIONS. Defaults to ['READ', 'DOWNLOAD'] |
modify_benefactor
|
Set as True when modifying a benefactor's ACL
TYPE:
|
warn_if_inherits
|
Set as False, when creating a new ACL. Trying to modify the ACL of an Entity that inherits its ACL will result in a warning
TYPE:
|
overwrite
|
By default this function overwrites existing permissions for the specified user. Set this flag to False to add new permissions non-destructively.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Union[str, list]]
|
An Access Control List object |
Setting permissions
Grant all registered users download access
File(id="syn123").set_permissions(principal_id=273948, access_type=['READ','DOWNLOAD'])
Grant the public view access
File(id="syn123").set_permissions(principal_id=273949, access_type=['READ'])
Source code in synapseclient/models/protocols/access_control_protocol.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|