Add support for applying mutability on all sections of an INI configuration

This commit is contained in:
2026-03-15 23:37:05 +01:00
parent e8e5bddf51
commit da2e102343
4 changed files with 45 additions and 7 deletions
+3
View File
@@ -84,6 +84,9 @@ For the full documentation, see the Python docstrings.
## Changelog ## Changelog
### Version 0.2.1
- support for applying mutability on all sections of an INI configuration
### Version 0.2.0 ### Version 0.2.0
- support for specifying mutability when parsing a configuration - support for specifying mutability when parsing a configuration
+1 -1
View File
@@ -4,6 +4,6 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "jcloud-config-parser" name = "jcloud-config-parser"
version = "0.2.0" version = "0.2.1"
description = "A configuration file parser." description = "A configuration file parser."
license = "Apache-2.0" license = "Apache-2.0"
+11 -5
View File
@@ -57,10 +57,10 @@ class INIConfiguration(Configuration):
configuration = cls() configuration = cls()
for section_name, content in parse_ini(data, comment_prefixes=comment_prefixes, quotation_marks=quotation_marks, ignore_errors=ignore_errors).items(): 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(): for key, value in content.items():
group[key] = value section[key] = value
configuration[section_name] = group configuration[section_name] = section
if default is not None: if default is not None:
for section in default._config: for section in default._config:
@@ -70,6 +70,10 @@ class INIConfiguration(Configuration):
if propk not in dict(configuration[section]).keys(): if propk not in dict(configuration[section]).keys():
configuration[section][propk] = propv configuration[section][propk] = propv
for section in configuration._config.values():
if isinstance(section, INIConfigurationSection):
set_mutability(section, mutable)
set_mutability(configuration, mutable) set_mutability(configuration, mutable)
return configuration return configuration
@@ -88,14 +92,16 @@ class INIConfiguration(Configuration):
return serialize_ini(self._config, separator) return serialize_ini(self._config, separator)
class INIConfigurationSection(INIConfiguration): 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. A class representing a group in an INI configuration.
:param name: The name of the group. :param name: The name of the group.
:type name: str :type name: str
:param mutable: The mutability of the section
:type mutable: bool
''' '''
super().__init__() super().__init__(mutable = mutable)
self._name = name self._name = name
@property @property
+30 -1
View File
@@ -116,4 +116,33 @@ def test_mutability():
pass pass
set_mutability(config, True) set_mutability(config, True)
config.key2 = 'value2' 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'