a
    d.                     @   s   d dl mZmZmZ d dlmZ d dlZd dlZG dd deZ	G dd de	Z
G dd	 d	e	ZG d
d de	ZG dd de	ZG dd de	ZG dd deZG dd de	ZdS )    )HeadInstanceErrorBaseInstanceErrorSchemaError)	JSONValueNc                   @   s    e Zd ZdZdd Zdd ZdS )Strategyz%Base class for merge strategies.
    c                 K   s   t dS )ah  Merge head instance into base.

        walk -- WalkInstance object for the current context.
        base -- JSONValue being merged into.
        head -- JSONValue being merged.
        schema -- Schema used for merging (also JSONValue)
        kwargs -- Dict with any extra options given in the 'mergeOptions'
        keyword

        Specific merge strategies should override this method to implement
        their behavior.

        The function should return the object resulting from the merge.

        Recursion into the next level, if necessary, is achieved by calling
        walk.descend() method.
        NNotImplementedselfwalkbaseheadschemakwargs r   ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/jsonmerge/strategies.pymerge   s    zStrategy.mergec                 K   s   t dS )aX  Return the schema for the merged document.

        walk -- WalkSchema object for the current context.
        schema -- Original document schema.
        kwargs -- Dict with any extra options given in the 'mergeOptions'
        keyword.

        Specific merge strategies should override this method to modify the
        document schema depending on the behavior of the merge() method.

        The function should return the schema for the object resulting from the
        merge.

        Recursion into the next level, if necessary, is achieved by calling
        walk.descend() method.
        Nr   r
   r   r   r   r   r   r   
get_schema!   s    zStrategy.get_schemaN__name__
__module____qualname____doc__r   r   r   r   r   r   r   	   s   r   c                   @   s   e Zd Zdd Zdd ZdS )	Overwritec                 K   s   |S Nr   r	   r   r   r   r   5   s    zOverwrite.mergec                 K   s   |S r   r   r   r   r   r   r   8   s    zOverwrite.get_schemaNr   r   r   r   r   r   r   r   r   r   4   s   r   c                   @   s   e Zd ZdddZdd ZdS )DiscardFc                 K   s   |  r|r|S |S d S r   )is_undef)r
   r   r   r   r   ZkeepIfUndefr   r   r   r   r   <   s    zDiscard.mergec                 K   s   |S r   r   r   r   r   r   r   B   s    zDiscard.get_schemaN)Fr   r   r   r   r   r   ;   s   
r   c                   @   s(   e Zd Zdd Zd	ddZd
ddZdS )Versionc                 C   s$   |d u rt  }nt |}||d< |S )Nvalue)dict)r
   r   metadatarvr   r   r   add_metadataG   s
    zVersion.add_metadataNTc	                 K   s  |du rd}|d ur.| t|dds.td| rPtg |jd}tdd}
nl| |dsftd	|tt|j|j}|jr|d
 }
| |
dstd|
d|
jvrtd|
n
tdd}
|r|
 s|
d j|jkr|j| 	|j| |d ur|j| d  |_|S )NF)valobjectz,'metadata' option does not contain an object)r%   refTZundefarrayzGBase is not an array. Base not previously generated with this strategy?zeLast entry in the versioned array is not an object. Base not previously generated with this strategy?r    zlLast entry in the versioned array has no 'value' property. Base not previously generated with this strategy?)
is_typer   r   r   r'   r   listr%   appendr$   )r
   r   r   r   r   limituniqueZ
ignoreDupsr"   r   Z
last_entryr   r   r   r   P   s:    


zVersion.mergec                 K   sr   |d urt ||}ni }d|vr.i |d< nt |d |d< |j|d d< d|d}|d urf||d< t||jS )N
propertiesr    r)   )typeitemsmaxItems)r!   Zresolve_subschema_option_refsr%   r   r'   )r
   r   r   r.   ZmetadataSchemar   itemr#   r   r   r   r   x   s    
zVersion.get_schema)NNTN)NN)r   r   r   r$   r   r   r   r   r   r   r   E   s   	
(r   c                   @   s   e Zd Zdd Zdd ZdS )Appendc                 K   sh   | |dstd|| r,tg |j}n(| |dsBtd|tt|j|j}| j|j7  _|S )Nr)   Head is not an arrayBase is not an array)r+   r   r   r   r'   r   r,   r%   r	   r   r   r   r      s    

zAppend.mergec                 K   s    |j dd  |j dd  |S )Nr3   ZuniqueItems)r%   popr   r   r   r   r      s    zAppend.get_schemaNr   r   r   r   r   r5      s   r5   c                   @   s.   e Zd Zdd Zdd ZdddZd	d
 ZdS )ArrayMergeByIdc                    s:    t|dr& fdd|D }nj j|}|S )Nr)   c                    s   g | ]}j  j|qS r   )resolverresolve_fragmentr%   ).0ir4   r   r   r   
