Skip to content

Commit

Permalink
Add possibility to load a default user configuration file in IRDIS.Im…
Browse files Browse the repository at this point in the history
…agingReduction()

Progress on #73
  • Loading branch information
Arthur Vigan committed Jun 13, 2022
1 parent 2a73804 commit c1fefd9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
14 changes: 12 additions & 2 deletions sphere/IRDIS/ImagingReduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
#
Expand Down
25 changes: 25 additions & 0 deletions sphere/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit c1fefd9

Please sign in to comment.