125 lines
4.5 KiB
Python
125 lines
4.5 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from iottb import definitions
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
DB_NAME = definitions.DB_NAME
|
|
|
|
|
|
class IottbConfig:
|
|
""" Class to handle operations on the testbed configuration.
|
|
|
|
TODO: Add instead of overwrite Database locations when initializing if a location with valid db
|
|
exists.
|
|
"""
|
|
|
|
@staticmethod
|
|
def warn():
|
|
logger.warning(f'DatabaseLocations are DatabaseLocationMap in the class {__name__}')
|
|
|
|
def __init__(self, cfg_file=definitions.CFG_FILE_PATH):
|
|
logger.info('Initializing Config object')
|
|
IottbConfig.warn()
|
|
self.cfg_file = Path(cfg_file)
|
|
self.default_database = None
|
|
self.default_db_location = None
|
|
self.db_path_dict = dict()
|
|
self.load_config()
|
|
|
|
def create_default_config(self):
|
|
"""Create default iottb config file."""
|
|
logger.info(f'Creating default config file at {self.cfg_file}')
|
|
self.default_database = DB_NAME
|
|
self.default_db_location = str(Path.home())
|
|
self.db_path_dict = {
|
|
DB_NAME: self.default_db_location
|
|
}
|
|
|
|
defaults = {
|
|
'DefaultDatabase': self.default_database,
|
|
'DefaultDatabasePath': self.default_db_location,
|
|
'DatabaseLocations': self.db_path_dict
|
|
}
|
|
|
|
try:
|
|
self.cfg_file.parent.mkdir(parents=True, exist_ok=True)
|
|
with self.cfg_file.open('w') as config_file:
|
|
json.dump(defaults, config_file, indent=4)
|
|
except IOError as e:
|
|
logger.error(f"Failed to create default configuration file at {self.cfg_file}: {e}")
|
|
raise RuntimeError(f"Failed to create configuration file: {e}") from e
|
|
|
|
def load_config(self):
|
|
"""Loads or creates default configuration from given file path."""
|
|
logger.info('Loading configuration file')
|
|
if not self.cfg_file.is_file():
|
|
logger.info('Config file does not exist.')
|
|
self.create_default_config()
|
|
else:
|
|
logger.info('Config file exists, opening.')
|
|
with self.cfg_file.open('r') as config_file:
|
|
data = json.load(config_file)
|
|
self.default_database = data.get('DefaultDatabase')
|
|
self.default_db_location = data.get('DefaultDatabasePath')
|
|
self.db_path_dict = data.get('DatabaseLocations', {})
|
|
|
|
def save_config(self):
|
|
"""Save the current configuration to the config file."""
|
|
data = {
|
|
'DefaultDatabase': self.default_database,
|
|
'DefaultDatabasePath': self.default_db_location,
|
|
'DatabaseLocations': self.db_path_dict
|
|
}
|
|
try:
|
|
with self.cfg_file.open('w') as config_file:
|
|
json.dump(data, config_file, indent=4)
|
|
except IOError as e:
|
|
logger.error(f"Failed to save configuration file at {self.cfg_file}: {e}")
|
|
raise RuntimeError(f"Failed to save configuration file: {e}") from e
|
|
|
|
def set_default_database(self, name, path):
|
|
"""Set the default database and its path."""
|
|
self.default_database = name
|
|
self.default_db_location = path
|
|
self.db_path_dict[name] = path
|
|
|
|
def get_default_database_location(self):
|
|
return self.default_db_location
|
|
|
|
def get_default_database(self):
|
|
return self.default_database
|
|
|
|
def get_database_location(self, name):
|
|
"""Get the location of a specific database."""
|
|
return self.db_path_dict.get(name)
|
|
|
|
def set_database_location(self, name, path):
|
|
"""Set the location for a database."""
|
|
logger.debug(f'Type of "path" parameter {type(path)}')
|
|
logger.debug(f'String value of "path" parameter {str(path)}')
|
|
logger.debug(f'Type of "name" parameter {type(name)}')
|
|
logger.debug(f'String value of "name" parameter {str(name)}')
|
|
path = Path(path)
|
|
name = Path(name)
|
|
logger.debug(f'path:name = {path}:{name}')
|
|
if path.name == name:
|
|
path = path.parent
|
|
self.db_path_dict[str(name)] = str(path)
|
|
|
|
def get_known_databases(self):
|
|
"""Get the set of known databases"""
|
|
logger.info(f'Getting known databases.')
|
|
|
|
return self.db_path_dict.keys()
|
|
|
|
def get_know_database_paths(self):
|
|
"""Get the paths of all known databases"""
|
|
logger.info(f'Getting known database paths.')
|
|
return self.db_path_dict.values()
|
|
|
|
def get_full_default_path(self):
|
|
return Path(self.default_db_location) / self.default_database
|