Config¶
ConfigParameter¶
- class mango.config.ConfigParameter(name: str, value_type: callable, default: int | float | bool | str | List = None, validate: List = None, secondary_type: callable = None, dict_types: Dict[str, callable] = None, required: bool = True, min_value: int | float = None, max_value: int | float = None)¶
Bases:
object
Represents a configuration parameter with validation and type conversion.
This class defines a parameter in a configuration file with INI syntax. Each parameter has a name, value type, default value, validation rules, and optional secondary type for complex data structures.
- Parameters:
name (str) – Name of the configuration parameter
value_type (callable) – Type function for the parameter value (int, float, bool, str, list, dict)
default (Union[int, float, bool, str, List], optional) – Default value if parameter is not found or invalid
validate (List, optional) – List of valid values for the parameter
secondary_type (callable, optional) – Type function for list elements (used when value_type is list)
dict_types (Dict[str, callable], optional) – Dictionary mapping keys to type functions (used when value_type is dict)
required (bool) – Whether the parameter is required
min_value (Union[int, float], optional) – Minimum allowed value for numeric parameters
max_value (Union[int, float], optional) – Maximum allowed value for numeric parameters
- Example:
>>> param = ConfigParameter( ... name="timeout", ... value_type=int, ... default=30, ... min_value=1, ... max_value=300 ... )
- parse(section: str, config_parser: ConfigParser) int | float | bool | str | List ¶
Parse and validate a parameter value from a configuration section.
Extracts the parameter value from the specified section using the ConfigParser, converts it to the appropriate type, and validates it against the defined constraints.
- param section:
Section name in the configuration file
- type section:
str
- param config_parser:
ConfigParser instance containing the configuration data
- type config_parser:
ConfigParser
- return:
Parsed and validated parameter value
- rtype:
Union[int, float, bool, str, List]
- raises ValueError:
If value is outside min/max range or not in validation list
- raises TypeError:
If value type is not supported
- Example:
>>> parser = ConfigParser() >>> parser.read_string('[database]
- port = 5432’)
>>> param = ConfigParameter("port", int, default=3306) >>> value = param.parse("database", parser) >>> print(value) # 5432
BaseConfig¶
- class mango.config.BaseConfig(file_name: str, extend_params: Dict)¶
Bases:
object
Base class for handling configuration files with INI structure.
This class provides a framework for parsing and managing configuration files with INI syntax. Parameters are defined in the class and can include type conversion, validation, and default values.
- Parameters:
file_name (str) – Path to the configuration file
extend_params (Dict) – Additional parameters to extend the base configuration
- Example:
>>> class MyConfig(BaseConfig): ... __params = { ... "database": [ ... ConfigParameter("host", str, default="localhost"), ... ConfigParameter("port", int, default=5432) ... ] ... } >>> config = MyConfig("config.ini", {}) >>> host = config("host")
- modify_value(key, value)¶
Modify the value of a configuration parameter.
Updates the value of an existing parameter after the configuration has been loaded. The parameter must exist in the configuration.
- Parameters:
key (str) – Name of the parameter to modify
value (Any) – New value for the parameter
- Returns:
None
- Raises:
KeyError – If the parameter is not found in the configuration
- Example:
>>> config = MyConfig("config.ini", {}) >>> config.modify_value("database_port", 3306)
- classmethod create_config_template(output_path: str)¶
Create a configuration template file with default values.
Generates a configuration file template based on the defined parameters. Parameters with default values will use those values, while parameters without defaults will be set to empty strings.
- Parameters:
output_path (str) – Path where the template file should be created
- Returns:
None
- Raises:
IOError – If the file cannot be written
- Example:
>>> MyConfig.create_config_template("config_template.ini")