Changed config get function to return correct type
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
@ -33,7 +33,7 @@ from twomartens.masterthesis import config as conf
|
|||||||
|
|
||||||
def config(args: argparse.Namespace) -> None:
|
def config(args: argparse.Namespace) -> None:
|
||||||
if args.action == "get":
|
if args.action == "get":
|
||||||
print(conf.get_property(args.property))
|
print(str(conf.get_property(args.property)))
|
||||||
elif args.action == "set":
|
elif args.action == "set":
|
||||||
conf.set_property(args.property, args.value)
|
conf.set_property(args.property, args.value)
|
||||||
elif args.action == "list":
|
elif args.action == "list":
|
||||||
|
|||||||
@ -29,7 +29,7 @@ Functions:
|
|||||||
"""
|
"""
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
CONFIG_FILE = "tm-masterthesis-config.ini"
|
CONFIG_FILE = "tm-masterthesis-config.ini"
|
||||||
_CONFIG_PROPS = {
|
_CONFIG_PROPS = {
|
||||||
@ -55,7 +55,7 @@ _CONFIG_PROPS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_property(key: str) -> str:
|
def get_property(key: str) -> Union[str, float, int, bool]:
|
||||||
parser = configparser.ConfigParser()
|
parser = configparser.ConfigParser()
|
||||||
config_file = f"{os.getcwd()}/{CONFIG_FILE}"
|
config_file = f"{os.getcwd()}/{CONFIG_FILE}"
|
||||||
|
|
||||||
@ -63,7 +63,19 @@ def get_property(key: str) -> str:
|
|||||||
parser.read(config_file)
|
parser.read(config_file)
|
||||||
|
|
||||||
section, prop = tuple(key.split("."))
|
section, prop = tuple(key.split("."))
|
||||||
return parser.get(section, prop)
|
cast = _CONFIG_PROPS[section][prop][0]
|
||||||
|
|
||||||
|
value = None
|
||||||
|
if cast is str:
|
||||||
|
value = parser.get(section, prop)
|
||||||
|
elif cast is float:
|
||||||
|
value = parser.getfloat(section, prop)
|
||||||
|
elif cast is int:
|
||||||
|
value = parser.getint(section, prop)
|
||||||
|
elif cast is bool:
|
||||||
|
value = parser.getboolean(section, prop)
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def set_property(key: str, value: str) -> None:
|
def set_property(key: str, value: str) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user