
    7ji%              
       f   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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dlmZ ddlmZmZ  ed      Z ed      Z e	 ed            Z eed   e jB                        Z" eed   e jB                        Z# eejI                         D  cg c]  } | jK                         s| jL                  ! c}       Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d  Z4d! Z5d" Z6d# Z7d$ Z8 ejr                  d%d&d'd(e*)       ejr                  d*d+d,d(e))       ejr                  d-d.d/d(e()       ejr                  d0d1d2d34       ejr                  d5d6d7d(8       ejt                  d9      d:                                           Z;yc c} w );zYThis script automates the creation of new keyboard directories using a starter template.
    N)date)Path)dotty)cli)choicequestionyesno)git_get_username)load_jsonschema)keyboard)InfoJSONEncoder)deep_update)MCU2BOOTLOADERQMK_FIRMWAREzlayouts/default/zdata/templates/keyboard/r   zproperties.processor.enum)keyz!properties.development_board.enumc                 0    | t         t        z   vrt        | S z&Callable for argparse validation.
    )
dev_boards	mcu_types
ValueErrormcus    M/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/cli/new/keyboard.pymcu_typer      s     :	)*J    c                 "    | t         vrt        | S r   )available_layoutsr   )layouts    r   layout_typer   &   s     &&Mr   c                 (    t        |       st        | S r   )validate_keyboard_namer   )names    r   keyboard_namer#   .   s     "$'Kr   c                 `    t        j                  d      }t        |j                  |             S )zhReturns True if the given keyboard name contains only lowercase a-z, 0-9 and underscore characters.
    z^[a-z0-9][a-z0-9/_]+$)recompileboolmatch)r"   regexs     r   r!   r!   6   s'     JJ/0ED!""r   c                 .    t        j                  | d      S )z)Provide sane defaults for bootloader
    custom)r   getr   s    r   select_default_bootloaderr-   =   s     c8,,r   c                     | j                         }|j                         D ]  \  }}|j                  d| d|      } |j                  |       y)z;Replaces the given placeholders in each template file.
    %N)	read_textitemsreplace
write_text)srcdesttokenscontentr   values         r   replace_placeholdersr9   C   sN     mmoGlln 5
U//AcU!*e45 	OOGr   c                 b    | j                  | j                         j                  ||             y N)r3   r0   r2   )r4   tokenr8   s      r   replace_stringr=   M   s!    NN3==?**5%89r   c                 V   t        j                  |j                               }t        j                  |j                               }t        ||       t        ||        t	        t        |d   j                                     d   }d\  }}|D ]8  }t        |t        |d         dz         }t        |t        |d         dz         }: dg|z  dg|z  d	|d
<   |D ]!  }t        |d         t        |d         g|d<   # |j                  t        j                  |t        d             y)z1Splice in any additional data into info.json
    layoutsr   )r   r   x   yC2D1)colsrowsmatrix_pinsmatrixT)cls	sort_keysN)jsonloadsr0   r   nextitervaluesmaxintr3   dumpsr   )	configr4   r5   infotemplatefirst_layoutwidthheightitems	            r   augment_community_inforZ   Q   s    ::cmmo&Dzz$..*+H hf T)_33567AL ME6 1E3tCy>A-.VSc^a/01
 D  :d3i.#d3i.9X: 	OODJJtDIJr   c                  n    |d   }|d= |d   }|d= | d   }d}|st        |fi |} ||      sd}|}|s|S )zAUgly workaround until 'milc' learns to display a repromt msg
    repromptvalidater   N)r   )argskwargsr\   r]   promptrets         r   	_questionrb   q   se    
 j!Hzj!Hz!WF
Cv((}CF	  Jr   c                     t         j                  j                  d|  d       t         j                  j                  |       y )Nz{fg_yellow}z{style_reset_all})r   logrT   )heading
subheadings     r   prompt_heading_subheadingrg      s,    GGLL=	)<=>GGLLr   c                  <    t        dd       d} t        d| d       S )NzName Your Keyboard ProjectzhFor more information, see:
https://docs.qmk.fm/hardware_keyboard_guidelines#naming-your-keyboard-projectz8Keyboard already exists! Please choose a different name:zKeyboard Name?c                 6    t        |       j                          S r;   )r   existsr@   s    r   <lambda>z!prompt_keyboard.<locals>.<lambda>   s    xXY{OaOaOcKc r   )r\   r]   )rg   rb   )errmsgs    r   prompt_keyboardrn      s-    : =Q R HF%Acddr   c                  D    t        dd       t        dt                     S )NAttribution$Used for maintainer, copyright, etc.zYour GitHub Username?default)rg   r   r
    r   r   prompt_userru      s    m-ST+5E5GHHr   c                 4    t        dd       t        d|       S )NzMore Attributionrq   zYour Real Name?rr   )rg   r   )def_names    r   prompt_namerx      s    02XY%x88r   c                       t        dd       t        D   cg c]   t         fddD              r  }} |j                  d       t	        d|t        |      dz
        S c c} w )	NzPick Base LayoutzSAs a starting point, one of the common layouts can be used to
