Skip to content

Commit

Permalink
consequently handle config data internally as libioc.Config.Data.Data
Browse files Browse the repository at this point in the history
  • Loading branch information
gronke committed Apr 18, 2019
1 parent 55dd131 commit b78fb93
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
9 changes: 8 additions & 1 deletion libioc/Config/Jail/BaseConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import libioc.Config.Data
import libioc.Config.Jail.Globals
import libioc.Config.Jail.Properties
import libioc.Config.Jail.Defaults
import libioc.errors
import libioc.helpers
import libioc.helpers_object
Expand Down Expand Up @@ -736,7 +737,13 @@ def set( # noqa: T484
existed_before = (key in self.keys()) is True

try:
hash_before = str(BaseConfig.__getitem__(self, key)).__hash__()
if isinstance(
self,
libioc.Config.Jail.Defaults.JailConfigDefaults
) is True:
hash_before = str(self.__getitem__(key)).__hash__()
else:
hash_before = str(BaseConfig.__getitem__(self, key)).__hash__()
except Exception:
if existed_before is True:
raise
Expand Down
10 changes: 6 additions & 4 deletions libioc/Config/Prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import os.path

import libioc.helpers_object
import libioc.Config.Data

# MyPy
import libioc.Logger
Expand All @@ -44,7 +45,7 @@ class Prototype:
"""Prototype of a JailConfig."""

logger: typing.Type['libioc.Logger.Logger']
data: ConfigDataDict = {}
data: ConfigDataDict
_file: str

def __init__(
Expand All @@ -54,6 +55,7 @@ def __init__(
) -> None:

self.logger = libioc.helpers_object.init_logger(self, logger)
self.data = libioc.Config.Data.Data()

if file is not None:
self._file = file
Expand All @@ -67,7 +69,7 @@ def file(self) -> str:
def file(self, value: str) -> None:
self._file = value

def read(self) -> ConfigDataDict:
def read(self) -> libioc.Config.Data.Data:
"""
Read from the configuration file.
Expand All @@ -93,14 +95,14 @@ def write(self, data: ConfigDataDict) -> None:
def map_input(
self,
data: typing.Union[typing.TextIO, ConfigDataDict]
) -> ConfigDataDict:
) -> libioc.Config.Data.Data:
"""
Map input data (for reading from the configuration).
Implementing classes may provide individual mappings.
"""
if not isinstance(data, typing.TextIO):
return data
return libioc.Config.Data.Data(data)

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

Expand Down
19 changes: 14 additions & 5 deletions libioc/Config/Type/JSON.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ class ConfigJSON(libioc.Config.Prototype.Prototype):

config_type = "json"

def map_input(self, data: typing.TextIO) -> typing.Dict[str, typing.Any]:
def map_input(self, data: typing.TextIO) -> libioc.Config.Data.Data:
"""Parse and normalize JSON data."""
if data == "":
return {}
try:
result = json.load(data) # type: typing.Dict[str, typing.Any]
return result
content = data.read().strip()
except (FileNotFoundError, PermissionError) as e:
raise libioc.errors.JailConfigError(
message=str(e),
logger=self.logger
)

if content == "":
return libioc.Config.Data.Data()

try:
result = json.loads(content) # type: typing.Dict[str, typing.Any]
return libioc.Config.Data.Data(result)
except json.decoder.JSONDecodeError as e:
raise libioc.errors.JailConfigError(
message=str(e),
Expand Down
6 changes: 4 additions & 2 deletions libioc/Config/Type/ZFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ def write(self, data: dict) -> None:
def map_input(
self,
data: typing.Dict[str, str]
) -> libioc.Config.Prototype.ConfigDataDict:
) -> libioc.Config.Data.Data:
"""Normalize data read from ZFS properties."""
parse_user_input = libioc.helpers.parse_user_input
return dict([(x, parse_user_input(y)) for (x, y) in data.items()])
return libioc.Config.Data.Data(
dict([(x, parse_user_input(y)) for (x, y) in data.items()])
)

def _to_string(
self,
Expand Down
4 changes: 2 additions & 2 deletions libioc/Resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def abspath(self, relative_path: str) -> str:
"""Return the absolute path of a path relative to the resource."""
return str(os.path.join(self.dataset.mountpoint, relative_path))

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

Expand Down Expand Up @@ -437,4 +437,4 @@ def destroy(

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

0 comments on commit b78fb93

Please sign in to comment.