
    Wwgh"                     H   d Z ddlZddlZddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZ g dZ ed      Zdee   fd	Zd
ej"                  fdZ ej&                  d       G d d             Zd Zd Zd Z ej0                  d      Zdee
   fdZd Zdee
   fdZdee	   fdZy)z
This module contains utilities for allowing dataclasses to be populated by
user-provided configuration (e.g. from a Yaml file).

.. note::
    On naming conventions: this module converts hyphens in key names to
    underscores as a matter of course.
    N)OptionalSetTypeUnion)	BitStringObjectIdentifier   )ConfigurationError)ConfigurableMixincheck_config_keys	OID_REGEXprocess_oidprocess_oidsprocess_bit_string_flagsreturnc                     t        | t              r| }n/ddlm}m}  ||       t
        ury 	  ||       \  }}|t        ury 	 |}t        |t              r|S d S # t        t        f$ r Y y w xY w)Nr   )get_args
get_origin)	
isinstancetypetypingr   r   r   	_noneType
ValueError	TypeError)thingthe_typer   r   type1type2s         I/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/config/api.py_unwrap_type_annotr       s|    %/ eE)	#E?LE5I% & !(D18;t; I& 		s   A A)(A)fc                 v    | j                   t        j                  uxs | j                  t        j                  uS N)default_factorydataclassesMISSINGdefault)r!   s    r   _has_defaultr(   2   s2    	!4!44 	099K///    T)frozenc                   \    e Zd ZdZed        Zed        Zedee   fd       Z	ed        Z
y)r   z+General configuration mixin for dataclassesc                      y)a$  
        Hook method that can modify the configuration dictionary
        to overwrite or tweak some of their values (e.g. to convert string
        parameters into more complex Python objects)

        Subclasses that override this method should call
        ``super().process_entries()``, and leave keys that they do not
        recognise untouched.

        :param config_dict:
            A dictionary containing configuration values.
        :raises ConfigurationError:
            when there is a problem processing a relevant entry.
        N )clsconfig_dicts     r   process_entriesz!ConfigurableMixin.process_entries=   s      	r)   c           
      t   t        j                  |       D ][  }t        |j                        }|t	        |t
              s+	 ||j                     }	 |j                  |      }|||j                  <   ] y # t        $ r Y jw xY w# t        $ r*}t        d|j                   dt        |             |d }~ww xY w)Nz+Error while processing configurable field 'z': )r%   fieldsr    r   
issubclassr   nameKeyErrorfrom_configr
   repr)r.   r/   r!   
field_typefield_config_dictfield_valuees          r   _process_configurable_fieldsz.ConfigurableMixin._process_configurable_fieldsO   s     ##C( 	.A+AFF3J!-* $/$7!(445FG #.K%	.   & (A!&&Awi! s*   A5B5	B B	B7%B22B7keys_suppliedc                     t        | j                  t        j                  |       D ch c]  }|j                   c}|       yc c}w )z
        Check whether all supplied keys are meaningful.

        :param keys_supplied:
            The keys supplied in the configuration.
        :raises ConfigurationError: if at least one key does not make sense.
        N)r   __name__r%   r2   r4   )r.   r=   r!   s      r   r   z#ConfigurableMixin.check_config_keysf   s6     	LL(//45QVV5	
