Initial commit

This commit is contained in:
2026-03-12 19:34:25 +01:00
commit b00e5d9829
22 changed files with 2916 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
# Copyright 2026 jCloud Services GbR
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ._configuration import Configuration
from .parse.json import parse_type, JSONObject, JSONString, JSONNumber, JSONNull, JSONBoolean, JSONArray
from .exceptions import JSONTypeError
__all__ = [
'JSONConfiguration'
]
def _configuration(data):
if isinstance(data, str):
return JSONString(data)
if isinstance(data, (int, float)):
return JSONNumber(data)
if data is None:
return JSONNull(None)
if isinstance(data, bool):
return JSONBoolean(data)
if isinstance(data, list):
return JSONArray(data)
if isinstance(data, dict):
return JSONObject(data)
class JSONConfiguration(Configuration):
def __iter__(self) -> iter:
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):
'''
Parses JSON configuration from a string and returns an instance of JSONConfiguration.
:param data: The JSON data
:type data: str
:param ignore_errors: If True, errors will be ignored.
:type ignore_errors: 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``.
:raises JSONArraySyntaxError: If an array is invalid and ``ignore_errors`` is ``False``.
:raises JSONStringSyntaxError: If a string is invalid and ``ignore_errors`` is ``False``.
:raises JSONNullSyntaxError: If a null value is invalid and ``ignore_errors`` is ``False``.
:raises JSONBooleanSyntaxError: If a null value is invalid and ``ignore_errors`` is ``False``.
:raises JSONNumberSyntaxError: If a number is invalid and ``ignore_errors`` is ``False``.
:raises EscapeSequenceSyntaxError: If an escape sequence is invalid and ``ignore_errors`` is ``False``.
:return: An instance of JSONConfiguration representing the parsed JSON configuration
:rtype: JSONConfiguration
'''
try:
data = parse_type(data).parse(data)
if isinstance(data, JSONObject):
return cls({k: _configuration(v) for k, v in data.value.items()})
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()
else:
raise