o
    &h3)                     @   s~   d Z ddlZddlZdZdd Zdd ZG dd	 d	Zd
d Zdd Z	dd Z
dd ZG dd dZddlZed ZdS )a  
Support for Key-Value Coding in Python. This provides a simple functional
interface to Cocoa's Key-Value coding that also works for regular Python
objects.

Public API:

    setKey(obj, key, value) -> None
    setKeyPath (obj, keypath, value) -> None

    getKey(obj, key) -> value
    getKeyPath (obj, keypath) -> value

A keypath is a string containing a sequence of keys separated by dots. The
path is followed by repeated calls to 'getKey'. This can be used to easily
access nested attributes.

This API is mirroring the 'getattr' and 'setattr' APIs in Python, this makes
it more natural to work with Key-Value coding from Python. It also doesn't
require changes to existing Python classes to make use of Key-Value coding,
making it easier to build applications as a platform independent core with
a Cocoa GUI layer.

See the Cocoa documentation on the Apple developer website for more
information on Key-Value coding. The protocol is basically used to enable
weaker coupling between the view and model layers.
    N)getKeysetKey
getKeyPath
setKeyPathc                 C   s   | d d   | dd   S )N   )
capitalize)s r	   q/Users/merlin/projects/employee-monitoring-system/venv/lib/python3.10/site-packages/PyObjCTools/KeyValueCoding.pykeyCaps$   s   r   c                 C   sz   g }| D ]3}d}|D ]%}t |t |k r||}}|| }|||  }|r-|||< |d7 }|}q
|g||d< qt|dS )zP
    Full precision summation using multiple floats for intermediate values
    r   r   N        )abssum)iterableZpartialsxiyhilor	   r	   r
   msum-   s   

r   c                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd ZdS )_ArrayOperatorsc                 C   s>   d |}t| |}t|}|dkrdS tdd |D | S )N.r   r   c                 s   $    | ]}|t urt|nd V  qdS r   N_nullfloat.0r   r	   r	   r
   	<genexpr>J      " z&_ArrayOperators.avg.<locals>.<genexpr>)joinr   lenr   )objsegmentspathlstcountr	   r	   r
   avgC   s   

z_ArrayOperators.avgc                 C   s   t | S N)r"   r#   r$   r	   r	   r
   r'   L   s   z_ArrayOperators.countc              
      s   d | g }t }g }| D ]=} fdd|D D ]1}z||v s$||v r&W q|| || W q tyJ   ||v r>Y q|| || Y qw q|S )Nr   c                 3       | ]}t | V  qd S r)   r   r   itemr%   r	   r
   r   W       z8_ArrayOperators.distinctUnionOfArrays.<locals>.<genexpr>r!   setappendadd	TypeError)r#   r$   rvalr   rr&   r.   r	   r/   r
   distinctUnionOfArraysP   s&   


z%_ArrayOperators.distinctUnionOfArraysc                    s@   d | t }| D ]} fdd|D D ]}|| qq
|S )Nr   c                 3   r+   r)   r,   r-   r/   r	   r
   r   k   r0   z6_ArrayOperators.distinctUnionOfSets.<locals>.<genexpr>)r!   r2   r4   )r#   r$   r6   r&   r.   r	   r/   r
   distinctUnionOfSetsf   s   
z#_ArrayOperators.distinctUnionOfSetsc              	      s   d | g }t }g } fdd| D D ]1}z||v s ||v r"W q|| || W q tyF   ||v r:Y q|| || Y qw |S )Nr   c                 3   r+   r)   r,   r-   r/   r	   r
   r   u   r0   z9_ArrayOperators.distinctUnionOfObjects.<locals>.<genexpr>r1   )r#   r$   r6   r   r7   r.   r	   r/   r
   distinctUnionOfObjectso   s"   


z&_ArrayOperators.distinctUnionOfObjectsc                 C   "   d |}tdd t| |D S )Nr   c                 s       | ]	}|t ur|V  qd S r)   r   r   r	   r	   r
   r          z&_ArrayOperators.max.<locals>.<genexpr>)r!   maxr   r#   r$   r%   r	   r	   r
   r?         
z_ArrayOperators.maxc                 C   r;   )Nr   c                 s   r<   r)   r=   r   r	   r	   r
   r      r>   z&_ArrayOperators.min.<locals>.<genexpr>)r!   minr   r@   r	   r	   r
   rB      rA   z_ArrayOperators.minc                 C   s&   d |}t| |}tdd |D S )Nr   c                 s   r   r   r   r   r	   r	   r
   r      r    z&_ArrayOperators.sum.<locals>.<genexpr>)r!   r   r   )r#   r$   r%   r&   r	   r	   r
   r      s   

z_ArrayOperators.sumc                    s4   d | g }| D ]}| fdd|D  q	|S )Nr   c                 3   r+   r)   r,   r-   r/   r	   r
   r      r0   z0_ArrayOperators.unionOfArrays.<locals>.<genexpr>)r!   extend)r#   r$   r6   r&   r	   r/   r
   unionOfArrays   s
   
z_ArrayOperators.unionOfArraysc                    s   d |  fdd| D S )Nr   c                    s   g | ]}t | qS r	   r,   r-   r/   r	   r
   
<listcomp>       z2_ArrayOperators.unionOfObjects.<locals>.<listcomp>)r!   r*   r	   r/   r
   unionOfObjects   s   
