
    7j              
          d 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	m
Z
 ddlmZmZmZmZ ddlmZ d	 Zd
 Z ej(                  dded       ej(                  ddg dddd       ej(                  ddddd       ej(                  ddddd       ej*                  dej,                  j.                  j0                  rdnd      d                                     Zy)!zVJSON Formatting Script

Spits out a JSON file formatted with one of QMK's formatters.
    N)ValidationError)cli)	info_json)	json_loadvalidate)InfoJSONEncoderKeymapJSONEncoderUserspaceJSONEncoderCommunityModuleJSONEncoder)normpathc                    d}	 t        |d       t        }|	 t        |d       t        }|	 t        |d       t        }|	 t        |d       t        }|S |S # t        $ r Y Nw xY w# t        $ r Y Hw xY w# t        $ r Y Bw xY w# t        $ rR}t
        j                  j                  d| |       t
        j                  j                  d|        t        }Y d}~|S d}~ww xY w)z&Detect the format of a json file.
    Nzqmk.user_repo.v1_1zqmk.user_repo.v1zqmk.community_module.v1zqmk.keyboard.v1zKFile %s did not validate as a keyboard info.json or userspace qmk.json:
	%szTreating %s as a keymap file.)
r   r
   r   r   r   r   logwarninginfor	   )file	json_datajson_encoderes       L/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/cli/format/json.py_detect_json_formatr      s    L01+ 	Y 23/L 	Y 9:5L 	-Y 12*L <5    		  		  	-GGOOkmqstuGGLL8$?,L	-sL   A A( A7 B 	A%$A%(	A43A47	BB	C!ACC!c                    d}t         j                  j                  dk(  rt        | |      }|S t         j                  j                  dk(  rt        }|S t         j                  j                  dk(  rt
        }|S t         j                  j                  dk(  rt        }|S t         j                  j                  dk(  rt        }|S t         j                  j                  dt         j                  j                         |S )z%Get the json encoder for a file.
    Nautokeyboardkeymap	userspacecommunity_modulezUnknown format: %s)
r   argsformatr   r   r	   r
   r   r   error)r   r   r   s      r   _get_json_encoderr    4   s     L
xx& *4;  
J	&&  
H	$(  
K	'+  
.	.1  	*CHHOO<    	json_fileTzJSON file to format)arg_onlytypehelpz-fz--format)r   r   r   r   r   r   z+JSON formatter to use (Default: autodetect))choicesdefaultr#   r%   z-iz	--inplace
store_truez/If set, will operate in-place on the input file)actionr#   r%   z-pz--printz0If set, will print the formatted json to stdout z*Generate an info.json file for a keyboard.F)hiddenc                 p   t        | j                  j                        }t        | j                  j                  |      }|y|t        k(  rd|v r|d   }t        |d         }||j                  di       v r|d   |   x}|d<   ||j                  d      v rnt        |d         D ]]  \  }}g }d}t        ||d   |   d         D ]4  \  }	}
||
d	   k7  r|j                  d
       |
d	   }|j                  |	       6 ||d   |<   _ t        j                  ||d      }| j                  j                  r@t        | j                  j                  ddd      5 }|j                  |dz          ddd       | j                  j                  s| j                  j                  st        |       yy# 1 sw Y   BxY w)zFormat a json file.
    NFlayoutr   layout_aliaseslayoutslayersr   yJSON_NEWLINET)cls	sort_keyszw+zutf-8
)encodingnewline)r   r   r"   r    r	   r   get	enumeratezipappendjsondumpsinplaceopenwriteprint)r   r   r   r,   	info_data	layer_numlayercurrent_layerlast_row
keymap_keyinfo_keyoutputoutfiles                r   format_jsonrJ   H   s    #((,,-I$SXX%7%7CL((X-B8$i
34	Y]]#3R88+45E+Fv+NNFYx(Y]]9--$-i.A$B ? 	5 ",/y7KF7ST\7],^ 5(J8C=0%,,^<#+C=!((45 2?	(#I.? ZZ	|tDF
xx#(($$dWdK 	)wMM&4-(	) xx~~SXX--f .	) 	)s   F,,F5)__doc__r;   
jsonschemar   milcr   qmk.infor   qmk.json_schemar   r   qmk.json_encodersr   r	   r
   r   qmk.pathr   r   r    argument
subcommandconfiguser	developerrJ    r!   r   <module>rX      s     &   / r r !H( kDx>STdJ(gqw  CG  N{  |dKtJ{|dIlTHz{<cjjooNgNgUmqr) s | } | U
)r!   