U
    Dvf                  /   @   s   d Z ddddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/h/Zd0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFhZdGZG dHdI dIeZdVedLdMdNZg dKdKfdOdPZg dKdKfdQdRZ	e
dSkrdTdUlZe  dUS )WzH
User name to file name conversion.
This was taken from the UFO 3 spec.
 	
"*+/:<>?[\]()|Zauxzclock$Zcom1Zcom2Zcom3Zcom4Zcom5Zcom6Zcom7Zcom8Zcom9conZlpt1Zlpt2Zlpt3Zlpt4Zlpt5Zlpt6Zlpt7Zlpt8Zlpt9nulZprn   c                   @   s   e Zd ZdS )NameTranslationErrorN)__name__
__module____qualname__ r7   r7   >/tmp/pip-unpacked-wheel-qlge9rch/fontTools/ufoLib/filenames.pyr3   [   s   r3   r7    )userNamec                 C   s  t | tstdt|}t|}|sB| d dkrBd| dd  } g }| D ]0}|tkr\d}n|| krp|d7 }|| qJd|} t| | }| d| } g }	| 	dD ]"}
|
 t
krd|
 }
|	|
 qd|	} ||  | }| |krt| |||}|S )a  
    `existing` should be a set-like object.

    >>> userNameToFileName("a") == "a"
    True
    >>> userNameToFileName("A") == "A_"
    True
    >>> userNameToFileName("AE") == "A_E_"
    True
    >>> userNameToFileName("Ae") == "A_e"
    True
    >>> userNameToFileName("ae") == "ae"
    True
    >>> userNameToFileName("aE") == "aE_"
    True
    >>> userNameToFileName("a.alt") == "a.alt"
    True
    >>> userNameToFileName("A.alt") == "A_.alt"
    True
    >>> userNameToFileName("A.Alt") == "A_.A_lt"
    True
    >>> userNameToFileName("A.aLt") == "A_.aL_t"
    True
    >>> userNameToFileName(u"A.alT") == "A_.alT_"
    True
    >>> userNameToFileName("T_H") == "T__H_"
    True
    >>> userNameToFileName("T_h") == "T__h"
    True
    >>> userNameToFileName("t_h") == "t_h"
    True
    >>> userNameToFileName("F_F_I") == "F__F__I_"
    True
    >>> userNameToFileName("f_f_i") == "f_f_i"
    True
    >>> userNameToFileName("Aacute_V.swash") == "A_acute_V_.swash"
    True
    >>> userNameToFileName(".notdef") == "_notdef"
    True
    >>> userNameToFileName("con") == "_con"
    True
    >>> userNameToFileName("CON") == "C_O_N_"
    True
    >>> userNameToFileName("con.alt") == "_con.alt"
    True
    >>> userNameToFileName("alt.con") == "alt._con"
    True
    z(The value for userName must be a string.    ._   Nr9   )
isinstancestr
ValueErrorlenillegalCharacterslowerappendjoinmaxFileNameLengthsplitreservedFileNameshandleClash1)r:   existingprefixsuffixprefixLengthsuffixLengthZfilteredUserName	charactersliceLengthpartspartfullNamer7   r7   r8   userNameToFileName_   s4    2


rU   c                 C   s   t |}t |}|t |  | d tkrP|t |  | d }t| }| d| } d}d}	|dkr| t|	d }
||
 | }| |kr|}qn|	d7 }	|	dkrXqqX|dkrt|||}|S )aO  
    existing should be a case-insensitive list
    of all existing file names.

    >>> prefix = ("0" * 5) + "."
    >>> suffix = "." + ("0" * 10)
    >>> existing = ["a" * 5]

    >>> e = list(existing)
    >>> handleClash1(userName="A" * 5, existing=e,
    ...		prefix=prefix, suffix=suffix) == (
    ... 	'00000.AAAAA000000000000001.0000000000')
    True

    >>> e = list(existing)
    >>> e.append(prefix + "aaaaa" + "1".zfill(15) + suffix)
    >>> handleClash1(userName="A" * 5, existing=e,
    ...		prefix=prefix, suffix=suffix) == (
    ... 	'00000.AAAAA000000000000002.0000000000')
    True

    >>> e = list(existing)
    >>> e.append(prefix + "AAAAA" + "2".zfill(15) + suffix)
    >>> handleClash1(userName="A" * 5, existing=e,
    ...		prefix=prefix, suffix=suffix) == (
    ... 	'00000.AAAAA000000000000001.0000000000')
    True
       Nr>   l   I5 )rB   rG   r@   zfillrD   handleClash2)r:   rK   rL   rM   rN   rO   lrQ   	finalNamecounternamerT   r7   r7   r8   rJ      s(    rJ   c                 C   s|   t t| t| }td| }d}d}|dkrh|t| | }| | krT|}qhn|d7 }||kr(qhq(|dkrxtd|S )a  
    existing should be a case-insensitive list
    of all existing file names.

    >>> prefix = ("0" * 5) + "."
    >>> suffix = "." + ("0" * 10)
    >>> existing = [prefix + str(i) + suffix for i in range(100)]

    >>> e = list(existing)
    >>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == (
    ... 	'00000.100.0000000000')
    True

    >>> e = list(existing)
    >>> e.remove(prefix + "1" + suffix)
    >>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == (
    ... 	'00000.1.0000000000')
    True

    >>> e = list(existing)
    >>> e.remove(prefix + "2" + suffix)
    >>> handleClash2(existing=e, prefix=prefix, suffix=suffix) == (
    ... 	'00000.2.0000000000')
    True
    9Nr>   zNo unique name could be found.)rG   rB   intr@   rD   r3   )rK   rL   rM   Z	maxLengthZmaxValuerZ   r[   rT   r7   r7   r8   rX      s    rX   __main__r;   N)r7   r9   r9   )__doc__rC   rI   rG   	Exceptionr3   r@   rU   rJ   rX   r4   doctesttestmodr7   r7   r7   r8   <module>   s   2X90