z_ArrayOperators.unionOfObjectsN)__name__
__module____qualname__staticmethodr(   r'   r8   r9   r:   r?   rB   r   rD   rG   r	   r	   r	   r
   r   B   s*    








r   c              
      s  | du rdS t | tjtjfr|  S t| dd}|dur0z| W S  tttfy/   Y nw t | t	j
jt	j
jfrTt | tt	j
jfsTdd  fddt| D S zt| dt  }W | S  tyj   Y nw zt| d  }W | S  ty   Y nw  d	t  fD ]A}zt| |}W n	 ty   Y qw t |tjr|j| u r|   S t |tjr|   S t |tjr|j| u r|   S |  S zt| d
  W S  ty   td  dw )a  
    Get the attribute referenced by 'key'. The key is used
    to build the name of an attribute, or attribute accessor method.

    The following attributes and accessors are tried (in this order):

    - Accessor 'getKey'
    - Accesoor 'get_key'
    - Accessor or attribute 'key'
    - Accessor or attribute 'isKey'
    - Attribute '_key'

    If none of these exist, raise KeyError
    N__getitem__c                 S   s$   zt | |W S  ty   t Y S w r)   )r   KeyErrorr   )r#   keyr	   r	   r
   	maybe_get   s
   zgetKey.<locals>.maybe_getc                    s   g | ]}| qS r	   r	   )r   r#   rN   rO   r	   r
   rE      rF   zgetKey.<locals>.<listcomp>getget_is_Key  does not exist)
isinstanceobjcobjc_object
objc_classZvalueForKey_getattrrM   
IndexErrorr5   collectionsabcSequenceSetstrMappingiterr   AttributeErrortypes
MethodType__self__BuiltinMethodTypeselectorself)r#   rN   getitemmZkeyNamer	   rP   r
   r      s`   




r   c              	   C   s|  | du rdS t | tjtjfr| || dS t | tjjr$|| |< dS dt| }|d |d| fD ]}t	| |d}|du r@q3z|| W  dS  t
yQ   Y q3w zt	| |}W n	 tyb   Y n&w t |tjro|j| u ronz	t| || W dS  ty   td| dw z	t	| d|  W n	 ty   Y nw t| d| | dS z	t| || W dS  ty   td| dw )a  
    Set the attribute referenced by 'key' to 'value'. The key is used
    to build the name of an attribute, or attribute accessor method.

    The following attributes and accessors are tried (in this order):
    - Mapping access (that is __setitem__ for collection.Mapping instances)
    - Accessor 'setKey_'
    - Accessor 'setKey'
    - Accessor 'set_key'
    - Attribute '_key'
    - Attribute 'key'

    Raises KeyError if the key doesn't exist.
    Nr2   rT   set_rU   rV   )rW   rX   rY   rZ   ZsetValue_forKey_r]   r^   rb   r   r[   r5   rd   re   rf   rg   setattrrM   )r#   rN   valueZaBaseaccessorrl   r	   r	   r
   r      sX   r   c              	   C   s   |st | du r
dS t| tjtjfr| |S |d}| }t|}|D ]1}|dd dkrQztt	|dd }W n t
yI   t d| dw |||  S t||}q%|S )z
    Get the value for the keypath. Keypath is a string containing a
    path of keys, path elements are separated by dots.
    Nr   r   @zArray operator z not implemented)rM   rW   rX   rY   rZ   ZvalueForKeyPath_splitrc   r[   r   rd   r   )r#   keypathelementscurZelemitereoperr	   r	   r
   r   6  s&   

r   c                 C   sd   | du rdS t | tjtjfr| ||S |d}| }|dd D ]}t||}q"t||d |S )zo
    Set the value at 'keypath'. The keypath is a string containing a
    path of keys, separated by dots.
    Nr   )rW   rX   rY   rZ   ZsetValue_forKeyPath_rr   r   r   )r#   rs   ro   rt   ru   rv   r	   r	   r
   r   R  s   
r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )kvcc                 C   s
   || _ d S r)   )__pyobjc_object__)rj   r#   r	   r	   r
   __init__f     
zkvc.__init__c                 C   s   t | j|S r)   )r   rz   )rj   attrr	   r	   r
   __getattr__i  s   zkvc.__getattr__c                 C   s
   t | jS r)   )reprrz   )rj   r	   r	   r
   __repr__l  r|   zkvc.__repr__c                 C   s.   | dst| j|| d S t| || d S )NrT   )
startswithr   rz   object__setattr__)rj   r}   ro   r	   r	   r
   r   o  s   
zkvc.__setattr__c                 C   s   t |ts	tdt| j|S NzKeys must be strings)rW   ra   r5   r   rz   )rj   r.   r	   r	   r
   rL   v  s   
zkvc.__getitem__c                 C   s$   t |ts	tdt| j|| d S r   )rW   ra   r5   r   rz   )rj   r.   ro   r	   r	   r
   __setitem__{  s   
zkvc.__setitem__N)	rH   rI   rJ   r{   r~   r   r   rL   r   r	   r	   r	   r
   ry   e  s    ry   ZNSNull)__doc__collections.abcr]   re   __all__r   r   r   r   r   r   r   ry   rX   ZlookUpClassnullr   r	   r	   r	   r
   <module>   s    	aPC!