def test_download_resource_file(urlretrieve_mock, verbose, tmp_path, capsys): def dummy_urlretrieve(url, local_path): with open(local_path, "w") as testf: testf.write("TEST") urlretrieve_mock.side_effect = dummy_urlretrieve _download_resource_file( file_url="test_url", short_name="test_file.txt", directory=tmp_path, verbose=verbose, sha256= "94ee059335e587e501cc4bf90613e0814f00a7b08bc7c648fd865a2af6a22cc2", ) urlretrieve_mock.assert_called_with("test_url", tmp_path / "test_file.txt.part") captured = capsys.readouterr() if not verbose: assert captured.out == "" else: assert captured.out == "Downloading: test_url\n" expected_file = tmp_path / "test_file.txt" assert expected_file.exists() assert (_sha256sum(expected_file) == "94ee059335e587e501cc4bf90613e0814f00a7b08bc7c648fd865a2af6a22cc2")
def test_download_resource_file__nosha256(urlretrieve_mock, tmp_path): def dummy_urlretrieve(url, local_path): local_path.touch() urlretrieve_mock.side_effect = dummy_urlretrieve _download_resource_file(file_url="test_url", short_name="test_file.txt", directory=tmp_path) urlretrieve_mock.assert_called_with("test_url", tmp_path / "test_file.txt.part") expected_file = tmp_path / "test_file.txt" assert expected_file.exists()
def test_download_resource_file__bad_sha256sum(urlretrieve_mock, tmp_path): def dummy_urlretrieve(url, local_path): local_path.touch() urlretrieve_mock.side_effect = dummy_urlretrieve with pytest.raises(RuntimeError, match="SHA256 mismatch: test_file.txt"): _download_resource_file( file_url="test_url", short_name="test_file.txt", directory=tmp_path, verbose=False, sha256="test", ) urlretrieve_mock.assert_called_with("test_url", tmp_path / "test_file.txt.part") assert not tmp_path.joinpath("test_file.txt.part").exists() assert not tmp_path.joinpath("test_file.txt").exists()
def download_grids( self, directory: Optional[Union[str, Path]] = None, open_license: bool = True, verbose: bool = False, ) -> None: """ .. versionadded:: 3.0.0 Download missing grids that can be downloaded automatically. .. warning:: There are cases where the URL to download the grid is missing. In those cases, you can enable enable :ref:`debugging-internal-proj` and perform a transformation. The logs will show the grids PROJ searches for. Parameters ---------- directory: str or Path, optional The directory to download the grids to. Defaults to :func:`pyproj.datadir.get_user_data_dir` open_license: bool, optional If True, will only download grids with an open license. Defaults to False. verbose: bool, optional If True, will print information about grids downloaded. Default is False. """ if directory is None: directory = get_user_data_dir(True) for unavailable_operation in self.unavailable_operations: for grid in unavailable_operation.grids: if ( not grid.available and grid.url.endswith(grid.short_name) and grid.direct_download and (grid.open_license or not open_license) ): _download_resource_file( file_url=grid.url, short_name=grid.short_name, directory=directory, verbose=verbose, ) elif not grid.available and verbose: warnings.warn(f"Skipped: {grid}")
def test_download_resource_file__exception(urlretrieve_mock, tmp_path): def dummy_urlretrieve(url, local_path): local_path.touch() raise URLError("Test") urlretrieve_mock.side_effect = dummy_urlretrieve with pytest.raises(URLError): _download_resource_file( file_url="test_url", short_name="test_file.txt", directory=str(tmp_path), verbose=False, sha256="test", ) urlretrieve_mock.assert_called_with("test_url", tmp_path / "test_file.txt.part") assert not tmp_path.joinpath("test_file.txt.part").exists() assert not tmp_path.joinpath("test_file.txt").exists()
def main(): args = parser.parse_args() if hasattr(args, "bbox") and any(( args.bbox, args.list_files, args.all, args.source_id, args.area_of_use, args.file, )): if args.all and (args.list_files or args.source_id or args.area_of_use or args.file or args.bbox): raise RuntimeError( "Cannot use '--all' with '--list-files', '--source-id'," "'--area-of-use', '--bbox', or '--file'.") bbox = None if args.bbox is not None: west, south, east, north = args.bbox.split(",") bbox = BBox( west=float(west), south=float(south), east=float(east), north=float(north), ) if args.target_directory and args.system_directory: raise RuntimeError( "Cannot set both --target-directory and --system-directory.") target_directory = args.target_directory if args.system_directory: target_directory = get_data_dir().split(os.path.sep)[0] elif not target_directory: target_directory = get_user_data_dir(True) grids = get_transform_grid_list( source_id=args.source_id, area_of_use=args.area_of_use, filename=args.file, bbox=bbox, spatial_test=args.spatial_test, include_world_coverage=not args.exclude_world_coverage, include_already_downloaded=args.include_already_downloaded, target_directory=target_directory, ) if args.list_files: print("filename | source_id | area_of_use") print("----------------------------------") else: endpoint = get_proj_endpoint() for grid in grids: if args.list_files: print( grid["properties"]["name"], grid["properties"]["source_id"], grid["properties"].get("area_of_use"), sep=" | ", ) else: filename = grid["properties"]["name"] _download_resource_file( file_url=f"{endpoint}/{filename}", short_name=filename, directory=target_directory, verbose=args.verbose, sha256=grid["properties"]["sha256sum"], ) elif not hasattr(args, "bbox") and args.verbose: _show_versions.show_versions() elif hasattr(args, "bbox"): sync_parser.print_help() else: parser.print_help()