# firebird.driver.config

This module defines the configuration system for the firebird-driver. It uses an INI-style format managed via the `DriverConfig` class, which allows defining settings for the driver itself, default server/database parameters, and named configurations for specific servers and databases.

Configuration can be loaded from files, strings, or dictionaries, and supports environment variable interpolation. The primary interaction point is usually the global `driver_config` instance.

## Classes

## `firebird.driver.config.DriverConfig`

Bases: `Config`

Main configuration object for the Firebird driver. Holds global settings, default configurations, and lists of registered servers and databases.

Settings loaded from specific server/database sections override those in the `db_defaults` and `server_defaults` sections. When reading multiple files, options from later files override those from earlier ones.

### `configs: list[Config]`

List of nested `Config` instances associated with this instance.

Includes:

- `Config` instances directly assigned as attributes.
- The `Config` instance held by any `ConfigOption` attribute.
- All `Config` instances within the list held by any `ConfigListOption` attribute.

### `databases: ConfigListOption`

Registered databases

### `db_defaults: DatabaseConfig`

Default database configuration ('firebird.db.defaults')

### `fb_client_library: StrOption`

Path to Firebird client library

### `name: str`

Name associated with Config (default section name).

### `optional: bool`

Whether config is optional (False) or mandatory (True) for configuration file (see `load_config()` for details).

### `options: list[Option]`

List of `Option` instances directly defined as attributes of this `Config` instance.

### `server_defaults: ServerConfig`

Default server configuration ('firebird.server.defaults')

### `servers: ConfigListOption`

Registered servers

### `stream_blob_threshold: IntOption`

BLOB size threshold. Larger BLOBs are streamed automatically; -1 disables this.

### `clear(*, to_default: bool = True) -> None`

Clears all owned options and options in owned sub-configs.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `to_default` | `bool` | If True, sets the option values to defaults, else to None. | `True` |

### `get_config(*, plain: bool = False) -> str`

Returns string containing text lines suitable for use in configuration file processed with configparser.ConfigParser.

> [!IMPORTANT]
>
> When config is optional and the name is an empty string, it returns empty string.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `plain` | `bool` | When True, it outputs only the option values. When False, it includes also option descriptions and other helpful information. | `False` |

### `get_database(name: str) -> DatabaseConfig`

Returns database configuration.

### `get_description() -> str`

Configuration description. Can span multiple lines.

Note: If description is not provided on instance creation, class doc string.

### `get_server(name: str) -> ServerConfig`

Returns server configuration.

### `load_config(config: ConfigParser, section: str | None = None) -> None`

Update configuration values from a `ConfigParser` instance.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `config` | `ConfigParser` | `ConfigParser` instance containing configuration values. | *required* |
| `section` | `str \| None` | Name of the `ConfigParser` section corresponding to this `Config` instance. If `None`, uses `self.name`. | `None` |

**Behavior**

- Reads values for directly owned `Option` instances from the specified `section`.
- Recursively calls `load_config` on directly owned `Config` instances using their respective `name` attribute as the section name.
- Recursively calls `load_config` on `Config` instances referenced by owned `ConfigOption` and `ConfigListOption` values, using the section names stored within those options.

Raises:

| Type | Description |
| --- | --- |
| `Error` | If `section` does not exist in `config` and `self.optional` is `False` (unless `section` is `DEFAULTSECT`). Also wraps underlying ValueError or KeyError from option parsing. |
| `KeyError` | Propagated if an invalid section name is used for a nested config. |
| `ValueError` | Propagated if an option string cannot be parsed correctly. |

### `load_proto(proto: ConfigProto) -> None`

Deserialize value from `ConfigProto` message.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `proto` | `ConfigProto` | Protobuf message that may contains option values and sub-configs. | *required* |

### `read(filenames: str | Iterable, encoding: str | None = None)`

Read configuration from a filename or an iterable of filenames.

