Rewritten to remove config redundancy

This commit is contained in:
Joel Collins 2019-01-31 13:28:15 +00:00
parent cb122e5ef6
commit e5c10adf8d

View file

@ -3,7 +3,6 @@ import os
import errno import errno
import logging import logging
import shutil import shutil
import copy
HERE = os.path.abspath(os.path.dirname(__file__)) HERE = os.path.abspath(os.path.dirname(__file__))
DEFAULT_CONFIG_PATH = os.path.join(HERE, 'microscoperc.default.yaml') DEFAULT_CONFIG_PATH = os.path.join(HERE, 'microscoperc.default.yaml')
@ -11,95 +10,11 @@ DEFAULT_CONFIG_PATH = os.path.join(HERE, 'microscoperc.default.yaml')
USER_CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".openflexure") #: str: Default path of the user-config directory, containing runtime-config and calibration files. Obtained from ``os.path.join(os.path.expanduser("~"), ".openflexure")``. USER_CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".openflexure") #: str: Default path of the user-config directory, containing runtime-config and calibration files. Obtained from ``os.path.join(os.path.expanduser("~"), ".openflexure")``.
USER_CONFIG_FILE = os.path.join(USER_CONFIG_DIR, "microscoperc.yaml") #: str: Default path of the user microscoperc.yaml runtime-config file. Obtained from ``os.path.join(USER_CONFIG_DIR, "microscoperc.yaml")`` USER_CONFIG_FILE = os.path.join(USER_CONFIG_DIR, "microscoperc.yaml") #: str: Default path of the user microscoperc.yaml runtime-config file. Obtained from ``os.path.join(USER_CONFIG_DIR, "microscoperc.yaml")``
with open(DEFAULT_CONFIG_PATH, 'r') as defaultrc: with open(DEFAULT_CONFIG_PATH, 'r') as default_rc:
DEFAULT_CONFIG = defaultrc.read() DEFAULT_CONFIG = default_rc.read()
class OpenflexureConfig(): def load_yaml_file(config_path) -> dict:
expandable_keys = [
'picamera_settings',
'openflexure_stage_settings',
] #: List of keys that can be passed as a file path string and expanded automatically
def __init__(self, config_path: str=None, expand: bool=True):
global DEFAULT_CONFIG, USER_CONFIG_FILE
# Set arguments
self.config_path = config_path or USER_CONFIG_FILE
self.expand = expand
# Create empty config dictionaries
self.raw_config = {}
# Expanded config dictionary (used by server)
self.config = copy.copy(self.raw_config)
# Initialise basic config file with defaults if it doesn't exist
self.initialise_file(self.config_path, populate=DEFAULT_CONFIG)
# Load the config in, setting self._config and self.config
self.load()
def update(self, update_dict: dict):
self.config.update(update_dict)
def overwrite(self, new_dict: dict):
self.config = new_dict
def load(self):
# Unexpanded config dictionary (used at load/save time)
self.raw_config = self.load_yaml_file(self.config_path)
# If the loaded config is in contracted format
if self.expand:
# Expand self.raw_config into self._config
self.expand_config()
def save(self, backup: bool=True):
# If the loaded config was in contracted format
if self.expand:
# Contract self._config into self.raw_config
self.contract_config()
if backup:
if os.path.isfile(self.config_path):
shutil.copyfile(self.config_path, self.config_path+".bk")
self.save_yaml_file(self.config_path, self.raw_config)
def expand_config(self):
# For each value in the raw loaded config
for key, value in self.raw_config.items():
# If it's a valid expandable parameter
if (key in OpenflexureConfig.expandable_keys and
type(value) is str):
logging.debug("Expanding {}".format(value))
# Initialise, load and expand
self.initialise_file(value)
self.config[key] = self.load_yaml_file(value) or {}
else:
self.config[key] = value
def contract_config(self):
for key, value in self.config.items():
# If it's a valid expandable parameter
if (key in OpenflexureConfig.expandable_keys and
type(value) is dict):
# Create the file if it doesn't exist
self.initialise_file(self.raw_config[key])
self.save_yaml_file(self.raw_config[key], value)
else:
self.raw_config[key] = value
def load_yaml_file(self, config_path) -> dict:
""" """
Open a .yaml config file Open a .yaml config file
@ -117,13 +32,14 @@ class OpenflexureConfig():
return config_data return config_data
def save_yaml_file(self, config_path: str, config_dict: dict, safe: bool=False): def save_yaml_file(config_path: str, config_dict: dict, safe: bool = False):
""" """
Save a .yaml config file Save a .yaml config file
Args: Args:
config_dict (dict): Dictionary of config data to save. config_dict (dict): Dictionary of config data to save.
config_path (str): Path to the config YAML file. config_path (str): Path to the config YAML file.
safe (bool): Whether to use PyYAML safe_dump instead of dump
""" """
config_path = os.path.expanduser(config_path) config_path = os.path.expanduser(config_path)
@ -136,7 +52,16 @@ class OpenflexureConfig():
yaml.safe_dump(config_dict, outfile) yaml.safe_dump(config_dict, outfile)
def initialise_file(self, config_path, populate: str=""): def create_file(config_path):
if not os.path.exists(os.path.dirname(config_path)):
try:
os.makedirs(os.path.dirname(config_path))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
def initialise_file(config_path, populate: str = ""):
""" """
Check if a file exists, and if not, create it Check if a file exists, and if not, create it
and optionally populate it with content and optionally populate it with content
@ -152,17 +77,111 @@ class OpenflexureConfig():
if not os.path.exists(config_path): # If user config file doesn't exist if not os.path.exists(config_path): # If user config file doesn't exist
logging.warning("No config file found at {}. Creating...".format(config_path)) logging.warning("No config file found at {}. Creating...".format(config_path))
self.create_file(config_path) create_file(config_path)
logging.info("Populating {}...".format(config_path)) logging.info("Populating {}...".format(config_path))
with open(config_path, 'w') as outfile: with open(config_path, 'w') as outfile:
outfile.write(populate) outfile.write(populate)
def create_file(self, config_path): class OpenflexureConfig:
if not os.path.exists(os.path.dirname(config_path)):
try: def __init__(self, config_path: str = None, expand: bool = True):
os.makedirs(os.path.dirname(config_path)) global DEFAULT_CONFIG, USER_CONFIG_FILE
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST: self.expandable_keys = {
raise 'picamera_settings': None,
'openflexure_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
self.expand = expand
# Create empty config dictionaries
self._config = {}
# Initialise basic config file with defaults if it doesn't exist
initialise_file(self.config_path, populate=DEFAULT_CONFIG)
# Load the config in, setting self._config and self.config
self.load()
@property
def config(self):
return self._config
def asdict(self):
return self._config
def update(self, update_dict: dict):
self._config.update(update_dict)
def overwrite(self, new_dict: dict):
self._config = new_dict
def load(self):
# Unexpanded config dictionary (used at load/save time)
self._config = load_yaml_file(self.config_path)
# If the loaded config is in contracted format
if self.expand:
# Expand self.raw_config into self._config
self._config = self.expand_config(self._config)
def save(self, backup: bool = True):
# If the loaded config was in contracted format
if self.expand:
# Contract self._config into self.raw_config
save_config = self.contract_config(self._config)
else:
save_config = self._config
if backup:
if os.path.isfile(self.config_path):
shutil.copyfile(self.config_path, self.config_path+".bk")
save_yaml_file(self.config_path, save_config)
def expand_config(self, config_dict):
return_config = {}
# 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_yaml_file(value) or {}
else:
return_config[key] = value
return return_config
def contract_config(self, config_dict):
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]))
logging.debug(value)
# Create the file if it doesn't exist
initialise_file(self.expandable_keys[key])
# Save the expanded config dictionary to the file
save_yaml_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