class AsyncIniFileParser(ConfigParser): (source)
Constructor: AsyncIniFileParser(name, default_paths, default_params, supervise_change)
This class handles a program ini file.
It keeps track of changed parameter values during program execution, if needed. Environment variables and secrets are expanded. Parameter values are accessed using dot notation.
If INI file validation fails during program startup, the program stops.
If an INI file change is detected when the program is running and the validation fails, the content is rejected and the old file content is restored, whilst the program keeps running.
Note
You should be aware of the consequences of changing values while the program is running. For example, changing path parameters is mostly a bad idea since the input path of this program might be the output path of another program. So changing file paths usually require a coordinated effort that results in changes to several INI files, while the programs are offline.
This goes for connection parameters as well. The program must have been programmed to handle disconnecting from the current connection and create a new connection using the new parameters, otherwise the change will have no effect.
- Expanded parameter ini file parameter syntax:
- @{<secrets-name>}
- &{<environment-variable-name>}
- ${<extended-interpolation-reference>}
- There are three methods that are designed to be overridden when needed:
- _add_default_parameters()
- _add_platform_parameters()
- _add_section_parameters()
That can be useful when you are referencing external values (in relation to the INI file content) in the ini file.
The way to do that is to use vars like this:
# In a *_ini_core.py file class AsyncDeployIni(AsyncIniFileParser): def __init__(self, name: str, dest_server: str): self.var_params = {'server': dest_server} def _add_platform_parameters(self) -> dict: # Add external parameter(s) to the parameter interpolation. return dict(self.items(PLATFORM, vars=self.var_params))
And the INI file can look something like this:
[DEFAULT]
passwd: @{environment_prod_pwd}
[win32]
root_path: /export
local_path: ${root_path}/Prod/Pgm
dest_path: //${server}/Prod/Pgm| Method | __getattr__ |
Dynamically return the value for the specified parameter. |
| Method | __init__ |
Initialize IniFileHandler class attributes. |
| Async Method | read |
Read the ini file and parse existing parameters. |
| Async Method | start |
Start the used resources in a controlled way. |
| Async Method | stop |
Stop the used resources in a controlled way. |
| Async Method | validate |
Read ini file, check existence of required parameters and validate their types. |
| Method | validate |
Extract and validate ini file parameters. |
| Instance Variable | default |
Default parameters defined. |
| Instance Variable | default |
Default paths defined. |
| Instance Variable | error |
Validation errors. |
| Instance Variable | file |
Timestamp for last ini file modification. |
| Instance Variable | filename |
Name if ini file. |
| Instance Variable | orig |
Is original ini file copied status. |
| Instance Variable | status |
Parameter change status when supervision is active. |
| Instance Variable | supervise |
Supervise parameter change when the program is running. |
| Instance Variable | valid |
Validated INI file parameters. |
| Property | file |
Return INI file change status since the last check. |
| Method | _add |
Return DEFAULT parameters. |
| Method | _add |
Return DEFAULT and platform parameters. |
| Method | _add |
Return specified section parameters. |
| Async Method | _handle |
Handle validation error severity. |
| Method | _handle |
Update parameter change status. |
| Async Method | _restore |
Restore original ini file since validation failed. |
| Method | _update |
Update parameter supervision change status. |
| Async Method | _validate |
Validate parameters against a pydantic validation model. |
Read the ini file and parse existing parameters.
Expand environment variables and secrets before parsing starts.
Callable, sections: Optional[ list] = None, read_ini: bool = True):
(source)
¶
Read ini file, check existence of required parameters and validate their types.
Update parameter change status when the value is changed, if required.
| Parameters | |
validationCallable | Pydantic BaseModel. |
sections:Optional[ | List of used section names (except DEFAULT). |
readbool | Read ini file (default is True). |
Return DEFAULT parameters.
This method exists, so it can be overridden in derived classes when needed (for example, when using vars).
| Returns | |
dict | DEFAULT parameters. |
Return DEFAULT and platform parameters.
This method exists, so it can be overridden in derived classes when needed (for example, when using vars).
| Returns | |
dict | DEFAULT and platform parameters. |
Handle validation error severity.
When you get a validation error during program startup, there's no valid original file that you can restore, so the consequence is to trigger a fatal stop by raising a IniValidationError exception.
When the program is running (after starting with a valid INI file), and a faulty INI file change happens, we can restore the valid original INI file and keep the program running after reporting the error by raising a RuntimeError exception.
| Raises | |
IniValidationError | When invalid ini file detected during startup. |
RuntimeError | When invalid ini file detected during running program. |
Update parameter change status.
- params structure:
- {'<param name>':[<value>, False], ...}
| Parameters | |
params:dict | All ini parameters |