Loading openflexure_microscope/config.py +4 −78 Original line number Diff line number Diff line Loading @@ -54,17 +54,11 @@ class OpenflexureSettingsFile: expand (bool): Expand paths to valid auxillary config files. """ def __init__(self, config_path: str = None, expand: bool = True): def __init__(self, config_path: str = None): global DEFAULT_CONFIG, USER_CONFIG_FILE_PATH self.expandable_keys = { "camera_settings": None, "stage_settings": None, } #: Dictionary of keys that can be passed as a file path string and expanded automatically # Set arguments self.config_path = config_path or USER_CONFIG_FILE_PATH self.expand = expand # Initialise basic config file with defaults if it doesn't exist initialise_file(self.config_path, populate=DEFAULT_CONFIG) Loading @@ -76,11 +70,6 @@ class OpenflexureSettingsFile: # Unexpanded config dictionary (used at load/save time) loaded_config = load_json_file(self.config_path) # If the loaded config is in contracted format if self.expand: # Expand self.raw_config into self._config loaded_config = self.expand_config(loaded_config) logging.debug("Reading settings from disk") return loaded_config Loading @@ -88,11 +77,7 @@ class OpenflexureSettingsFile: """ Save config to a file on-disk, and splits into auxillary config files if available. """ # If the loaded config was in contracted format if self.expand: # Contract self._config into self.raw_config save_settings = self.contract_config(config) else: save_settings = config if backup: Loading @@ -109,68 +94,9 @@ class OpenflexureSettingsFile: return settings def expand_config(self, config_dict): """ Search a config dictionary for paths to valid auxillary config files, and expand into the config dictionary if available. Args: config_dict (dict): Dictionary of config data to expand """ return_config = {} if not config_dict: config_dict = {} # For each value in the raw loaded config for key, value in config_dict.items(): # If it's a valid expandable parameter if key in self.expandable_keys and type(value) is str: logging.debug("Expanding {}".format(value)) # Store expansion path self.expandable_keys[key] = config_dict[key] # Create the expansion file if it doesn't yet exist initialise_file(value) # Load the expansion file into _config return_config[key] = load_json_file(value) or {} else: return_config[key] = value return return_config def contract_config(self, config_dict): """ Split a config dictionary into auxillary config files, if available. Args: config_dict (dict): Dictionary of config data to contract/split """ return_config = {} # For each value in the expanded config for key, value in config_dict.items(): # If it's a valid expandable parameter if ( key in self.expandable_keys and self.expandable_keys[key] is not None and type(value) is dict ): logging.debug("Saving to {}".format(self.expandable_keys[key])) # Create the file if it doesn't exist initialise_file(self.expandable_keys[key]) # Save the expanded config dictionary to the file save_json_file(self.expandable_keys[key], value) # Replace the expanded dictionary with a file path return_config[key] = self.expandable_keys[key] else: return_config[key] = value return return_config # HANDLE BASIC LOADING AND SAVING OF SETTINGS FILES def load_json_file(config_path) -> dict: """ Open a .json config file Loading Loading @@ -262,4 +188,4 @@ with open(DEFAULT_CONFIG_FILE_PATH, "r") as default_rc: DEFAULT_CONFIG = default_rc.read() # Create the default user settings object user_settings = OpenflexureSettingsFile(config_path=USER_CONFIG_FILE_PATH, expand=True) user_settings = OpenflexureSettingsFile(config_path=USER_CONFIG_FILE_PATH) Loading
openflexure_microscope/config.py +4 −78 Original line number Diff line number Diff line Loading @@ -54,17 +54,11 @@ class OpenflexureSettingsFile: expand (bool): Expand paths to valid auxillary config files. """ def __init__(self, config_path: str = None, expand: bool = True): def __init__(self, config_path: str = None): global DEFAULT_CONFIG, USER_CONFIG_FILE_PATH self.expandable_keys = { "camera_settings": None, "stage_settings": None, } #: Dictionary of keys that can be passed as a file path string and expanded automatically # Set arguments self.config_path = config_path or USER_CONFIG_FILE_PATH self.expand = expand # Initialise basic config file with defaults if it doesn't exist initialise_file(self.config_path, populate=DEFAULT_CONFIG) Loading @@ -76,11 +70,6 @@ class OpenflexureSettingsFile: # Unexpanded config dictionary (used at load/save time) loaded_config = load_json_file(self.config_path) # If the loaded config is in contracted format if self.expand: # Expand self.raw_config into self._config loaded_config = self.expand_config(loaded_config) logging.debug("Reading settings from disk") return loaded_config Loading @@ -88,11 +77,7 @@ class OpenflexureSettingsFile: """ Save config to a file on-disk, and splits into auxillary config files if available. """ # If the loaded config was in contracted format if self.expand: # Contract self._config into self.raw_config save_settings = self.contract_config(config) else: save_settings = config if backup: Loading @@ -109,68 +94,9 @@ class OpenflexureSettingsFile: return settings def expand_config(self, config_dict): """ Search a config dictionary for paths to valid auxillary config files, and expand into the config dictionary if available. Args: config_dict (dict): Dictionary of config data to expand """ return_config = {} if not config_dict: config_dict = {} # For each value in the raw loaded config for key, value in config_dict.items(): # If it's a valid expandable parameter if key in self.expandable_keys and type(value) is str: logging.debug("Expanding {}".format(value)) # Store expansion path self.expandable_keys[key] = config_dict[key] # Create the expansion file if it doesn't yet exist initialise_file(value) # Load the expansion file into _config return_config[key] = load_json_file(value) or {} else: return_config[key] = value return return_config def contract_config(self, config_dict): """ Split a config dictionary into auxillary config files, if available. Args: config_dict (dict): Dictionary of config data to contract/split """ return_config = {} # For each value in the expanded config for key, value in config_dict.items(): # If it's a valid expandable parameter if ( key in self.expandable_keys and self.expandable_keys[key] is not None and type(value) is dict ): logging.debug("Saving to {}".format(self.expandable_keys[key])) # Create the file if it doesn't exist initialise_file(self.expandable_keys[key]) # Save the expanded config dictionary to the file save_json_file(self.expandable_keys[key], value) # Replace the expanded dictionary with a file path return_config[key] = self.expandable_keys[key] else: return_config[key] = value return return_config # HANDLE BASIC LOADING AND SAVING OF SETTINGS FILES def load_json_file(config_path) -> dict: """ Open a .json config file Loading Loading @@ -262,4 +188,4 @@ with open(DEFAULT_CONFIG_FILE_PATH, "r") as default_rc: DEFAULT_CONFIG = default_rc.read() # Create the default user settings object user_settings = OpenflexureSettingsFile(config_path=USER_CONFIG_FILE_PATH, expand=True) user_settings = OpenflexureSettingsFile(config_path=USER_CONFIG_FILE_PATH)