mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-13 10:34:28 +05:00
chore(scp): update SimpleConfigParser
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -314,9 +314,7 @@ class SimpleConfigParser:
|
|||||||
elements.pop(i)
|
elements.pop(i)
|
||||||
break
|
break
|
||||||
|
|
||||||
def getval(
|
def getval(self, section: str, option: str, fallback: str | _UNSET = _UNSET) -> str:
|
||||||
self, section: str, option: str, fallback: str | _UNSET = _UNSET
|
|
||||||
) -> str | List[str]:
|
|
||||||
"""
|
"""
|
||||||
Return the value of the given option in the given section
|
Return the value of the given option in the given section
|
||||||
|
|
||||||
@@ -329,22 +327,34 @@ class SimpleConfigParser:
|
|||||||
if option not in self.get_options(section):
|
if option not in self.get_options(section):
|
||||||
raise NoOptionError(option, section)
|
raise NoOptionError(option, section)
|
||||||
|
|
||||||
# Find the option in the elements list
|
|
||||||
for element in self.config[section]["elements"]:
|
for element in self.config[section]["elements"]:
|
||||||
if element["type"] in [LineType.OPTION.value, LineType.OPTION_BLOCK.value] and element["name"] == option:
|
if element["type"] is LineType.OPTION.value and element["name"] == option:
|
||||||
raw_value = element["value"]
|
return str(element["value"].strip().replace("\n", ""))
|
||||||
if isinstance(raw_value, str) and raw_value.endswith("\n"):
|
return ""
|
||||||
return raw_value[:-1].strip()
|
|
||||||
elif isinstance(raw_value, list):
|
except (NoSectionError, NoOptionError):
|
||||||
values: List[str] = []
|
if fallback is _UNSET:
|
||||||
for i, val in enumerate(raw_value):
|
raise
|
||||||
val = val.strip().strip("\n")
|
return fallback
|
||||||
if len(val) < 1:
|
|
||||||
continue
|
def getvals(self, section: str, option: str, fallback: List[str] | _UNSET = _UNSET) -> List[str]:
|
||||||
values.append(val.strip())
|
"""
|
||||||
return values
|
Return the values of the given multi-line option in the given section
|
||||||
return str(raw_value)
|
|
||||||
raise NoOptionError(option, section)
|
If the key is not found and 'fallback' is provided, it is used as
|
||||||
|
a fallback value.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if section not in self.get_sections():
|
||||||
|
raise NoSectionError(section)
|
||||||
|
if option not in self.get_options(section):
|
||||||
|
raise NoOptionError(option, section)
|
||||||
|
|
||||||
|
for element in self.config[section]["elements"]:
|
||||||
|
if element["type"] is LineType.OPTION_BLOCK.value and element["name"] == option:
|
||||||
|
return [val.strip() for val in element["value"] if val.strip()]
|
||||||
|
return []
|
||||||
|
|
||||||
except (NoSectionError, NoOptionError):
|
except (NoSectionError, NoOptionError):
|
||||||
if fallback is _UNSET:
|
if fallback is _UNSET:
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ def test_getval(parser):
|
|||||||
assert parser.getval("section_2", "option_2") == "value_2"
|
assert parser.getval("section_2", "option_2") == "value_2"
|
||||||
|
|
||||||
# test multiline option values
|
# test multiline option values
|
||||||
ml_val = parser.getval("section number 5", "multi_option")
|
ml_val = parser.getvals("section number 5", "multi_option")
|
||||||
assert isinstance(ml_val, list)
|
assert isinstance(ml_val, list)
|
||||||
assert len(ml_val) > 0
|
assert len(ml_val) > 0
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ def test_set_new_option(parser):
|
|||||||
assert parser.getval("new_section", "very_new_option") == "very_new_value"
|
assert parser.getval("new_section", "very_new_option") == "very_new_value"
|
||||||
|
|
||||||
parser.set_option("section_2", "array_option", ["value_1", "value_2", "value_3"])
|
parser.set_option("section_2", "array_option", ["value_1", "value_2", "value_3"])
|
||||||
assert parser.getval("section_2", "array_option") == [
|
assert parser.getvals("section_2", "array_option") == [
|
||||||
"value_1",
|
"value_1",
|
||||||
"value_2",
|
"value_2",
|
||||||
"value_3",
|
"value_3",
|
||||||
|
|||||||
Reference in New Issue
Block a user