Operations
FMU ¶
Unpack and repack facilities for FMU archives.
Extracts an FMU (.fmu zip archive) into a temporary directory so that operations can be applied to its modelDescription.xml descriptor. After manipulation, the FMU can be repacked into a new archive.
Attributes:
| Name | Type | Description |
|---|---|---|
FMI2_TYPES | tuple[str, ...] | FMI 2.0 scalar variable type names. |
FMI3_TYPES | tuple[str, ...] | FMI 3.0 variable type names. |
fmu_filename | str | Path to the original |
tmp_directory | str | Path to the temporary extraction directory. |
fmi_version | int | None | Detected FMI version ( |
descriptor_filename | str | Path to the extracted |
Raises:
| Type | Description |
|---|---|
FMUError | If the file does not exist or is not a valid FMU. |
Source code in fmu_manipulation_toolbox/operations.py
apply_operation(operation, apply_on=None) ¶
Apply an operation to the FMU's modelDescription.xml.
Parses the descriptor, invokes the operation's callbacks for each element, and writes back the modified descriptor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation | OperationAbstract | The operation to apply. | required |
apply_on | list[str] | None | If set, only apply the operation to ports with a causality in this list. | None |
Source code in fmu_manipulation_toolbox/operations.py
repack(filename) ¶
Repack the (possibly modified) FMU into a new .fmu archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | str | Output path for the repacked FMU. | required |
Source code in fmu_manipulation_toolbox/operations.py
save_descriptor(filename) ¶
Save a copy of the current modelDescription.xml to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | str | Destination path for the descriptor copy. | required |
Source code in fmu_manipulation_toolbox/operations.py
FMUError ¶
Bases: Exception
Exception raised for FMU-related errors.
Attributes:
| Name | Type | Description |
|---|---|---|
reason | str | Human-readable description of the error. |
Source code in fmu_manipulation_toolbox/operations.py
FMUPort ¶
Represents a port (variable) parsed from modelDescription.xml.
Stores the XML attributes from one or more levels of the port definition. For FMI 2.0, this includes the <ScalarVariable> attributes and the child type element (e.g. <Real>). For FMI 3.0, all attributes are on the type element itself.
Supports dict-like access to attributes across all levels.
Attributes:
| Name | Type | Description |
|---|---|---|
fmi_type | str | None | The FMI type name (e.g. |
attrs_list | list[dict[str, str]] | Stacked attribute dictionaries, one per XML nesting level. |
dimension | int | None | Array dimension, if applicable. |
Source code in fmu_manipulation_toolbox/operations.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 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 | |
dict_level(nb) ¶
Format one level of attributes as an XML attribute string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nb | int | Index into | required |
Returns:
| Name | Type | Description |
|---|---|---|
str | Space-separated |
Source code in fmu_manipulation_toolbox/operations.py
push_attrs(attrs) ¶
Push a new attribute dictionary onto the stack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs | dict[str, str] | XML attributes from a nested element. | required |
write_xml(fmi_version, file) ¶
Write the port definition as XML to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmi_version | int | FMI version ( | required |
file | Writable text file handle. | required |
Raises:
| Type | Description |
|---|---|
FMUError | If the FMI version is not supported. |
Source code in fmu_manipulation_toolbox/operations.py
Manipulation ¶
SAX-based parser that applies an operation to modelDescription.xml.
Parses the XML descriptor using xml.parsers.expat, invokes the operation's callbacks for each relevant element, and writes back the modified XML. Handles port renumbering and dependency tree updates when ports are removed.
Attributes:
| Name | Type | Description |
|---|---|---|
output_filename | str | Path to the temporary output file. |
operation | OperationAbstract | The operation being applied. |
fmu | FMU | The FMU being manipulated. |
current_port | FMUPort | None | The port currently being parsed. |
current_port_number | int | Running count of kept ports (1-based). |
port_translation | list[int | None] | Maps original port indices to new indices, or |
port_names_list | list[str] | Names of all encountered ports. |
port_removed_vr | set[str] | Value references of removed ports. |
Source code in fmu_manipulation_toolbox/operations.py
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 | |
escape(value) staticmethod ¶
HTML-escape a string value for safe XML output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | str | The value to escape. | required |
Returns:
| Name | Type | Description |
|---|---|---|
str | The escaped value. Non-string values are returned unchanged. |
Source code in fmu_manipulation_toolbox/operations.py
manipulate(descriptor_filename, apply_on=None) ¶
Parse and rewrite the descriptor file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
descriptor_filename | str | Path to the | required |
apply_on | list[str] | None | If set, only process ports with a causality in this list. | None |
Source code in fmu_manipulation_toolbox/operations.py
ManipulationSkipTag ¶
Bases: Exception
Internal exception used to skip XML content until a matching closing tag.
Raised during SAX parsing to signal that the current element and all its children should be omitted from the output.
Source code in fmu_manipulation_toolbox/operations.py
OperationAbstract ¶
Base class for all FMU manipulation operations.
Subclass this to implement custom operations on FMU descriptors. The methods act as callbacks invoked during SAX parsing of modelDescription.xml.
Attributes:
| Name | Type | Description |
|---|---|---|
fmu | FMU | None | The FMU being processed, set via |
Source code in fmu_manipulation_toolbox/operations.py
closure() ¶
Called after the descriptor has been fully parsed.
Override this for post-processing or cleanup.
cosimulation_attrs(attrs) ¶
Called when the <CoSimulation> element is encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs | dict[str, str] | XML attributes of the co-simulation element. | required |
experiment_attrs(attrs) ¶
Called when the <DefaultExperiment> element is encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs | dict[str, str] | XML attributes of the default experiment. | required |
fmi_attrs(attrs) ¶
Called when the <fmiModelDescription> element is encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs | dict[str, str] | XML attributes of the root element. | required |
port_attrs(fmu_port) ¶
Called for each port (variable) in the descriptor.
Override this to inspect or modify port attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmu_port | FMUPort | The port being processed. Attributes can be modified in place. | required |
Returns:
| Name | Type | Description |
|---|---|---|
int | int |
|
Source code in fmu_manipulation_toolbox/operations.py
OperationError ¶
Bases: Exception
Exception raised for operation-related errors.
Attributes:
| Name | Type | Description |
|---|---|---|
reason | str | Human-readable description of the error. |
Source code in fmu_manipulation_toolbox/operations.py
OperationKeepOnlyRegexp ¶
Bases: OperationAbstract
Keep only ports whose names match a regular expression; remove all others.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex_string | str | Regular expression pattern. Only ports with names matching this pattern (from the start) are kept. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
regex_string | str | The original regex pattern string. |
regex | Pattern | Compiled regular expression. |
Source code in fmu_manipulation_toolbox/operations.py
OperationMergeTopLevel ¶
Bases: OperationAbstract
Merge the top-level bus prefix into port names by replacing "." with "_".
Only the first dot is replaced. For example, "Bus1.signal_name" becomes "Bus1_signal_name".
Source code in fmu_manipulation_toolbox/operations.py
OperationRemoveRegexp ¶
Bases: OperationAbstract
Remove all ports whose names match a regular expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex_string | str | Regular expression pattern. Ports with names matching this pattern (from the start) are removed. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
regex_string | str | The original regex pattern string. |
regex | Pattern | Compiled regular expression. |
Source code in fmu_manipulation_toolbox/operations.py
OperationRemoveSources ¶
Bases: OperationAbstract
Remove the sources/ directory from the FMU.
Strips the embedded C/C++ source files that some FMUs include for recompilation on the target platform.
Source code in fmu_manipulation_toolbox/operations.py
OperationRenameFromCSV ¶
Bases: OperationAbstract
Rename or remove ports according to a CSV mapping file.
Reads a semicolon-delimited CSV where column 0 is the original name and column 1 is the new name. If the new name is empty, the port is removed. Ports not listed in the CSV are kept unchanged.
Attributes:
| Name | Type | Description |
|---|---|---|
csv_filename | str | Path to the CSV mapping file. |
translations | dict[str, str] | Mapping from original to new names. |
Raises:
| Type | Description |
|---|---|
OperationError | If the CSV file is not found or malformed. |
Source code in fmu_manipulation_toolbox/operations.py
OperationSaveNamesToCSV ¶
Bases: OperationAbstract
Export all port names and metadata to a CSV file.
Generates a semicolon-delimited CSV with columns: name, newName, valueReference, causality, variability, scalarType, startValue.
The resulting file can be edited and used with OperationRenameFromCSV to rename or remove ports.
Attributes:
| Name | Type | Description |
|---|---|---|
output_filename | str | Path to the output CSV file. |
Source code in fmu_manipulation_toolbox/operations.py
OperationStripTopLevel ¶
Bases: OperationAbstract
Remove the top-level bus prefix from all port names.
Strips everything before and including the first "." separator. For example, "Bus1.signal_name" becomes "signal_name".
Source code in fmu_manipulation_toolbox/operations.py
OperationSummary ¶
Bases: OperationAbstract
Log a detailed summary of the FMU contents.
Reports FMI properties, co-simulation capabilities, default experiment values, supported platforms, embedded resources, and port counts grouped by causality.
Attributes:
| Name | Type | Description |
|---|---|---|
nb_port_per_causality | dict[str, int] | Count of ports per causality. |
Source code in fmu_manipulation_toolbox/operations.py
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 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 751 752 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 | |
OperationTrimUntil ¶
Bases: OperationAbstract
Trim port names up to and including a separator string.
For example, with separator "__", the name "prefix__signal" becomes "signal".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
separator | str | The separator string to search for in port names. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
separator | str | The separator string. |