a
    1$e                     @   sr   d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZ eeZG dd deZG d	d
 d
eZdS )z3Classes capable of reading and writing collections
    N)MemoryFileBase_listdir_listlayers)
Collection)supports_vsi)DriverErrorc                
       sP   e Zd ZdZd fdd	ZdddZdd	d
ZdddZdd Zdd Z	  Z
S )
MemoryFilea  A BytesIO-like object, backed by an in-memory file.

    This allows formatted files to be read and written without I/O.

    A MemoryFile created with initial bytes becomes immutable. A
    MemoryFile created without initial bytes may be written to using
    either file-like or dataset interfaces.

    Parameters
    ----------
    file_or_bytes : an open Python file, bytes, or None
        If not None, the MemoryFile becomes immutable and read-only.
        If None, it is write-only.
    filename : str
        An optional filename. The default is a UUID-based name.
    ext : str
        An optional file extension. Some format drivers require a
        specific value.

    N c                    s,   |r| dsd| }t j|||d d S )N.)file_or_bytesfilenameext)
startswithsuper__init__selfr   r   r   	__class__ D/var/www/html/django/DPS/env/lib/python3.9/site-packages/fiona/io.pyr   #   s
    zMemoryFile.__init__Fc                 K   s   | j rtd|
s*|dur*t|s*td|dv rB|  sBtd|du rb|dkrb|  rbtd|  rr|dkr|dur| | d}n|du rd}t| j|f|||||||
|	d	|S )
a  Open the file and return a Fiona collection object.

        If data has already been written, the file is opened in 'r'
        mode. Otherwise, the file is opened in 'w' mode.

        Parameters
        ----------
        Note well that there is no `path` parameter: a `MemoryFile`
        contains a single dataset and there is no need to specify a
        path.

        Other parameters are optional and have the same semantics as the
        parameters of `fiona.open()`.
        I/O operation on closed file.Nz)Driver {} does not support virtual files.)razMemoryFile does not exist.wzMemoryFile already exists.r   )crsdriverschemaencodinglayerenabled_driversallow_unsupported_driverscrs_wkt)closedOSErrorr   r   existsZ_ensure_extensionr   name)r   moder   r   r   r   r   Zvfsr    r"   r!   kwargsr   r   r   open)   sD    
zMemoryFile.openc                 C   s8   | j rtd|r(d| j|d}n| j }t|S )zList files in a directory.

        Parameters
        ----------
        path : URI (str or pathlib.Path)
            A dataset resource identifier.

        Returns
        -------
        list
            A list of filename strings.

        r   {}/{}/)r#   r$   formatr&   lstripr   r   pathvsi_pathr   r   r   listdiri   s    zMemoryFile.listdirc                 C   s8   | j rtd|r(d| j|d}n| j }t|S )a  List layer names in their index order

        Parameters
        ----------
        path : URI (str or pathlib.Path)
            A dataset resource identifier.

        Returns
        -------
        list
            A list of layer name strings.

        r   r*   r+   )r#   r$   r,   r&   r-   r   r.   r   r   r   
listlayers   s    zMemoryFile.listlayersc                 C   s   | S Nr   )r   r   r   r   	__enter__   s    zMemoryFile.__enter__c                 O   s   |    d S r3   )close)r   argsr(   r   r   r   __exit__   s    zMemoryFile.__exit__)NNr	   )
NNNNNNNNNF)N)N)__name__
__module____qualname____doc__r   r)   r1   r2   r4   r7   __classcell__r   r   r   r   r      s"             
@

r   c                       s,   e Zd ZdZd	 fdd	Zd
ddZ  ZS )ZipMemoryFilea)  A read-only BytesIO-like object backed by an in-memory zip file.

    This allows a zip file containing formatted files to be read
    without I/O.

    Parameters
    ----------
    file_or_bytes : an open Python file, bytes, or None
        If not None, the MemoryFile becomes immutable and read-only. If
        None, it is write-only.
    filename : str
        An optional filename. The default is a UUID-based name.
    ext : str
        An optional file extension. Some format drivers require a
        specific value. The default is ".zip".
    N.zipc                    s$   t  j|||d d| j | _d S )N)r   r   z/vsizip)r   r   r&   r   r   r   r   r      s    zZipMemoryFile.__init__Fc           	      K   st   |rd | j|d}nd | j}| jr4td|rNd | j|d}n| j }t|df|||||d|S )a  Open a dataset within the zipped stream.

        Parameters
        ----------
        path : str
            Path to a dataset in the zip file, relative to the root of the
            archive.

        Returns
        -------
        A Fiona collection object

        z/vsizip{0}/{1}r+   z
/vsizip{0}r   r*   r   )r   r   r   r    r!   )r,   r&   r-   r#   r$   r   )	r   r/   r   r   r   r    r!   r(   r0   r   r   r   r)      s(    zZipMemoryFile.open)NNr>   )NNNNNF)r8   r9   r:   r;   r   r)   r<   r   r   r   r   r=      s         r=   )r;   loggingZfiona.ogrextr   r   r   Zfiona.collectionr   Z
fiona.metar   Zfiona.errorsr   	getLoggerr8   logr   r=   r   r   r   r   <module>   s   
 