Skip to content

Commit b78fb93

Browse files
committed
consequently handle config data internally as libioc.Config.Data.Data
1 parent 55dd131 commit b78fb93

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

libioc/Config/Jail/BaseConfig.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import libioc.Config.Data
3030
import libioc.Config.Jail.Globals
3131
import libioc.Config.Jail.Properties
32+
import libioc.Config.Jail.Defaults
3233
import libioc.errors
3334
import libioc.helpers
3435
import libioc.helpers_object
@@ -736,7 +737,13 @@ def set( # noqa: T484
736737
existed_before = (key in self.keys()) is True
737738

738739
try:
739-
hash_before = str(BaseConfig.__getitem__(self, key)).__hash__()
740+
if isinstance(
741+
self,
742+
libioc.Config.Jail.Defaults.JailConfigDefaults
743+
) is True:
744+
hash_before = str(self.__getitem__(key)).__hash__()
745+
else:
746+
hash_before = str(BaseConfig.__getitem__(self, key)).__hash__()
740747
except Exception:
741748
if existed_before is True:
742749
raise

libioc/Config/Prototype.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import os.path
2828

2929
import libioc.helpers_object
30+
import libioc.Config.Data
3031

3132
# MyPy
3233
import libioc.Logger
@@ -44,7 +45,7 @@ class Prototype:
4445
"""Prototype of a JailConfig."""
4546

4647
logger: typing.Type['libioc.Logger.Logger']
47-
data: ConfigDataDict = {}
48+
data: ConfigDataDict
4849
_file: str
4950

5051
def __init__(
@@ -54,6 +55,7 @@ def __init__(
5455
) -> None:
5556

5657
self.logger = libioc.helpers_object.init_logger(self, logger)
58+
self.data = libioc.Config.Data.Data()
5759

5860
if file is not None:
5961
self._file = file
@@ -67,7 +69,7 @@ def file(self) -> str:
6769
def file(self, value: str) -> None:
6870
self._file = value
6971

70-
def read(self) -> ConfigDataDict:
72+
def read(self) -> libioc.Config.Data.Data:
7173
"""
7274
Read from the configuration file.
7375
@@ -93,14 +95,14 @@ def write(self, data: ConfigDataDict) -> None:
9395
def map_input(
9496
self,
9597
data: typing.Union[typing.TextIO, ConfigDataDict]
96-
) -> ConfigDataDict:
98+
) -> libioc.Config.Data.Data:
9799
"""
98100
Map input data (for reading from the configuration).
99101
100102
Implementing classes may provide individual mappings.
101103
"""
102104
if not isinstance(data, typing.TextIO):
103-
return data
105+
return libioc.Config.Data.Data(data)
104106

105107
raise NotImplementedError("Mapping not implemented on the prototype")
106108

libioc/Config/Type/JSON.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,22 @@ class ConfigJSON(libioc.Config.Prototype.Prototype):
3838

3939
config_type = "json"
4040

41-
def map_input(self, data: typing.TextIO) -> typing.Dict[str, typing.Any]:
41+
def map_input(self, data: typing.TextIO) -> libioc.Config.Data.Data:
4242
"""Parse and normalize JSON data."""
43-
if data == "":
44-
return {}
4543
try:
46-
result = json.load(data) # type: typing.Dict[str, typing.Any]
47-
return result
44+
content = data.read().strip()
45+
except (FileNotFoundError, PermissionError) as e:
46+
raise libioc.errors.JailConfigError(
47+
message=str(e),
48+
logger=self.logger
49+
)
50+
51+
if content == "":
52+
return libioc.Config.Data.Data()
53+
54+
try:
55+
result = json.loads(content) # type: typing.Dict[str, typing.Any]
56+
return libioc.Config.Data.Data(result)
4857
except json.decoder.JSONDecodeError as e:
4958
raise libioc.errors.JailConfigError(
5059
message=str(e),

libioc/Config/Type/ZFS.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ def write(self, data: dict) -> None:
9595
def map_input(
9696
self,
9797
data: typing.Dict[str, str]
98-
) -> libioc.Config.Prototype.ConfigDataDict:
98+
) -> libioc.Config.Data.Data:
9999
"""Normalize data read from ZFS properties."""
100100
parse_user_input = libioc.helpers.parse_user_input
101-
return dict([(x, parse_user_input(y)) for (x, y) in data.items()])
101+
return libioc.Config.Data.Data(
102+
dict([(x, parse_user_input(y)) for (x, y) in data.items()])
103+
)
102104

103105
def _to_string(
104106
self,

libioc/Resource.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def abspath(self, relative_path: str) -> str:
323323
"""Return the absolute path of a path relative to the resource."""
324324
return str(os.path.join(self.dataset.mountpoint, relative_path))
325325

326-
def _write_config(self, data: dict) -> None:
326+
def _write_config(self, data: libioc.Config.Data.Data) -> None:
327327
"""Write the configuration to disk."""
328328
self.config_handler.write(data)
329329

@@ -437,4 +437,4 @@ def destroy(
437437

438438
def save(self) -> None:
439439
"""Save changes to the default configuration."""
440-
self._write_config(self.config.user_data.nested)
440+
self._write_config(self.config.user_data)

0 commit comments

Comments
 (0)