o
    i                     @   s   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 deZeG dd dZdedefddZd!dededededef
ddZd"dededee defddZdefddZdeeeeef f fd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                   @   s   e Zd ZdZdS )ConfigurationErrorz5Exception raised for configuration validation errors.N)__name__
__module____qualname____doc__ r   r   g/home/ubuntu/.local/lib/python3.10/site-packages/model_hosting_container_standards/supervisor/models.pyr      s    r   c                   @   sd   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d
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   
 &r   valuereturnc                 C   s   |   dv S )zParse boolean from string.)true1yeson)lower)r"   r   r   r   _parse_bool2   s   r)   d   namedefaultmin_valmax_valc                 C   sx   t | }|s	|S z!t|}||  kr|ks(n t|  d| d| d| |W S  ty;   t|  d| d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)r+   r,   r-   r.   r"   parsedr   r   r   _get_env_int7   s   
r4   allowedc                 C   s>   t | | }|r| |vrt|  d| d| d|S )z,Get string from environment with validation.z must be one of z, got 'r/   )r0   r1   stripr(   r   )r+   r,   r5   r"   r   r   r   _get_env_strH   s   r7   c               
   C   sj   zt  } ttttdttdtt	dtt
dg d| dW S  ty4 } z	td|   d}~w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)   r0   r1   r   r4   r	   r7   r   r
   r   loggerr:   )r   er   r   r   parse_environment_variablesP   s$   r?   c            	   	   C   s   ddl } | d}i }tj D ]Q\}}|dkrq||}|s/|dr.td| d q|	d}|	d	
 }|d
d
 }||vrKi ||< | || |< td| d| d|  q|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compiler0   environitemsmatch
startswithr=   warninggroupr(   replacer6   r8   )	rF   patternr   env_varr"   rJ   section_partkey_namesection_namer   r   r   r<   f   s2   



r<   )r   r*   )N)r   r0   dataclassesr   r   typingr   r   logging_configr   r   r=   r   r	   r
   r   	Exceptionr   r   r    r   r)   r   r4   listr7   r?   r<   r   r   r   r   <module>   s$      "