
    7jy                     f   d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	m
Z
mZmZ ddlmZ d Z eej                   d         fdZ eej                   d         fd	Z eej                   d         fd
Z eej                   d         fdZd Zd Zd Zd Zd Z G d dej4                        Zy)z4Functions that help us work with files and folders.
    N)PathPureWindowsPathPurePosixPath)MAX_KEYBOARD_SUBFOLDERSQMK_FIRMWAREQMK_USERSPACEHAS_QMK_USERSPACE)NoSuchKeyboardErrorc                 z    | syt        |       j                         ryt        dz  | z  dz  }|j                         S )zBReturns True if `keyboard_name` is a keyboard we can compile.
    F	keyboardszkeyboard.json)r   is_absoluter   exists)keyboard_namekeyboard_jsons     A/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/path.pyis_keyboardr      sC     
 M&&( ;.>PM!!    ORIG_CWDc                 L    	 | j                  t              S # t        $ r Y yw xY w)zVReturns a Path object representing the relative path under qmk_firmware, or None.
    N)relative_tor   
ValueErrorpaths    r   under_qmk_firmwarer      s*    -- s    	##c                 \    	 t         r| j                  t              S 	 y# t        $ r Y yw xY w)zXReturns a Path object representing the relative path under $QMK_USERSPACE, or None.
    N)r	   r   r   r   r   s    r   under_qmk_userspacer   &   s<    ##M22    s    	++c                     | y	 t        t        j                  j                  t        |       t        g            t        k(  S # t
        $ r Y yw xY w)zGReturns a boolean if the input path is a child under qmk_firmware.
    F)r   osr   
commonpathr   r   r   s    r   is_under_qmk_firmwarer    1   sK     |BGG&&T
L'ABC|SS s   =A 	AAc                     | y	 t         r>t        t        j                  j	                  t        |       t
        g            t
        k(  S y# t        $ r Y yw xY w)zIReturns a boolean if the input path is a child under $QMK_USERSPACE.
    NF)r	   r   r   r   r   r   r   r   s    r   is_under_qmk_userspacer"   <   sS     |**DJ+FGHMYY  s   AA
 
	AAc                     t        d      | z  S )zIReturns the path to a keyboard's directory relative to the qmk root.
    r   )r   )r   s    r   keyboardr$   H   s     },,r   c                 "   t        |       }g }t        rt        t              |z  }t	        t
              D ]j  }|dz  j                         r"|j                  |dz  j                                |j                  }|j                         t        j                         k(  sj n t        |      dk(  r)|j                  t        |z  dz  j                                t        |z  }t	        t
              D ]j  }|dz  j                         r"|j                  |dz  j                                |j                  }|j                         t        j                         k(  sj n t        |      dkD  r|S t        j                  d       t        d| z        )zReturns all of the `keymaps/` directories for a given keyboard.

    Args:

        keyboard_name
            The name of the keyboard. Example: clueboard/66/rev3
    keymapsr   z%Could not find the keymaps directory!z(Could not find keymaps directory for: %s)r$   r	   r   r   ranger   r   appendresolveparentlenr   loggingerrorr
   )r   keyboard_folder
found_dirsthis_keyboard_folder_s        r   r&   r&   N   sj    }-OJ#M2_D./ 	A$y088:!!#7)#C"L"L"NO#7#>#> #++-1F1F1HH	 z?a}>JSSUV'/9*+  9,4463i?HHJK3::'')\-A-A-CC :MM9:
H=X
YYr   c                 t    t        |       D ]*  }||z  j                         s||z  j                         c S  y)zLocate the directory of a given keymap.

    Args:

        keyboard_name
            The name of the keyboard. Example: clueboard/66/rev3
        keymap_name
            The name of the keymap. Example: default
    N)r&   r   r)   )r   keymap_name
keymap_dirs      r   keymapr5   w   s=     m, 8
$,,.,55778r   c                 z    t        |       } | j                         r| S t        t        j                  d         | z  S )zReturns a `pathlib.Path()` object for a given path.

    This will use the path to a file as seen from the directory the script was called from. You should use this to normalize filenames supplied from the command line.
    r   )r   r   r   environr   s    r   normpathr8      s7    
 :D

:&'$..r   c                     t        | t              r9t        | j                        }d|d   d   j	                          |d<   t        | } | S )a  Converts a Windows-style path with drive letter to a Unix path.

    Path().as_posix() normally returns the path with drive letter and forward slashes, so is inappropriate for `Makefile` paths.

    Passes through unadulterated if the path is not a Windows-style path.

    Args:

        path
            The path to convert.

    Returns:
        The input path converted to Unix format.
    /r   )
isinstancer   listpartslowerr   )r   ps     r   unix_style_pathr@      sJ     $(1Q47==?#$!a Kr   c                   (     e Zd Z fdZ fdZ xZS )FileTypec                 4    d|vrd|d<   t        |   |i |S )NencodingzUTF-8)super__init__)selfargskwargs	__class__s      r   rF   zFileType.__init__   s*    V#!(F:w000r   c                 \    t        |      }|j                         r|S t        |   |      S )zonormalize and check exists
            otherwise magic strings like '-' for stdin resolve to bad paths
        )r8   r   rE   __call__)rG   stringnormrJ   s      r   rL   zFileType.__call__   s,     {{}tB%'*:6*BBr   )__name__
__module____qualname__rF   rL   __classcell__)rJ   s   @r   rB   rB      s    1C Cr   rB   )__doc__r,   r   argparsepathlibr   r   r   qmk.constantsr   r   r   r	   
qmk.errorsr
   r   r7   r   r   r    r"   r$   r&   r5   r8   r@   rB    r   r   <module>rY      s     	  8 8 a a *"" !J!78  ""**Z"89   $BJJz$:;  !%RZZ
%; < 	-&ZR8
/,Cx   Cr   