Remove config contraction
This commit is contained in:
parent
c9ac940a27
commit
f972609e4c
1 changed files with 4 additions and 78 deletions
|
|
@ -54,17 +54,11 @@ class OpenflexureSettingsFile:
|
||||||
expand (bool): Expand paths to valid auxillary config files.
|
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
|
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
|
# Set arguments
|
||||||
self.config_path = config_path or USER_CONFIG_FILE_PATH
|
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 basic config file with defaults if it doesn't exist
|
||||||
initialise_file(self.config_path, populate=DEFAULT_CONFIG)
|
initialise_file(self.config_path, populate=DEFAULT_CONFIG)
|
||||||
|
|
@ -76,11 +70,6 @@ class OpenflexureSettingsFile:
|
||||||
# Unexpanded config dictionary (used at load/save time)
|
# Unexpanded config dictionary (used at load/save time)
|
||||||
loaded_config = load_json_file(self.config_path)
|
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")
|
logging.debug("Reading settings from disk")
|
||||||
return loaded_config
|
return loaded_config
|
||||||
|
|
||||||
|
|
@ -88,12 +77,8 @@ class OpenflexureSettingsFile:
|
||||||
"""
|
"""
|
||||||
Save config to a file on-disk, and splits into auxillary config files if available.
|
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:
|
save_settings = config
|
||||||
# Contract self._config into self.raw_config
|
|
||||||
save_settings = self.contract_config(config)
|
|
||||||
else:
|
|
||||||
save_settings = config
|
|
||||||
|
|
||||||
if backup:
|
if backup:
|
||||||
if os.path.isfile(self.config_path):
|
if os.path.isfile(self.config_path):
|
||||||
|
|
@ -109,68 +94,9 @@ class OpenflexureSettingsFile:
|
||||||
|
|
||||||
return settings
|
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
|
# HANDLE BASIC LOADING AND SAVING OF SETTINGS FILES
|
||||||
|
|
||||||
|
|
||||||
def load_json_file(config_path) -> dict:
|
def load_json_file(config_path) -> dict:
|
||||||
"""
|
"""
|
||||||
Open a .json config file
|
Open a .json config file
|
||||||
|
|
@ -262,4 +188,4 @@ with open(DEFAULT_CONFIG_FILE_PATH, "r") as default_rc:
|
||||||
DEFAULT_CONFIG = default_rc.read()
|
DEFAULT_CONFIG = default_rc.read()
|
||||||
|
|
||||||
# Create the default user settings object
|
# 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…
Add table
Add a link
Reference in a new issue