
    )`i                     6   d Z ddlZddlmZmZ ddlmZmZ ddlm	Z	  e	e
          ZdZdZd	Zd
Z G d de          Ze G d d                      ZdedefdZddededededef
dZddededee         defdZdefdZdeeeeef         f         fdZdS )z;Configuration management for supervisor process management.    N)	dataclassfield)DictOptional   )
get_loggerPROCESS_AUTO_RECOVERYPROCESS_MAX_START_RETRIES	LOG_LEVELSUPERVISOR_CONFIG_PATHc                       e Zd ZdZdS )ConfigurationErrorz5Exception raised for configuration validation errors.N)__name__
__module____qualname____doc__     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/model_hosting_container_standards/supervisor/models.pyr   r      s        ??Dr   r   c                       e Zd ZU dZdZeed<   dZeed<   dZ	e
ed<   dZe
ed	<    ee
          Zee
ee
e
f         f         ed<   dS )SupervisorConfiga  Configuration for supervisor process management system.

    Hybrid Environment Variable Design:
    - Application config: PROCESS_ prefixed names (PROCESS_AUTO_RECOVERY, PROCESS_MAX_START_RETRIES, LOG_LEVEL)
    - Supervisord config: SUPERVISOR_{SECTION}_{KEY} pattern for custom overrides
    - Section names with colons: Use double underscore __ to represent colon :

    Examples:
    - PROCESS_AUTO_RECOVERY=false (application behavior)
    - PROCESS_MAX_START_RETRIES=5 (application behavior)
    - LOG_LEVEL=debug (application behavior)
    - SUPERVISOR_PROGRAM_STARTSECS=10 (supervisord [program] section override)
    - SUPERVISOR_SUPERVISORD_LOGLEVEL=debug (supervisord [supervisord] section override)
    - SUPERVISOR_PROGRAM__WEB_COMMAND="gunicorn app:app" (supervisord [program:web] section)
    - SUPERVISOR_RPCINTERFACE__SUPERVISOR_FACTORY=... (supervisord [rpcinterface:supervisor] section)
    Tauto_recovery   max_start_retries/tmp/supervisord.confconfig_pathinfo	log_level)default_factorycustom_sectionsN)r   r   r   r   r   bool__annotations__r   intr   strr   r   dictr    r   r   r   r   r   r      s          " M4s.K...Is16t1L1L1LOT#tCH~-.LLLLLr   r   valuereturnc                 .    |                                  dv S )zParse boolean from string.)true1yeson)lower)r&   s    r   _parse_boolr.   2   s    ;;==666r   d   namedefaultmin_valmax_valc           	          t          j        |           }|s|S 	 t          |          }||cxk    r|k    sn t          |  d| d| d|           |S # t          $ r t          |  d| d          w xY w)z-Get integer from environment with validation.z must be between z and z, got z must be an integer, got '')osgetenvr#   r   
ValueError)r0   r1   r2   r3   r&   parseds         r   _get_env_intr:   7   s    IdOOE NU6,,,,W,,,,$OO'OOOOvOO    N N N D!L!LE!L!L!LMMMNs   9A  A4allowedc                     t          j        | |                                          }|r.|                                |vrt	          |  d| d| d          |S )z,Get string from environment with validation.z must be one of z, got 'r5   )r6   r7   stripr-   r   )r0   r1   r;   r&   s       r   _get_env_strr>   H   sf    IdG$$**,,E T5;;==// D!R!R'!R!R%!R!R!RSSSLr   c                  j   	 t                      } t          t          t          j        t
          d                    t          t          d          t          t          d          t          t          dg d          |           S # t          $ r#}t                              d|             d}~ww xY w)	zAParse environment variables and return SupervisorConfig instance.r)   r   r   r   )debugr   warnerrorcritical)r   r   r   r   r    z!Configuration validation failed: N)!_parse_supervisor_custom_sectionsr   r.   r6   r7   r	   r:   r
   r>   r   r   r   loggerrB   )r    es     r   parse_environment_variablesrG   P   s    ;==%bi0Ev&N&NOO*+DaHH$%;=TUU">>> 
 ,

 

 

 
	
    <<<===s   BB 
B2B--B2c            	         ddl } |                     d          }i }t          j                                        D ]\  }}|dk    r|                    |          }|s4|                    d          rt                              d| d           X|	                    d          }|	                    d	          
                                }|                    d
d          
                                }||vri ||<   |                                ||         |<   t                              d| d| d|            |S )a  
    Parse SUPERVISOR_{SECTION}_{KEY}=VALUE environment variables for supervisord configuration.

    Pattern: SUPERVISOR_SECTION_KEY -> [section] key=value
    Special handling for section names with colons:
    - Double underscore __ in section name becomes colon :

    Examples:
    - SUPERVISOR_PROGRAM_STARTSECS=10 -> [program] startsecs=10
    - SUPERVISOR_SUPERVISORD_LOGLEVEL=debug -> [supervisord] loglevel=debug
    - SUPERVISOR_PROGRAM__WEB_COMMAND="gunicorn app:app" -> [program:web] command=gunicorn app:app
    - SUPERVISOR_RPCINTERFACE__SUPERVISOR_FACTORY=... -> [rpcinterface:supervisor] factory=...

    Skips SUPERVISOR_CONFIG_PATH (used for file path, not supervisord config).

    Returns:
        Dictionary mapping section names to their key-value configurations
    r   Nz`^SUPERVISOR_(?P<section>[A-Z0-9]+(?:__[A-Z0-9]+|_[A-Z0-9]+)*)_(?P<key>[A-Z0-9]+(?:_[A-Z0-9]+)*)$r   SUPERVISOR_z2Invalid SUPERVISOR_ environment variable format: 'z'. Expected format: SUPERVISOR_SECTION_KEY=value (alphanumeric with underscores, no leading/trailing underscores, use __ for section colons)sectionkey__:z(Found custom supervisor configuration: [z] =)recompiler6   environitemsmatch
startswithrE   warninggroupr-   replacer=   r@   )	rO   patternr    env_varr&   rS   section_partkey_namesection_names	            r   rD   rD   f   s   & III
 jj	/ G 24O***,, 
 
...g&& 	!!-00 S S S S  
  {{9--;;u%%++-- $++D#66<<>> ..,.OL) 38++--%h/Y|YYxYYRWYY	
 	
 	
 	
 r   )r   r/   )N)r   r6   dataclassesr   r   typingr   r   logging_configr   r   rE   r	   r
   r   r   	Exceptionr   r   r$   r!   r.   r#   r:   listr>   rG   rD   r   r   r   <module>rb      s   A A 				 ( ( ( ( ( ( ( ( ! ! ! ! ! ! ! ! ' ' ' ' ' '	H		 0 7 	1 	 	 	 	 	 	 	 	 M M M M M M M M27s 7t 7 7 7 7
N Ns NS N3 NS NSV N N N N" s S 8D> S    %5    ,A4T#s(^0C+D A A A A A Ar   