configz
Class info¶
Re-exported classes¶
| Class | Module | Description |
|---|---|---|
| DumpingError | configz.exceptions | Common exception for all dumping errors in configz. |
| JsonDumpError | configz.json_tools.base | Unified exception for all JSON serialization errors. |
| JsonLoadError | configz.json_tools.base | Unified exception for all JSON parsing errors. |
| ParsingError | configz.exceptions | Common exception for all parsing errors in configz. |
| TomlDumpError | configz.toml_tools.base | Unified exception for all TOML serialization errors. |
| TomlLoadError | configz.toml_tools.base | Unified exception for all TOML parsing errors. |
| YAMLError | configz.yaml_tools.yaml_errors | YAML error with rich formatting capabilities. |
| YAMLParser | configz.yaml_tools.yamlparser | Manages custom YAML tags and provides YAML loading capabilities. |
🛈 DocStrings¶
configz: main package.
Serialization stuff for config files.
DumpingError
¶
Bases: Exception
Common exception for all dumping errors in configz.
Source code in src/configz/exceptions.py
JsonDumpError
¶
JsonLoadError
¶
Bases: Exception
Unified exception for all JSON parsing errors.
Source code in src/configz/json_tools/base.py
source_path
property
¶
Path to the source file that caused the error.
format
¶
Format error with source context for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_lines
|
int
|
Number of lines to show before/after the error line. |
2
|
use_color
|
bool
|
Whether to include ANSI color codes. |
True
|
Source code in src/configz/json_tools/base.py
ParsingError
¶
Bases: Exception
Common exception for all parsing errors in configz.
Source code in src/configz/exceptions.py
TomlDumpError
¶
TomlLoadError
¶
Bases: Exception
Unified exception for all TOML parsing errors.
Source code in src/configz/toml_tools/base.py
source_path
property
¶
Path to the source file that caused the error.
format
¶
Format error with source context for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_lines
|
int
|
Number of lines to show before/after the error line. |
2
|
use_color
|
bool
|
Whether to include ANSI color codes. |
True
|
Source code in src/configz/toml_tools/base.py
YAMLError
¶
Bases: YAMLError
YAML error with rich formatting capabilities.
Inherits from yaml.YAMLError so existing code that catches YAMLError will continue to work, but adds rich formatting methods.
Source code in src/configz/yaml_tools/yaml_errors.py
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 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 | |
format
¶
Return the error as a formatted string.
Source code in src/configz/yaml_tools/yaml_errors.py
render
¶
Display the error with rich formatting to stderr.
Source code in src/configz/yaml_tools/yaml_errors.py
YAMLParser
¶
Manages custom YAML tags and provides YAML loading capabilities.
Source code in src/configz/yaml_tools/yamlparser.py
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 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 | |
create_constructor
¶
Create a YAML constructor function for a specific tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag_name
|
str
|
Name of the tag without prefix |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Loader, Node], Any]
|
Constructor function for the YAML loader |
Source code in src/configz/yaml_tools/yamlparser.py
get_handler
¶
Get the handler function for a specific tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
str
|
Full tag name (including prefix) |
required |
Returns:
| Type | Description |
|---|---|
HandlerFunc[Any] | None
|
Handler function if found, None otherwise |
Source code in src/configz/yaml_tools/yamlparser.py
list_tags
¶
load_yaml
¶
load_yaml(
text: YAMLInput,
*,
mode: LoaderStr | LoaderType = "unsafe",
include_base_path: str | PathLike[str] | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: None = None,
) -> Any
load_yaml(
text: YAMLInput,
*,
mode: LoaderStr | LoaderType = "unsafe",
include_base_path: str | PathLike[str] | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: type[TVerify],
) -> TVerify
load_yaml(
text: YAMLInput,
*,
mode: LoaderStr | LoaderType = "unsafe",
include_base_path: str | PathLike[str] | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: type[TVerify] | None = None,
) -> Any | TVerify
Load YAML content with custom tag handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
YAMLInput
|
The YAML content to load |
required |
mode
|
LoaderStr | LoaderType
|
YAML loader safety mode ('unsafe', 'full', or 'safe') Custom YAML loader classes are also accepted |
'unsafe'
|
include_base_path
|
str | PathLike[str] | AbstractFileSystem | None
|
Base path for resolving !include directives |
None
|
resolve_strings
|
bool
|
Whether to resolve MiniJinja template strings |
False
|
resolve_dict_keys
|
bool
|
Whether to resolve MiniJinja templates in dictionary keys |
False
|
resolve_inherit
|
bool
|
Whether to resolve INHERIT directives |
False
|
variables
|
dict[str, Any] | None
|
An optional dictionary to resolving !var tags |
None
|
jinja_env
|
Environment | None
|
Optional MiniJinja environment for template resolution |
None
|
verify_type
|
type[TVerify] | None
|
Type to verify and cast the output to |
None
|
Returns:
| Type | Description |
|---|---|
Any | TVerify
|
Parsed YAML data with custom tag handling, |
Any | TVerify
|
typed according to verify_type if provided |
Example
yaml_parser = YAMLParser()
@yaml_parser.register("person")
def handle_person(data: dict) -> Person:
return Person(**data)
# Without type verification
data = yaml_parser.load_yaml(
"person: !person {name: John, age: 30}",
mode="safe",
resolve_strings=True
)
# With type verification
data = yaml_parser.load_yaml(
"config: {key: value}",
mode="safe",
verify_type=dict
)
Source code in src/configz/yaml_tools/yamlparser.py
load_yaml_file
¶
load_yaml_file(
path: str | PathLike[str],
*,
mode: LoaderStr | LoaderType = "unsafe",
include_base_path: str | PathLike[str] | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: None = None,
) -> Any
load_yaml_file(
path: str | PathLike[str],
*,
mode: LoaderStr | LoaderType = "unsafe",
include_base_path: str | PathLike[str] | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: type[TVerify],
) -> TVerify
load_yaml_file(
path: str | PathLike[str],
*,
mode: LoaderStr | LoaderType = "unsafe",
include_base_path: str | PathLike[str] | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: type[TVerify] | None = None,
) -> Any | TVerify
Load YAML file with custom tag handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to the YAML file |
required |
mode
|
LoaderStr | LoaderType
|
YAML loader safety mode ('unsafe', 'full', or 'safe') Custom YAML loader classes are also accepted |
'unsafe'
|
include_base_path
|
str | PathLike[str] | AbstractFileSystem | None
|
Base path for resolving !include directives |
None
|
resolve_inherit
|
bool
|
Whether to resolve INHERIT directives |
False
|
resolve_strings
|
bool
|
Whether to resolve MiniJinja template strings |
False
|
resolve_dict_keys
|
bool
|
Whether to resolve MiniJinja templates in dictionary keys |
False
|
jinja_env
|
Environment | None
|
Optional MiniJinja environment for template resolution |
None
|
variables
|
dict[str, Any] | None
|
An optional dictionary to resolving !var tags |
None
|
storage_options
|
dict[str, Any] | None
|
Additional keywords to pass to fsspec backend |
None
|
verify_type
|
type[TVerify] | None
|
Type to verify and cast the output to |
None
|
Returns:
| Type | Description |
|---|---|
Any | TVerify
|
Parsed YAML data with custom tag handling, |
Any | TVerify
|
typed according to verify_type if provided |
Example
yaml_parser = YAMLParser()
@yaml_parser.register("config")
def handle_config(data: dict) -> Config:
return Config(**data)
# Without type verification
data = yaml_parser.load_yaml_file(
"config.yml",
resolve_inherit=True,
include_base_path="configs/"
)
# With type verification
config = yaml_parser.load_yaml_file(
"config.yml",
resolve_inherit=True,
verify_type=dict
)
Source code in src/configz/yaml_tools/yamlparser.py
process_tag
¶
Process data with the registered handler for the given tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
str
|
Full tag name (including prefix) |
required |
data
|
Any
|
Data to be processed |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no handler is registered for the tag |
Source code in src/configz/yaml_tools/yamlparser.py
register
¶
Decorator to register a new tag handler or class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag_name
|
str | None
|
Optional name of the tag. If None and decorating a class, the lowercase class name will be used. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[T], T]
|
Decorator function that registers the handler or the original class |
Usage
Function registration¶
@yaml_parser.register("person") def handle_person(data: dict) -> Person: return Person(**data)
Class registration¶
@yaml_parser.register() # will use "person" as tag class Person: def init(self, name: str, age: int): self.name = name self.age = age
Source code in src/configz/yaml_tools/yamlparser.py
register_class
¶
Register a class as a tag handler.
The class will be instantiated with the YAML data as kwargs. If no tag_name is provided, the lowercase class name will be used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
The class to register |
required |
tag_name
|
str | None
|
Optional custom tag name. If None, lowercase class name is used |
None
|
Example
@dataclass class Person: name: str age: int
Using class name as tag (will use "person" as tag name)¶
yaml_parser.register_class(Person)
Using custom tag name¶
yaml_parser.register_class(Person, "user")
Source code in src/configz/yaml_tools/yamlparser.py
register_handler
¶
Explicitly register a tag handler function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag_name
|
str
|
Name of the tag without prefix |
required |
handler
|
HandlerFunc[Any]
|
Function that processes the tagged data |
required |
Source code in src/configz/yaml_tools/yamlparser.py
register_with_loader
¶
Register all tags with a YAML loader class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader_class
|
LoaderType | None
|
The YAML loader class to register with |
None
|
Source code in src/configz/yaml_tools/yamlparser.py
dump
¶
Dump data to a string in the specified format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Data structure to dump (also accepts pydantic v2 models and dataclasses) |
required |
mode
|
SupportedFormats
|
Format to dump the data in ("yaml", "toml", "json", or "ini") |
required |
**kwargs
|
Any
|
Additional keyword arguments passed to the underlying dump functions |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
String containing the formatted data |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not supported |
DumpingError
|
If the data cannot be dumped in the specified format |
Source code in src/configz/dump_universal.py
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 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 | |
dump_file
¶
dump_file(
data: Any,
path: JoinablePathLike,
mode: FormatType = "auto",
overwrite: bool = False,
create_dirs: bool = False,
**kwargs: Any,
) -> None
Dump data to a file, automatically detecting the format from extension if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Data structure to dump |
required |
path
|
JoinablePathLike
|
Path to the file to write |
required |
mode
|
FormatType
|
Format to write the file in ("yaml", "toml", "json", "ini" or "auto") |
'auto'
|
overwrite
|
bool
|
Whether to overwrite the file if it already exists |
False
|
create_dirs
|
bool
|
Whether to create parent directories if they don't exist |
False
|
**kwargs
|
Any
|
Additional keyword arguments passed to the underlying dump functions |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format cannot be determined or is not supported |
DumpingError
|
If the data cannot be dumped or the file cannot be written |
Source code in src/configz/dump_universal.py
dump_json
¶
dump_json(
data: Any,
indent: bool = False,
naive_utc: bool = False,
serialize_numpy: bool = False,
sort_keys: bool = False,
backend: BackendType = "auto",
) -> str
Serialize data to a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The data to serialize |
required |
indent
|
bool
|
Whether to format the output with indentation |
False
|
naive_utc
|
bool
|
Whether to interpret naive datetime objects as UTC |
False
|
serialize_numpy
|
bool
|
Whether to serialize numpy arrays |
False
|
sort_keys
|
bool
|
Sort dictionary keys |
False
|
backend
|
BackendType
|
JSON backend to use for serialization |
'auto'
|
Returns:
| Type | Description |
|---|---|
str
|
The serialized JSON string |
Raises:
| Type | Description |
|---|---|
JsonDumpError
|
If serialization fails |
Source code in src/configz/json_tools/__init__.py
dump_toml
¶
Serialize data to a TOML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The data to serialize |
required |
pretty
|
bool
|
Whether to format the output with pretty formatting |
False
|
backend
|
BackendType
|
TOML backend to use for serialization |
'auto'
|
Returns:
| Type | Description |
|---|---|
str
|
The serialized TOML string |
Raises:
| Type | Description |
|---|---|
TomlDumpError
|
If serialization fails |
Source code in src/configz/toml_tools/__init__.py
dump_yaml
¶
dump_yaml(
obj: Any,
class_mappings: dict[type, type] | None = None,
indent: int | None = None,
default_flow_style: bool | None = None,
allow_unicode: bool | None = None,
**kwargs: Any,
) -> str
Dump a data structure to a YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to serialize (also accepts pydantic models) |
required |
class_mappings
|
dict[type, type] | None
|
Dict mapping classes to built-in types for YAML representation |
None
|
indent
|
int | None
|
Indentation level for YAML output |
None
|
default_flow_style
|
bool | None
|
Whether to use flow style for YAML output |
None
|
allow_unicode
|
bool | None
|
Whether to allow unicode characters in YAML output |
None
|
kwargs
|
Any
|
Additional arguments for yaml.dump |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
YAML string representation |
Source code in src/configz/yaml_tools/yaml_dumpers.py
get_loader
¶
get_loader(
base_loader_cls: LoaderType,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
enable_include: bool = True,
enable_env: bool = True,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
) -> LoaderType
Construct an enhanced YAML loader with optional support for !env and !include tags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_loader_cls
|
LoaderType
|
Base loader class to extend |
required |
include_base_path
|
JoinablePathLike | AbstractFileSystem | None
|
Base path for !include tag resolution. If None, use cwd. |
None
|
enable_include
|
bool
|
Whether to enable !include tag support |
True
|
enable_env
|
bool
|
Whether to enable !ENV tag support |
True
|
resolve_strings
|
bool
|
Whether to resolve strings using MiniJinja |
False
|
resolve_dict_keys
|
bool
|
Whether to resolve dictionary keys using MiniJinja |
False
|
variables
|
dict[str, Any] | None
|
An optional dictionary to resolving !var tags |
None
|
jinja_env
|
Environment | None
|
Optional MiniJinja environment for template resolution |
None
|
Returns:
| Type | Description |
|---|---|
LoaderType
|
Enhanced loader class |
Source code in src/configz/yaml_tools/yaml_loaders.py
load
¶
load(
text: str,
mode: SupportedFormats,
verify_type: type[T] | None = None,
resolve_inherit: bool | JoinablePathLike = False,
resolve_env_vars: bool = False,
inherit_from: list[str] | str | None = None,
**kwargs: Any,
) -> Any | T
Load data from a string in the specified format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String containing data in the specified format |
required |
mode
|
SupportedFormats
|
Format of the input data ("yaml", "toml", "json", or "ini") |
required |
verify_type
|
type[T] | None
|
Type to verify and cast the output to (supports TypedDict) |
None
|
resolve_inherit
|
bool | JoinablePathLike
|
Whether to resolve inheritance in the loaded data |
False
|
resolve_env_vars
|
bool
|
Whether to resolve {env:VAR_NAME} or {env:VAR_NAME:default} patterns |
False
|
inherit_from
|
list[str] | str | None
|
Additional paths to inherit from. These form the base layer, meaning explicit INHERIT directives in the file take precedence. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to the underlying load functions |
{}
|
Returns:
| Type | Description |
|---|---|
Any | T
|
Parsed data structure, typed according to verify_type if provided |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not supported |
ParsingError
|
If the text cannot be parsed in the specified format |
TypeError
|
If verify_type is provided and the loaded data doesn't match |
Example
Source code in src/configz/load_universal.py
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 | |
load_file
¶
load_file(
path: JoinablePathLike,
mode: FormatType = "auto",
*,
storage_options: dict[str, Any] | None = None,
verify_type: type[T] | None = None,
resolve_inherit: bool = True,
resolve_env_vars: bool = False,
inherit_from: list[str] | str | None = None,
) -> Any | T
Load data from a file, automatically detecting the format from extension if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
JoinablePathLike
|
Path to the file to load |
required |
mode
|
FormatType
|
Format of the file ("yaml", "toml", "json", "ini" or "auto") |
'auto'
|
storage_options
|
dict[str, Any] | None
|
Additional keyword arguments to pass to the fsspec backend |
None
|
verify_type
|
type[T] | None
|
Type to verify and cast the output to (supports TypedDict) |
None
|
resolve_inherit
|
bool
|
Whether to resolve inheritance in the loaded data |
True
|
resolve_env_vars
|
bool
|
Whether to resolve {env:VAR_NAME} or {env:VAR_NAME:default} patterns |
False
|
inherit_from
|
list[str] | str | None
|
Additional paths to inherit from. These form the base layer, meaning explicit INHERIT directives in the file take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
Any | T
|
Parsed data structure, typed according to verify_type if provided |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format cannot be determined or is not supported |
OSError
|
If the file cannot be read |
FileNotFoundError
|
If the file does not exist |
PermissionError
|
If file permissions prevent reading |
ParsingError
|
If the text cannot be parsed in the specified format |
TypeError
|
If verify_type is provided and the loaded data doesn't match |
Example
Source code in src/configz/load_universal.py
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 | |
load_json
¶
load_json(
data: str | bytes | TextIOWrapper,
return_type: type[T] | None = None,
backend: BackendType = "auto",
) -> T
Load JSON data with optional type validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str | bytes | TextIOWrapper
|
The JSON data to parse (string, bytes, or file-like object) |
required |
return_type
|
type[T] | None
|
Optional type to validate the data against |
None
|
backend
|
BackendType
|
JSON backend to use for parsing |
'auto'
|
Returns:
| Type | Description |
|---|---|
T
|
The parsed and validated data |
Raises:
| Type | Description |
|---|---|
JsonLoadError
|
If parsing fails |
TypeError
|
If validation against return_type fails |
Source code in src/configz/json_tools/__init__.py
load_toml
¶
load_toml(
data: str | bytes | TextIOWrapper | Path,
return_type: type[T] | None = None,
backend: BackendType = "auto",
) -> T
Load TOML data with optional type validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str | bytes | TextIOWrapper | Path
|
The TOML data to parse (string, bytes, file-like object, or Path) |
required |
return_type
|
type[T] | None
|
Optional type to validate the data against |
None
|
backend
|
BackendType
|
TOML backend to use for parsing |
'auto'
|
Returns:
| Type | Description |
|---|---|
T
|
The parsed and validated data |
Raises:
| Type | Description |
|---|---|
TomlLoadError
|
If parsing fails |
TypeError
|
If validation against return_type fails |
Source code in src/configz/toml_tools/__init__.py
load_yaml
¶
load_yaml(
text: YAMLInput,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool | JoinablePathLike = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: None = None,
enable_env: bool = True,
) -> Any
load_yaml(
text: YAMLInput,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool | JoinablePathLike = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: type[T],
enable_env: bool = True,
) -> T
load_yaml(
text: YAMLInput,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool | JoinablePathLike = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: type[T] | None = None,
enable_env: bool = True,
) -> T | Any
load_yaml(
text: YAMLInput,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
resolve_inherit: bool | JoinablePathLike = False,
variables: dict[str, Any] | None = None,
jinja_env: Environment | None = None,
verify_type: type[T] | None = None,
enable_env: bool = True,
) -> Any | T
Load a YAML string with specified safety mode and include path support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
YAMLInput
|
The YAML content to load |
required |
mode
|
LoaderStr | LoaderType
|
YAML loader safety mode ('unsafe', 'full', or 'safe') Custom YAML loader classes are also accepted. |
'unsafe'
|
include_base_path
|
JoinablePathLike | AbstractFileSystem | None
|
Base path for resolving !include directives |
None
|
resolve_strings
|
bool
|
Whether to resolve MiniJinja template strings |
False
|
resolve_dict_keys
|
bool
|
Whether to resolve MiniJinja templates in dictionary keys |
False
|
resolve_inherit
|
bool | JoinablePathLike
|
Whether to resolve INHERIT directives If True, then requires IO object with name attribute for text If str or PathLike, its interpreted as the base path |
False
|
jinja_env
|
Environment | None
|
Optional MiniJinja environment for template resolution |
None
|
variables
|
dict[str, Any] | None
|
An optional dictionary to resolving !var tags |
None
|
verify_type
|
type[T] | None
|
Type to verify and cast the output to (supports TypedDict) |
None
|
enable_env
|
bool
|
Whether to enable the !ENV tag |
True
|
Returns:
| Type | Description |
|---|---|
Any | T
|
The parsed YAML data, typed according to verify_type if provided |
Example
# Simple YAML loading
data = load_yaml("key: value")
# With TypedDict verification
from typing import TypedDict
class Config(TypedDict):
host: str
port: int
config = load_yaml('''
host: localhost
port: 8080
''', verify_type=Config)
# With MiniJinja template resolution
from minijinja import Environment
env = Environment(globals={"name": "World"})
data = load_yaml(
"message: Hello {{ name }}!",
resolve_strings=True,
jinja_env=env
)
Source code in src/configz/yaml_tools/yaml_loaders.py
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 | |
load_yaml_file
¶
load_yaml_file(
path: JoinablePathLike,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: None = None,
enable_env: bool = True,
inherit_from: list[str] | str | None = None,
) -> Any
load_yaml_file(
path: JoinablePathLike,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: type[T],
enable_env: bool = True,
inherit_from: list[str] | str | None = None,
) -> T
load_yaml_file(
path: JoinablePathLike,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: type[T] | None = None,
enable_env: bool = True,
inherit_from: list[str] | str | None = None,
) -> T | Any
load_yaml_file(
path: JoinablePathLike,
mode: LoaderStr | LoaderType = "unsafe",
*,
include_base_path: JoinablePathLike | AbstractFileSystem | None = None,
resolve_inherit: bool = False,
resolve_strings: bool = False,
resolve_dict_keys: bool = False,
jinja_env: Environment | None = None,
variables: dict[str, Any] | None = None,
storage_options: dict[str, Any] | None = None,
verify_type: type[T] | None = None,
enable_env: bool = True,
inherit_from: list[str] | str | None = None,
) -> Any | T
Load a YAML file with specified options.
Use verify_type to get proper output type for LSPs and runtime validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
JoinablePathLike
|
Path to the YAML file to load |
required |
mode
|
LoaderStr | LoaderType
|
YAML loader safety mode ('unsafe', 'full', or 'safe'). Custom YAML loader classes are also accepted. |
'unsafe'
|
include_base_path
|
JoinablePathLike | AbstractFileSystem | None
|
Base path for resolving !include directives |
None
|
resolve_inherit
|
bool
|
Whether to resolve INHERIT directives |
False
|
resolve_strings
|
bool
|
Whether to resolve MiniJinja template strings |
False
|
resolve_dict_keys
|
bool
|
Whether to resolve MiniJinja templates in dictionary keys |
False
|
jinja_env
|
Environment | None
|
Optional MiniJinja environment for template resolution |
None
|
variables
|
dict[str, Any] | None
|
An optional dictionary to resolving !var tags |
None
|
storage_options
|
dict[str, Any] | None
|
Additional keywords to pass to fsspec backend |
None
|
verify_type
|
type[T] | None
|
Type to verify and cast the output to (supports TypedDict) |
None
|
enable_env
|
bool
|
Whether to enable the !ENV tag |
True
|
inherit_from
|
list[str] | str | None
|
Additional paths to inherit from. These form the base layer, meaning explicit INHERIT directives in the file take precedence. |
None
|
Returns:
| Type | Description |
|---|---|
Any | T
|
The parsed YAML data |
Example
from typing import TypedDict
class Config(TypedDict):
database: str
port: int
debug: bool
# Load YAML file with inheritance and TypedDict verification
config = load_yaml_file(
"config.yml",
resolve_inherit=True, # Resolve INHERIT directives
include_base_path="configs/", # Base path for includes
verify_type=Config # Verify against TypedDict
)
Source code in src/configz/yaml_tools/yaml_loaders.py
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 | |