rs_common/prefect_utils.md¶
Utility Python module for the tutorials, to be shared with the prefect or dask workers.
WARNING: AFTER EACH MODIFICATION, RESTART THE JUPYTER NOTEBOOK KERNEL !
delete_payload_file(payload_path, log=False)
¶
Delete either an S3 object or a local file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload_path
|
str
|
Path to delete (S3 path starting with s3:// or local file path) |
required |
log
|
bool
|
Whether to log the deletion (for S3 paths only) |
False
|
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
format_env_user(block_name, any_owner_id)
¶
Format the Prefect secret block for the current user
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
get_ip_address()
¶
Return IP address, see: https://stackoverflow.com/a/166520
get_s3_bucket(s3_path)
¶
Return a prefect S3 bucket object and S3 "object name" (= S3 path without s3://bucket-name) from the given S3 path. We use the prefect higher-level functions instead of those from boto3.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
get_share_bucket(sub_folder='')
async
¶
Get the prefect share bucket folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sub_folder
|
str
|
subfolder to use in the bucket. If empty, use the default folder and secret block. |
''
|
Else use a specific secret block. WARNING
|
this only works in a single-threaded environment |
required |
Returns:
| Type | Description |
|---|---|
tuple[S3Bucket, str]
|
The prefect block for the share bucket and the block name. |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
hack_for_jupyter(func, *args, **kwargs)
¶
From Jupyter we need this hack to deploy prefect flows
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
init_global_env(new_owner_id=None)
¶
Init the global variables above from the env vars. Needs to be called from the client, then called again from the prefect workers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_owner_id
|
str | None
|
new ower/user id, if known. |
None
|
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
init_prefect_blocks()
async
¶
Init prefect blocks and variables from the client environment (= from jupyter)
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
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 | |
read_apikey(optional=False, save_to_env=True)
async
¶
Read the API key, either from the environment variable or from an interactive input form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optional
|
bool
|
If False and if the env var is missing, ask it from an interactive input form. |
False
|
save_to_env
|
bool
|
If True, saves the API key to the ~/.env file. |
True
|
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
read_artifact(http_session, flow_run_id, artifact_key)
¶
Read artifact value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
http_session
|
Session
|
HTTP request session |
required |
flow_run_id
|
str | UUID
|
Prefect flow run ID |
required |
artifact_key
|
str
|
Artifact key (=name) |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Artifact value |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
read_prefect_blocks(any_owner_id=None)
async
¶
Read prefect blocks from the prefect flow and tasks into env vars and global vars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_id
|
Read prefect blocks for a specific user. |
required |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
remove_bucket_credentials(obs_ids)
async
¶
Remove bucket credentials from the prefect secret block that contains the env variables for the current user/owner_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_ids
|
list[str] | str
|
" |
required |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_copy_from_s3(from_s3_path, to_s3_path)
async
¶
Copy all objects found from one s3 path to another s3 path.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_delete(s3_prefix, log=False)
¶
Remove all files from S3 bucket with the given prefix, using low-level client.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_download_dir(s3_path, local_path=None)
async
¶
See: S3Bucket.get_directory
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_download_file(s3_path, to_path, **download_kwargs)
async
¶
See: S3Bucket.download_object_to_path
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_read_bytes(s3_path)
async
¶
Read an object's bytes straight from the S3 bucket, without a temporary file.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_upload_bytes(data, s3_path, **upload_kwargs)
async
¶
Upload in-memory bytes straight to the S3 bucket, without writing a temporary file.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_upload_dir(from_folder, s3_path, **upload_kwargs)
async
¶
See: S3Bucket.upload_from_folder
Uploads files within a folder (excluding the folder itself) to the object storage service folder.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_upload_empty_file(s3_path, **upload_kwargs)
async
¶
Upload an empty temp file to the S3 bucket.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
s3_upload_file(from_path, s3_path, **upload_kwargs)
async
¶
See: S3Bucket.upload_from_path
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
save_bucket_credentials(osam_client=None, obs_credentials=None)
async
¶
Save bucket credentials in the prefect secret block that contains the env variables for the current user/owner_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
osam_client
|
OsamClient | None
|
Used to read the user's credentials from the OSAM service. They are saved in the env vars: |
None
|
obs_credentials
|
dict[str, BucketCredentials] | None
|
Used to save custom bucket credentials. For each dict key " |
None
|
saved in the env vars
|
S3_ |
required |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
update_prefect_block(block_name, add_values=None, remove_keys=None)
async
¶
Write a prefect secret block with values, or update it if it already exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_name
|
str
|
Prefect secret block name |
required |
add_values
|
dict | None
|
Key/values to add from the block |
None
|
remove_keys
|
list | None
|
Key/values to remove from the block |
None
|
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
upload_payload_bytes(data, payload_path, **upload_kwargs)
async
¶
Upload in-memory bytes to either S3 bucket or local file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The data to upload as bytes |
required |
payload_path
|
str
|
Destination path (S3 path starting with s3:// or local file path) |
required |
upload_kwargs
|
dict[str, Any]
|
Additional keyword arguments for S3 upload |
{}
|
Returns:
| Type | Description |
|---|---|
|
The payload_path that was written to |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
wait_for_deployment(name, wait=1, max_retry=30)
async
¶
Wait for prefect deployment to be finished.