SnakeBridge.Registry (SnakeBridge v0.15.1)

Copy Markdown View Source

Registry system for tracking generated SnakeBridge adapters.

The registry maintains a record of all generated Python library adapters, allowing agents and tools to introspect what libraries are available without parsing code.

Registry Format

The registry stores library information including:

  • Python module name and version
  • Generated Elixir module name
  • Generation timestamp
  • File locations and structure
  • Statistics (function count, class count, etc.)

Usage

# Register a new library
SnakeBridge.Registry.register("numpy", %{
  python_module: "numpy",
  python_version: "1.26.0",
  elixir_module: "Numpy",
  generated_at: ~U[2024-12-24 14:00:00Z],
  path: "lib/snakebridge/adapters/numpy/",
  files: ["numpy.ex", "linalg.ex", "_meta.ex"],
  stats: %{functions: 165, classes: 2, submodules: 4}
})

# Check if a library is generated
SnakeBridge.Registry.generated?("numpy")
# => true

# Get library information
SnakeBridge.Registry.get("numpy")
# => %{python_module: "numpy", ...}

# List all generated libraries
SnakeBridge.Registry.list_libraries()
# => ["json", "numpy", "sympy"]

Persistence

The registry is automatically persisted to a JSON file at: priv/snakebridge/registry.json

Use save/0 to persist changes and load/0 to restore from disk.

Summary

Functions

Returns a specification to start this module under a supervisor.

Clears all entries from the registry.

Checks if a library is registered.

Gets information about a registered library.

Returns a list of all registered library names, sorted alphabetically.

Loads the registry from the JSON file.

Registers a library in the registry.

Saves the registry to the JSON file.

Starts the registry agent.

Removes a library from the registry.

Types

library_name()

@type library_name() :: String.t()

registry_entry()

@type registry_entry() :: %{
  python_module: String.t(),
  python_version: String.t(),
  elixir_module: String.t(),
  generated_at: DateTime.t(),
  path: String.t(),
  files: [String.t()],
  stats: %{
    functions: non_neg_integer(),
    classes: non_neg_integer(),
    submodules: non_neg_integer()
  }
}

registry_state()

@type registry_state() :: %{optional(library_name()) => registry_entry()}

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Clears all entries from the registry.

Examples

iex> SnakeBridge.Registry.clear()
:ok

generated?(library_name)

@spec generated?(library_name()) :: boolean()

Checks if a library is registered.

Examples

iex> SnakeBridge.Registry.generated?("numpy")
true

iex> SnakeBridge.Registry.generated?("nonexistent")
false

get(library_name)

@spec get(library_name()) :: registry_entry() | nil

Gets information about a registered library.

Returns nil if the library is not registered.

Examples

iex> SnakeBridge.Registry.get("numpy")
%{python_module: "numpy", python_version: "1.26.0", ...}

iex> SnakeBridge.Registry.get("nonexistent")
nil

list_libraries()

@spec list_libraries() :: [library_name()]

Returns a list of all registered library names, sorted alphabetically.

Examples

iex> SnakeBridge.Registry.register("numpy", entry)
:ok
iex> SnakeBridge.Registry.list_libraries()
["numpy"]

load()

@spec load() :: :ok | {:error, term()}

Loads the registry from the JSON file.

If the file doesn't exist, initializes an empty registry.

Returns

  • :ok on success
  • {:error, reason} if loading fails

Examples

iex> SnakeBridge.Registry.load()
:ok

register(library_name, entry)

@spec register(library_name(), map()) :: :ok | {:error, String.t()}

Registers a library in the registry.

Updates the entry if the library is already registered.

Parameters

  • library_name - The library identifier (e.g., "numpy")
  • entry - A map containing library information (see module documentation)

Returns

  • :ok on success
  • {:error, reason} if the entry is invalid

Examples

iex> entry = %{
...>   python_module: "numpy",
...>   python_version: "1.26.0",
...>   elixir_module: "Numpy",
...>   generated_at: ~U[2024-12-24 14:00:00Z],
...>   path: "lib/snakebridge/adapters/numpy/",
...>   files: ["numpy.ex"],
...>   stats: %{functions: 10, classes: 0, submodules: 1}
...> }
iex> SnakeBridge.Registry.register("numpy", entry)
:ok

save()

@spec save() :: :ok | {:error, term()}

Saves the registry to the JSON file.

Creates the parent directory if it doesn't exist.

Returns

  • :ok on success
  • {:error, reason} if saving fails

Examples

iex> SnakeBridge.Registry.save()
:ok

start_link(opts \\ [])

@spec start_link(keyword()) :: Agent.on_start()

Starts the registry agent.

This is typically called by the application supervisor.

unregister(library_name)

@spec unregister(library_name()) :: :ok

Removes a library from the registry.

Returns :ok even if the library was not registered.

Examples

iex> SnakeBridge.Registry.unregister("numpy")
:ok