Skip to content

Commit cb3dc26

Browse files
committed
feat: add cache clear CLI command
1 parent 5b53cd4 commit cb3dc26

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ None at present
3636
ckanext.iso19115.misc.cache_dir = /var/data/iso19115_cache
3737
```
3838

39+
## CLI
40+
41+
Clear compiled schema caches:
42+
43+
```sh
44+
ckan -c /path/to/ckan.ini iso19115 cache clear
45+
```
46+
3947
## Usage
4048

4149
Customize the way of mapping dataset into ISO 19115 by implementing `IIso18115` interface.

ckanext/iso19115/cli.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,52 @@ def build_example(
8181
click.echo(example)
8282

8383

84+
@iso19115.group()
85+
def cache():
86+
"""Cache management."""
87+
pass
88+
89+
90+
@cache.command("clear")
91+
def cache_clear():
92+
"""Remove compiled schema cache files."""
93+
cache_dir = utils.get_cache_dir(create=False)
94+
persistent_dir = tk.config.get(utils.CONFIG_CACHE_DIR)
95+
96+
if not persistent_dir:
97+
click.echo(
98+
f"Persistent cache directory is not configured "
99+
f"({utils.CONFIG_CACHE_DIR}). Using temporary path: {cache_dir}"
100+
)
101+
102+
if not cache_dir.exists():
103+
click.echo(f"Cache directory {cache_dir} does not exist.")
104+
return
105+
106+
if not cache_dir.is_dir():
107+
raise click.ClickException(f"Cache path {cache_dir} is not a directory.")
108+
109+
removed = 0
110+
errors = []
111+
for cache_file in cache_dir.glob("*.pickle"):
112+
try:
113+
cache_file.unlink()
114+
removed += 1
115+
except OSError as exc:
116+
errors.append((cache_file, exc))
117+
118+
for path, exc in errors:
119+
tk.error_shout(f"Failed to remove {path}: {exc}")
120+
121+
if errors:
122+
raise click.ClickException("Unable to clear cache completely.")
123+
124+
if removed:
125+
click.secho(f"Removed {removed} cache file(s) from {cache_dir}", fg="green")
126+
else:
127+
click.echo(f"No cache files found in {cache_dir}")
128+
129+
84130
@iso19115.group()
85131
def validate():
86132
"""Validate data agains ISO 19115 schema."""

ckanext/iso19115/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,15 @@
8585
), f"Schema {f} does not exists. Have you extracted namespaces.zip?"
8686

8787

88-
def _get_cache_path(name):
88+
def get_cache_dir(create: bool = True) -> Path:
8989
cache_dir = Path(tk.config.get(CONFIG_CACHE_DIR) or _tempdir)
90-
cache_dir.mkdir(parents=True, exist_ok=True)
90+
if create:
91+
cache_dir.mkdir(parents=True, exist_ok=True)
92+
return cache_dir
93+
94+
95+
def _get_cache_path(name):
96+
cache_dir = get_cache_dir()
9197
return cache_dir / f"{name}.pickle"
9298

9399

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "ckanext-iso19115"
7-
version = "0.2.1"
7+
version = "0.2.2"
88
description = ""
99
classifiers = [ "Development Status :: 4 - Beta", "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10",]
1010
keywords = [ "CKAN",]

0 commit comments

Comments
 (0)