pyrompt.stores#

Storage layer for pyrompt using dol abstractions.

Provides clean Mapping/MutableMapping interfaces to filesystem storage.

Functions

get_default_base_path()

Get platform-appropriate base path for pyrompt data.

mk_collection_path(collection_name[, base_path])

Get the path for a collection.

mk_collection_stores(collection_name[, ...])

Create all stores for a collection.

mk_metadata_store(rootdir[, for_type])

Create a JSON metadata store.

mk_prompt_store(rootdir[, extension])

Create a file store for prompts with clean interface.

mk_template_store(rootdir[, extensions])

Create a template store that handles multiple extensions.

pyrompt.stores.get_default_base_path()[source]#

Get platform-appropriate base path for pyrompt data.

Returns:

%LOCALAPPDATA%/pyrompt - macOS/Linux: ~/.local/share/pyrompt

Return type:

str

pyrompt.stores.mk_collection_path(collection_name, base_path=None)[source]#

Get the path for a collection.

Parameters:
  • collection_name (str) – Name of the collection

  • base_path (Optional[str]) – Optional base path (uses default if None)

Return type:

Path

Returns:

Path object for the collection directory

pyrompt.stores.mk_collection_stores(collection_name, base_path=None, create_metadata=False)[source]#

Create all stores for a collection.

Parameters:
  • collection_name (str) – Name of the collection

  • base_path (Optional[str]) – Optional base path

  • create_metadata (bool) – Whether to create metadata stores

Returns:

‘prompts’, ‘templates’, and optionally ‘prompt_meta’, ‘template_meta’

Return type:

dict

pyrompt.stores.mk_metadata_store(rootdir, for_type='prompts')[source]#

Create a JSON metadata store.

Parameters:
  • rootdir (str) – Directory to store metadata

  • for_type (str) – Type of metadata (just for documentation)

Returns:

MutableMapping[str, dict] with JSON file storage

Example

>>> store = mk_metadata_store('/tmp/meta')
>>> store['greeting'] = {'author': 'thor', 'version': '1.0'}
>>> meta = store['greeting']
pyrompt.stores.mk_prompt_store(rootdir, extension='txt')[source]#

Create a file store for prompts with clean interface.

  • Keys don’t show extension

  • Values are strings (not bytes)

  • Supports tab completion

Parameters:
  • rootdir (str) – Directory to store prompts

  • extension (str) – File extension (without dot)

Returns:

MutableMapping[str, str] with file-backed storage

Example

>>> store = mk_prompt_store('/tmp/prompts')
>>> store['greeting'] = "Hello, {name}!"
>>> print(store['greeting'])
Hello, {name}!
pyrompt.stores.mk_template_store(rootdir, extensions=None)[source]#

Create a template store that handles multiple extensions.

Keeps the extension in the key so we can detect template engine.

Parameters:
  • rootdir (str) – Directory to store templates

  • extensions (Optional[list]) – List of extensions to support (with dots) Defaults to [‘.txt’, ‘.jinja2’, ‘.j2’, ‘.jinja’, ‘.mustache’, ‘.hbs’]

Returns:

MutableMapping[str, str] with file-backed storage

Example

>>> store = mk_template_store('/tmp/templates')
>>> store['greeting.jinja2'] = "Hello {{ name }}!"
>>> store['simple.txt'] = "Hello {name}!"