Compare commits

...

5 Commits

Author SHA1 Message Date
Clemens Schwaighofer
37a197e7f1 v0.23.0: json dumps updates for functions, safe dict dump 2025-09-03 18:15:48 +09:00
Clemens Schwaighofer
74cb3d2c54 dump_data and new json_dumps
dump_data adds flag to dump without indent

json_dumps is dump_data like, but will be geared towards secure dump of dict to json for strage
2025-09-03 18:14:26 +09:00
Clemens Schwaighofer
d19abcabc7 v0.22.6: Empty settings loader config for just data load 2025-08-26 14:40:22 +09:00
Clemens Schwaighofer
f8ae6609c7 Allow empty config settings for settings loader if only loading is needed 2025-08-26 14:38:55 +09:00
Clemens Schwaighofer
cbd39ff161 v0.22.5: settings loader clean up 2025-08-26 14:33:26 +09:00
6 changed files with 22 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
# MARK: Project info # MARK: Project info
[project] [project]
name = "corelibs" name = "corelibs"
version = "0.22.4" version = "0.23.0"
description = "Collection of utils for Python scripts" description = "Collection of utils for Python scripts"
readme = "README.md" readme = "README.md"
requires-python = ">=3.13" requires-python = ">=3.13"

View File

@@ -57,7 +57,7 @@ class SettingsLoader:
def load_settings( def load_settings(
self, self,
config_id: str, config_id: str,
config_validate: dict[str, list[str]], config_validate: dict[str, list[str]] | None = None,
allow_not_exist: bool = False allow_not_exist: bool = False
) -> dict[str, str]: ) -> dict[str, str]:
""" """
@@ -97,6 +97,8 @@ class SettingsLoader:
settings: dict[str, dict[str, Any]] = { settings: dict[str, dict[str, Any]] = {
config_id: {}, config_id: {},
} }
if config_validate is None:
config_validate = {}
if self.config_parser is not None: if self.config_parser is not None:
try: try:
# load all data as is, validation is done afterwards # load all data as is, validation is done afterwards

View File

@@ -6,7 +6,7 @@ import json
from typing import Any from typing import Any
def dump_data(data: Any) -> str: def dump_data(data: Any, use_indent: bool = True) -> str:
""" """
dump formated output from dict/list dump formated output from dict/list
@@ -16,6 +16,7 @@ def dump_data(data: Any) -> str:
Returns: Returns:
str: _description_ str: _description_
""" """
return json.dumps(data, indent=4, ensure_ascii=False, default=str) indent = 4 if use_indent else None
return json.dumps(data, indent=indent, ensure_ascii=False, default=str)
# __END__ # __END__

View File

@@ -3,7 +3,7 @@ json encoder for datetime
""" """
from typing import Any from typing import Any
from json import JSONEncoder from json import JSONEncoder, dumps
from datetime import datetime, date from datetime import datetime, date
@@ -28,4 +28,16 @@ def default(obj: Any) -> str | None:
return obj.isoformat() return obj.isoformat()
return None return None
def json_dumps(data: Any):
"""
wrapper for json.dumps with sure dump without throwing Exceptions
Arguments:
data {Any} -- _description_
Returns:
_type_ -- _description_
"""
return dumps(data, ensure_ascii=False, default=str)
# __END__ # __END__

View File

@@ -115,7 +115,7 @@ def main():
try: try:
config_load = 'LoadTest' config_load = 'LoadTest'
config_data = sl.load_settings(config_load, {}) config_data = sl.load_settings(config_load)
print(f"[{config_load}] Load: {config_load} -> {dump_data(config_data)}") print(f"[{config_load}] Load: {config_load} -> {dump_data(config_data)}")
except ValueError as e: except ValueError as e:
print(f"Could not load settings: {e}") print(f"Could not load settings: {e}")

2
uv.lock generated
View File

@@ -53,7 +53,7 @@ wheels = [
[[package]] [[package]]
name = "corelibs" name = "corelibs"
version = "0.22.4" version = "0.22.6"
source = { editable = "." } source = { editable = "." }
dependencies = [ dependencies = [
{ name = "jmespath" }, { name = "jmespath" },