o
    _�¿jD  ã                   @  s¼   d Z ddlmZ ddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZ ddlmZmZmZ dd	lmZ d
dlmZ d
dlmZmZ d
dlmZ e
rTddlmZ G dd„ deƒZdS )zSecrets file settings source.é    )ÚannotationsN)ÚPath)ÚTYPE_CHECKINGÚAny)Ú	FieldInfo)Ú_settings_debug_enabledÚloggerÚpath_type_labelé   )ÚSettingsErroré   )ÚPydanticBaseEnvSettingsSource)ÚEnvPrefixTargetÚPathType)Ú	InitState)ÚBaseSettingsc                      sd   e Zd ZdZ								d)d*‡ fdd„Zd+‡ fdd„Zed,dd „ƒZd-d%d&„Zd.d'd(„Z	‡  Z
S )/ÚSecretsSettingsSourcezE
    Source class for loading settings values from secret files.
    NÚsettings_clsútype[BaseSettings]Úsecrets_dirúPathType | NoneÚcase_sensitiveúbool | NoneÚ
env_prefixú
str | NoneÚenv_prefix_targetúEnvPrefixTarget | NoneÚenv_ignore_emptyÚenv_parse_none_strÚenv_parse_enumsÚ_init_stateúInitState | NoneÚreturnÚNonec
           
   
     s>   t ƒ  ||||||||	¡ |d ur|| _d S | j d¡| _d S )Nr   )ÚsuperÚ__init__ÚconfigÚgetr   )
Úselfr   r   r   r   r   r   r   r   r    ©Ú	__class__© úf/home/dinkstrade/pdmp/venv/lib/python3.10/site-packages/pydantic_settings/sources/providers/secrets.pyr%       s   ø$
zSecretsSettingsSource.__init__údict[str, Any]c                   s¶   i }| j du r	|S t| j ttjfƒr| j gn| j }dd„ |D ƒ}g | _|D ]}| ¡ s5t d|› d�¡ q%| j 	|¡ q%t
| jƒsC|S | jD ]}| ¡ sUtdt|ƒ› �ƒ‚qFtƒ  ¡ S )z4
        Build fields from "secrets" files.
        Nc                 S  s   g | ]}t |ƒ ¡ ‘qS r+   )r   Ú
expanduser)Ú.0Úpr+   r+   r,   Ú
<listcomp>B   s    z2SecretsSettingsSource.__call__.<locals>.<listcomp>zdirectory "z" does not existz.secrets_dir must reference a directory, not a )r   Ú
isinstanceÚstrÚosÚPathLikeÚsecrets_pathsÚexistsÚwarningsÚwarnÚappendÚlenÚis_dirr   r	   r$   Ú__call__)r(   ÚsecretsÚsecrets_dirsr6   Úpathr)   r+   r,   r=   8   s"   
 

ÿ
zSecretsSettingsSource.__call__Údir_pathr   Ú	file_namer3   ÚboolúPath | Nonec                 C  s:   |  ¡ D ]}|j|ks|s|j ¡ | ¡ kr|  S qdS )af  
        Find a file within path's directory matching filename, optionally ignoring case.

        Args:
            dir_path: Directory path.
            file_name: File name.
            case_sensitive: Whether to search for file name case sensitively.

        Returns:
            Whether file path or `None` if file does not exist in directory.
        N)ÚiterdirÚnameÚlower)ÚclsrA   rB   r   Úfr+   r+   r,   Úfind_case_pathT   s
    €z$SecretsSettingsSource.find_case_pathÚfieldr   Ú
field_nameútuple[Any, str, bool]c           	      C  sÌ   t ƒ ot tj¡}|  ||¡D ]Q\}}}t| jƒD ]F}|  ||| j	¡}|s2|r1t 
d||  ¡ ¡ q| ¡ rO|r@t 
d| ¡ ¡ |jdd� ¡ ||f    S tjd|› dt|ƒ› d�dd	� qqd
||fS )al  
        Gets the value for field from secret file and a flag to determine whether value is complex.

        Args:
            field: The field.
            field_name: The field name.

        Returns:
            A tuple that contains the value (`None` if the file does not exist), key, and
                a flag to determine whether value is complex.
        z#Secret file not found, skipping: %szLoading secret file: %szutf-8)Úencodingzattempted to load secret file "z" but found a z	 instead.é   )Ú
stacklevelN)r   r   ÚisEnabledForÚloggingÚDEBUGÚ_extract_field_infoÚreversedr6   rJ   r   ÚdebugÚresolveÚis_fileÚ	read_textÚstripr8   r9   r	   )	r(   rK   rL   rV   Ú	field_keyÚenv_nameÚvalue_is_complexÚsecrets_pathr@   r+   r+   r,   Úget_field_valuef   s$   þð
z%SecretsSettingsSource.get_field_valuec                 C  s   | j j› d| j›d�S )Nz(secrets_dir=ú))r*   Ú__name__r   )r(   r+   r+   r,   Ú__repr__Ž   s   zSecretsSettingsSource.__repr__)NNNNNNNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   )r"   r-   )rA   r   rB   r3   r   rC   r"   rD   )rK   r   rL   r3   r"   rM   )r"   r3   )ra   Ú
__module__Ú__qualname__Ú__doc__r%   r=   ÚclassmethodrJ   r_   rb   Ú__classcell__r+   r+   r)   r,   r      s     ö
(r   )re   Ú
__future__r   Ú_annotationsrR   r4   r8   Úpathlibr   Útypingr   r   Úpydantic.fieldsr   Úpydantic_settings.utilsr   r   r	   Ú
exceptionsr   Úbaser   Útypesr   r   Úutilsr   Úpydantic_settings.mainr   r   r+   r+   r+   r,   Ú<module>   s     