o
    Q `w                     @   s   d Z ddlmZ ddl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
 d
eje Zdd Zdd Zdd ZedfddZdS )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                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dfddZdd Zedd Zedd Zedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zd%S )&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.

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

    Raises:
        ValueError: If the mode string is invalid.

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

    c                 C   s   || _ |   d S N)_modevalidate)selfmode r   )/usr/lib/python3/dist-packages/fs/mode.py__init__5   s   zMode.__init__c                 C   s   d | jS )Nz
Mode({!r}))formatr   r   r   r   r   __repr__:   s   zMode.__repr__c                 C   s   | j S r   )r   r   r   r   r   __str__>   s   zMode.__str__c                 C   s   t |tsJ || jv S )z4Check if a mode contains a given character.
        )
isinstancer   r   )r   	characterr   r   r   __contains__B   s   
zMode.__contains__c                 C   s   t jr
| 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)sixZPY2r   replacer   r   r   r   to_platformI   s   zMode.to_platformc                 C   s$   |   dd}d|v r|S |d 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   r   r   r   to_platform_binS   s   zMode.to_platform_binzrwxtab+c                 C   s^   | j }|s	td||std||d dvrtdd|v r+d|v r-tdd	S d	S )
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   r   r   r   r   ]   s   
zMode.validatec                 C   s   |    d| v rtddS )zValidate a mode for opening a binary file.

        Raises:
            ValueError: if the mode contains invalid chars.

        r!   zmode must be binaryN)r   r(   r   r   r   r   validate_bino   s   zMode.validate_binc                 C   s   d| v pd| v pd| v S )z8`bool`: `True` if the mode would create a file.
        ar   r   r   r   r   r   r   create{   s   zMode.createc                 C      d| v pd| v S )z4`bool`: `True` if the mode permits reading.
        r+r   r   r   r   r   reading      zMode.readingc                 C   s    d| v pd| v pd| v pd| v S )z4`bool`: `True` if the mode permits writing.
        r   r,   r0   r   r   r   r   r   r   writing   s    zMode.writingc                 C      d| v S )z6`bool`: `True` if the mode permits appending.
        r,   r   r   r   r   r   	appending      zMode.appendingc                 C   r4   )zE`bool`: `True` if the mode permits both reading and writing.
        r0   r   r   r   r   r   updating   r6   zMode.updatingc                 C   r.   )zD`bool`: `True` if the mode would truncate an existing file.
        r   r   r   r   r   r   r   truncate   r2   zMode.truncatec                 C   r4   )z?`bool`: `True` if the mode require exclusive creation.
        r   r   r   r   r   r   	exclusive   r6   zMode.exclusivec                 C   r4   )z3`bool`: `True` if a mode specifies binary.
        r#   r   r   r   r   r   binary   r6   zMode.binaryc                 C   s   d| v pd| vS )z1`bool`: `True` if a mode specifies text.
        r!   r#   r   r   r   r   r   text   r2   z	Mode.textN)__name__
__module____qualname____doc__r   r   r   r   r    r$   	frozensetr   r+   propertyr-   r1   r3   r5   r7   r8   r9   r:   r;   r   r   r   r   r	      s8    









r	   c                 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	   r1   r   r   r   r   r
         
r
   c                 C   rB   )zCheck a mode string allows writing.

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

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

    )r	   r3   rC   r   r   r   r      rD   r   c                 C   s   t |  dS )zCheck ``mode`` parameter of `~fs.base.FS.open` is valid.

    Arguments:
        mode (str): Mode parameter.

    Raises:
        `ValueError` if mode is not valid.

    N)r	   rC   r   r   r   validate_open_mode   s   rE   zrwxab+c                 C   sL   d| v rt d| st d| d dvrt d|| s$t d| dS )	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   r'   z)mode must start with 'r', 'w', 'a' or 'x'r&   N)r(   r)   r   )r   r*   r   r   r   r      s   
r   )r?   Z
__future__r   r   typingr   Z_typingr   ZTYPE_CHECKINGr   r   r   __all__Zpython_2_unicode_compatible	Containerr	   r
   r   rE   r@   r   r   r   r   r   <module>   s      !