INI format¶
The ini module provides the generic logic for parsing Deltares ini based files, such as the mdu, structures files, as well as more complex files such as the boundary condition (bc) files.
Note that specific attribute files that employ this ini format often have their own dedicated module (and separate API doc page). These include:
- MDU file (
hydrolib.core.dflowfm.mdu
) - External forcing file (
hydrolib.core.dflowfm.ext
) - Initial fields and parameter file (
hydrolib.core.dflowfm.inifield
) - Structure file (
hydrolib.core.dflowfm.structure
) - 1D roughness (
hydrolib.core.dflowfm.friction
) - Cross section files (
hydrolib.core.dflowfm.crosssection
) - Storage node file (
hydrolib.core.dflowfm.storagenode
)
Following below is the documentation for the INI format base classes.
Model¶
DataBlockINIBasedModel
¶
Bases: INIBasedModel
DataBlockINIBasedModel defines the base model for ini models with datablocks.
Attributes:
Name | Type | Description |
---|---|---|
datablock |
Datablock
|
(class attribute) the actual data columns. |
Source code in hydrolib/core/dflowfm/ini/models.py
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 |
|
INIBasedModel
¶
Bases: BaseModel
, ABC
INIBasedModel defines the base model for blocks/chapters inside an INIModel (*.ini file).
INIBasedModel instances can be created from Section instances obtained through parsing ini documents. It further supports adding arbitrary fields to it, which will be written to file. Lastly, no arbitrary types are allowed for the defined fields.
Attributes:
Name | Type | Description |
---|---|---|
comments |
Optional[Comments]
|
Optional Comments if defined by the user, containing descriptions for all data fields. |
Source code in hydrolib/core/dflowfm/ini/models.py
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 |
|
Comments
¶
Bases: BaseModel
, ABC
Comments defines the comments of an INIBasedModel
Source code in hydrolib/core/dflowfm/ini/models.py
109 110 111 112 113 114 |
|
get_list_delimiter()
classmethod
¶
List delimiter string that will be used for serializing list field values for any IniBasedModel, if that field has no custom list delimiter.
This function should be overridden by any subclass for a particular filetype that needs a specific/different list separator.
Source code in hydrolib/core/dflowfm/ini/models.py
78 79 80 81 82 83 84 85 86 87 |
|
get_list_field_delimiter(field_key)
classmethod
¶
List delimiter string that will be used for serializing the given field's value. The returned delimiter is either the field's custom list delimiter if that was specified using Field(.., delimiter=".."), or the default list delimiter for the model class that this field belongs to.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_key
|
str
|
the original field key (not its alias). |
required |
Source code in hydrolib/core/dflowfm/ini/models.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
INIModel
¶
Bases: ParsableFileModel
INI Model representation of a *.ini file.
Typically subclasses will implement the various sorts of ini files, specifically for their fileType/contents. Child elements of this class associated with chapters/blocks in the ini file will be (sub)class of INIBasedModel.
Source code in hydrolib/core/dflowfm/ini/models.py
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 |
|
Parser¶
Parser
¶
Parser defines a generic Parser for Deltares ini files.
The Parser can be configured with a ParserConfig object.
Source code in hydrolib/core/dflowfm/ini/parser.py
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 |
|
__init__(config)
¶
Creates a new Parser configured with the provided config
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
ParserConfig
|
The configuration of this Parser |
required |
Source code in hydrolib/core/dflowfm/ini/parser.py
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 |
|
feed_line(line)
¶
Parse the next line with this Parser.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line
|
str
|
The line to parse |
required |
Source code in hydrolib/core/dflowfm/ini/parser.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
finalize()
¶
Finalize parsing and return the constructed Document.
Returns:
Name | Type | Description |
---|---|---|
Document |
Document
|
A Document describing the parsed ini file. |
Source code in hydrolib/core/dflowfm/ini/parser.py
186 187 188 189 190 191 192 193 194 195 196 |
|
parse(filepath, config=None)
classmethod
¶
Parses an INI file without a specific model type and returns it as a Document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
Path
|
File path to the INI-format file. |
required |
config
|
ParserConfig
|
Parser configuration to use. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Document |
Document
|
Representation of the parsed INI-file. |
Source code in hydrolib/core/dflowfm/ini/parser.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
|
parse_as_dict(filepath, config=None)
classmethod
¶
Parses an INI file without a specific model type and returns it as a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
Path
|
File path to the INI-format file. |
required |
config
|
ParserConfig
|
Parser configuration to use. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Representation of the parsed INI-file. |
Source code in hydrolib/core/dflowfm/ini/parser.py
344 345 346 347 348 349 350 351 352 353 354 355 356 |
|
ParserConfig
¶
Bases: BaseModel
ParserConfig defines the configuration options of the Parser
Note that we cannot set both allow_only_keywords and parse_datablocks to True because we cannot distinguish between datablocks and key only properties. As such this will lead to a validation error.
Attributes:
Name | Type | Description |
---|---|---|
allow_only_keywords |
bool
|
Whether to allow properties with only keys (no '=' or value). Defaults to False. |
parse_datablocks |
bool
|
Whether to allow parsing of datablocks at the bottom of sections. Defaults to False. |
parse_comments |
bool
|
Whether we allow parsing of comments defined with the comment_delimeter. Defaults to True. |
comment_delimiter |
str
|
The character or sequence of character used to define a comment. Defaults to '#'. |
Source code in hydrolib/core/dflowfm/ini/parser.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 |
|
Serializer¶
DataBlockINIBasedSerializerConfig
¶
Bases: INISerializerConfig
Class that holds the configuration settings for INI files with data blocks serialization.
Source code in hydrolib/core/dflowfm/ini/serializer.py
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 |
|
float_format_datablock = ''
class-attribute
instance-attribute
¶
str: The string format that will be used for float serialization of the datablock. 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
INISerializerConfig
¶
Bases: SerializerConfig
SerializerConfig defines the configuration options of the Serializer
Attributes:
Name | Type | Description |
---|---|---|
section_indent |
int
|
The number of spaces with which whole sections should be indented. Defaults to 0. |
property_indent |
int
|
The number of spaces with which properties should be indented relative to the section header (i.e. the full indent equals the section_indent plus property_indent). Defaults to 4. |
datablock_indent |
int
|
The number of spaces with which datablock rows are indented relative to the section header (i.e. the full indent equals the section_indent plus datablock_indent). Defaults to 8. |
datablock_spacing |
int
|
The number of spaces between datablock columns. Note that there might be additional offset to ensure . is lined out. Defaults to 2. |
comment_delimiter |
str
|
The character used to delimit comments. Defaults to '#'. |
skip_empty_properties |
bool
|
Whether or not to skip properties with a value that is empty or None. Defaults to True. |
Source code in hydrolib/core/dflowfm/ini/serializer.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 |
|
MaxLengths
¶
Bases: BaseModel
MaxLengths defines the maxmimum lengths of the parts of a section
Attributes:
Name | Type | Description |
---|---|---|
key |
int
|
The maximum length of all the keys of the properties within a section. If no properties are present it should be 0. |
value |
int
|
The maximum length of all the non None values of the properties within a section. If no properties are present, or all values are None, it should be 0. |
datablock |
Optional[Sequence[int]]
|
The maximum length of the values of each column of the Datablock. If no datablock is present it defaults to None. |
Source code in hydrolib/core/dflowfm/ini/serializer.py
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 |
|
from_section(section)
classmethod
¶
Generate a MaxLengths instance from the given Section
Parameters:
Name | Type | Description | Default |
---|---|---|---|
section
|
Section
|
The section of which the MaxLengths are calculated |
required |
Returns:
Name | Type | Description |
---|---|---|
MaxLengths |
MaxLengths
|
The MaxLengths corresponding with the provided section |
Source code in hydrolib/core/dflowfm/ini/serializer.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
SectionSerializer
¶
SectionSerializer provides the serialize method to serialize a Section
The entrypoint of this method is the serialize method, which will construct an actual instance and serializes the Section with it.
Source code in hydrolib/core/dflowfm/ini/serializer.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 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 |
|
config
property
¶
The SerializerConfig used while serializing the section.
max_length
property
¶
The MaxLengths of the Section being serialized by this SectionSerializer.
__init__(config, max_length)
¶
Create a new SectionSerializer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
SerializerConfig
|
The config describing the serialization options |
required |
max_length
|
MaxLengths
|
The max lengths of the section being serialized |
required |
Source code in hydrolib/core/dflowfm/ini/serializer.py
170 171 172 173 174 175 176 177 178 |
|
serialize(section, config)
classmethod
¶
Serialize the provided section with the given config
Parameters:
Name | Type | Description | Default |
---|---|---|---|
section
|
Section
|
The section to serialize |
required |
config
|
SerializerConfig
|
The config describing the serialization options |
required |
Returns:
Name | Type | Description |
---|---|---|
Lines |
Lines
|
The iterable lines of the serialized section |
Source code in hydrolib/core/dflowfm/ini/serializer.py
180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
Serializer
¶
Serializer serializes Document to its corresponding lines.
Source code in hydrolib/core/dflowfm/ini/serializer.py
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 |
|
__init__(config)
¶
Creates a new Serializer with the provided configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
SerializerConfig
|
The configuration of this Serializer. |
required |
Source code in hydrolib/core/dflowfm/ini/serializer.py
272 273 274 275 276 277 278 |
|
serialize(document)
¶
Serialize the provided document into an iterable of lines.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
document
|
Document
|
The Document to serialize. |
required |
Returns:
Name | Type | Description |
---|---|---|
Lines |
Lines
|
An iterable returning each line of the serialized Document. |
Source code in hydrolib/core/dflowfm/ini/serializer.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
write_ini(path, document, config)
¶
Write the provided document to the specified path
If the provided path already exists, it will be overwritten. If the parent folder do not exist, they will be created.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path
|
The path to which the document should be written. |
required |
document
|
Document
|
The document to serialize to the specified path. |
required |
config
|
INISerializerConfig
|
The configuration settings for the serializer. |
required |
Source code in hydrolib/core/dflowfm/ini/serializer.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
|