Skip to content

Commit

Permalink
Merge pull request #1 from aoamusat:feature-parse-config-values
Browse files Browse the repository at this point in the history
Feature-parse-config-values
  • Loading branch information
aoamusat authored Feb 5, 2024
2 parents 05ff98b + 9bb2ce5 commit ad73068
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
34 changes: 27 additions & 7 deletions iniutil/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
from typing import Generator, Dict
from typing import Generator, Dict, Any


def cast_config_value(value: str) -> Any:
"""Cast the given config value to appropriate type
Args:
value (Any): config value to cast
Returns:
Any: casted value
"""

BOOLEAN_VALUES = ["true", "false"]

if value.lower() in BOOLEAN_VALUES:
return True if value.lower() == "true" else False
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return value


def read_ini(path: str) -> Generator:
"""Read ini configuration file
Expand Down Expand Up @@ -44,13 +68,9 @@ def parse_ini(path: str) -> Dict[str, Dict[str, str]]:
key, val = line.split("=", 1)

if current_section is not None:
config[current_section][key.strip()] = val.strip()
config[current_section][key.strip()] = cast_config_value(val.strip())
continue
else:
config[key.strip()] = val.strip()
config[key.strip()] = cast_config_value(val.strip())
current_section = None
return config

if __name__ == "__main__":
print(parse_ini("samples/sample.ini"))
print(parse_ini("samples/generic.ini"))
File renamed without changes.
File renamed without changes.
46 changes: 20 additions & 26 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import unittest
from iniutil.parser import parse_ini
from iniutil.parser import cast_config_value, parse_ini


class Test(unittest.TestCase):
def setUp(self):
# Set up paths relative to the test script
self.sample_ini_path = "resources/sample.ini"
self.generic_ini_path = "resources/generic.ini"
class ParserTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.test_file_path = "resources/sample.ini"

def test_example_case(self):
"""example case"""
@classmethod
def tearDownClass(cls) -> None:
del cls.test_file_path

def test_ini_parser(self):
expected = {
"owner": {
"name": "John Doe",
Expand All @@ -24,25 +26,17 @@ def test_example_case(self):
"connection": "",
},
}
actual = parse_ini(self.sample_ini_path)
self.assertEqual(actual, expected)
self.assertEqual(expected, parse_ini(self.test_file_path))

def test_generic_case(self):
"""generic case"""
expected = {
"section": {
"b": False,
"f": 206.201,
"i": -55,
"i1": 1,
"b1": True,
"b2": False,
"s": "",
},
}
actual = parse_ini(self.generic_ini_path)
self.assertEqual(actual, expected)
def test_value_cast(self):
self.assertEqual(
cast_config_value("https://api.example.com"), "https://api.example.com"
)
self.assertEqual(cast_config_value("3306"), 3306)
self.assertEqual(cast_config_value("false"), False)
self.assertEqual(cast_config_value("true"), True)
self.assertEqual(cast_config_value("9.5"), 9.5)


if __name__ == "__main__":
unittest.main()
unittest.main()

0 comments on commit ad73068

Please sign in to comment.