API¶
Much of HYDROLIB-core's classes that represent input data are based on BaseModel
and FileModel
.
These build upon the pydantic package for data validation.
BaseModel and FileModel¶
Here we define our Pydantic BaseModel
with custom settings,
as well as a FileModel
that inherits from a BaseModel
but
also represents a file on disk.
BaseModel
¶
Bases: BaseModel
Source code in hydrolib/core/base/models.py
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 |
|
__init__(**data)
¶
Initialize a BaseModel with the provided data.
Raises:
Type | Description |
---|---|
ValidationError
|
A validation error when the data is invalid. |
Source code in hydrolib/core/base/models.py
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 |
|
is_file_link()
¶
Generic attribute for models backed by a file.
Source code in hydrolib/core/base/models.py
101 102 103 |
|
is_intermediate_link()
¶
Generic attribute for models that have children fields that could contain files.
Source code in hydrolib/core/base/models.py
105 106 107 |
|
show_tree(indent=0)
¶
Recursive print function for showing a tree of a model.
Source code in hydrolib/core/base/models.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
DiskOnlyFileModel
¶
Bases: FileModel
DiskOnlyFileModel provides a stub implementation for file based models which are not explicitly implemented within hydrolib.core.
It implements the FileModel with a void parser and serializer, and a save method which copies the file associated with the FileModel to a new location if it exists.
We further explicitly assume that when the filepath is None, no file will be written.
Actual file model implementations should not inherit from the DiskOnlyFileModel and instead inherit directly from FileModel.
Source code in hydrolib/core/base/models.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
|
FileModel
¶
Bases: BaseModel
, ABC
Base class to represent models with a file representation.
It therefore always has a filepath
and if it is given on
initilization, it will parse that file. The filepath can be
relative, in which case the paths are expected to be resolved
relative to some root model. If a path is absolute, this path
will always be used, regardless of a root parent.
When saving a model, if the current filepath is relative, the last resolved absolute path will be used. If the model has just been read, the
This class extends the validate
option of Pydantic,
so when when a Path is given to a field with type FileModel
,
it doesn't error, but actually initializes the FileModel
.
Attributes:
Name | Type | Description |
---|---|---|
filepath |
Optional[Path]
|
The path of this FileModel. This path can be either absolute or relative. If it is a relative path, it is assumed to be resolved from some root model. |
save_location |
Path
|
A readonly property corresponding with the (current) save location of this FileModel. If read from a file or after saving recursively or after calling synchronize_filepath, this value will be updated to its new state. If made from memory and filepath is not set, it will correspond with cwd / filename.extension |
Source code in hydrolib/core/base/models.py
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 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
|
save_location
property
¶
Get the current save location which will be used when calling save()
This value can be None if the filepath is None and no name can be generated.
Returns:
Name | Type | Description |
---|---|---|
Path |
Optional[Path]
|
The location at which this model will be saved. |
__init__(filepath=None, resolve_casing=False, recurse=True, path_style=None, *args, **kwargs)
¶
Create a new FileModel from the given filepath.
If no filepath is provided, the model is initialized as an empty model with default values. If the filepath is provided, it is read from disk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
Optional[PathOrStr]
|
The file path. Defaults to None. |
None
|
resolve_casing
|
bool
|
Whether or not to resolve the file name references so that they match the case with what is on disk. Defaults to False. |
False
|
recurse
|
bool
|
Whether or not to recursively load the model. Defaults to True. |
True
|
path_style
|
Optional[str]
|
Which path style is used in the loaded files. Defaults to the path style that matches the current operating system. Options: 'unix', 'windows'. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
When an unsupported path style is passed. |
Source code in hydrolib/core/base/models.py
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 |
|
__new__(filepath=None, *args, **kwargs)
¶
Create a new model. If the file at the provided file path was already parsed, this instance is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
Optional[PathOrStr]
|
The file path to the file. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
FileModel |
A file model. |
Source code in hydrolib/core/base/models.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
save(filepath=None, recurse=False, path_style=None, exclude_unset=False)
¶
Save the model to disk.
If recurse is set to True, all of the child FileModels will be saved as well. Relative child models are stored relative to this Model, according to the model file hierarchy specified with the respective filepaths. Absolute paths will be written to their respective locations. Note that this will overwrite any existing files that are stored in this location.
Note that if recurse is set to True, the save_location properties of the children are updated to their respective new locations.
If filepath it is specified, the filepath of this FileModel is set to the specified path before the save operation is executed. If none is specified it will use the current filepath.
If the used filepath is relative, it will be stored at the current save_location. If you only want to save a child model of some root model, it is recommended to first call synchronize_filepaths on the root model, to ensure the child model's save_location is correctly determined.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
Optional[Path]
|
The file path at which this model is saved. If None is specified it defaults to the filepath currently stored in the filemodel. Defaults to None. |
None
|
recurse
|
bool
|
Whether to save all children of this FileModel (when set to True), or only save this model (when set to False). Defaults to False. |
False
|
path_style
|
Optional[str]
|
With which file path style to save the model. File references will be written with the specified path style. Defaults to the path style used by the current operating system. Options: 'unix', 'windows'. |
None
|
exclude_unset
|
bool
|
Whether or not to exclude unset values when saving the model. Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
When an unsupported path style is passed. |
Source code in hydrolib/core/base/models.py
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 |
|
synchronize_filepaths()
¶
Synchronize the save_location properties of all child models respective to this FileModel's save_location.
Source code in hydrolib/core/base/models.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
ModelSaveSettings
¶
A class that holds the global settings for model saving.
Source code in hydrolib/core/base/models.py
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 |
|
path_style
property
¶
Gets the path style setting.
Returns:
Name | Type | Description |
---|---|---|
PathStyle |
PathStyle
|
Which path style is used to save the files. |
__init__(path_style=None, exclude_unset=False)
¶
Initializes a new instance of the ModelSaveSettings class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_style
|
Optional[PathStyle]
|
Which file path style to use when saving the model. Defaults to the path style that matches the current operating system. |
None
|
exclude_unset
|
bool
|
Whether or not to exclude unset values when saving the model. Defaults to False. |
False
|
Source code in hydrolib/core/base/models.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
ModelTreeTraverser
¶
Bases: Generic[TAcc]
ModelTreeTraverser is responsible for traversing a ModelTree using the provided functions.
The ModelTreeTraverser will only traverse BaseModel and derived objects. Type parameter TAcc defines the type of Accumulator to be used.
Source code in hydrolib/core/base/models.py
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 |
|
__init__(should_traverse=None, should_execute=None, pre_traverse_func=None, post_traverse_func=None)
¶
Create a new ModelTreeTraverser with the given functions.
If a predicate it is not defined, it is assumed to always be true, i.e. we will always traverse to the next node, or always execute the traverse functions.
If a traverse function is not defined, it will be skipped.
The traverse functions share an accumulator, i.e. the accumulator argument is passed through all evaluated traverse functions. It is expected that the traverse function return the (potentially) changed accumulator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
should_traverse
|
Optional[Callable[[BaseModel, TAcc], bool]]
|
Function to evaluate whether to traverse to the provided BaseModel. Defaults to None. |
None
|
should_execute
|
Optional[Callable[[BaseModel, TAcc], bool]]
|
Function to evaluate whether to execute the traverse functions for the provided BaseModel. Defaults to None. |
None
|
pre_traverse_func
|
Callable[[BaseModel, TAcc], TAcc]
|
Traverse function executed before we traverse into the next BaseModel, i.e. top-down traversal. Defaults to None. |
None
|
post_traverse_func
|
Callable[[BaseModel, TAcc], TAcc]
|
Traverse function executed after we traverse into the next BaseModel, i.e. bottom-up traversal. Defaults to None. |
None
|
Source code in hydrolib/core/base/models.py
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 |
|
traverse(model, acc)
¶
Traverse the model tree of BaseModels including the model as the root, with the provided state of the acc and return the final accumulator.
The actual executed functions as well as the predicates defining whether these functions should be executed for this model as well as whether child BaseModel objects should be traversed are provided in the constructor of the ModelTreeTraverser.
The final accumulator is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
BaseModel
|
The root model in which the traversal of the model tree starts. |
required |
acc
|
TAcc
|
The current accumulator. |
required |
Returns:
Name | Type | Description |
---|---|---|
TAcc |
TAcc
|
The accumulator after the traversal of the model tree. |
Source code in hydrolib/core/base/models.py
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 |
|
ParsableFileModel
¶
Bases: FileModel
ParsableFileModel defines a FileModel which can be parsed and serialized with a serializer .
Each ParsableFileModel has a default _filename and _ext, which are used to generate the file name of any instance where the filepath is not (yet) set.
Children of the ParsableFileModel are expected to implement a serializer function which takes a Path and Dict and writes the ParsableFileModel to disk, and a parser function which takes a Path and outputs a Dict.
If more complicated solutions are required, a ParsableFileModel child can also opt to overwrite the _serialize and _parse methods, to skip the _get_serializer and _get_parser methods respectively.
Source code in hydrolib/core/base/models.py
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 |
|
SerializerConfig
¶
Bases: BaseModel
, ABC
Class that holds the configuration settings for serialization.
Source code in hydrolib/core/base/models.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
|
float_format = ''
class-attribute
instance-attribute
¶
str: The string format that will be used for float serialization. If empty, the original number will be serialized. Defaults to an empty string.
Examples:
Input value = 123.456
Format | Output | Description¶
".0f" | 123 | Format float with 0 decimal places. "f" | 123.456000 | Format float with default (=6) decimal places. ".2f" | 123.46 | Format float with 2 decimal places. "+.1f" | +123.5 | Format float with 1 decimal place with a + or sign. "e" | 1.234560e+02 | Format scientific notation with the letter 'e' with default (=6) decimal places. "E" | 1.234560E+02 | Format scientific notation with the letter 'E' with default (=6) decimal places. ".3e" | 1.235e+02 | Format scientific notation with the letter 'e' with 3 decimal places. "<15" | 123.456 | Left aligned in space with width 15 "^15.0f" | 123 | Center aligned in space with width 15 with 0 decimal places. ">15.1e" | 1.2e+02 | Right aligned in space with width 15 with scientific notation with 1 decimal place. ">15.1f" | ***123.5 | Right aligned in space with width 15 with 1 decimal place and fill empty space with * "%" | 12345.600000% | Format percentage with default (=6) decimal places. ".3%" | 12345.600% | Format percentage with 3 decimal places.
More information: https://docs.python.org/3/library/string.html#format-specification-mini-language
validator_set_default_disk_only_file_model_when_none()
¶
Validator to ensure a default empty DiskOnlyFileModel is created when the corresponding field is initialized with None.
Returns:
Name | Type | Description |
---|---|---|
classmethod |
classmethod
|
Validator to adjust None values to empty DiskOnlyFileModel objects |
Source code in hydrolib/core/base/models.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|