<listcomp>       z*ArrayMergeById.get_key.<locals>.<listcomp>)r+   r   r:   r;   r%   )r
   r   r4   idRefkeyr   r>   r   get_key   s    zArrayMergeById.get_keyc              	   c   sN   t |D ]@\}}z| |||}W n tjy:   Y qY n0 |||fV  qd S r   )	enumeraterC   
jsonschemaZRefResolutionError)r
   r   jvrA   r=   r4   rB   r   r   r   iter_index_key_item   s    
z"ArrayMergeById.iter_index_key_itemidNc              	   K   s  | |dstd|| r,tg |j}n(| |dsBtd|tt|j|j}|d}| |drtt	d|| 
|||D ]J\}	}
}| 
|||D ]0\}}}||	k r|
|krtd|
f |q qqq| 
|||D ]\}	}}||krqg }| 
|||D ]$\}}}||kr|| |}qt|dkrR|d }||||||< qt|dkr||||td	d
| q|d }td|f || q|S )Nr)   r6   r7   r2   z7This strategy is not supported when 'items' is an arrayzId '%s' was not unique in head   r   Tr(   zId '%s' was not unique in base)r+   r   r   r   r'   r   r,   r%   getr   rG   r-   lendescend)r
   r   r   r   r   rA   ZignoreIdr   	subschemar=   Zkey_1Zitem_1jZkey_2Zitem_2Zhead_keyZ	head_itemZ
matching_jZbase_keyZ	base_itemZmatched_itemr   r   r   r      s@    





zArrayMergeById.mergec                 K   s$   | d}| s |||d< |S )Nr2   )rJ   r   rL   )r
   r   r   r   rM   r   r   r   r      s    
zArrayMergeById.get_schema)rH   N)r   r   r   rC   rG   r   r   r   r   r   r   r9      s   	
1r9   c                   @   s   e Zd Zdd ZdS )ArrayMergeByIndexc                 c   s"   t |D ]\}}|||fV  qd S r   )rD   )r
   r   rF   rA   r=   r4   r   r   r   rG      s    z%ArrayMergeByIndex.iter_index_key_itemN)r   r   r   rG   r   r   r   r   rO      s   rO   c                   @   s"   e Zd ZdZdddZdd ZdS )	ObjectMergea`  A Strategy for merging objects.

    Resulting objects have properties from both base and head. Any
    properties that are present both in base and head are merged based
    on the strategy specified further down in the hierarchy (e.g. in
    properties, patternProperties or additionalProperties schema
    keywords). 

    walk -- WalkInstance object for the current context.
    base -- JSONValue being merged into.
    head -- JSONValue being merged.
    schema -- Schema used for merging (also JSONValue)
    objclass_menu -- A dictionary of classes to use as a JSON object.
    kwargs -- Any extra options given in the 'mergeOptions' keyword.

    objclass_menu should be a dictionary that maps a string name to a function
    or class that will return an empty dictionary-like object to use as a JSON
    object.  The function must accept either no arguments or a dictionary-like
    object.  The name '_default' represents the default object to use if not
    overridden by the objClass option.

    One mergeOption is supported:

    objClass -- a name for the class to use as a JSON object in the output.
    N_defaultc                 K   sT  | |dstd||d u r&dti}||}|d u rFtd| || r^t| |j}n(| |dsttd|t||j	|j}|
 D ]\}	}
tdd}| s6|d}| s||	}| r|d	}| s|
 D ]\}}t||	r|}q| r6|d
}| s6| |dr6|}||||	|
||	< q|S )Nr&   zHead is not an objectrQ   zobjClass '%s' not recognizedzBase is not an objectTr(   r0   patternPropertiesadditionalProperties)r+   r   r!   rJ   r   r   r   r'   r   r%   r2   researchrL   )r
   r   r   r   r   Zobjclass_menuZobjClassr   ZobjclskvrM   ppatternsr   r   r   r     s<    











zObjectMerge.mergec                    sb   t t j j fdd}|d |d  d}| s^|dr^|d< S )Nc                    s:     | }| s6| D ]\}}||  |< qd S r   )rJ   r   r2   rL   )keywordrX   rV   rW   r   Zschema2r   r   r   descend_keywordC  s    
z/ObjectMerge.get_schema.<locals>.descend_keywordr0   rR   rS   r&   )r   r!   r%   r'   rJ   r   r+   rL   )r
   r   r   r   r]   rX   r   r\   r   r   @  s    
zObjectMerge.get_schema)NrQ   r   r   r   r   r   rP      s   
.rP   )Zjsonmerge.exceptionsr   r   r   Zjsonmerge.jsonvaluer   rE   rT   r&   r   r   r   r   r5   r9   rO   rP   r   r   r   r   <module>   s   +
IL