capturegraph.procedures.procedure
#
Core Procedure Framework#
This module defines the base Procedure class and supporting infrastructure for the CaptureGraph library. The Procedure class is the foundation for all workflow nodes, providing type safety, validation, serialization, and fluent API capabilities.
Key Features:#
-
Generic typing system with return type validation
-
Automatic input and substep type checking
-
Unique identification and graph serialization
-
Fluent API for chaining procedure operations
-
Extension methods for common operations
All procedure nodes in CaptureGraph extend this base class to inherit these capabilities.
Procedure
#
Base class for all procedures in the CaptureGraph library.
Procedures represent nodes in a directed acyclic graph (DAG) that define actions and transformations to be executed by the app. Each procedure has a return type T that extends PType, indicating what kind of data it produces.
Key features: - Type safety through generic return types - Automatic validation of inputs and substeps - Unique identification via UUIDs - Serialization to JSON for app execution - Fluent API for building procedure chains
Attributes:
| Name | Type | Description |
|---|---|---|
label |
Optional[str]
|
Optional human-readable description of what this procedure does |
_kind |
type
|
The specific procedure class type |
_return_type |
type[PType]
|
The PType that this procedure returns |
_uuid |
str
|
Unique identifier for this procedure instance |
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | |
__post_init__()
#
Validate all procedure inputs, substeps, and settings at initialization.
set_label(label)
#
make_optional()
#
require()
#
new_directory(name)
#
Creates a new subdirectory within this directory.
Creates a procedure that will create a new subdirectory for organizing captured data. The directory will be created if it doesn't exist, or reused if it already exists. Does not overwrite existing directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the new directory to create |
required |
Returns:
| Type | Description |
|---|---|
Procedure[PDirectory]
|
A procedure that returns a PDirectory reference to the subdirectory |
Example
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
new_session(name)
#
Creates a new timestamped session directory for this execution.
Creates a procedure that will create a new session with a unique hex-encoded microsecond timestamp. Sessions enable multiple executions of the same procedure within a target, with each execution's data organized separately and chronologically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The session type name (e.g., "daily_capture", "field_measurement") |
required |
Returns:
| Type | Description |
|---|---|
Procedure[PDirectory]
|
A procedure that returns a PDirectory reference to the session directory |
Example
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
new_file(name)
#
Creates a file reference within this directory.
Creates a procedure that returns a reference to a file that can be used with save(), cache(), or load() operations. The file itself is not created until data is actually saved to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the file to create a reference for |
required |
Returns:
| Type | Description |
|---|---|
Procedure[PFile]
|
A procedure that returns a PFile reference to the file |
Example
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
get_first_session(name)
#
get_last_session(name)
#
get_user_directory(name, user=None)
#
Creates a user-specific subdirectory for per-user configurations.
Creates a procedure that creates a subdirectory with the structure: {parent}/{name}/{user.identifier}/. Useful for collaborative capture scenarios where multiple users share a target but need separate configuration directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The base directory name (e.g., "config", "preferences") |
required |
user
|
Optional[Procedure[PUserID]]
|
Optional user ID procedure. Defaults to GetUserID() if not provided. |
None
|
Returns:
| Type | Description |
|---|---|
Procedure[PDirectory]
|
A procedure that returns a PDirectory reference to the user's directory |
Example
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
get_thumbnail_file()
#
get_location_file()
#
get_notification_file()
#
mark_for_clear_after_backup()
#
save(procedure, skip_if_exists=False)
#
Saves data from a procedure to this file and returns PVoid.
Creates a procedure that stores the output of the given procedure to this file. Unlike cache(), this returns PVoid, making it suitable for use in ProcedureSequence workflows where no return value is needed.
The data is persisted at the end of procedure execution, after the user indicates they are done. If the user cancels early, no partial data is saved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
procedure
|
Procedure[PSaveable]
|
The procedure whose output should be saved to this file |
required |
skip_if_exists
|
bool
|
Whether to skip procedure if the file already exists |
False
|
Returns:
| Type | Description |
|---|---|
Procedure[PVoid]
|
A procedure that returns PVoid after saving is complete |
Example
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
save_bundle(procedure, skip_if_exists=False)
#
Saves data from a procedure to this folder and returns PVoid.
Creates a procedure that stores the output of the given procedure to this folder. Unlike cache_bundle(), this returns PVoid, making it suitable for use in ProcedureSequence workflows where no return value is needed.
The data is persisted at the end of procedure execution, after the user indicates they are done. If the user cancels early, no partial data is saved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
procedure
|
Procedure[PSaveableBundle]
|
The procedure whose output should be saved to this folder |
required |
skip_if_exists
|
bool
|
Whether to skip procedure if the folder already exists |
False
|
Returns:
| Type | Description |
|---|---|
Procedure[PVoid]
|
A procedure that returns PVoid after saving is complete |
Example
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
cache(procedure, skip_if_exists=False)
#
Caches data from a procedure to this file and returns the original data type.
Creates a procedure that stores the output of the given procedure to this file AND returns the data for continued use in the workflow. This is useful for persistent configuration data that you want to use in future sessions and also reference in conditional logic.
The data is persisted at the end of procedure execution. If skip_if_exists is True and the file exists, the existing data is preloaded for user editing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
procedure
|
Procedure[T2]
|
The procedure whose output should be cached to this file |
required |
skip_if_exists
|
bool
|
Whether to skip procedure if the file already exists |
False
|
Returns:
| Type | Description |
|---|---|
Procedure[T2]
|
A procedure that returns the same type as the input procedure |
Example
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
cache_bundle(procedure, skip_if_exists=False)
#
Caches data from a procedure to this folder and returns the original data type.
Creates a procedure that stores the output of the given procedure to this folder AND returns the data for continued use in the workflow. This is useful for persistent configuration data that you want to use in future sessions and also reference in conditional logic.
The data is persisted at the end of procedure execution. If skip_if_exists is True and the folder exists, the existing data is preloaded for user editing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
procedure
|
Procedure[T2]
|
The procedure whose output should be cached to this folder |
required |
skip_if_exists
|
bool
|
Whether to skip procedure if the folder already exists |
False
|
Returns:
| Type | Description |
|---|---|
Procedure[T2]
|
A procedure that returns the same type as the input procedure |
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
load(or_else)
#
Loads existing data from this file or executes fallback procedure.
Creates a procedure that tries to load previously saved data from this file. If the file doesn't exist or has no data, it executes the fallback procedure instead. This is commonly used for reference-based workflows where you want to load existing references or create new ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
or_else
|
Procedure[T2]
|
The fallback procedure to execute if no data exists in the file |
required |
Returns:
| Type | Description |
|---|---|
Procedure[T2]
|
A procedure that returns the loaded data or fallback result |
Example
Source code in capturegraph-lib/capturegraph/procedures/procedure.py
load_bundle(or_else)
#
Shortcut for CacheProcedureToBundle node (load mode).
__or__(other)
#
__and__(other)
#
__invert__()
#
__lt__(other)
#
__gt__(other)
#
Shortcut for NumberLessThan node (with operands swapped).
__le__(other)
#
Shortcut for BoolNot(NumberLessThan) node combination.
__ge__(other)
#
Shortcut for BoolNot(NumberLessThan) node combination (with operands swapped).
forward_types(base_type, *argument_names)
#
Decorator to propagate the return type of a Procedure subclass based on the return_type of other Procedure instances passed as fields.
This decorator wraps the class's post_init method, inspects the specified dataclass fields (by name), and ensures all of them are Procedure instances returning the same PType. The subclass's _return_type attribute is then set to this common return type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*argument_names
|
str
|
Names of the dataclass fields whose return types should be forwarded. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[C], C]
|
The class decorator that updates post_init on the target class. |