Skip to main content

Python wrapper for api use in the cave_app

Project description

Cave Utilities

PyPI version License: MIT

Python utilities for the CAVE App framework: API validation, data builders, and shared helpers.

Overview

cave_utils is a foundational library for building CAVE applications. It provides:

  • API validation: validate session_data dicts against the full CAVE API spec, with structured error and warning reporting
  • Data builders: helper classes for constructing hierarchical and date-based group structures
  • Shared utilities: logging, geographic calculations, coordinate systems, and argument parsing

It is designed to be embedded in a CAVE API server, but can also be used standalone for testing and data development.

Features

  • Validates all 10 top-level CAVE API keys (settings, appBar, pages, panes, maps, mapFeatures, globalOutputs, groupedOutputs, draggables, extraKwargs)
  • Cross-field validation with structured error paths (e.g. maps.data.myMap.currentProjection)
  • Time-series (timeValues) and ordering (order) validation built in
  • GroupsBuilder and DateGroupsBuilder for constructing hierarchical group structures from flat data
  • GeoUtils for shortest-path calculations over geographic networks
  • CustomCoordinateSystem for converting Cartesian 2D/3D coordinates to lat/long
  • Runtime type enforcement via type_enforced

Requirements

  • Python ≥ 3.11

Installation

pip install cave_utils

Quick Start

Validating Session Data

from cave_utils import Validator

session_data = {
    "settings": {
        "iconUrl": "https://example.com/icon.png",
    },
    "appBar": {
        "data": {
            "btn_home": {
                "icon": "md/MdHome",
                "type": "button",
            }
        }
    },
}

result = Validator(session_data=session_data)

# Inspect errors and warnings
for entry in result.log.log:
    print(entry)
# Empty list means fully valid

Using GroupsBuilder

from cave_utils.builders.groups import GroupsBuilder

builder = GroupsBuilder(
    data=[
        {"id": "region_east", "name": "East", "parent": None},
        {"id": "state_ny", "name": "New York", "parent": "region_east"},
        {"id": "state_ma", "name": "Massachusetts", "parent": "region_east"},
    ],
    id_field="id",
    name_field="name",
    parent_field="parent",
)

# Serialize to the CAVE API groupedOutputs format
groups_data = builder.serialize()

Public API

Class Import Description
Validator from cave_utils import Validator Validates a session_data dict against the full CAVE API spec
LogObject from cave_utils import LogObject Structured log container for errors and warnings
Socket from cave_utils import Socket No-op WebSocket stub for use in tests
Arguments from cave_utils import Arguments Parses kwargs, flags, and positional args from raw argument lists
GeoUtils from cave_utils import GeoUtils Geographic utilities including shortest-path via scgraph
CustomCoordinateSystem from cave_utils import CustomCoordinateSystem Converts 2D/3D Cartesian coordinates to lat/long

Full API reference: mit-cave.github.io/cave_utils

LLM Docs

cave_utils can generate plain text documentation for all its modules, suitable for use with AI assistants (Claude, Gemini, etc.).

From Python:

from cave_utils import generate_docs
generate_docs("path/to/output_dir")

From the command line:

cave-utils-docs path/to/output_dir

If no directory is specified, docs are written to ./cave_utils_docs/.

The output directory will contain one .txt file per module plus a README.txt index and a PROJECT_README.md with this project overview. You can point your AI assistant at this directory or reference individual files in a system prompt.

Development

All development tasks run inside Docker. Make sure Docker is installed and running.

Command Description
./run.sh Drop into an interactive Docker shell
./run.sh test Run all tests
./run.sh prettify Format code with autoflake + black
./run.sh docs Regenerate pdoc documentation

./run.sh requires a TTY. Run it directly in your terminal, not from a non-interactive CI environment.

Running Tests

Tests live in test/. The main test file (test_validator.py) imports every example in test/api_examples/, runs it through Validator, and asserts no errors or warnings are produced. Unit tests exist for each module.

./run.sh test

Hot-Reload with a Cave App

To develop cave_utils against a running Cave App, mount the local source as a Docker volume:

cave run --docker-args "--volume {local_path_to_cave_utils}/cave_utils:/cave_utils"

Then in the Cave App's utils/run_server.sh, add pip install -e /cave_utils before the server starts. Changes to cave_utils source will be reflected on the next API call without rebuilding the container.

Set LIVE_API_VALIDATION_PRINT=True in the Cave App's .env to see validation output on every API command.

Release Process

  1. Ensure all tests pass (./run.sh test) and code is formatted (./run.sh prettify)
  2. Update version in both pyproject.toml and setup.cfg
  3. Update the version in utils/docs.sh and regenerate docs (./run.sh docs)
  4. Build and publish:
    python3 -m virtualenv venv
    source venv/bin/activate
    pip install -r requirements.txt
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cave_utils-3.5.3.tar.gz (64.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cave_utils-3.5.3-py3-none-any.whl (70.5 kB view details)

Uploaded Python 3

File details

Details for the file cave_utils-3.5.3.tar.gz.

File metadata

  • Download URL: cave_utils-3.5.3.tar.gz
  • Upload date:
  • Size: 64.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for cave_utils-3.5.3.tar.gz
Algorithm Hash digest
SHA256 8252e7907ea631d8f0276602570fd8158735176779aef9a2f6e5c5e34d4aedc6
MD5 d23d55b38583f0216ac835637bc4c486
BLAKE2b-256 b67f6f80f0b3b7c5574bdcf8828f7e72b2b637b9eb7785a7e8da4951346a64b7

See more details on using hashes here.

File details

Details for the file cave_utils-3.5.3-py3-none-any.whl.

File metadata

  • Download URL: cave_utils-3.5.3-py3-none-any.whl
  • Upload date:
  • Size: 70.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for cave_utils-3.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bfe5b8d5593b2843056620c9a5d94a2d72375608e80c6c748ef4520baff21b65
MD5 e08d140df8e1e3caf53d3b806acfa42e
BLAKE2b-256 ac0ff1a9d64cc910836755a274829dec5606b3a0a0d598c2c65186527d7f5b9f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page