From c1fefd9ba9b439991dbca983ecf32408cdc706bf Mon Sep 17 00:00:00 2001 From: Arthur Vigan Date: Mon, 13 Jun 2022 13:34:14 +0200 Subject: [PATCH] Add possibility to load a default user configuration file in IRDIS.ImagingReduction() Progress on #73 --- sphere/IRDIS/ImagingReduction.py | 14 ++++++++++++-- sphere/utils/config.py | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/sphere/IRDIS/ImagingReduction.py b/sphere/IRDIS/ImagingReduction.py index 35a77fe..e1a596f 100644 --- a/sphere/IRDIS/ImagingReduction.py +++ b/sphere/IRDIS/ImagingReduction.py @@ -52,7 +52,7 @@ class ImagingReduction(object): # Constructor ################################################## - def __new__(cls, path, log_level='info', sphere_handler=None): + def __new__(cls, path, log_level='info', user_config=None, sphere_handler=None): '''Custom instantiation for the class and initialization for the instances @@ -66,9 +66,13 @@ def __new__(cls, path, log_level='info', sphere_handler=None): path : str Path to the directory containing the dataset - level : {'debug', 'info', 'warning', 'error', 'critical'} + log_level : {'debug', 'info', 'warning', 'error', 'critical'} The log level of the handler + user_config : str + Path to a user-provided configuration. Default is None, i.e. the + reduction will use the package default configuration parameters + sphere_handler : log handler Higher-level SPHERE.Dataset log handler @@ -170,6 +174,12 @@ def __new__(cls, path, log_level='info', sphere_handler=None): cfg[key] = val reduction._config = utils.Configuration(reduction._path, reduction._logger, cfg) + # load user-provided default configuration parameters + if user_config: + user_config = Path(user_config) + + reduction._config.load_from_file(user_config) + # # reduction and recipes status # diff --git a/sphere/utils/config.py b/sphere/utils/config.py index f4fbb46..aba137d 100644 --- a/sphere/utils/config.py +++ b/sphere/utils/config.py @@ -93,3 +93,28 @@ def load(self): self._logger.error('An error occured while loading previous configuration file') else: self.save() + + def load_from_file(self, filepath): + ''' + Load configuration from provided file + + Parameters + ---------- + filepath : str + Path of the configuration file + ''' + + if filepath.exists(): + try: + self._logger.info(f'Load configuration file at path {filepath}') + + cfg = None + with open(filepath, 'r') as file: + cfg = json.load(file) + + for key in cfg.keys(): + self.data[key] = cfg[key] + + # self.save() + except: + self._logger.error('An error occured while loading configuration file')