Compare commits

..

4 Commits

Author SHA1 Message Date
dw-0
e68979967d Merge commit '2f924a2550d9011604ce0170b21d1550501e6108' as 'kiauh/core/submodules/simple_config_parser' 2024-09-24 19:22:13 +02:00
dw-0
2f924a2550 Squashed 'kiauh/core/submodules/simple_config_parser/' content from commit 90081a6
git-subtree-dir: kiauh/core/submodules/simple_config_parser
git-subtree-split: 90081a6539ec38adf6a1a5bb707a0e9934567c7f
2024-09-24 19:22:13 +02:00
dw-0
453e7181a5 chore: remove scp submodule
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-09-24 19:22:04 +02:00
dw-0
2897f38e4e refactor: log successfully added sections
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-09-24 19:20:36 +02:00
3 changed files with 16 additions and 48 deletions

View File

@@ -192,24 +192,11 @@ class SimpleConfigParser:
self.config[section] = {"_raw": f"[{section}]\n"}
def _check_set_section_spacing(self):
prev_section_name: str = self.get_sections()[-1]
prev_section_content: Dict = self.config[prev_section_name]
last_option_name: str = list(prev_section_content.keys())[-1]
if last_option_name.startswith("#_"):
last_elem_value: str = prev_section_content[last_option_name][-1]
# if the last section is a collector, we first check if the last element
# in the collector ends with a newline. if it does not, we append a newline.
# this can happen if the config file does not end with a newline.
if not last_elem_value.endswith("\n"):
prev_section_content[last_option_name][-1] = f"{last_elem_value}\n"
# if the last item in a collector is not a newline, we append a newline, so
# that the new section is seperated from the options of the previous section
# by a newline
if last_elem_value != "\n":
prev_section_content[last_option_name].append("\n")
prev_section: str = self.get_sections()[-1]
prev_section_content: Dict = self.config[prev_section]
last_item: str = list(prev_section_content.keys())[-1]
if last_item.startswith("#_") and prev_section_content[last_item][-1] != "\n":
prev_section_content[last_item].append("\n")
else:
prev_section_content[self._generate_rand_id()] = ["\n"]
@@ -311,7 +298,7 @@ class SimpleConfigParser:
"""Return the value of the given option in the given section as a converted value"""
try:
return conv(self.getval(section, option, fallback))
except (ValueError, TypeError, AttributeError) as e:
except ValueError as e:
if fallback is not _UNSET:
return fallback
raise ValueError(

View File

@@ -57,7 +57,6 @@ def test_getval(parser):
def test_getval_fallback(parser):
assert parser.getval("section_1", "option_128", "fallback") == "fallback"
assert parser.getval("section_1", "option_128", None) is None
def test_getval_exceptions(parser):
@@ -90,7 +89,6 @@ def test_getint_from_boolean(parser):
def test_getint_fallback(parser):
assert parser.getint("section_1", "option_128", 128) == 128
assert parser.getint("section_1", "option_128", None) is None
def test_getboolean(parser):
@@ -117,7 +115,6 @@ def test_getboolean_from_float(parser):
def test_getboolean_fallback(parser):
assert parser.getboolean("section_1", "option_128", True) is True
assert parser.getboolean("section_1", "option_128", False) is False
assert parser.getboolean("section_1", "option_128", None) is None
def test_getfloat(parser):
@@ -142,7 +139,6 @@ def test_getfloat_from_boolean(parser):
def test_getfloat_fallback(parser):
assert parser.getfloat("section_1", "option_128", 1.234) == 1.234
assert parser.getfloat("section_1", "option_128", None) is None
def test_set_existing_option(parser):

View File

@@ -25,65 +25,50 @@ def parser():
return parser
def test_get_int_conv(parser):
def test_get_conv(parser):
# Test conversion to int
should_be_int = parser._get_conv("section_1", "option_1_2", int)
assert isinstance(should_be_int, int)
def test_get_float_conv(parser):
# Test conversion to float
should_be_float = parser._get_conv("section_1", "option_1_3", float)
assert isinstance(should_be_float, float)
def test_get_bool_conv(parser):
# Test conversion to boolean
should_be_bool = parser._get_conv(
"section_1", "option_1_1", parser._convert_to_boolean
)
assert isinstance(should_be_bool, bool)
def test_get_int_conv_fallback(parser):
# Test fallback for int
should_be_fallback_int = parser._get_conv(
"section_1", "option_128", int, fallback=128
)
assert isinstance(should_be_fallback_int, int)
assert should_be_fallback_int == 128
assert parser._get_conv("section_1", "option_128", int, None) is None
def test_get_float_conv_fallback(parser):
# Test fallback for float
should_be_fallback_float = parser._get_conv(
"section_1", "option_128", float, fallback=1.234
)
assert isinstance(should_be_fallback_float, float)
assert should_be_fallback_float == 1.234
assert parser._get_conv("section_1", "option_128", float, None) is None
def test_get_bool_conv_fallback(parser):
# Test fallback for boolean
should_be_fallback_bool = parser._get_conv(
"section_1", "option_128", parser._convert_to_boolean, fallback=True
)
assert isinstance(should_be_fallback_bool, bool)
assert should_be_fallback_bool is True
assert (
parser._get_conv("section_1", "option_128", parser._convert_to_boolean, None)
is None
)
def test_get_int_conv_exception(parser):
# Test ValueError exception for invalid int conversion
with pytest.raises(ValueError):
parser._get_conv("section_1", "option_1", int)
def test_get_float_conv_exception(parser):
# Test ValueError exception for invalid float conversion
with pytest.raises(ValueError):
parser._get_conv("section_1", "option_1", float)
def test_get_bool_conv_exception(parser):
# Test ValueError exception for invalid boolean conversion
with pytest.raises(ValueError):
parser._get_conv("section_1", "option_1", parser._convert_to_boolean)