
    jke                          d Z dj                  d      Ze edd      D  cg c]
  }  e|        c} z  Ze ed      gz  Zdj                         j                  d      Zedj                         j                  d      z  ZdZ G d	 d
e      Z	g ddfdZ
g ddfdZg ddfdZedk(  r4ddlZddlZ ej                    ej"                         j$                         yyc c} w )a  
This module implements the algorithm for converting between a "user name" -
something that a user can choose arbitrarily inside a font editor - and a file
name suitable for use in a wide range of operating systems and filesystems.

The `UFO 3 specification <http://unifiedfontobject.org/versions/ufo3/conventions/>`_
provides an example of an algorithm for such conversion, which avoids illegal
characters, reserved file names, ambiguity between upper- and lower-case
characters, and clashes with existing files.

This code was originally copied from
`ufoLib <https://github.com/unified-font-object/ufoLib/blob/8747da7/Lib/ufoLib/filenames.py>`_
by Tal Leming and is copyright (c) 2005-2016, The RoboFab Developers:

-	Erik van Blokland
-	Tal Leming
-	Just van Rossum
z\" * + / : < > ? [ \ ] | \0           z!CON PRN AUX CLOCK$ NUL A:-Z: COM1zLPT1 LPT2 LPT3 COM2 COM3 COM4   c                       e Zd Zy)NameTranslationErrorN)__name__
__module____qualname__     :/usr/lib/python3/dist-packages/fontTools/misc/filenames.pyr   r      s    r   r    c                 D   t        | t              st        d      t        |      }t        |      }|s| d   dk(  rd| dd z   } g }| D ]6  }|t        v rd}n||j                         k7  r|dz  }|j                  |       8 dj                  |      } t        |z
  |z
  }| d| } g }	| j                  d      D ].  }
|
j                         t        v rd|
z   }
|	j                  |
       0 dj                  |	      } || z   |z   }|j                         |v rt        | |||      }|S )a  Converts from a user name to a file name.

    Takes care to avoid illegal characters, reserved file names, ambiguity between
    upper- and lower-case characters, and clashes with existing files.

    Args:
            userName (str): The input file name.
            existing: A case-insensitive list of all existing file names.
            prefix: Prefix to be prepended to the file name.
            suffix: Suffix to be appended to the file name.

    Returns:
            A suitable filename.

    Raises:
            NameTranslationError: If no suitable name could be generated.

    Examples::

            >>> 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.    ._r   Nr   )
isinstancestr
ValueErrorlenillegalCharacterslowerappendjoinmaxFileNameLengthsplitreservedFileNameshandleClash1)userNameexistingprefixsuffixprefixLengthsuffixLengthfilteredUserName	charactersliceLengthpartspartfullNames               r   userNameToFileNamer,   !   sF   D h$CDDv;Lv;L hqkS(!"% +	))I)//++I	*+ ww'(H#l2\AK%HEs# ::<,,:DT xxH 6)H~~8#(FFCOr   c                 f   t        |      }t        |      }|t        |       z   |z   dz   t        kD  r"|t        |       z   |z   dz   }t        |z
  }| d| } d}d}	|H| t        |	      j                  d      z   }
||
z   |z   }|j	                         |vr|}n|	dz  }	|	dk\  rn|H|t        |||      }|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 )r   r   r   zfillr   handleClash2)r    r!   r"   r#   r$   r%   lr(   	finalNamecounternamer+   s               r   r   r      s    > v;Lv;Lc(m#l2R7:KK3x=(<7"<'!+L[)IG

#g,,,R00D=6)>>8+ IqLGo% 
  66:	r   c                     t         t        |      z
  t        |      z
  }t        d|z        }d}d}|4|t        |      z   |z   }|j	                         | vr|}n|dz  }||k\  rn|4|t        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.)r   r   intr   r   r   )r!   r"   r#   	maxLengthmaxValuer2   r3   r+   s           r   r0   r0      s    6 "CK/#f+=I3?#HIG

CL(61>>8+ IqLGh 
 "#CDDr   __main__r   N)__doc__r   r   rangechrr   r   r   	Exceptionr   r,   r   r0   r	   doctestsysexittestmodfailed)is   0r   <module>rE      s   ( 388=  eArl3c!f3 3  c$i[   7==?EEcJ  4::<BB3G G  	9 	 +-R eP %'r" 6r R -` zCHH_W__%%&	 y 4s   C