def get_install_paths(install_config_name="install.ini"): """Pick the necessary paths to configure the external files for the transit route.""" source_dir = ObjDirectory(__name__, "config") user_data = UserDataDirectory("dtocean_logistics", "DTOcean", "config") user_data_map = DirectoryMap(user_data, source_dir) user_data_map.safe_copy_file(install_config_name, "{}.txt".format(install_config_name)) user_ini_reader = ReadINI(user_data_map, install_config_name) # Get the root path from the site data path. site_data = SiteDataDirectory("DTOcean Logistics", "DTOcean") site_ini_reader = ReadINI(site_data, install_config_name) if user_ini_reader.config_exists(): config = user_ini_reader.get_config() elif site_ini_reader.config_exists(): config = site_ini_reader.get_config() else: errStr = ("No suitable configuration file found at paths " "{} or {}").format(site_ini_reader.get_config_path(), user_ini_reader.get_config_path()) raise RuntimeError(errStr) path_dict = { "logistics_include": config["dtocean_logistics"]["include_path"] } return path_dict
def get_install_paths(): """Pick the necessary paths to configure the external files for the wave and tidal packages.""" source_dir = ObjDirectory(__name__, "config") user_data = UserDataDirectory("dtocean_hydro", "DTOcean", "config") user_data_map = DirectoryMap(user_data, source_dir) install_src_name = "install.ini" # Check for bundled indicator file if source_dir.isfile(".bundled"): install_dst_name = "install_bundled.ini" else: install_dst_name = "install.ini" log_msg = ("Install configuration file name set to " "'{}'").format(install_dst_name) module_logger.debug(log_msg) user_data_map.safe_copy_file(install_src_name, "{}.txt".format(install_dst_name)) user_ini_reader = ReadINI(user_data_map, install_dst_name) # Get the root path from the site data path. site_data = SiteDataDirectory("DTOcean Hydrodynamics", "DTOcean") site_ini_reader = ReadINI(site_data, install_dst_name) if user_ini_reader.config_exists(): config = user_ini_reader.get_config() elif site_ini_reader.config_exists(): config = site_ini_reader.get_config() else: errStr = ("No suitable configuration file found at paths " "{} or {}").format(site_ini_reader.get_config_path(), user_ini_reader.get_config_path()) raise RuntimeError(errStr) path_dict = {"bin" : config["dtocean_wec"]["bin_path"], "wec_include" : config["dtocean_wec"]["include_path"], "tidal_include" : config["dtocean_tidal"]["include_path"] } return path_dict
def start_logging(level=None): """Start python logger""" objdir = ObjDirectory(__name__, "config") datadir = UserDataDirectory("dtocean_hydro", "DTOcean", "config") dirmap = DirectoryMap(datadir, objdir) log = Logger(dirmap) log("dtocean_hydro", level=level, info_message="Begin logging for dtocean_hydro.")
def test_user_config_exists(tmpdir, directory): '''Test if the user_config_exists function returns false''' # Make a local directory locd = tmpdir.mkdir("config") configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) logger = Logger(dirmap) assert not configdir.isfile(logger.config_file_name)
def init_config(logging=False, files=False, install=False, overwrite=False): """Copy config files to user data directory""" if not any([logging, files, install]): return objdir = ObjDirectory(__name__, "..", "config") datadir = UserDataDirectory("dtocean_app", "DTOcean", "config") dirmap = DirectoryMap(datadir, objdir) if logging: dirmap.copy_file("logging.yaml", overwrite=overwrite) if files: dirmap.copy_file("files.ini", overwrite=overwrite) if install: dirmap.copy_file("install.ini", overwrite=overwrite) return datadir.get_path()
def test_call_logger_options(tmpdir, directory): '''Test if logger can be called''' # Make a local directory locd = tmpdir.mkdir("config") configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) logger = Logger(dirmap) logger('my_logger', level="CRITICAL", info_message="test") assert True
def test_call_logger(tmpdir, directory): '''Test if logger can be called''' # Make a local directory locd = tmpdir.mkdir("config") configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) logger = Logger(dirmap) logger('my_logger') assert True
def init_config(install=False, overwrite=False): """Copy config files to user data directory""" objdir = ObjDirectory(__name__, "config") datadir = UserDataDirectory("dtocean_app", "DTOcean", "config") dirmap = DirectoryMap(datadir, objdir) dirmap.copy_file("logging.yaml", overwrite=overwrite) dirmap.copy_file("files.ini", overwrite=overwrite) # Copy the manuals installation configuration if install: dirmap.copy_file("install.ini", overwrite=overwrite) return
def get_config(config_name="configuration.ini", valid_name="validation.ini"): """Pick the necessary paths to configure the external files for the wave and tidal packages.""" source_dir = ObjDirectory(__name__, "config") user_data = UserDataDirectory("dtocean_dummy", "DTOcean", "config") user_data_map = DirectoryMap(user_data, source_dir) user_ini_reader = ReadINI(user_data_map, config_name, valid_name) user_ini_reader.copy_config() # Collect the configuration data config = user_ini_reader.get_valid_config() return config
def test_config_exists(tmpdir, directory): '''Test that the configuration file is read correctly''' # Make a local directory locd = tmpdir.mkdir("config") # Create Logger object and change path to user data dir to tmp configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) # Copy config and check for existance ini_reader = ReadINI(dirmap) ini_reader.copy_config() test = ini_reader.config_exists() assert test
def test_write_yaml(tmpdir, directory): '''Test if the configuration file is correctly copied.''' # Make a local directory locd = tmpdir.mkdir("config") # Create Logger object and change path to user data dir to tmp configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) yaml_reader = ReadYAML(dirmap, "logging.yaml") test_list = ["curly", "larry", "moe"] yaml_reader.write(test_list) assert os.path.basename(str(locd.listdir()[0])) == "logging.yaml"
def test_read_yaml(tmpdir, directory): '''Test if the configuration file is correctly copied.''' # Make a local directory locd = tmpdir.mkdir("config") # Create Logger object and change path to user data dir to tmp configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) yaml_reader = ReadYAML(dirmap, "logging.yaml") yaml_reader.copy_config() yaml_dict = yaml_reader.read() assert "loggers" in yaml_dict
def test_configure_logger(tmpdir, directory): '''Test if logger configuration can be loaded''' # Make a local directory locd = tmpdir.mkdir("config") configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) logger = Logger(dirmap) # Copy the logging file logger.copy_config() # Attempt to configure the logger log_config_dict = logger.read() logger.configure_logger(log_config_dict)
def test_copy_logger_config(tmpdir, directory): '''Test if logger configuration is copied''' # Make a local directory locd = tmpdir.mkdir("config") locp = locd.join("logging.yaml") configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) logger = Logger(dirmap) # Copy the logging file logger.copy_config() assert dirmap.last_copy_path == str(locp) assert len(locd.listdir()) == 1 assert os.path.basename(str(locd.listdir()[0])) == "logging.yaml"
def test_copy_ini_config(tmpdir, directory): '''Test if the configuration file is correctly copied.''' # Make a local directory locd = tmpdir.mkdir("config") locp = locd.join("configuration.ini") # Create Logger object and change path to user data dir to tmp configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) ini_reader = ReadINI(dirmap) # Copy the logging file ini_reader.copy_config() assert dirmap.last_copy_path == str(locp) assert len(locd.listdir()) == 1 assert os.path.basename(str(locd.listdir()[0])) == "configuration.ini"
def test_not_valid_config(tmpdir, directory): '''Test that the configuration file is read correctly''' # Make a local directory locd = tmpdir.mkdir("config") # Create Logger object and change path to user data dir to tmp configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) ini_reader = ReadINI(dirmap, config_file_name="bad_configuration.ini", validation_file_name="validation.ini") # Copy the logging file ini_reader.copy_config() with pytest.raises(ValidateError): # Read the config file ini_reader.get_valid_config()
def test_get_valid_config(tmpdir, directory): '''Test that the configuration file is read correctly''' # Make a local directory locd = tmpdir.mkdir("config") # Create Logger object and change path to user data dir to tmp configdir = Directory(str(locd)) dirmap = DirectoryMap(configdir, directory) ini_reader = ReadINI(dirmap, validation_file_name="validation.ini") # Copy the logging file ini_reader.copy_config() # Read the config file config = ini_reader.get_valid_config() assert isinstance(config, configobj.ConfigObj) assert set(config.keys()) == set(['Spreadsheet']) assert set(config['Spreadsheet'].keys()) == set(['high', 'low'])