Add support for applying mutability on all sections of an INI configuration
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
+1
-1
@@ -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"
|
||||
@@ -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
|
||||
|
||||
+30
-1
@@ -116,4 +116,33 @@ def test_mutability():
|
||||
pass
|
||||
|
||||
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'
|
||||
Reference in New Issue
Block a user