
    Ki                         d 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	Z	 e
h d          Zddeded	efd
Zddeded	efdZdddeeef         dededed	df
dZdddddeeef         dededededz  d	dfdZdS )z*Shared utility functions for hermes-agent.    N)Path)AnyUnion>   1onyestrueFvaluedefaultreturnc                     | |S t          | t                    r| S t          | t                    r-|                                                                 t
          v S t          |           S )zDCoerce bool-ish values using the project's shared truthy string set.)
isinstanceboolstrstriplowerTRUTHY_STRINGS)r
   r   s     "/home/ubuntu/hermes-agent/utils.pyis_truthy_valuer      s`    }% % 7{{}}""$$66;;     namec                 J    t          t          j        | |          d          S )zBReturn True when an environment variable is set to a truthy value.F)r   )r   osgetenv)r   r   s     r   env_var_enabledr      s!    29T733UCCCCr      )indentpathdatar   dump_kwargsc                Z   t          |           } | j                            dd           t          j        t          | j                  d| j         dd          \  }}	 t          j        |dd	          5 }t          j
        ||f|d
d| |                                 t          j        |                                           ddd           n# 1 swxY w Y   t          j        ||            dS # t          $ r( 	 t          j        |           n# t"          $ r Y nw xY w w xY w)a  Write JSON data to a file atomically.

    Uses temp file + fsync + os.replace to ensure the target file is never
    left in a partially-written state. If the process crashes mid-write,
    the previous version of the file remains intact.

    Args:
        path: Target file path (will be created or overwritten).
        data: JSON-serializable data to write.
        indent: JSON indentation (default 2).
        **dump_kwargs: Additional keyword args forwarded to json.dump(), such
            as default=str for non-native types.
    Tparentsexist_ok._.tmpdirprefixsuffixwutf-8encodingF)r   ensure_asciiN)r   parentmkdirtempfilemkstempr   stemr   fdopenjsondumpflushfsyncfilenoreplaceBaseExceptionunlinkOSError)r   r    r   r!   fdtmp_pathfs          r   atomic_json_writerD      s   ( ::DKdT222#49  LB
Yr3111 		!QI "	 
    GGIIIHQXXZZ   		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 		! 	
8T"""""   	Ih 	 	 	D	sU   "C8 9AC
C8 CC8 CC8 8
D*DD*
D%"D*$D%%D*)default_flow_style	sort_keysextra_contentrE   rF   rG   c                   t          |           } | j                            dd           t          j        t          | j                  d| j         dd          \  }}	 t          j        |dd	          5 }t          j
        ||||
           |r|                    |           |                                 t          j        |                                           ddd           n# 1 swxY w Y   t          j        ||            dS # t           $ r( 	 t          j        |           n# t$          $ r Y nw xY w w xY w)an  Write YAML data to a file atomically.

    Uses temp file + fsync + os.replace to ensure the target file is never
    left in a partially-written state.  If the process crashes mid-write,
    the previous version of the file remains intact.

    Args:
        path: Target file path (will be created or overwritten).
        data: YAML-serializable data to write.
        default_flow_style: YAML flow style (default False).
        sort_keys: Whether to sort dict keys (default False).
        extra_content: Optional string to append after the YAML dump
            (e.g. commented-out sections for user reference).
    Tr#   r&   r'   r(   r)   r-   r.   r/   )rE   rF   N)r   r2   r3   r4   r5   r   r6   r   r7   yamlr9   writer:   r;   r<   r=   r>   r?   r@   )r   r    rE   rF   rG   rA   rB   rC   s           r   atomic_yaml_writerK   Q   s   , ::DKdT222#49  LB
Yr3111 	!QIdA2DPYZZZZ '&&&GGIIIHQXXZZ   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	
8T"""""   	Ih 	 	 	D	sU   "D 9A*C/#D /C33D 6C37D 
ED10E1
D>;E=D>>E)F)r   )__doc__r8   r   r4   pathlibr   typingr   r   rI   	frozensetr   r   r   r   r   intrD   rK    r   r   <module>rR      s   0 0  				                 55566 3  $    D D# D DT D D D D 	/ / /
T	
/
/ 	/
 / 
/ / / /l  % $- - -
T	
-
- 	-
 - :- 
- - - - - -r   