Add support for specifying mutability when parsing a configuration
This commit is contained in:
@@ -84,6 +84,9 @@ For the full documentation, see the Python docstrings.
|
|||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
|
### Version 0.2.0
|
||||||
|
- support for specifying mutability when parsing a configuration
|
||||||
|
|
||||||
### Version 0.1.1
|
### Version 0.1.1
|
||||||
- backward compatibility for Python < 3.12: in Python < 3.12, f-string expression parts could not contain backslashes. The code contained an f-string with backslahes in the expression part. As an alternative to the f-string, normal strings are concatenated now.
|
- backward compatibility for Python < 3.12: in Python < 3.12, f-string expression parts could not contain backslashes. The code contained an f-string with backslahes in the expression part. As an alternative to the f-string, normal strings are concatenated now.
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,6 +4,6 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "jcloud-config-parser"
|
name = "jcloud-config-parser"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
description = "A configuration file parser."
|
description = "A configuration file parser."
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
@@ -40,7 +40,6 @@ class Configuration:
|
|||||||
raise TypeError('configuration is immutable')
|
raise TypeError('configuration is immutable')
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
def _set(self, name: str, value: str) -> None:
|
def _set(self, name: str, value: str) -> None:
|
||||||
@self._mutate
|
@self._mutate
|
||||||
def _set(name: str, value: str) -> None:
|
def _set(name: str, value: str) -> None:
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import typing
|
import typing
|
||||||
from ._configuration import Configuration
|
from ._configuration import Configuration, set_mutability
|
||||||
from .parse.ini import _COMMENT_PREFIXES, _QUOTATION_MARKS, parse_ini
|
from .parse.ini import _COMMENT_PREFIXES, _QUOTATION_MARKS, parse_ini
|
||||||
from .serialize.ini import serialize as serialize_ini
|
from .serialize.ini import serialize as serialize_ini
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ class INIConfiguration(Configuration):
|
|||||||
return iter({k: dict(v) if isinstance(v, INIConfigurationSection) else v for k, v in self._config.items()}.items())
|
return iter({k: dict(v) if isinstance(v, INIConfigurationSection) else v for k, v in self._config.items()}.items())
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_string(cls, data: str | bytes, comment_prefixes: typing.Collection[str] = _COMMENT_PREFIXES, quotation_marks: typing.Collection[str] = _QUOTATION_MARKS, ignore_errors: bool = False, default: INIConfiguration = None):
|
def from_string(cls, data: str | bytes, comment_prefixes: typing.Collection[str] = _COMMENT_PREFIXES, quotation_marks: typing.Collection[str] = _QUOTATION_MARKS, ignore_errors: bool = False, default: INIConfiguration = None, mutable: bool = True):
|
||||||
'''
|
'''
|
||||||
Parses INI configuration from a string and returns an instance of INIConfiguration.
|
Parses INI configuration from a string and returns an instance of INIConfiguration.
|
||||||
|
|
||||||
@@ -44,6 +44,8 @@ class INIConfiguration(Configuration):
|
|||||||
:type ignore_errors: bool
|
:type ignore_errors: bool
|
||||||
:param default: The default configuration.
|
:param default: The default configuration.
|
||||||
:type default: INIConfiguration
|
:type default: INIConfiguration
|
||||||
|
:param mutable: The mutability of the configuration
|
||||||
|
:type mutable: bool
|
||||||
|
|
||||||
:raises INISyntaxError: If there is a syntax error in the INI configuration and ``ignore_errors`` is ``False``.
|
:raises INISyntaxError: If there is a syntax error in the INI configuration and ``ignore_errors`` is ``False``.
|
||||||
|
|
||||||
@@ -68,6 +70,8 @@ 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
|
||||||
|
|
||||||
|
set_mutability(configuration, mutable)
|
||||||
|
|
||||||
return configuration
|
return configuration
|
||||||
|
|
||||||
def to_string(self, separator: str = '='):
|
def to_string(self, separator: str = '='):
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class JSONConfiguration(Configuration):
|
|||||||
return iter({k: v.value for k, v in self._config.items()}.items())
|
return iter({k: v.value for k, v in self._config.items()}.items())
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_string(cls, data: str | bytes, ignore_errors: bool = False):
|
def from_string(cls, data: str | bytes, ignore_errors: bool = False, mutable: bool = True):
|
||||||
'''
|
'''
|
||||||
Parses JSON configuration from a string and returns an instance of JSONConfiguration.
|
Parses JSON configuration from a string and returns an instance of JSONConfiguration.
|
||||||
|
|
||||||
@@ -47,6 +47,8 @@ class JSONConfiguration(Configuration):
|
|||||||
:type data: str
|
:type data: str
|
||||||
:param ignore_errors: If True, errors will be ignored.
|
:param ignore_errors: If True, errors will be ignored.
|
||||||
:type ignore_errors: bool
|
:type ignore_errors: bool
|
||||||
|
:param mutable: The mutability of the configuration
|
||||||
|
:type mutable: bool
|
||||||
|
|
||||||
:raises JSONValueSyntaxError: If a value is invalid and ``ignore_errors`` is ``False``.
|
:raises JSONValueSyntaxError: If a value is invalid and ``ignore_errors`` is ``False``.
|
||||||
:raises JSONObjectSyntaxError: If an object is invalid and ``ignore_errors`` is ``False``.
|
:raises JSONObjectSyntaxError: If an object is invalid and ``ignore_errors`` is ``False``.
|
||||||
@@ -63,11 +65,11 @@ class JSONConfiguration(Configuration):
|
|||||||
try:
|
try:
|
||||||
data = parse_type(data).parse(data)
|
data = parse_type(data).parse(data)
|
||||||
if isinstance(data, JSONObject):
|
if isinstance(data, JSONObject):
|
||||||
return cls({k: _configuration(v) for k, v in data.value.items()})
|
return cls({k: _configuration(v) for k, v in data.value.items()}, mutable = mutable)
|
||||||
elif not ignore_errors:
|
elif not ignore_errors:
|
||||||
raise JSONTypeError(f'expected object, got {data._type}. Use jcloud_config_parser.parse.json.parse_json to parse JSONs.')
|
raise JSONTypeError(f'expected object, got {data._type}. Use jcloud_config_parser.parse.json.parse_json to parse JSONs.')
|
||||||
except:
|
except:
|
||||||
if ignore_errors:
|
if ignore_errors:
|
||||||
return cls()
|
return cls(mutable = mutable)
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
+23
-1
@@ -94,4 +94,26 @@ def test_mutability():
|
|||||||
del config.key
|
del config.key
|
||||||
assert False, 'Expected TypeError'
|
assert False, 'Expected TypeError'
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
configuration = '[section]\nkey=value'
|
||||||
|
config = INIConfiguration.from_string(configuration)
|
||||||
|
config.key1 = 'value1'
|
||||||
|
|
||||||
|
set_mutability(config, False)
|
||||||
|
try:
|
||||||
|
config.key2 = 'value2'
|
||||||
|
assert False, 'Expected TypeError'
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
config = INIConfiguration.from_string(configuration, mutable = False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
config.key1 = 'value1'
|
||||||
|
assert False, 'Expected TypeError'
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
set_mutability(config, True)
|
||||||
|
config.key2 = 'value2'
|
||||||
+26
-2
@@ -15,6 +15,7 @@
|
|||||||
from src.jcloud_config_parser.json import JSONConfiguration
|
from src.jcloud_config_parser.json import JSONConfiguration
|
||||||
from src.jcloud_config_parser.exceptions import JSONObjectSyntaxError, JSONValueSyntaxError, JSONStringSyntaxError, JSONArraySyntaxError, EscapeSequenceSyntaxError
|
from src.jcloud_config_parser.exceptions import JSONObjectSyntaxError, JSONValueSyntaxError, JSONStringSyntaxError, JSONArraySyntaxError, EscapeSequenceSyntaxError
|
||||||
from src.jcloud_config_parser import set_mutability
|
from src.jcloud_config_parser import set_mutability
|
||||||
|
from src.jcloud_config_parser.parse.json import JSONString
|
||||||
|
|
||||||
def test_json_configuration():
|
def test_json_configuration():
|
||||||
# Test valid JSON configuration parsing
|
# Test valid JSON configuration parsing
|
||||||
@@ -224,7 +225,7 @@ def test_json_configuration():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def test_mutability():
|
def test_mutability():
|
||||||
config = JSONConfiguration(mutable=False)
|
config = JSONConfiguration(mutable = False)
|
||||||
try:
|
try:
|
||||||
config.key = 'value'
|
config.key = 'value'
|
||||||
assert False, 'Expected TypeError'
|
assert False, 'Expected TypeError'
|
||||||
@@ -243,4 +244,27 @@ def test_mutability():
|
|||||||
del config.key
|
del config.key
|
||||||
assert False, 'Expected TypeError'
|
assert False, 'Expected TypeError'
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
configuration = '{"key": "value"}'
|
||||||
|
config = JSONConfiguration.from_string(configuration)
|
||||||
|
config.key1 = JSONString("value1")
|
||||||
|
|
||||||
|
set_mutability(config, False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
config.key2 = JSONString('value2')
|
||||||
|
assert False, 'Expected TypeError'
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
config = JSONConfiguration.from_string(configuration, mutable = False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
config.key1 = JSONString('value1')
|
||||||
|
assert False, 'Expected TypeError'
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
set_mutability(config, True)
|
||||||
|
config.key2 = JSONString('value2')
|
||||||
Reference in New Issue
Block a user