
    7j                        d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ d
 Z ed      dd       ZddZ ed      d        Z ed      d        Z ed      d        Zd Zd Zd Zy)z9Functions that help us generate and use info.json files.
    N)Mapping)	lru_cache)OrderedDict)Path)deepcopy)cli)
maybe_exitc                 L    i }| D ]  \  }}||v rt        d|      |||<    |S )zReject duplicate keys.zduplicate key: )
ValueError)ordered_pairsdkvs       H/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/json_schema.py_dict_raise_on_duplicatesr      s?    
A 16A788AaD	
 H       )maxsizec                    	 t        | t              r| j                  d      } t        j                  | |rt
              S d      S # t        j                  j                  t        j                  f$ r6}t        j                  j                  d| |       t        d       Y d}~yd}~wt        $ r6}t        j                  j                  d| |       t        d       Y d}~yd}~ww xY w)zGLoad a json file from disk.

    Note: file must be a Path object.
    zutf-8)encodingN)object_pairs_hookzNInvalid JSON encountered attempting to load {fg_cyan}%s{fg_reset}:
	{fg_red}%s   zCUnknown error attempting to load {fg_cyan}%s{fg_reset}:
	{fg_red}%s)
isinstancer   openhjsonloadr   jsondecoderJSONDecodeErrorHjsonDecodeErrorr   logerrorr	   	Exception)	json_filestrictes      r   _json_load_implr'      s     i&!8Izz)TZ7Pee`deeLL((%*@*@A hjsuvw1 ]_hjkl1s)   >A A -C(5,B&&C(2,C##C(c                 .    t        t        | |            S )Nr$   r%   )r   r'   r)   s     r   	json_loadr*   1   s    OiGHHr   c                     t        |       j                         rt        |       S t        d|  d      }|j                         st        d      }t        |      S )z&Read a jsonschema file from disk.
    zdata/schemas/z.jsonschemazdata/schemas/false.jsonschema)r   existsr*   )schema_nameschema_paths     r   load_jsonschemar/   5   sU     K!%%{m;?@K:;[!!r   r   c                      i } t        d      j                  d      D ]^  }t        |      }t        |t              s!t
        j                  j                  d|       ?|| |d   <   || t        |      j                  <   ` | S )z1Compile all our schemas into a schema store.
    zdata/schemasz*.jsonschemazSkipping schema file %sz$id)	r   globr/   r   dictr   r!   debugname)schema_storeschema_fileschema_datas      r   compile_schema_storer8   D   s     LN+00@ 
;%k2+t,GGMM3[A ,7['( 0;T+&++,
; r   c                     t               }t        j                  j                  ||    |      }t        j                  ||    |      j
                  S )z1Creates a validator for the given schema id.
    )store)resolver)r8   
jsonschemaRefResolverfrom_schemaDraft202012Validatorvalidate)schemar5   r;   s      r   create_validatorrB   Y   sJ     ()L%%11,v2Fl1[H**<+?(S\\\r   c                 (    t        |      } ||       S )z%Validates data against a schema.
    )rB   )datarA   	validators      r   r@   r@   c   s     !(IT?r   c                     |j                         D ]:  \  }}t        |t              r t        | j	                  |i       |      | |<   6|| |<   < | S )zKUpdate a dictionary in place, recursing to do a depth-first deep copy.
    )itemsr   r   deep_updateget)origdictnewdictkeyvalues       r   rH   rH   k   sT     mmo "
UeW%'S"(=uEHSM "HSM" Or   c                 r    t               }d }| D ]$  }|j                         D ]  \  }} ||||        & |S )a  Merges nested OrderedDict objects resulting from reading a hjson file.
    Later input dicts overrides earlier dicts for plain values.
    If any value is "!delete!", the existing value will be removed from its parent.
    Arrays will be appended. If the first entry of an array is "!reset!", the contents of the array will be cleared and replaced with RHS.
    Dictionaries will be recursively merged. If any entry is "!reset!", the contents of the dictionary will be cleared and replaced with RHS.
    c                 :   || v rBt        |t        t        f      r,d|v r|| |<   nt        | |   |g      | |<   d| |   v r| |   d= y y || v r-t        |t              r|d   dk(  r	|dd  | |<   y | |   |z   | |<   y |dk(  rt        | t        t        f      r| |= y || |<   y )Nz!reset!r   r   z!delete!)r   r   r2   merge_ordered_dictslist)targetr   r   s      r   	add_entryz&merge_ordered_dicts.<locals>.add_entry   s    ;:a+t)<=A~q	/A?q	F1I%1Ii( &&[Z40ty abEq	"1IMq	*_F[$4G!Hq	F1Ir   )r   rG   )dictsresultrS   r   r   r   s         r   rP   rP   x   sM     ]F$  $ggi 	$FQfa#	$$ Mr   )T)__doc__r   r   r<   collections.abcr   	functoolsr   typingr   pathlibr   copyr   milcr   qmk.utilr	   r   r'   r*   r/   r8   rB   r@   rH   rP    r   r   <module>r_      s       #       2 (I 2" " 1 ( 2] ]
r   