Files that cannot be opened are silently ignored; this is designed so that you can specify an iterable of potential configuration file locations (e.g. current directory, user's home directory, systemwide directory), and all existing configuration files in the iterable will be read. A single filename may also be given.

Return list of successfully read files.

### `read_dict(dictionary: dict[str, str]) -> None`

Read configuration from a dictionary.

Keys are section names, values are dictionaries with keys and values that should be present in the section. If the used dictionary type preserves order, sections and their keys will be added in order.

All types held in the dictionary are converted to strings during reading, including section names, option names and keys.

### `read_file(f)`

Read configuration from a file-like object.

The `f` argument must be iterable, returning one line at a time.

### `read_string(string: str) -> None`

Read configuration from a given string.

### `register_database(name: str, config: str | None = None) -> DatabaseConfig`

Register database.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `name` | `str` | Database name. | *required* |
| `config` | `str \| None` | Optional database configuration string in ConfigParser format in \[name\] section. | `None` |

Returns:

| Name | Type | Description |
| --- | --- | --- |
| `DatabaseConfig` | `DatabaseConfig` | For newly registered database |

Raises:

| Type | Description |
| --- | --- |
| `ValueError` | If database is already registered. |

### `register_server(name: str, config: str | None = None) -> ServerConfig`

Register server.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `name` | `str` | Server name. | *required* |
| `config` | `str \| None` | Optional server configuration string in ConfigParser format in \[name\] section. | `None` |

Returns:

| Name | Type | Description |
| --- | --- | --- |
| `ServerConfig` | `ServerConfig` | For newly registered server |

Raises:

| Type | Description |
| --- | --- |
| `ValueError` | If server is already registered. |

### `save_proto(proto: ConfigProto) -> None`

Serialize value into `ConfigProto` message.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `proto` | `ConfigProto` | Protobuf message where option values and sub-configs should be stored. | *required* |

### `validate() -> None`

Recursively validates all directly owned options and sub-configs.

**Checks whether**

- all required options have a non-`None` value.
- required `ConfigOption` values have a non-empty section name.
- required `ConfigListOption` values have a non-empty list.
- all options are defined as instance attributes with the same name as `option.name`.
- calls `validate()` on all nested `Config` instances (direct attributes, values of `ConfigOption`, and items in `ConfigListOption`).

Raises:

| Type | Description |
| --- | --- |
| `Error` | When any validation constraint is violated. |

## `firebird.driver.config.ServerConfig`

Bases: `Config`

Represents configuration options specific to a named Firebird server entry.

### `auth_plugin_list: StrOption`

List of authentication plugins override

### `config: StrOption`

Configuration override

### `configs: list[Config]`

List of nested `Config` instances associated with this instance.

Includes:

- `Config` instances directly assigned as attributes.
- The `Config` instance held by any `ConfigOption` attribute.
- All `Config` instances within the list held by any `ConfigListOption` attribute.

### `encoding: StrOption`

Encoding used for text data exchange with server

### `encoding_errors: StrOption`

Handler used for encoding errors. See `codecs error handlers <codecs>` for details.

### `host: StrOption`

Server host machine specification

### `name: str`

Name associated with Config (default section name).

### `optional: bool`

Whether config is optional (False) or mandatory (True) for configuration file (see `load_config()` for details).

### `options: list[Option]`

List of `Option` instances directly defined as attributes of this `Config` instance.

### `password: StrOption`

Default user password, default is envar ISC\_PASSWORD or None if not specified

### `port: StrOption`

Port used by Firebird server

### `trusted_auth: BoolOption`

Use trusted authentication, default: False

### `user: StrOption`

Defaul user name, default is envar ISC\_USER or None if not specified

### `clear(*, to_default: bool = True) -> None`

Clears all owned options and options in owned sub-configs.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `to_default` | `bool` | If True, sets the option values to defaults, else to None. | `True` |

### `get_config(*, plain: bool = False) -> str`

Returns string containing text lines suitable for use in configuration file processed with configparser.ConfigParser.

> [!IMPORTANT]
>
> When config is optional and the name is an empty string, it returns empty string.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `plain` | `bool` | When True, it outputs only the option values. When False, it includes also option descriptions and other helpful information. | `False` |

### `get_description() -> str`

Configuration description. Can span multiple lines.

Note: If description is not provided on instance creation, class doc string.

### `load_config(config: ConfigParser, section: str | None = None) -> None`

Update configuration values from a `ConfigParser` instance.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `config` | `ConfigParser` | `ConfigParser` instance containing configuration values. | *required* |
| `section` | `str \| None` | Name of the `ConfigParser` section corresponding to this `Config` instance. If `None`, uses `self.name`. | `None` |

**Behavior**

- Reads values for directly owned `Option` instances from the specified `section`.
- Recursively calls `load_config` on directly owned `Config` instances using their respective `name` attribute as the section name.
- Recursively calls `load_config` on `Config` instances referenced by owned `ConfigOption` and `ConfigListOption` values, using the section names stored within those options.

Raises:

| Type | Description |
| --- | --- |
| `Error` | If `section` does not exist in `config` and `self.optional` is `False` (unless `section` is `DEFAULTSECT`). Also wraps underlying ValueError or KeyError from option parsing. |
| `KeyError` | Propagated if an invalid section name is used for a nested config. |
| `ValueError` | Propagated if an option string cannot be parsed correctly. |

### `load_proto(proto: ConfigProto) -> None`

Deserialize value from `ConfigProto` message.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `proto` | `ConfigProto` | Protobuf message that may contains option values and sub-configs. | *required* |

### `save_proto(proto: ConfigProto) -> None`

Serialize value into `ConfigProto` message.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `proto` | `ConfigProto` | Protobuf message where option values and sub-configs should be stored. | *required* |

### `validate() -> None`

Recursively validates all directly owned options and sub-configs.

**Checks whether**

- all required options have a non-`None` value.
- required `ConfigOption` values have a non-empty section name.
- required `ConfigListOption` values have a non-empty list.
- all options are defined as instance attributes with the same name as `option.name`.
- calls `validate()` on all nested `Config` instances (direct attributes, values of `ConfigOption`, and items in `ConfigListOption`).

Raises:

| Type | Description |
| --- | --- |
| `Error` | When any validation constraint is violated. |

## `firebird.driver.config.DatabaseConfig`

Bases: `Config`

Represents configuration options specific to a named Firebird database entry, including connection and creation parameters.

### `auth_plugin_list: StrOption`

List of authentication plugins override

### `cache_size: IntOption`

Page cache size override for database connection

### `charset: StrOption`

Character set for database connection

### `config: StrOption`

Configuration override

### `configs: list[Config]`

List of nested `Config` instances associated with this instance.

Includes:

- `Config` instances directly assigned as attributes.
- The `Config` instance held by any `ConfigOption` attribute.
- All `Config` instances within the list held by any `ConfigListOption` attribute.

### `database: StrOption`

Database file specification or alias

### `db_cache_size: IntOption`

Database create option. Page cache size override for database.

### `db_charset: StrOption`

Database create option. Character set for the database.

### `db_sql_dialect: IntOption`

Database create option. SQL dialect for the database.

### `decfloat_round: EnumOption`

Set DECFLOAT ROUND \[Firebird 4\], value is `.DecfloatRound`

### `decfloat_traps: ListOption`

Specifies which DECFLOAT exceptional conditions should cause a trap (raise an error) \[Firebird 4\]. Accepts a list of .DecfloatTraps enum members.

### `dsn: StrOption`

Database connection string

### `dummy_packet_interval: IntOption`

Dummy packet interval for this database connection

### `forced_writes: BoolOption`

Database create option. Write mode (True = sync/False = async).

### `name: str`

Name associated with Config (default section name).

### `no_linger: BoolOption`

Do not use linger for database connection

### `optional: bool`

Whether config is optional (False) or mandatory (True) for configuration file (see `load_config()` for details).

### `options: list[Option]`

List of `Option` instances directly defined as attributes of this `Config` instance.

### `page_size: IntOption`

Database create option. Page size to be used.

### `parallel_workers`

Number of parallel workers

### `password: StrOption`

Default user password, default is envar ISC\_PASSWORD or None if not specified

### `protocol: EnumOption`

Network protocol to use for the connection. Value must be a member of the .NetProtocol enum.

### `reserve_space: BoolOption`

Database create option. Data page space usage (True = reserve space, False = Use all space).

### `role: StrOption`

User role

### `server: StrOption`

Name of server where database is located

### `session_time_zone: StrOption`

Session time zone \[Firebird 4\]

### `set_bind: StrOption`

Set BIND \[Firebird 4\]

### `sql_dialect: IntOption`

SQL Dialect for database connection, default: 3

### `sweep_interval: IntOption`

Database create option. Sweep interval for the database.

### `timeout: IntOption`

Connection timeout

### `trusted_auth: BoolOption`

Use trusted authentication, default: False

### `user: StrOption`

Defaul user name, default is envar ISC\_USER or None if not specified

### `utf8filename: BoolOption`

Specifies whether the database parameter (filename) is encoded in UTF-8 when passed to the server.

### `clear(*, to_default: bool = True) -> None`

Clears all owned options and options in owned sub-configs.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `to_default` | `bool` | If True, sets the option values to defaults, else to None. | `True` |

### `get_config(*, plain: bool = False) -> str`

Returns string containing text lines suitable for use in configuration file processed with configparser.ConfigParser.

> [!IMPORTANT]
>
> When config is optional and the name is an empty string, it returns empty string.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `plain` | `bool` | When True, it outputs only the option values. When False, it includes also option descriptions and other helpful information. | `False` |

### `get_description() -> str`

Configuration description. Can span multiple lines.

Note: If description is not provided on instance creation, class doc string.

### `load_config(config: ConfigParser, section: str | None = None) -> None`

Update configuration values from a `ConfigParser` instance.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `config` | `ConfigParser` | `ConfigParser` instance containing configuration values. | *required* |
| `section` | `str \| None` | Name of the `ConfigParser` section corresponding to this `Config` instance. If `None`, uses `self.name`. | `None` |

**Behavior**

- Reads values for directly owned `Option` instances from the specified `section`.
- Recursively calls `load_config` on directly owned `Config` instances using their respective `name` attribute as the section name.
- Recursively calls `load_config` on `Config` instances referenced by owned `ConfigOption` and `ConfigListOption` values, using the section names stored within those options.

Raises:

| Type | Description |
| --- | --- |
| `Error` | If `section` does not exist in `config` and `self.optional` is `False` (unless `section` is `DEFAULTSECT`). Also wraps underlying ValueError or KeyError from option parsing. |
| `KeyError` | Propagated if an invalid section name is used for a nested config. |
| `ValueError` | Propagated if an option string cannot be parsed correctly. |

### `load_proto(proto: ConfigProto) -> None`

Deserialize value from `ConfigProto` message.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `proto` | `ConfigProto` | Protobuf message that may contains option values and sub-configs. | *required* |

### `save_proto(proto: ConfigProto) -> None`

Serialize value into `ConfigProto` message.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `proto` | `ConfigProto` | Protobuf message where option values and sub-configs should be stored. | *required* |

### `validate() -> None`

Recursively validates all directly owned options and sub-configs.

**Checks whether**

- all required options have a non-`None` value.
- required `ConfigOption` values have a non-empty section name.
- required `ConfigListOption` values have a non-empty list.
- all options are defined as instance attributes with the same name as `option.name`.
- calls `validate()` on all nested `Config` instances (direct attributes, values of `ConfigOption`, and items in `ConfigListOption`).

Raises:

| Type | Description |
| --- | --- |
| `Error` | When any validation constraint is violated. |

## Globals

## `firebird.driver.config.driver_config: DriverConfig`

Global driver configuration instance. Load settings from files/strings/dicts into this object before connecting, or modify its attributes directly for programmatic configuration.
