Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyogrio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
read_bounds,
read_info,
set_gdal_config_options,
vsi_curl_clear_cache,
vsi_listtree,
vsi_rmtree,
vsi_unlink,
Expand All @@ -49,6 +50,7 @@
"read_dataframe",
"read_info",
"set_gdal_config_options",
"vsi_curl_clear_cache",
"vsi_listtree",
"vsi_rmtree",
"vsi_unlink",
Expand Down
2 changes: 2 additions & 0 deletions pyogrio/_ogr.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ cdef extern from "cpl_vsi.h" nogil:
int VSIFCloseL(VSILFILE *fp)
int VSIFFlushL(VSILFILE *fp)
int VSIUnlink(const char *path)
void VSICurlPartialClearCache(const char *prefix)
void VSICurlClearCache()

VSILFILE* VSIFileFromMemBuffer(const char *path,
void *data,
Expand Down
28 changes: 28 additions & 0 deletions pyogrio/_vsi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,31 @@ def ogr_vsi_unlink(str path):
errcode = VSIUnlink(path_c)
if errcode != 0:
raise OSError(f"Error removing '{path}': {errcode=}")


def ogr_vsi_curl_clear_all_cache():
"""Clean local cache associated with /vsicurl/.

"""

VSICurlClearCache()


def ogr_vsi_curl_clear_cache(str prefix):
"""Clean local cache associated with /vsicurl/.

Parameters
----------
prefix : str
Filename or prefix to clear associated cache.

"""
cdef const char *prefix_c

try:
prefix_b = prefix.encode("UTF-8")
except UnicodeDecodeError:
prefix_b = prefix
prefix_c = prefix_b

VSICurlPartialClearCache(prefix_c)
19 changes: 19 additions & 0 deletions pyogrio/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_mask_to_wkb,
_preprocess_options_key_value,
get_vsi_path_or_buffer,
vsi_path,
)

with GDALEnv():
Expand All @@ -26,6 +27,8 @@
set_gdal_config_options as _set_gdal_config_options,
)
from pyogrio._vsi import (
ogr_vsi_curl_clear_all_cache,
ogr_vsi_curl_clear_cache,
ogr_vsi_listtree,
ogr_vsi_rmtree,
ogr_vsi_unlink,
Expand Down Expand Up @@ -385,3 +388,19 @@ def vsi_unlink(path: str | Path):
path = path.as_posix()

ogr_vsi_unlink(path)


def vsi_curl_clear_cache(prefix: str = ""):
"""Clean local cache associated with /vsicurl/.

Parameters
----------
prefix : str
Filename or prefix to clear associated cache. If not specified clear all cache.

"""
if prefix == "":
ogr_vsi_curl_clear_all_cache()
else:
vsi_prefix = vsi_path(prefix)
ogr_vsi_curl_clear_cache(vsi_prefix)
11 changes: 11 additions & 0 deletions pyogrio/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
read_bounds,
read_info,
set_gdal_config_options,
vsi_curl_clear_cache,
vsi_listtree,
vsi_rmtree,
vsi_unlink,
Expand Down Expand Up @@ -700,3 +701,13 @@ def test_vsimem_unlink_error(naturalearth_lowres_vsimem):

with pytest.raises(FileNotFoundError, match="Path does not exist"):
vsi_unlink("/vsimem/non-existent.gpkg")


def test_vsi_curl_clear_cache_empty_default():
# Validate call doesn't raise any error when no prefix is used
vsi_curl_clear_cache()


def test_vsi_curl_clear_cache():
# Validate call doesn't raise any error when prefix/path is used
vsi_curl_clear_cache("http://example.com/prefix")
Loading