-
Notifications
You must be signed in to change notification settings - Fork 66
FXC-3903: tidy3d CLI Cache Helpers #2959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """Cache-related CLI commands.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional | ||
|
|
||
| import click | ||
|
|
||
| from tidy3d import config | ||
| from tidy3d.web.cache import LocalCache, resolve_local_cache | ||
| from tidy3d.web.cache import clear as clear_cache | ||
|
|
||
|
|
||
| def _fmt_size(num_bytes: int) -> str: | ||
| """Format bytes into human-readable form.""" | ||
| for unit in ["B", "KB", "MB", "GB", "TB"]: | ||
| if num_bytes < 1024.0 or unit == "TB": | ||
| return f"{num_bytes:.2f} {unit}" | ||
| num_bytes /= 1024.0 | ||
| return f"{num_bytes:.2f} B" # fallback, though unreachable | ||
|
|
||
|
|
||
| def _get_cache(ensure: bool = True) -> Optional[LocalCache]: | ||
| """Resolve the local cache object, surfacing errors as ClickExceptions.""" | ||
| try: | ||
| cache = resolve_local_cache(use_cache=True) | ||
| except Exception as exc: # pragma: no cover - defensive guard | ||
| raise click.ClickException(f"Failed to access local cache: {exc}") from exc | ||
| if cache is None and ensure: | ||
| raise click.ClickException("Local cache is disabled in the current configuration.") | ||
| return cache | ||
|
|
||
|
|
||
| @click.group(name="cache") | ||
| def cache_group() -> None: | ||
| """Inspect or manage the local cache.""" | ||
|
|
||
|
|
||
| @cache_group.command() | ||
| def info() -> None: | ||
| """Display current cache configuration and usage statistics.""" | ||
|
|
||
| enabled = bool(config.local_cache.enabled) | ||
| directory = config.local_cache.directory | ||
| max_entries = config.local_cache.max_entries | ||
| max_size_gb = config.local_cache.max_size_gb | ||
|
|
||
| cache = _get_cache(ensure=False) | ||
| entries = 0 | ||
| total_size = 0 | ||
| if cache is not None: | ||
| stats = cache.sync_stats() | ||
| entries = stats.total_entries | ||
| total_size = stats.total_size | ||
|
|
||
| click.echo(f"Enabled: {'yes' if enabled else 'no'}") | ||
| click.echo(f"Directory: {directory}") | ||
| click.echo(f"Entries: {entries}") | ||
| click.echo(f"Total size: {_fmt_size(total_size)}") | ||
| click.echo("Max entries: " + (str(max_entries) if max_entries else "unlimited")) | ||
| click.echo( | ||
| "Max size: " + (f"{_fmt_size(max_size_gb * 1024**3)}" if max_size_gb else "unlimited") | ||
| ) | ||
|
|
||
|
|
||
| @cache_group.command(name="list") | ||
| def list() -> None: | ||
| """List cached entries in a readable, separated format.""" | ||
|
|
||
| cache = _get_cache() | ||
| entries = cache.list() | ||
| if not entries: | ||
| click.echo("Cache is empty.") | ||
| return | ||
|
|
||
| def fmt_key(key: str) -> str: | ||
| return key.replace("_", " ").capitalize() | ||
|
|
||
| for i, entry in enumerate(entries, start=1): | ||
| entry.pop("simulation_hash", None) | ||
| entry.pop("checksum", None) | ||
| entry["file_size"] = _fmt_size(entry["file_size"]) | ||
|
|
||
| click.echo(f"\n=== Cache Entry #{i} ===") | ||
| for k, v in entry.items(): | ||
| click.echo(f"{fmt_key(k)}: {v}") | ||
| click.echo("") | ||
|
|
||
|
|
||
| @cache_group.command() | ||
| def clear() -> None: | ||
| """Remove all cache contents.""" | ||
|
|
||
| clear_cache() | ||
| click.echo("Local cache cleared.") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.