Implemented config functionality
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
@ -18,26 +18,95 @@
|
|||||||
"""
|
"""
|
||||||
Takes care of config functionality.
|
Takes care of config functionality.
|
||||||
|
|
||||||
|
Constants:
|
||||||
|
CONFIG_FILE: name of config file relative to working directory
|
||||||
|
|
||||||
Functions:
|
Functions:
|
||||||
get_property(key: str): returns the value of given property
|
get_property(key: str): returns the value of given property (e.g. "section.option")
|
||||||
set_property(key: str, value: str): sets the given property to given value
|
set_property(key: str, value: str): sets the given property to given value
|
||||||
list_property_values(): prints out list of config values
|
list_property_values(): prints out list of config values
|
||||||
get_config(): returns key-value store
|
get_config(): returns key-value store
|
||||||
"""
|
"""
|
||||||
from typing import Union, Dict
|
import configparser
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
CONFIG_FILE = "tm-masterthesis-config.ini"
|
||||||
|
_CONFIG_PROPS = {
|
||||||
|
"Paths": {
|
||||||
|
"coco": (str, ""),
|
||||||
|
"scenenet": (str, ""),
|
||||||
|
"scenenet_gt_train": (str, ""),
|
||||||
|
"scenenet_gt_val": (str, ""),
|
||||||
|
"scenenet_gt_test": (str, ""),
|
||||||
|
"output": (str, ""),
|
||||||
|
"summaries": (str, ""),
|
||||||
|
"weights": (str, "")
|
||||||
|
},
|
||||||
|
"Debug": {
|
||||||
|
"summaries": (bool, True),
|
||||||
|
"train_images": (bool, False)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_property(key: str) -> str:
|
def get_property(key: str) -> str:
|
||||||
raise NotImplementedError
|
parser = configparser.ConfigParser()
|
||||||
|
config_file = f"{os.getcwd()}/{CONFIG_FILE}"
|
||||||
|
|
||||||
|
_initialise_config(config_file)
|
||||||
|
parser.read(config_file)
|
||||||
|
|
||||||
|
section, prop = tuple(key.split("."))
|
||||||
|
return str(parser.get(section, prop))
|
||||||
|
|
||||||
|
|
||||||
def set_property(key: str, value: str) -> None:
|
def set_property(key: str, value: str) -> None:
|
||||||
raise NotImplementedError
|
parser = configparser.ConfigParser()
|
||||||
|
config_file = f"{os.getcwd()}/{CONFIG_FILE}"
|
||||||
|
|
||||||
|
_initialise_config(config_file)
|
||||||
|
parser.read(config_file)
|
||||||
|
|
||||||
|
section, prop = tuple(key.split("."))
|
||||||
|
cast_method = _CONFIG_PROPS[section][key][0]
|
||||||
|
parser.set(section, prop, cast_method(value))
|
||||||
|
|
||||||
|
|
||||||
def list_property_values() -> None:
|
def list_property_values() -> None:
|
||||||
raise NotImplementedError
|
parser = configparser.ConfigParser()
|
||||||
|
config_file = f"{os.getcwd()}/{CONFIG_FILE}"
|
||||||
|
|
||||||
|
_initialise_config(config_file)
|
||||||
|
parser.read(config_file)
|
||||||
|
|
||||||
|
print(parser) # simple implementation, more sophisticated might follow
|
||||||
|
|
||||||
|
|
||||||
def get_config() -> Dict[str, Union[str, int, float, bool]]:
|
def get_config() -> configparser.ConfigParser:
|
||||||
raise NotImplementedError
|
parser = configparser.ConfigParser()
|
||||||
|
config_file = f"{os.getcwd()}/{CONFIG_FILE}"
|
||||||
|
_initialise_config(config_file)
|
||||||
|
parser.read(config_file)
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
def _initialise_config(config_file: str) -> None:
|
||||||
|
# work-around for implementation detail of config parser
|
||||||
|
# a non-existing file does not lead to an exception but is simply ignored
|
||||||
|
# therefore a manual check via this construction is required
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(config_file, "r"):
|
||||||
|
pass
|
||||||
|
except FileNotFoundError:
|
||||||
|
with open(config_file, "w") as file:
|
||||||
|
parser = configparser.ConfigParser()
|
||||||
|
for section in _CONFIG_PROPS:
|
||||||
|
parser[section] = {}
|
||||||
|
for option in _CONFIG_PROPS[section]:
|
||||||
|
_, default = _CONFIG_PROPS[section][option]
|
||||||
|
parser[section][option] = default
|
||||||
|
|
||||||
|
parser.write(file)
|
||||||
|
|||||||
Reference in New Issue
Block a user