bootstrap the processc              3   &   K   | ]  }|v  
 y wr;   rt   .0xsr@   s     r   	<genexpr>z prompt_layout.<locals>.<genexpr>   s     ?}Ba?}   )_split_blocker_tsangan_f13none of the abovezDefault Layout?rA   rr   )rg   r   anyappendr   len)r@   filtered_layoutss   ` r   prompt_layoutr      si    0 3  $5~aC?}R|?}<}~~/0#%5sCS?TWX?XYY s
   A"A"c                  0    t        dd       t        d      S )NzWhat Powers Your ProjectzIs your board using a separate development board, such as a Pro Micro,
or is the microcontroller integrated onto the PCB?

For more information, see:
https://docs.qmk.fm/compatible_microcontrollerszUsing a Development Board?)rg   r	   rt   r   r   prompt_mcu_typer      s     " %3 -..r   c                  d    t        dd       t        dt        t        j                  d            S )NzSelect Development BoardJFor more information, see:
https://docs.qmk.fm/compatible_microcontrollerszDevelopment Board?promicrorr   )rg   r   r   indexrt   r   r   prompt_dev_boardr      s/    8 ;3 4 &
J<L<LZ<XYYr   c                       t        dd       t        D   cg c]   t         fddD              r  }} t        d||j	                  d            S c c} w )NzSelect Microcontrollerr   c              3   &   K   | ]  }|v  
 y wr;   rt   r{   s     r   r~   zprompt_mcu.<locals>.<genexpr>   s     3\B!G3\r   )cortexunknownzMicrocontroller?
atmega32u4rr   )rg   r   r   r   r   )r@   filtered_mcus   ` r   
prompt_mcur      sW    6 93 4  )]!3\F[3\0\A]L]$lL<N<N|<\]] ^s
   AAz-kbz
--keyboardz/Specify the name for the new keyboard directoryT)helparg_onlytypez-lz--layoutz"Community layout to bootstrap withz-tz--typez=Specify the keyboard MCU type (or "development_board" preset)z-uz
--usernamez/Specify your username (default from Git config)r"   )r   r5   z-nz
--realnamezDSpecify your real name if you want to use that. Defaults to username)r   r   z Creates a new keyboard directoryc                    | j                   j                  d        | j                  d       | j                  j                  r| j                  j                  n	t               }t        |      s| j                   j                  d       yt	        |      j                         r | j                   j                  d| d       y| j                  j                  j                  r | j                  j                  j                  n	t               }| j                  j                  s | j                  j                  j                  r8| j                  j                  xs+ | j                  j                  j                  n
t        |      }| j                  j                  r| j                  j                  n	t!               }| j                  j"                  r| j                  j"                  }nt%               r
t'               n	t)               }i }|t*        v r||d<   n||d<   t-        |      |d	<   d
}|dk(  rd}d}t/        t1        j2                         j4                        |||d}t	        |      dz  }	|	j7                  d       t9        t:        | d| dz        }
t=        j>                  |
|	dz         tA        tB        jE                               D ]%  }tG        |t	        |      |j                  z  |       ' t9        t:        | dz        }tI        ||t	        |      dz         |r)tK        t	        |      dz  dd       tK        |	dz  dd       | j                   j                  d| d       | j                   j                  d| d       | j                   j                  dtL         dt	        |       d       | j                   j                  d       y )!zCreates a new keyboard.
    zC{style_bright}Generating a new QMK keyboard directory{style_normal} zKeyboard names must contain only {fg_cyan}lowercase a-z{fg_reset}, {fg_cyan}0-9{fg_reset}, and {fg_cyan}_{fg_reset}! Please choose a different name.rA   zKeyboard {fg_cyan}z:{fg_reset} already exists! Please choose a different name.development_board	processor
bootloaderFr   	ortho_4x4T)YEARKEYBOARD	USER_NAME	REAL_NAMEzkeymaps/)parentsz	/default_/rs   z
/info.jsonzkeyboard.jsonLAYOUT_ortho_4x4LAYOUTzdefault/keymap.cz1{fg_green}Created a new keyboard called {fg_cyan}z{fg_green}.{fg_reset}z*Build Command: {fg_yellow}qmk compile -kb z -km default{fg_reset}.zProject Location: {fg_cyan}z{fg_reset}.zG{fg_yellow}Now update the config files to match the hardware!{fg_reset}N)'rd   rT   echor^   r   rn   r!   errorrj   rS   new_keyboardr"   ru   realnamerx   r   r   r   r   r   r   r   r-   strr   todayyearmkdirr   	COMMUNITYshutilcopytreelistTEMPLATEiterdirr9   rZ   r=   r   )r   kb_name	user_name	real_namedefault_layoutr   rS   detach_layoutr6   keymaps_pathcommunity_keymapfilecommunity_infos                r   r   r      s.    GGLLVWCHHRL#&88#4#4chh/:KG!'*  m  	n!,WI5qrs03

0G0G0L0L

'',,R]R_IEHXXEVEVZ]ZdZdZqZqZvZv!!ASZZ%<%<%A%A  }H  IR  }SI(+SXX__]_N
xx}}hhmm$3$5 :<F
j&)"#!{8=|M,,$ DJJL%%&	F G$z1Lt$ I>*:)NCSST(UUV
OO$lY&>? X%%'( JT8G#4tyy#@&IJ )(8
&CCDN6>8G3D3VW x(?:<NPXY|&88:LhWGGLLH	QjklGGLL?yHabcGGLL0a@Q?RR_`aGGLLZ[r   )<__doc__r%   rK   r   datetimer   pathlibr   
dotty_dictr   milcr   milc.questionsr   r   r	   qmk.gitr
   qmk.json_schemar   qmk.pathr   qmk.json_encodersr   r   qmk.constantsr   r   r   r   schemasortedr   casefoldr   r   r   is_dirr"   r   r   r   r#   r!   r-   r9   r=   rZ   rb   rg   rn   ru   rx   r   r   r   r   argument
subcommandr   rk   s   0r   <module>r      s   	       2 2 $ +  - ' 6#$	*+ 
z*	+656CLLI	F>?S\\R
I,=,=,?Nq188:AFFNO #-:K@*
eI9Z	/Z^ e\(Ydho|}dJ%ITX_jkdH#bmq  yA  BdL'X_efdL'mx|}23C\ 4 ~ g B l ~C\g Os   7F.F.