Add support for specifying mutability when parsing a configuration
This commit is contained in:
@@ -39,7 +39,7 @@ class JSONConfiguration(Configuration):
|
||||
return iter({k: v.value for k, v in self._config.items()}.items())
|
||||
|
||||
@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.
|
||||
|
||||
@@ -47,6 +47,8 @@ class JSONConfiguration(Configuration):
|
||||
:type data: str
|
||||
:param ignore_errors: If True, errors will be ignored.
|
||||
: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 JSONObjectSyntaxError: If an object is invalid and ``ignore_errors`` is ``False``.
|
||||
@@ -63,11 +65,11 @@ class JSONConfiguration(Configuration):
|
||||
try:
|
||||
data = parse_type(data).parse(data)
|
||||
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:
|
||||
raise JSONTypeError(f'expected object, got {data._type}. Use jcloud_config_parser.parse.json.parse_json to parse JSONs.')
|
||||
except:
|
||||
if ignore_errors:
|
||||
return cls()
|
||||
return cls(mutable = mutable)
|
||||
else:
|
||||
raise
|
||||
Reference in New Issue
Block a user