def test_config_file_caching(tmpdir): """ The config file is cached to read if faster as it is read is every single time a LASIF command is executed. """ # Create a new project. pr = Project(str(tmpdir), init_project="TestProject") config = copy.deepcopy(pr.config) domain = copy.deepcopy(pr.domain) del pr # Check that the config file cache has been created. cache = os.path.join(str(tmpdir), "CACHE", "config.xml_cache.pickle") assert os.path.exists(cache) # Delete it. os.remove(cache) assert not os.path.exists(cache) pr = Project(str(tmpdir), init_project="TestProject") # Assert that everything is still the same. assert config == pr.config assert domain == pr.domain del pr # This should have created the cached file. assert os.path.exists(cache) pr = Project(str(tmpdir), init_project="TestProject") # Assert that nothing changed. assert config == pr.config assert domain == pr.domain
def comm_simple(tmpdir): proj_dir = os.path.join( os.path.dirname( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe())))), "tests", "data", "example_project", ) tmpdir = str(tmpdir) shutil.copytree(proj_dir, os.path.join(tmpdir, "proj")) proj_dir = os.path.join(tmpdir, "proj") folder_path = pathlib.Path(proj_dir).absolute() toml_file = folder_path / "lasif_config.toml" config = toml.load(toml_file) config["lasif_project"]["solver_used"] = "other" with open(toml_file, "w") as fh: toml.dump(config, fh) project = Project(project_root_path=folder_path, init_project=False) os.chdir(os.path.abspath(folder_path)) return project.comm
def test_config_file_creation_and_parsing(tmpdir): """ Tests the creation of a default config file and the reading of the file. """ # Create a new project. pr = Project( project_root_path=pathlib.Path(str(tmpdir)).absolute(), init_project="TestProject", ) del pr # Init it once again. pr = Project(pathlib.Path(str(tmpdir)).absolute()) # Assert the config file will test the creation of the default file and the # reading. assert pr.lasif_config["project_name"] == "TestProject" assert pr.lasif_config["description"] == "" assert pr.lasif_config["download_settings"]["channel_priorities"] == [ "BH?", "LH[Z,N,E]", "HH[Z,N,E]", "EH[Z,N,E]", "MH[Z,N,E]", ] assert pr.lasif_config["download_settings"]["location_priorities"] == [ "", "00", "10", "20", "01", "02", ] assert pr.lasif_config["download_settings"]["networks"] == "None" assert (pr.lasif_config["download_settings"]["interstation_distance_in_m"] == 1000.0) assert ( pr.lasif_config["download_settings"]["seconds_after_event"] == 3600.0) assert ( pr.lasif_config["download_settings"]["seconds_before_event"] == 300.0) assert not pr.salvus_settings["attenuation"] assert pr.salvus_settings["absorbing_boundaries_in_km"] == 100.0 assert pr.optimization_settings["misfit_type"] == "tf_phase_misfit"
def dummy_project(tmpdir): """ Fixture returning a communicator instance for a dummy project. The fixture has a couple of convenience method to modify the project to test different things. """ # Create and empty project and get the communicator instance. project_path = os.path.join(str(tmpdir), "DummyProject") proj = Project(project_root_path=project_path, init_project="DummyProject") return proj, proj.comm
def comm(tmpdir): proj_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath( inspect.getfile(inspect.currentframe())))), "data", "ExampleProject") tmpdir = str(tmpdir) shutil.copytree(proj_dir, os.path.join(tmpdir, "proj")) proj_dir = os.path.join(tmpdir, "proj") project = Project(project_root_path=proj_dir, init_project=False) return project.comm
def test_config_file_creation_and_parsing(tmpdir): """ Tests the creation of a default config file and the reading of the file. """ # Create a new project. pr = Project(project_root_path=str(tmpdir), init_project="TestProject") del pr # Init it once again. pr = Project(str(tmpdir)) # Assert the config file will test the creation of the default file and the # reading. assert pr.config["name"] == "TestProject" assert pr.config["description"] == "" assert pr.config["download_settings"]["channel_priorities"] == [ "BH[Z,N,E]", "LH[Z,N,E]", "HH[Z,N,E]", "EH[Z,N,E]", "MH[Z,N,E]" ] assert pr.config["download_settings"]["location_priorities"] == [ "", "00", "10", "20", "01", "02" ] assert pr.config["download_settings"]["interstation_distance_in_m"] == \ 1000.0 assert pr.config["download_settings"]["seconds_after_event"] == 3600.0 assert pr.config["download_settings"]["seconds_before_event"] == 300.0 d = RectangularSphericalSection(min_latitude=-20.0, max_latitude=20.0, min_longitude=-20.0, max_longitude=20.0, min_depth_in_km=0.0, max_depth_in_km=200.0, rotation_angle_in_degree=-45.0, rotation_axis=[1.0, 1.0, 1.0], boundary_width_in_degree=3.0) assert pr.domain == d
def _find_project_comm(self): """ Get lasif communicator. """ from lasif.components.project import Project folder = pathlib.Path(self.lasif_root).absolute() max_folder_depth = 4 for _ in range(max_folder_depth): if (folder / "lasif_config.toml").exists(): return Project(folder).get_communicator() folder = folder.parent raise ValueError(f"Path {self.lasif_root} is not a LASIF project")
def comm(tmpdir): proj_dir = os.path.join( os.path.dirname( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe())))), "data", "example_project", ) tmpdir = str(tmpdir) shutil.copytree(proj_dir, os.path.join(tmpdir, "proj")) proj_dir = os.path.join(tmpdir, "proj") folder_path = pathlib.Path(proj_dir).absolute() project = Project(project_root_path=folder_path, init_project=False) return project.comm
def comm(tmpdir): """ Most visualizations need a valid project in any case, so use one for the tests. """ proj_dir = os.path.join( os.path.dirname( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe())))), "data", "ExampleProject") tmpdir = str(tmpdir) shutil.copytree(proj_dir, os.path.join(tmpdir, "proj")) proj_dir = os.path.join(tmpdir, "proj") project = Project(project_root_path=proj_dir, init_project=False) return project.comm
def communicator(tmpdir): """ Fixture returning the initialized example project. It will be a fresh copy every time that will be deleted after the test has finished so every test can mess with the contents of the folder. """ # A new project will be created many times. ObsPy complains if objects that # already exists are created again. obspy.core.event.ResourceIdentifier\ ._ResourceIdentifier__resource_id_weak_dict.clear() # Copy the example project example_project = os.path.join(DATA, "ExampleProject") project_path = os.path.join(str(tmpdir), "ExampleProject") shutil.copytree(example_project, project_path) # Init it. This will create the missing paths. return Project(project_path).comm
def find_project_comm(folder): """ Will search upwards from the given folder until a folder containing a LASIF root structure is found. The absolute path to the root is returned. :param folder: Path to folder where you want to search from :type folder: Union[str, pathlib.Path, object] """ if isinstance(folder, Communicator): return folder folder = pathlib.Path(folder).absolute() max_folder_depth = 10 folder = folder for _ in range(max_folder_depth): if (folder / "lasif_config.toml").exists(): return Project(folder).get_communicator() folder = folder.parent msg = "Not inside a LASIF project." raise LASIFCommandLineException(msg)
def init_project(project_path: Union[str, pathlib.Path]): """ Create a new project :param project_path: Path to project root directory. Can use absolute paths or relative paths from current working directory. :type project_path: Union[str, pathlib.Path] """ project_path = pathlib.Path(project_path).absolute() if project_path.exists(): msg = "The given PROJECT_PATH already exists. It must not exist yet." raise LASIFError(msg) try: os.makedirs(project_path) except Exception: msg = f"Failed creating directory {project_path}. Permissions?" raise LASIFError(msg) Project(project_root_path=project_path, init_project=project_path.name) print(f"Initialized project in {project_path.name}")