From da2e102343cf5364ecb10357a62656b288481bb5 Mon Sep 17 00:00:00 2001 From: Jakob Scheid Date: Sun, 15 Mar 2026 23:37:05 +0100 Subject: [PATCH] Add support for applying mutability on all sections of an INI configuration --- README.md | 3 +++ pyproject.toml | 2 +- src/jcloud_config_parser/ini.py | 16 +++++++++++----- tests/ini/test_ini.py | 31 ++++++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 62df85c..a5bb933 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,9 @@ For the full documentation, see the Python docstrings. ## Changelog +### Version 0.2.1 +- support for applying mutability on all sections of an INI configuration + ### Version 0.2.0 - support for specifying mutability when parsing a configuration diff --git a/pyproject.toml b/pyproject.toml index 7b3ad2e..767b037 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,6 @@ build-backend = "setuptools.build_meta" [project] name = "jcloud-config-parser" -version = "0.2.0" +version = "0.2.1" description = "A configuration file parser." license = "Apache-2.0" \ No newline at end of file diff --git a/src/jcloud_config_parser/ini.py b/src/jcloud_config_parser/ini.py index e6bad64..8b7ab58 100644 --- a/src/jcloud_config_parser/ini.py +++ b/src/jcloud_config_parser/ini.py @@ -57,10 +57,10 @@ class INIConfiguration(Configuration): configuration = cls() for section_name, content in parse_ini(data, comment_prefixes=comment_prefixes, quotation_marks=quotation_marks, ignore_errors=ignore_errors).items(): - group = INIConfigurationSection(section_name) + section = INIConfigurationSection(section_name) for key, value in content.items(): - group[key] = value - configuration[section_name] = group + section[key] = value + configuration[section_name] = section if default is not None: for section in default._config: @@ -70,6 +70,10 @@ class INIConfiguration(Configuration): if propk not in dict(configuration[section]).keys(): configuration[section][propk] = propv + for section in configuration._config.values(): + if isinstance(section, INIConfigurationSection): + set_mutability(section, mutable) + set_mutability(configuration, mutable) return configuration @@ -88,14 +92,16 @@ class INIConfiguration(Configuration): return serialize_ini(self._config, separator) class INIConfigurationSection(INIConfiguration): - def __init__(self, name: str): + def __init__(self, name: str, mutable: bool = True): ''' A class representing a group in an INI configuration. :param name: The name of the group. :type name: str + :param mutable: The mutability of the section + :type mutable: bool ''' - super().__init__() + super().__init__(mutable = mutable) self._name = name @property diff --git a/tests/ini/test_ini.py b/tests/ini/test_ini.py index 2aead6d..d7a34bd 100644 --- a/tests/ini/test_ini.py +++ b/tests/ini/test_ini.py @@ -116,4 +116,33 @@ def test_mutability(): pass set_mutability(config, True) - config.key2 = 'value2' \ No newline at end of file + config.key2 = 'value2' + + config = INIConfiguration.from_string(configuration, mutable = False) + try: + config.section.key1 = 'value1' + assert False, 'Expected TypeError' + except TypeError: + pass + + set_mutability(config.section, True) + config.section.key2 = 'value2' + + try: + config.section1 = INIConfigurationSection('section1') + assert False, 'Expected TypeError' + except TypeError: + pass + + + config = INIConfiguration() + config.section = INIConfigurationSection('section', mutable = False) + + try: + config.section.key1 = 'value1' + assert False, 'Expected TypeError' + except TypeError: + pass + + set_mutability(config.section, True) + config.section.key1 = 'value1' \ No newline at end of file