
    obL                         d Z ddlmZmZ ddlZddlZddlmZ ej                  r
ddlm	Z	m
Z
mZ g dZej                   G d d	ej                  e                Zd
 Zd Zd Z ed      fdZy)ziAbstract I/O mode container.

Mode strings are used in in `~fs.base.FS.open` and
`~fs.base.FS.openbin`.

    )print_functionunicode_literalsN   )Text)	FrozenSetSetUnion)Modecheck_readablecheck_writablevalidate_openbin_modec                       e Zd ZdZd Zd Zd Zd Zd Zd Z	 e
d      fd	Zd
 Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zy)r
   a  An abstraction for I/O modes.

    A mode object provides properties that can be used to interrogate the
    `mode strings <https://docs.python.org/3/library/functions.html#open>`_
    used when opening files.

    Example:
        >>> mode = Mode('rb')
        >>> mode.reading
        True
        >>> mode.writing
        False
        >>> mode.binary
        True
        >>> mode.text
        False

    c                 2    || _         | j                          y)zCreate a new `Mode` instance.

        Arguments:
            mode (str): A *mode* string, as used by `io.open`.

        Raises:
            ValueError: If the mode string is invalid.

        N)_modevalidate)selfmodes     )/usr/lib/python3/dist-packages/fs/mode.py__init__zMode.__init__-   s     
    c                 8    dj                  | j                        S )Nz
Mode({!r}))formatr   r   s    r   __repr__zMode.__repr__;   s    ""4::..r   c                     | j                   S )N)r   r   s    r   __str__zMode.__str__?   s    zzr   c                 B    t        |t              sJ || j                  v S )z+Check if a mode contains a given character.)
isinstancer   r   )r   	characters     r   __contains__zMode.__contains__C   s#     )T***DJJ&&r   c                 r    t         j                  r| j                  j                  dd      S | j                  S )zGet a mode string for the current platform.

        Currently, this just removes the 'x' on PY2 because PY2 doesn't
        support exclusive mode.

        xw)sixPY2r   replacer   s    r   to_platformzMode.to_platformI   s*     03wwtzz!!#s+FDJJFr   c                 X    | j                         j                  dd      }d|v r|S |dz   S )zvGet a *binary* mode string for the current platform.

        This removes the 't' and adds a 'b' if needed.

        t b)r'   r&   )r   r   s     r   to_platform_binzMode.to_platform_binS   s4       "**33uu5%#+5r   zrwxtab+c                     | j                   }|st        d      |j                  |      st        dj                  |            |d   dvrt        d      d|v rd|v rt        d      y	y	)
zpValidate the mode string.

        Raises:
            ValueError: if the mode contains invalid chars.

        mode must not be empty%mode '{}' contains invalid charactersr   rwxaz*mode must start with 'r', 'w', 'x', or 'a'r)   r+   z)mode can't be binary ('b') and text ('t')N)r   
ValueError
issupersetr   )r   _valid_charsr   s      r   r   zMode.validate]   sy     zz566&&t,DKKDQRR7& IJJ$;3$;HII ';r   c                 B    | j                          d| v rt        d      y)zValidate a mode for opening a binary file.

        Raises:
            ValueError: if the mode contains invalid chars.

        r)   zmode must be binaryN)r   r1   r   s    r   validate_binzMode.validate_bino   s%     	$;233 r   c                 "    d| v xs
 d| v xs d| v S )z/`bool`: `True` if the mode would create a file.ar#   r"    r   s    r   createzMode.create{   s      d{8cTk8SD[8r   c                     d| v xs d| v S )z+`bool`: `True` if the mode permits reading.r+r8   r   s    r   readingzMode.reading        d{)cTk)r   c                 .    d| v xs d| v xs
 d| v xs d| v S )z+`bool`: `True` if the mode permits writing.r#   r7   r<   r"   r8   r   s    r   writingzMode.writing   s)     d{GcTkGSD[GC4KGr   c                 
    d| v S )z-`bool`: `True` if the mode permits appending.r7   r8   r   s    r   	appendingzMode.appending        d{r   c                 
    d| v S )z<`bool`: `True` if the mode permits both reading and writing.r<   r8   r   s    r   updatingzMode.updating   rC   r   c                     d| v xs d| v S )z;`bool`: `True` if the mode would truncate an existing file.r#   r"   r8   r   s    r   truncatezMode.truncate   r>   r   c                 
    d| v S )z6`bool`: `True` if the mode require exclusive creation.r"   r8   r   s    r   	exclusivezMode.exclusive   rC   r   c                 
    d| v S )z*`bool`: `True` if a mode specifies binary.r+   r8   r   s    r   binaryzMode.binary   rC   r   c                     d| v xs d| vS )z(`bool`: `True` if a mode specifies text.r)   r+   r8   r   s    r   textz	Mode.text   s     d{-co-r   N)__name__
__module____qualname____doc__r   r   r   r    r'   r,   	frozensetr   r5   propertyr9   r=   r@   rB   rE   rG   rI   rK   rM   r8   r   r   r
   r
      s    &/'G6 %.i$8 J$
4 9 9
 * *
 H H
  
  
 * *
  
  
 . .r   r
   c                 ,    t        |       j                  S )zCheck a mode string allows reading.

    Arguments:
        mode (str): A mode string, e.g. ``"rt"``

    Returns:
        bool: `True` if the mode allows reading.

    )r
   r=   r   s    r   r   r           :r   c                 ,    t        |       j                  S )zCheck a mode string allows writing.

    Arguments:
        mode (str): A mode string, e.g. ``"wt"``

    Returns:
        bool: `True` if the mode allows writing.

    )r
   r@   rU   s    r   r   r      rV   r   c                     t        |        y)zCheck ``mode`` parameter of `~fs.base.FS.open` is valid.

    Arguments:
        mode (str): Mode parameter.

    Raises:
        `ValueError` if mode is not valid.

    N)r
   rU   s    r   validate_open_moderY      s     	Jr   zrwxab+c                     d| v rt        d      | st        d      | d   dvrt        d      |j                  |       st        dj                  |             y)	zCheck ``mode`` parameter of `~fs.base.FS.openbin` is valid.

    Arguments:
        mode (str): Mode parameter.

    Raises:
        `ValueError` if mode is not valid.

    r)   ztext mode not valid in openbinr.   r   r0   z)mode must start with 'r', 'w', 'a' or 'x'r/   N)r1   r2   r   )r   r3   s     r   r   r      sh     d{9::122AwfDEE""4(@GGMNN )r   )rQ   
__future__r   r   typingr$   _typingr   TYPE_CHECKINGr   r   r	   __all__python_2_unicode_compatible	Containerr
   r   r   rY   rR   r   r8   r   r   <module>rb      s~    8  
 	,, P   V.6D! V. !V.r .7x-@ Or   