flib.io.ini module

File I/O for text files in Microsoft Windows INI file-format.

decode(text: str, scheme: Union[Dict[str, Optional[type]], Dict[str, Dict[str, Optional[type]]], None] = None, autocast: bool = False, flat: Optional[bool] = None) → Union[Dict[str, Optional[type]], Dict[str, Dict[str, Optional[type]]]]

Load configuration dictionary from INI-formated text.

Parameters:
  • text – Text, that describes a configuration in INI-format.
  • scheme – Dictionary of dictionaries, which determines the structure of the configuration dictionary. If scheme is None, the INI-file is completely imported and all values are interpreted as strings. If the scheme is a dictionary of dictionaries, the keys of the outer dictionary describe valid section names by strings, that are interpreted as regular expressions. Therupon, the keys of the respective inner dictionaries describe valid parameter names as strings, that are also interpreted as regular expressions. Finally the values of the inner dictionaries define the type of the parameters by their own type, e.g. str, int, float etc. Accepted types can be found in the documentation of the function literal.decode.
  • autocast – If no scheme is given autocast determines, if the values are automatically converted to types, estimated by the function literal.estimate
  • flat – Determines if the desired INI format structure contains sections or not. By default sections are used, if the first non blank, non comment line in the string identifies a section.
Returns:

Structured configuration dictionary.

encode(config: dict, flat: Optional[bool] = None, comment: Optional[str] = None) → str

Convert configuration dictionary to INI formated string.

Parameters:
  • config – Configuration dictionary
  • flat – Determines if the desired INI format structure contains sections or not. By default sections are used, if the dictionary contains subdictionaries.
  • comment – String containing comment lines, which are stored as initial ‘#’ lines in the INI-file. By default no comment is written.
Returns:

Text with INI-file structure.

get_comment(file: Union[IO[Any], os.PathLike, flib.io.abc.Connector]) → str

Read initial comment lines from INI-file.

Parameters:fileFile reference to a file object. The reference can ether be given as a String or path-like object, that points to a valid entry in the file system, a file accessor or an opened file object in reading or writing mode.
Returns:String containing the initial comment lines of the INI-file or an empty string, if no initial comment lines could be detected.
load(file: Union[IO[Any], os.PathLike, flib.io.abc.Connector], scheme: Union[Dict[str, Optional[type]], Dict[str, Dict[str, Optional[type]]], None] = None, autocast: bool = False, flat: Optional[bool] = None) → Union[Dict[str, Optional[type]], Dict[str, Dict[str, Optional[type]]]]

Import configuration dictionary from INI file.

Parameters:
  • fileFile reference to a file object. The reference can ether be given as a String or path-like object, that points to a valid entry in the file system, a file accessor or an opened file object in reading mode.
  • scheme – Dictionary of dictionaries, which determines the structure of the configuration dictionary. If scheme is None, the INI-file is completely imported and all values are interpreted as strings. If the scheme is a dictionary of dictionaries, the keys of the outer dictionary describe valid section names by strings, that are interpreted as regular expressions. Therupon, the keys of the respective inner dictionaries describe valid parameter names as strings, that are also interpreted as regular expressions. Finally the values of the inner dictionaries define the type of the parameters by their own type, e.g. str, int, float etc. Accepted types can be found in the documentation of the function literal.decode.
  • autocast – If no scheme is given autocast determines, if the values are automatically converted to types, estimated by the function literal.estimate
  • flat – Determines if the desired INI format structure contains sections or not. By default sections are used, if the first non empty, non comment line in the string identifies a section.
Returns:

Structured configuration dictionary

parse(parser: configparser.ConfigParser, scheme: Optional[Dict[str, Dict[str, Optional[type]]]] = None, autocast: bool = False) → Dict[str, Dict[str, Any]]

Import configuration dictionary from INI formated text.

Parameters:
  • parser – ConfigParser instance that contains an unstructured configuration dictionary
  • scheme – Dictionary of dictionaries, which determines the structure of the configuration dictionary. If scheme is None, the INI-file is completely imported and all values are interpreted as strings. If the scheme is a dictionary of dictionaries, the keys of the outer dictionary describe valid section names by strings, that are interpreted as regular expressions. Therupon, the keys of the respective inner dictionaries describe valid parameter names as strings, that are also interpreted as regular expressions. Finally the values of the inner dictionaries define the type of the parameters by their own type, e.g. str, int, float etc. Accepted types can be found in the documentation of the function literal.decode.
  • autocast – If no scheme is given autocast determines, if the values are automatically converted to types, estimated by the function literal.estimate
Returns:

Structured configuration dictionary.

save(config: dict, file: Union[IO[Any], os.PathLike, flib.io.abc.Connector], flat: Optional[bool] = None, comment: Optional[str] = None) → None

Save configuration dictionary to INI-file.

Parameters:
  • config – Configuration dictionary
  • fileFile reference to a file object. The reference can ether be given as a String or path-like object, that points to a valid entry in the file system, a file accessor or an opened file object in writing mode.
  • flat – Determines if the desired INI format structure contains sections. By default sections are used, if the dictionary contains subdictionaries.
  • comment – String containing comment lines, which are stored as initial ‘#’ lines in the INI-file. By default no comment is written.