
    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	m
Z
 ddlmZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZmZ ddlmZmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# d Z$d Z%d Z&d Z'd Z(d Z) ejT                  ddeed       ejT                  ddd       ejT                  dd       ejT                  d d!d"d#$       ejV                  d%      eed&                                                  Z,y)'zNThis script automates the copying of the default keymap into your own keymap.
    N)Path)cli)questionchoice)HAS_QMK_USERSPACEQMK_USERSPACE)NoSuchKeyboardError)is_keyboardkeymapskeymap)git_get_username)automagic_keyboardautomagic_keymap)keyboard_completerkeyboard_folder)UserspaceDefs)	json_load)KeymapJSONEncoder)	info_jsonc                     t        |       syt        |       }|j                  d      }|syt        d t	        d      j                  d| d      D              S )zBSearch for converters that can be applied to a given keyboard
    Npin_compatiblec              3   X   K   | ]"  }|j                   j                  d       d    $ yw)_to_N)namesplit).0folders     K/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/cli/new/keymap.py	<genexpr>z-_list_available_converters.<locals>.<genexpr>"   s$     |F&++##F+B/|s   (*	platformsz*/converters/z_to_*)r
   r   getsortedr   glob)kb_nameinfor   s      r   _list_available_convertersr'      s\     wWDXX./N|d;>O>T>TWdesdttyUz>{|||    c                     | j                         rt        |       ni }||d<   t        j                  |t        d      }| j                  |dz   d       y)zBadd/overwrite any existing converter specified in keymap.json
    	converterT)cls	sort_keys
zutf-8)encodingN)existsr   jsondumpsr   
write_text)filer*   	json_dataoutputs       r   _set_converterr6   %   sH     $(;;=	$bI&IkZZ	'8DIFOOFTMGO4r(   c                 `    t        j                  d      }t        |j                  |             S )z\Returns True if the given keymap name contains only a-z, 0-9 and underscore characters.
    z^[a-zA-Z0-9][a-zA-Z0-9_]+$)recompileboolmatch)r   regexs     r   validate_keymap_namer=   0   s'     JJ45ED!""r(   c                      d} t        |       S )Nz{fg_yellow}Select Keyboard{style_reset_all}
If you're unsure you can view a full list of supported keyboards with {fg_yellow}qmk list-keyboards{style_reset_all}.

Keyboard Name? )r   prompts    r   prompt_keyboardrA   7   s    F Fr(   c                  0    d} t        | t                     S )Nz<
{fg_yellow}Name Your Keymap{style_reset_all}

Keymap name? )default)r   r   r?   s    r   prompt_userrD   ?   s    F F$4$677r(   c                 x    d}t        |       }|sy dg|}t        ||d      }|j                  |      dk(  rd S |S )Nz
{fg_yellow}Configure Development Board{style_reset_all}
For more information, see:
https://docs.qmk.fm/feature_converters

Use converter? zNo (default)r   )optionsrC   )r'   r   index)r%   r@   
converterschoicesanswers        r   prompt_converterrK   G   sQ    F ,G4J+
+GFGQ7F==(A-4969r(   z-kbz
--keyboardz5Specify keyboard name. Example: 1upkeyboards/1up60hse)type	completerhelpz-kmz--keymapz-Specify the name for the new keymap directory)rN   z--converterz,Specify the name of a converter to configurez--skip-converterT
store_truezSkip converter)arg_onlyactionrN   z6Creates a new keymap for the keyboard of your choosingc                    | j                   j                  d        | j                  d       | j                  j                  j
                  r | j                  j                  j
                  n	t               }| j                  j                  j                  r | j                  j                  j                  n	t               }| j                  j                  s | j                  j                  j                  r | j                  j                  j                  n
t        |      }	 t        |      }	 t!        |      }|d   |z  }t        |d	      }|s | j                   j                  d
| d       yt%        |      s| j                   j                  d       y|j'                         r | j                   j                  d| d       yt)        j*                  ||d       |rt-        |dz  |       | j                   j                  d| d| d       | j                   j                  d| d| d       t.        r6t1        t2        dz        }|j5                  ||d       |j7                         S y# t        $ r" | j                   j                  d| d       Y yw xY w# t"        $ r" | j                   j                  d| d       Y yw xY w)z<Creates a new keymap for the keyboard of your choosing.
    z3{style_bright}Generating a new keymap{style_normal} zKeyboard {fg_cyan}z6{fg_reset} does not exist! Please choose a valid name.Fr   zKeymap folder for {fg_cyan}z{fg_reset} does not exist!rC   zDefault keymap for {fg_cyan}zKeymap names must contain only {fg_cyan}a-z{fg_reset}, {fg_cyan}0-9{fg_reset} and {fg_cyan}_{fg_reset}! Please choose a different name.zKeymap {fg_cyan}z:{fg_reset} already exists! Please choose a different name.T)symlinkszkeymap.jsonz/{fg_green}Created a new keymap called {fg_cyan}z{fg_green} in: {fg_cyan}z{fg_reset}.zNCompile a firmware with your new keymap by typing: {fg_yellow}qmk compile -kb z -km zqmk.json)keyboardr   do_printN)logr&   echoconfig
new_keymaprU   rA   r   rD   argsskip_converterr*   rK   r   
ValueErrorerrorr   r	   r=   r/   shutilcopytreer6   r   r   r   
add_targetsave)r   r%   	user_namer*   keymaps_dirskeymap_path_newkeymap_path_default	userspaces           r   rZ   rZ   Y   s    GGLLFGCHHRL 14

0E0E0N0Ncjj##,,TcTeG03

0E0E0L0L

%%,,R]R_I36883J3JcjjNcNcNmNm

%%//  tD  EL  tMI!'*w'&q/I5
 !)46wi?[\]	*  `  	a*9+5qrs OO'4H6	B GGLLFykQmn}m~  L  M  NGGLLcdkcllqr{q|  }J  K  L !-*"<=	gi%P~~ K  ,WI5mno  5gY>Z[\s$   $I2 0J  2(JJ (K
K)-__doc__r8   r0   r_   pathlibr   milcr   milc.questionsr   r   qmk.constantsr   r   
qmk.errorsr	   qmk.pathr
   r   r   qmk.gitr   qmk.decoratorsr   r   qmk.keyboardr   r   qmk.userspacer   qmk.json_schemar   qmk.json_encodersr   qmk.infor   r'   r6   r=   rA   rD   rK   argument
subcommandrZ    r(   r   <module>ry      s    	     + : * 1 1 $ ? < ' % / }5#8:$ e\CU  ]T  UeZ&UVm"PQ 4K[\HI6    J ] R W U6 r(   