
    i                    v    S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	  SSK
JrJr   " S S\5      r " S	 S
5      rg)a#  
File-based prompt registry implementation that stores all prompts in a single JSON file.

This module provides functionality to store and retrieve prompts using a single JSON file
as the storage backend. The file contains an index of all prompts with their associated
metadata and content.
    )annotations)Path)	BaseModel)InvalidPromptErrorPromptNotFoundError)PromptPromptModelc                  (    \ rS rSr% Sr/ rS\S'   Srg)PromptRegistryIndex   z
Model representing the registry index containing all prompts.

Stores a list of PromptModel objects that represent all prompts in the registry.
zlist[PromptModel]prompts N)__name__
__module____qualname____firstlineno____doc__r   __annotations____static_attributes__r       O/home/james-whalen/.local/lib/python3.13/site-packages/banks/registries/file.pyr   r      s     "$G#r   r   c                  Z    \ rS rSrSrSS jrSS.SS jjrSS.SS	 jjrSS
 jrSS jr	Sr
g)FilePromptRegistry    z@A prompt registry storing all prompt data in a single JSON file.c                   [        U5      U l        [        / S9U l         [        R	                  U R                  R                  SS95      U l        g! [         a'    U R                  R                  R                  SSS9   gf = f)z
Initialize the file prompt registry.

Args:
    registry_index: Path to the JSON file that will store the prompts

Note:
    Creates parent directories if they don't exist.
)r   utf-8encodingT)parentsexist_okN)	r   _index_fpathr   _indexmodel_validate_json	read_textFileNotFoundErrorparentmkdir)selfregistry_indexs     r   __init__FilePromptRegistry.__init__#   sz     #'~"6+>r+J	H-AA$BSBSB]B]gnB]BopDK  	H$$**4$*G	Hs   1A .BBN)versionc               Z    U R                  X5      u  p4[        S0 UR                  5       D6$ )z
Retrieve a prompt by name and version.

Args:
    name: Name of the prompt to retrieve
    version: Version of the prompt (optional)

Returns:
    The requested Prompt object

Raises:
    PromptNotFoundError: If the requested prompt doesn't exist
r   )_get_prompt_modelr   
model_dump)r(   namer,   _models        r   getFilePromptRegistry.get5   s-     ))$8+((*++r   F)	overwritec                   U R                  UR                  UR                  5      u  p4U(       a>  [        R                  " U5      U R
                  R                  U'   U R                  5         gSUR                   S3n[        U5      e! [         aN    [        R                  " U5      nU R
                  R                  R                  U5        U R                  5          gf = f)z
Store a prompt in the registry.

Args:
    prompt: The Prompt object to store
    overwrite: Whether to overwrite an existing prompt

Raises:
    InvalidPromptError: If prompt exists and overwrite=False
zPrompt with name 'z1' already exists. Use overwrite=True to overwriteN)r.   r0   r,   r	   from_promptr"   r   _saver   r   append)r(   promptr5   idxp_modelmsgs         r   setFilePromptRegistry.setF   s    	11&++v~~NLC+6+B+B6+J##C(

*6;;-7hi(--" 	!--f5GKK&&w/JJL	s   A,B
 /B
 
AC"!C"c                    [        U R                  SSS9 nUR                  U R                  R	                  5       5        SSS5        g! , (       d  f       g= f)z\
Save the prompt index to the JSON file.

Writes the current state of the registry to disk.
wr   r   N)openr!   writer"   model_dump_json)r(   fs     r   r8   FilePromptRegistry._save^   s=     $##S7;qGGDKK//12 <;;s   *A		
Ac                    [        U R                  R                  5       H,  u  p4UR                  U:X  d  M  UR                  U:X  d  M)  X44s  $    SU SU S3n[        U5      e)z
Find a prompt model in the index by name and version.

Args:
    name: Name of the prompt
    version: Version of the prompt

Returns:
    Tuple of (index position, PromptModel)

Raises:
    PromptNotFoundError: If the prompt doesn't exist in the index
zcannot find prompt with name 'z' and version '')	enumerater"   r   r0   r,   r   )r(   r0   r,   ir2   r=   s         r   r.   $FilePromptRegistry._get_prompt_modelg   s^     "$++"5"56HAzzT!emmw&>x 7 /tfOG9AN!#&&r   )r"   r!   )r)   strreturnNone)r0   rL   r,   
str | NonerM   r   )r:   r   r5   boolrM   rN   )rM   rN   )r0   rO   r,   rO   rM   ztuple[int, PromptModel])r   r   r   r   r   r*   r3   r>   r8   r.   r   r   r   r   r   r       s)    JH$ 7; ," 8= 03'r   r   N)r   
__future__r   pathlibr   pydanticr   banks.errorsr   r   banks.promptr   r	   r   r   r   r   r   <module>rV      s3    #   @ ,$) $Z' Z'r   