5s   A
c           	      J   t        |t              st        | j                   d      | j	                  t        |j                                      |j                         D ci c]  \  }}|j                  dd      | }}}| j                  |       | j                  |       t        | j                  t        j                  |       D ch c]  }t        |      r|j                   c}|       	  | di |S c c}}w c c}w # t         $ r}t        d      |d}~ww xY w)a=  
        Attempt to instantiate an object of the class on which it is called,
        by means of the configuration settings passed in.

        First, we check that the keys supplied in the dictionary correspond
        to data fields on the current class.
        Then, the dictionary is processed using the :meth:`process_entries`
        method. The resulting dictionary is passed to the initialiser
        of the current class as a kwargs dict.

        :param config_dict:
            A dictionary containing configuration values.
        :return:
            An instance of the class on which it is called.
        :raises ConfigurationError:
            when an unexpected configuration key is encountered or left
            unfilled, or when there is a problem processing one of the config
            values.
        z% requires a dictionary to initialise.-_z!Failed to instantiate from configNr-   )r   dictr
   r?   r   setkeysitemsreplacer0   r<   enforce_required_keysr%   r2   r(   r4   r   )r.   r/   keyvr!   r;   s         r   r6   zConfigurableMixin.from_configv   s   * +t,$<<. EF  	c+"2"2"456 4?3D3D3F
)/aCKKS!1$
 
 	K(((5LL(//4LLOQVVL	

	Q%%%
 M  	Q$%HIqP	Qs*   $C=D
D
5D 	D"DD"N)r?   
__module____qualname____doc__classmethodr0   r<   r   strr   r6   r-   r)   r   r   r   9   s`    5 " . ., 
c#h 
 
 +Q +Qr)   r   c           
          t        ||      }|r4t        dt        |      dk(  rdnd d|  ddj                  |       d      y )	NzUnexpected r	   rI   rE    in configuration for : , .)_check_subsetr
   lenjoin)config_nameexpected_keyssupplied_keysunexpected_keyss       r   r   r      s^     $M=AO 3#71#<%&I J$$/=yy)*!-
 	
 r)   c                     | D ch c]  }|j                  dd       } }|D ch c]  }|j                  dd       }}| |z
  S c c}w c c}w )NrB   rA   )rG   )expected_subexpected_suprI   s      r   rU   rU      sR    5ABcCKKS)BLB5ABcCKKS)BLB,&& CBs
   AA	c           
          t        ||j                               }|r4t        dt        |      dk(  rdnd d|  ddj	                  |       d      y )	NzMissing required r	   rI   rE   rQ   rR   rS   rT   )rU   rE   r
   rV   rW   )rX   required_keysr/   missing_keyss       r   rH   rH      se     0@0@0BCL \):a)?VL M$$/=yy&'q*
 	
 r)   z
\d(\.\d+)+asn1crypto_classc           	      "   t        |t              st        dt        |       d| d      t        j                  |      r| j                  |      S 	 | j                  |       |S # t        $ r t        d| d| d| j                         w xY w)NzIdentifier 'z' in '' is not a string.'' is not a valid )
r   rO   r
   r7   r   	fullmatchmapunmapr   r?   )rb   	id_string
param_names      r   r   r      s     i% 4	?+6*=OP
 	
 9%  ##I..	 ""9-   	$I;fZL0A#,,-/ 	s   A& &(Bc                 ~    d| d}t        | t              r| f} | S t        | t        t        f      st	        |      | S )Nre   z6' must be specified as a list of strings, or a string.)r   rO   listtupler
   )stringsrk   err_msgs      r   _ensure_stringsrq      sR    
J< ( 	(  '3* N $/ ))Nr)   c              #   P   K   t        ||      }|D ]  }t        | ||        y wr#   )rq   r   )rb   ro   rk   usage_strings       r   r   r      s3     gz2G F*L*EEFs   $&c              #     K   t        ||      }| j                  j                         }|D ]W  }t        |t              st        dt        |       d      ||vr%t        dt        |       d| j                   d      | Y y w)NzFlag identifier 'rd   re   rf   z flag name.)rq   _mapvaluesr   rO   r
   r7   r?   )rb   ro   rk   valid_valuesflag_strings        r   r   r      s      gz2G#((//1L +s+$#D$5#66HI  ,$D%&&7#,,-[:  s   BB)rM   r%   rer   r   r   r   r   asn1crypto.corer   r   errorsr
   __all__r   r   r    Fieldr(   	dataclassr   r   rU   rH   compiler   r   rq   r   r   r-   r)   r   <module>r      s     	 - - 7 & J	<$ <(K%%  d#hQ hQ $hQV
 '
 BJJ}%	+,0
F4(8#9 F9or)   