configtool¶
Main Module¶
Author: Jacek ‘Szumak’ Kotlarski –<szumak@virthost.pl> Created: 29.10.2023
Purpose: Main class for creating and processes config files.
- class jsktoolbox.configtool.main.Config(filename, main_section_name, auto_create=False)[source]¶
Bases:
BData,NoDynamicAttributesHigh-level configuration manager combining data and file processors.
### Purpose: Loads, modifies, and saves INI-like configuration files.
- __init__(filename, main_section_name, auto_create=False)[source]¶
Initialise Config object.
### Arguments: * filename: str - Path to configuration file. * main_section_name: str - Name of the primary section. * auto_create: bool - Create file on demand when True.
- property file_exists: bool¶
Check if file exists.
### Returns: [bool] - True when configuration file exists.
- load()[source]¶
Load config file to DataProcessor.
### Returns: [bool] - True when at least one line was processed.
- Return type:
- save()[source]¶
Save config file from DataProcessor.
### Returns: [bool] - True after successful write.
- Return type:
- get(section, varname=None, desc=False)[source]¶
Get and return data.
### Arguments: * section: str - Section name. * varname: Optional[str] - Variable name. * desc: bool - Whether to fetch description.
### Returns: [Any] - Value or description depending on desc.
- set(section, varname=None, value=None, desc=None)[source]¶
Set data.
### Arguments: * section: str - Section name. * varname: Optional[str] - Variable name. * value: Optional[Any] - Value payload. * desc: Optional[str] - Description text.
- has_section(section)[source]¶
Check section name in config file.
### Arguments: * section: str - Section name to validate.
### Returns: [bool] - True when section exists.
### Raises: * TypeError: Section name is not a string.
- has_varname(section_name, varname)[source]¶
Check varname in section.
### Arguments: * section_name: str - Section name. * varname: str - Variable name.
### Returns: [bool] - True when variable exists.
### Raises: * TypeError: Variable name is not a string.
Configuration Data¶
Author: Jacek ‘Szumak’ Kotlarski –<szumak@virthost.pl> Created: 29.10.2023
Purpose: DataProcessor class for processing dataset operations.
- class jsktoolbox.configtool.libs.data.IModel[source]¶
Bases:
ABCModel class interface.
- abstract property dump: List[str] | VariableModel¶
Dump data.
### Returns: Union[List[str], VariableModel] - Dumped data as list of strings or model object.
- class jsktoolbox.configtool.libs.data.VariableModel(name=None, value=None, desc=None)[source]¶
Bases:
BData,IModel,NoDynamicAttributesRepresentation of a configuration variable entry.
### Purpose: Stores name, value, and description for a single configuration variable.
- __init__(name=None, value=None, desc=None)[source]¶
Initialise a variable record.
### Arguments: * name: Optional[str] - Variable identifier. * value: Optional[Union[str, int, float, bool, List]] - Payload to store. * desc: Optional[str] - Optional human-friendly description.
- __repr__()[source]¶
Return representation class string.
### Returns: [str] - Debug-friendly section representation.
- Return type:
- __str__()[source]¶
Return formatted section header.
### Returns: [str] - String in INI-style header format.
- Return type:
- property desc: str | None¶
Get description property.
### Returns: [Optional[str]] - Description string or None.
- property dump: VariableModel¶
Dump data.
### Returns: [List[Any]] - Shallow copy of section with variables.
- parser(value)[source]¶
Parse raw string input ensuring valid state.
### Arguments: * value: str - Raw value string.
### Raises: * ValueError: Raised when the parsed value is empty after trimming.
- Parameters:
value (str)
- Return type:
None
- search(name)[source]¶
Search method.
### Arguments: * name: str - Section name to match.
### Returns: [bool] - True when names coincide.
- class jsktoolbox.configtool.libs.data.SectionModel(name=None)[source]¶
Bases:
BData,IModel,NoDynamicAttributesRepresentation of a configuration section.
### Purpose: Aggregates variables and descriptions under a named section.
- Parameters:
name (str | None)
- __init__(name=None)[source]¶
Initialise section state.
### Arguments: * name: Optional[str] - Optional section header value.
- Parameters:
name (str | None)
- Return type:
None
- __repr__()[source]¶
Return representation class string.
### Returns: [str] - Debug-friendly section representation.
- Return type:
- __str__()[source]¶
Return formatted section header.
### Returns: [str] - String in INI-style header format.
- Return type:
- property dump: List[Any]¶
Dump data.
### Returns: [List[Any]] - Shallow copy of section with variables.
- parser(value)[source]¶
Parse and validate section name.
### Arguments: * value: Optional[str] - Raw header text.
### Raises: * ValueError: Raised when the name resolves to an empty string.
- Parameters:
value (str | None)
- Return type:
None
- search(name)[source]¶
Search method.
### Arguments: * name: str - Section name to match.
### Returns: [bool] - True when names coincide.
- get_variable(name)[source]¶
Search and return VariableModel if exists.
### Arguments: * name: str - Variable name to locate.
### Returns: [Optional[VariableModel]] - Matching variable instance or None.
- Parameters:
name (str)
- Return type:
VariableModel | None
- set_variable(name=None, value=None, desc=None)[source]¶
Add or update VariableModel.
### Arguments: * name: Optional[str] - Variable name to set or update. * value: Optional[Any] - Value payload. * desc: Optional[str] - Description text.
### Raises: * ValueError: Raised when both name and value are None.
- property variables: List[VariableModel]¶
Return list of VariableModel.
### Returns: [List[VariableModel]] - Mutable list of section variables.
- class jsktoolbox.configtool.libs.data.DataProcessor[source]¶
Bases:
BData,NoDynamicAttributesManage configuration data composed of sections and variables.
### Purpose: Provides helpers for reading, setting, and dumping configuration structures.
- __init__()[source]¶
Initialise DataProcessor state.
### Purpose: Ensures the internal container for sections exists.
- Return type:
None
- __setattr__(name, value)¶
Prevent dynamic attribute assignment on instances.
### Raises: * AttributeError: Attribute not previously declared.
- property main_section: str | None¶
Return main section name.
### Returns: [Optional[str]] - Name of the primary section.
- property sections: Tuple¶
Return sections keys tuple.
### Returns: [Tuple] - Tuple containing names of tracked sections.
- add_section(name)[source]¶
Add section object to dataset.
### Arguments: * name: str - Raw section name.
### Returns: [str] - Sanitised section name actually stored.
- get_section(name)[source]¶
Get section object if exists.
### Arguments: * name: str - Section name.
### Returns: [Optional[SectionModel]] - Matching section or None.
- Parameters:
name (str)
- Return type:
SectionModel | None
- set(section, varname=None, value=None, desc=None)[source]¶
Set data to [SectionModel]->[VariableModel].
### Arguments: * section: str - Section name. * varname: Optional[str] - Variable name. * value: Optional[Any] - Value payload. * desc: Optional[str] - Description text.
- get(section, varname=None, desc=False)[source]¶
Return value.
### Arguments: * section: str - Section name. * varname: Optional[str] - Variable name to fetch. * desc: bool - When True returns description.
### Returns: [Optional[Any]] - Value or description based on desc.
### Raises: * KeyError: Section not present.
File Operations¶
Author: Jacek ‘Szumak’ Kotlarski –<szumak@virthost.pl> Created: 29.10.2023
Purpose: Class for creating and processes config files.
- class jsktoolbox.configtool.libs.file.FileProcessor[source]¶
Bases:
BData,NoDynamicAttributesHandle filesystem interactions for configuration files.
### Purpose: Offers helpers to validate paths, read, and persist config data.
- __init__()[source]¶
Initialise FileProcessor instance.
The constructor does not perform extra work but keeps parity with other mixins.
- Return type:
None
- property file: str | None¶
Return config file path.
### Returns: [Optional[str]] - Absolute path or None when not set.
- property file_exists: bool¶
Check if the file exists and is a file.
### Returns: [bool] - True when the file exists and is not a directory.
### Raises: * AttributeError: Path not configured prior to call.
- file_create()[source]¶
Try to create file.
### Returns: [bool] - True when file already exists or was created.
### Raises: * AttributeError: Path not configured. * OSError: Path points to an existing directory.
- Return type:
- read()[source]¶
Try to read config file.
### Returns: [str] - Entire file contents as a string.
- Return type:
- readlines()[source]¶
Try to read config file and create list of strings.
### Returns: [List[str]] - List of lines stripped from end markers.
- __setattr__(name, value)¶
Prevent dynamic attribute assignment on instances.
### Raises: * AttributeError: Attribute not previously declared.