U
    [eq                     @   sx   d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	 dZ
G dd deZG d	d
 d
eZG dd deZdd ZdS )z
    pyexcel_io.book
    ~~~~~~~~~~~~~~~~~~~

    The io interface to file extensions

    :copyright: (c) 2014-2022 by Onni Software Ltd.
    :license: New BSD License, see LICENSE for more details
    N)OrderedDictisstream   )MESSAGE_ERROR_03MESSAGE_WRONG_IO_INSTANCEzxDeprecated since v0.6.0! Although backward compatibility is preserved, it is recommended to upgrade to get new features.c                   @   sT   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd ZdS )RWInterfacez7
    The common methods for book reader and writer
    Nc                 C   s   t t d | _d S N)warningswarnDEPRECATED_SINCE_0_6_0
_file_typeself r   Q/var/www/html/services/stratfitenv/lib/python3.8/site-packages/pyexcel_io/book.py__init__   s    
zRWInterface.__init__c                 K   s   t ddS )zopen a file for read or writePlease implement this methodNNotImplementedErrorr   	file_namekeywordsr   r   r   open#   s    zRWInterface.openc                 K   s   t ddS )z$open a file stream for read or writer   Nr   r   file_streamr   r   r   r   open_stream'   s    zRWInterface.open_streamc                 K   s   t ddS )%open a file content for read or writer   Nr   r   r   r   r   open_content+   s    zRWInterface.open_contentc                 C   s
   || _ dS )z
        set the file type for the instance

        file type is needed when a third party library could
        handle more than one file typeN)r   )r   	file_typer   r   r   set_type/   s    zRWInterface.set_typec                 C   s   dS )z4
        close the file handle if necessary
        Nr   r   r   r   r   close7   s    zRWInterface.closec                 C   s   | S r   r   r   r   r   r   	__enter__?   s    zRWInterface.__enter__c                 C   s   |    d S r   )r    )r   a_typevalue	tracebackr   r   r   __exit__B   s    zRWInterface.__exit__)__name__
__module____qualname____doc__Zstream_typer   r   r   r   r   r    r!   r%   r   r   r   r   r      s   r   c                       s`   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Z  ZS )
BookReaderz
    Standard book reader
    c                    s*   t t|   d | _d | _d | _d | _d S r   )superr*   r   
_file_name_file_stream	_keywords_native_bookr   	__class__r   r   r   K   s
    zBookReader.__init__c                 K   s   || _ || _dS )zk
        open a file with unlimited keywords

        keywords are passed on to individual readers
        N)r,   r.   r   r   r   r   r   R   s    zBookReader.openc                 K   sb   t |rVddlm} z|d W n$ |k
rF   t| | j}Y nX || _|| _nt	t
dS )zw
        open a file with unlimited keywords for reading

        keywords are passed on to individual readers
        r   )UnsupportedOperationN)r   ior2   seek_convert_content_to_streamreadr   r-   r.   IOErrorr   )r   r   r   r2   r   r   r   r   [   s     
zBookReader.open_streamc                 K   s   t || j}| j|f| dS )z
        read file content as if it is a file stream with
        unlimited keywords for reading

        keywords are passed on to individual readers
        N)r5   r   r   )r   file_contentr   r   r   r   r   r   q   s    zBookReader.open_contentc                    sH    fdd| j D }t|dkr8|d j| |d iS td  dS )z;
        read a named sheet from a excel data book
        c                    s   g | ]}|j  kr|qS r   )name).0content
sheet_namer   r   
<listcomp>   s   
z1BookReader.read_sheet_by_name.<locals>.<listcomp>r   r   zCannot find sheet %sN)r/   lenr9   
read_sheet
ValueError)r   r=   Znamed_contentsr   r<   r   read_sheet_by_name{   s    
zBookReader.read_sheet_by_namec                 C   s@   z| j | }|j| |iW S  tk
r:   |    Y nX dS )z>
        read an indexed sheet from a excel data book
        N)r/   r9   r@   
IndexErrorr    )r   Zsheet_indexsheetr   r   r   read_sheet_by_index   s    
zBookReader.read_sheet_by_indexc                 C   s&   t  }| jD ]}| |||j< q|S z8
        read everything from a excel data book
        )r   r/   r@   r9   )r   resultrD   r   r   r   read_all   s    
zBookReader.read_allc                 C   s@   t  }|D ]0}t|tr*|| | q
|| | q
|S rF   )r   
isinstanceintupdaterE   rB   )r   ZsheetsrG   rD   r   r   r   	read_many   s    
zBookReader.read_manyc                 C   s   t ddS )zE
        Return a context specific sheet from a native sheet
        r   Nr   )r   Znative_sheetr   r   r   r@      s    zBookReader.read_sheet)r&   r'   r(   r)   r   r   r   r   rB   rE   rH   rL   r@   __classcell__r   r   r0   r   r*   F   s   	
	r*   c                       sH   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	  Z
S )
BookWriterz
    Standard book writer
    c                    s   t t|   d | _d | _d S r   )r+   rN   r   _file_alike_objectr.   r   r0   r   r   r      s    zBookWriter.__init__c                 K   s   || _ || _dS )zw
        open a file with unlimited keywords for writing

        keywords are passed on to individual writers
        N)rO   r.   r   r   r   r   r      s    zBookWriter.openc                 K   s"   t |stt| j|f| dS )z~
        open a file stream with unlimited keywords for writing

        keywords are passed on to individual writers
        N)r   r7   r   r   r   r   r   r   r      s    zBookWriter.open_streamc                 K   s   t ddS )r   z+Normal writer would not need this interfaceN)	Exceptionr   r   r   r   r      s    zBookWriter.open_contentc                 C   s<   |D ]2}|  |}|r.|||  |  qtdqdS )z7
        write a dictionary into an excel file
        zCannot create a sheet writer!N)create_sheetwrite_arrayr    rP   )r   Zincoming_dictr=   Zsheet_writerr   r   r   write   s    

zBookWriter.writec                 C   s   t ddS )z:
        implement this method for easy extension
        zPlease implement create_sheet()Nr   )r   r=   r   r   r   rQ      s    zBookWriter.create_sheet)r&   r'   r(   r)   r   r   r   r   rS   rQ   rM   r   r   r0   r   rN      s   	rN   c                 C   sp   t |}t |}|dko&t| t }|dko8t| t}|rJ| d} n|rX| d} ||  |d |S )Nbytesstringzutf-8r   )	managerZget_ioZget_io_typerI   rT   encodedecoderS   r4   )r8   r   streamZtarget_content_typeZneeds_encodeZneeds_decoder   r   r   r5      s"    


 
 


r5   )r)   r	   Zpyexcel_io.managerrV   Zpyexcel_io._compactr   r   	constantsr   r   r   objectr   r*   rN   r5   r   r   r   r   <module>   s   	.l5