Skip to content
Snippets Groups Projects
Commit 0252b34c authored by pascal.mouquet_ird.fr's avatar pascal.mouquet_ird.fr
Browse files

improve config.py to auto update config file and set default values

parent c1361816
No related branches found
No related tags found
No related merge requests found
...@@ -38,18 +38,35 @@ class Config: ...@@ -38,18 +38,35 @@ class Config:
_TILES_TO_WATCH = _CONFIG_DIR / "tiles_to_watch.csv" _TILES_TO_WATCH = _CONFIG_DIR / "tiles_to_watch.csv"
def __init__(self) -> None: def __init__(self) -> None:
self._config_params = {"temp_path", "l1c_path", "l2a_path",
"indices_path", "time_series_path",
"sen2cor_bashrc_path", "scihub_id",
"scihub_pwd", "peps_config_path",
"proxy_http_url", "proxy_https_url"}
self.cfg = None
self.config_dict = dict()
if not self._CONFIG_FILE.exists(): self._config_params = ConfigParser()
self._config_params["DATA PATHS"] = {"temp_path": "",
"l1c_path": "",
"l2a_path": "",
"indices_path": "",
"time_series_path": ""}
self._config_params["SEN2COR PATH"] = {"sen2cor_bashrc_path": ""}
self._config_params["HUBS LOGINS"] = {"scihub_id": "",
"scihub_pwd": "",
"peps_config_path": ""}
self._config_params["PROXY SETTINGS"] = {"proxy_http_url": "",
"proxy_https_url": ""}
if self._CONFIG_FILE.exists():
self._config_params.read(str(self._CONFIG_FILE))
self._config_params_disk = ConfigParser()
self._config_params_disk.read(str(self._CONFIG_FILE))
if self._config_params_disk != self._config_params:
self._create_config()
else:
self._create_config() self._create_config()
self.config_dict = dict()
for section in self._config_params.sections():
for key in self._config_params[section].keys():
self.config_dict[key] = self._config_params[section][key]
self._load_config()
self._check_data_paths() self._check_data_paths()
def _create_config(self) -> None: def _create_config(self) -> None:
...@@ -57,46 +74,9 @@ class Config: ...@@ -57,46 +74,9 @@ class Config:
self._USER_DIR.mkdir(exist_ok=True) self._USER_DIR.mkdir(exist_ok=True)
self._CONFIG_DIR.mkdir(exist_ok=True) self._CONFIG_DIR.mkdir(exist_ok=True)
self._DEFAULT_DATA_DIR.mkdir(exist_ok=True) self._DEFAULT_DATA_DIR.mkdir(exist_ok=True)
with open(str(self._CONFIG_FILE), "w") as cfg_file: with open(str(self._CONFIG_FILE), "w") as cfg_file:
self.cfg = ConfigParser() self._config_params.write(cfg_file)
self.cfg.read(str(self._CONFIG_FILE))
self.cfg.add_section("DATA PATHS")
self.cfg.add_section("SEN2COR PATH")
self.cfg.add_section("HUBS LOGINS")
self.cfg.add_section("PROXY SETTINGS")
self.cfg.set("DATA PATHS", "temp_path", "")
self.cfg.set("DATA PATHS", "l1c_path", "")
self.cfg.set("DATA PATHS", "l2a_path", "")
self.cfg.set("DATA PATHS", "indices_path", "")
self.cfg.set("DATA PATHS", "time_series_path", "")
self.cfg.set("SEN2COR PATH", "sen2cor_bashrc_path", "")
self.cfg.set("HUBS LOGINS", "peps_config_path", "")
self.cfg.set("HUBS LOGINS", "scihub_id", "")
self.cfg.set("HUBS LOGINS", "scihub_pwd", "")
self.cfg.set("PROXY SETTINGS", "proxy_http_url", "")
self.cfg.set("PROXY SETTINGS", "proxy_https_url", "")
self.cfg.write(cfg_file)
def _load_config(self) -> None:
"""Loads configuration parameters in a dictionnary."""
self.cfg = ConfigParser()
self.cfg.read(str(self._CONFIG_FILE))
# check if a parameter is missing in the config file
for section in self.cfg.sections():
for key in self.cfg[section].keys():
if key in self._config_params:
self.config_dict[key] = self.cfg[section][key]
else:
raise ValueError("Invalid parameter:", key)
if self._config_params != set(self.config_dict.keys()):
missing_params = self._config_params - set(self.config_dict.keys())
raise ValueError(
"Parameter not found in config file: {}".format(missing_params)
)
def _check_data_paths(self) -> None: def _check_data_paths(self) -> None:
""" """
...@@ -107,12 +87,11 @@ class Config: ...@@ -107,12 +87,11 @@ class Config:
""" """
Update a setting in config.ini Update a setting in config.ini
""" """
self.cfg.set(section, key, val) self._config_params.set(section, key, val)
with open(str(self._CONFIG_FILE), "w") as cfg_file: with open(str(self._CONFIG_FILE), "w") as cfg_file:
self.cfg.write(cfg_file) self._config_params.write(cfg_file)
data_paths = ["temp_path", "l1c_path", "l2a_path", data_paths = [option for option in self._config_params["DATA PATHS"]]
"indices_path", "time_series_path"]
for path in data_paths: for path in data_paths:
value = self.config_dict[path] value = self.config_dict[path]
...@@ -136,7 +115,7 @@ class Config: ...@@ -136,7 +115,7 @@ class Config:
:param param: parameter. :param param: parameter.
""" """
if param not in self._config_params: if param not in self.config_dict:
raise ValueError("Invalid parameter", param) raise ValueError("Invalid parameter", param)
return self.config_dict.get(param) return self.config_dict.get(param)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment