o
    i?                     @   s,  U d dl Z d dlZd dlZd dlmZ d dlZd dlmZmZ d dl	m
Z
mZmZ d dlmZ e jdp:e jeZeed< e jdpJe jeZeed	< d
d Ze Zdd Zdd Z			ddee dee dee fddZi e
eee e e e eddZG dd deZeeZdS )    N)Optional)PIPECAT_CREDENTIALS_PATHPIPECAT_DEPLOY_CONFIG_PATH)	_SETTINGSConfig_Setting)ConfigErrorPIPECAT_CONFIG_PATHuser_config_pathr   deploy_config_pathc               
      s  i } d}t jtrztt}t|} W d    n1 sw   Y  W nQ tjy= } zd| }W Y d }~nd }~w tyJ   dt }Y np t	y` } zd| }W Y d }~n^d }~w t
yv } zd| }W Y d }~nHd }~ww ddh  fdd	|  D }td
d | D sd}t jdkrt tj}|tjtjB @ rtjdt dt| dt ddd |rt|| S )N z$Invalid TOML syntax in config file: z,Permission denied when reading config file: z$I/O error when reading config file: zError reading config file: tokenorgc                    s   i | ]\}}| vr||qS  r   ).0kvtop_level_keysr   K/home/ubuntu/.local/lib/python3.10/site-packages/pipecatcloud/cli/config.py
<dictcomp>0   s    z%_read_user_config.<locals>.<dictcomp>c                 s   s    | ]}t |tV  qd S N)
isinstancedict)r   er   r   r   	<genexpr>2   s    z$_read_user_config.<locals>.<genexpr>zxPipecat Cloud config file is not valid TOML. Organization sections must be dictionaries. Please log out and log back in.ntzCredentials file 'z%' has overly permissive permissions (zM). Other users on this system may be able to read your token. Run 'chmod 600 z1' to fix, or login again to repair automatically.   )
stacklevel)ospathexistsr
   opentomlloadTomlDecodeErrorPermissionErrorIOError	Exceptionitemsallvaluesnamestatst_modeS_IRWXGS_IRWXOwarningswarnfilemoder   )config_dataconfig_problemfexcorg_sections	file_moder   r   r   _read_user_config   sL   

r:   c                 C   sv   t jt}t j|dd ttd}t| | W d    n1 s#w   Y  t |t	j
 t tt	jt	jB  d S )NT)exist_okw)r   r    dirnamer
   makedirsr"   r#   dumpchmodr-   S_IRWXUS_IRUSRS_IWUSR)
new_configdir_pathr6   r   r   r   _write_user_configI   s   rF   c                   C   s   t t d S r   )r   remover
   r   r   r   r   remove_user_configW   s   rH   r   
active_orgadditional_datac              
   C   s   t  }| r	| |d< |r!||d< ||vri ||< |r || | n|r'tdzt| W d S  ty<   tdt  tyI   tdt  ty] } z	tdt	| d }~w t
yq } z	tdt	| d }~ww )Nr   r   z=Attempt to store additional data without specifying namespacez"Permission denied when writing to z*Cannot create configuration directory for z%IO error when writing configuration: z)Unexpected error updating configuration: )r:   update
ValueErrorrF   r&   r   r
   FileNotFoundErrorr'   strr(   )r   rI   rJ   existing_configr   r   r   r   update_user_config[   s2   rP   INFO)r
   r   r   default_public_keydefault_public_key_namecli_log_levelc                   @   s   e Zd ZdddZdS )	ConfigCLINTc                 C   s   t rt t ddi ni }t| }d|  }|r'|tjv r'|tj| S t dur6|t v r6|t | S |durE||v rE||| S |jrK|jS |S )a  Looks up a configuration value.

        Will check (in decreasing order of priority):
        1. Any environment variable of the form PIPECAT_FOO_BAR (when use_env is True)
        2. Settings in the user's .toml configuration file
        3. The default value of the setting
        r   r   PIPECAT_N)user_configget_CLI_SETTINGSupperr   environ	transformdefault)selfkeyr]   use_envorg_profilesenv_var_keyr   r   r   rX      s   zConfigCLI.get)NT)__name__
__module____qualname__rX   r   r   r   r   rU      s    rU   )NNN) r   r-   r1   typingr   r#   pipecatcloud.clir   r   pipecatcloud.configr   r   r   pipecatcloud.exceptionr   r[   rX   r    
expanduserr
   rN   __annotations__r   r:   rW   rF   rH   r   rP   rY   rU   configr   r   r   r   <module>   sP   
(
#