Wrapper
wrapper
EnvironmentVars
Bases: TypedDict
Required keys for Cascade's environmentVariables argument.
Source code in cascade_cms/wrapper.py
25 26 27 28 29 30 | |
Cascade
Context-manager entry point tying together the logger, REST driver, and Operations builder for a single script/session.
Failure handling is owned by this context manager, not the caller (see
submit_requests() and __exit__): a CascadeError, a network error,
or a library-side error stops only the chain it occurred on and is
recorded on the returned ChainResults; a broken batch raises
CascadeBatchError. At __exit__, with exit_on_failure=True (the
default), any recorded failure ends the script with a non-zero exit
code — code written after the with block does NOT run in that case.
Pass exit_on_failure=False (e.g. when embedding this in a longer-lived
process, such as an MCP server) to have __exit__ never raise; results
stay available to the caller as values via .success/.failed.
Use as
with Cascade(env_vars) as cascade: cascade.operations.read(identifier) results = cascade.submit_requests() # results.success / results.failed — no isinstance() checks needed.
Source code in cascade_cms/wrapper.py
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 | |
__init__(environmentVariables, debug=None, *, exit_on_failure=True, log_dir=None)
Initialize the logger, driver, and operations builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environmentVariables
|
EnvironmentVars
|
Must contain "SERVER" (label used in log output), "API_KEY" (Cascade bearer token), and "CASCADE_URL" (base URL of the Cascade instance). |
required |
debug
|
dict[str, Any] | None
|
Optional debug config for |
None
|
exit_on_failure
|
bool
|
When True (default), |
True
|
log_dir
|
str | PathLike | None
|
Directory for this run's logfile (and, in debug mode,
its request/response JSON files). Created if missing. None
keeps the default, |
None
|
Source code in cascade_cms/wrapper.py
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 | |
__exit__(exc_type, exc_value, traceback)
Run cleanup, then decide how (or whether) to propagate.
Cleanup (log_exit + driver.close) always runs first, exactly as
before. After that (see D8):
- KeyboardInterrupt/SystemExit in flight: propagate untouched.
- CascadeBatchError in flight: with exit_on_failure, raise
SystemExit(1) (no extra traceback — already logged); otherwise
propagate unchanged.
- Any other exception in flight: propagate untouched.
- No exception, exit_on_failure False: never raise.
- No exception, recorded callback failures: raise the first one
(chain-creation order) unwrapped.
- No exception, recorded CASCADE/NETWORK/LIBRARY failures: raise
SystemExit(1).
- No failures: return normally.
Code after the with block does not run when this raises.
Source code in cascade_cms/wrapper.py
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 | |
submit_requests(result_type=None, *, executor=None)
submit_requests(result_type: type[T], *, executor: Executor | None = None) -> ChainResults[T]
submit_requests(*, executor: Executor | None = None) -> ChainResults[CascadeObjects]
Run every registered operation chain and return one result per chain.
Chains run concurrently, but the nodes inside a chain run strictly in order — each operation or callback receives the previous node's result. A chain stops at its first failure without affecting any other chain.
Returns a ChainResults — a list, in chain-creation order (so
results[0] belongs to the first chain built), with .success (results
from chains that didn't fail) and .failed (a ChainFailure per
failed chain, naming the exact step and its category) added. Raw
values (a CascadeError, or the exception a callback raised) stay
in the list unchanged — nothing is dropped or re-typed. There's no
need for an isinstance(result, CascadeError | Exception) check;
Cascade.__exit__ owns whether a failure ends the
script (see its docstring and exit_on_failure).
If the batch itself breaks (not an individual chain), this raises
CascadeBatchError instead of returning — it no longer swallows
the error and returns an empty list.
The chain list is cleared afterwards, so callbacks registered for one batch never run again in the next.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_type
|
type[T] | None
|
Type hint for Pylance/mypy (e.g., submit_requests(Asset)) |
None
|
executor
|
Executor | None
|
Optional Executor for sync callbacks.
Use ThreadPoolExecutor (default) for I/O-bound work.
Use ProcessPoolExecutor(max_workers= |
None
|
Example (CPU-bound callbacks): from concurrent.futures import ProcessPoolExecutor from os import cpu_count
with ProcessPoolExecutor(max_workers=cpu_count()) as executor:
cascade.operations.read(id).then(optimize_image)
results = cascade.submit_requests(executor=executor)
Returns:
| Type | Description |
|---|---|
ChainResults[CascadeObjects] | ChainResults[T]
|
One entry per chain: its final result, or the error that |
ChainResults[CascadeObjects] | ChainResults[T]
|
stopped it — wrapped in a |
Raises:
| Type | Description |
|---|---|
CascadeBatchError
|
The run itself broke (e.g. the driver's event loop failed) before chains could report their own results. |
Source code in cascade_cms/wrapper.py
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 | |