o
    w¨ÊhÇ  ã                   @   sp   d Z ddlmZ ddlmZmZmZ ddlmZm	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-2017 by Onni Software Ltd.
    :license: New BSD License, see LICENSE for more details
é    N)ÚOrderedDictÚisstreamÚPY2é   )ÚMESSAGE_ERROR_03ÚMESSAGE_WRONG_IO_INSTANCEc                   @   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
   d | _ d S ©N©Ú
_file_type©Úself© r   úA/var/www/html/env/lib/python3.10/site-packages/pyexcel_io/book.pyÚ__init__   s   
zRWInterface.__init__c                 K   ó   t dƒ‚)zopen a file for read or writeúPlease implement this method©ÚNotImplementedError©r   Ú	file_nameÚkeywordsr   r   r   Úopen   ó   zRWInterface.openc                 K   r   )z$open a file stream for read or writer   r   ©r   Úfile_streamr   r   r   r   Úopen_stream   r   zRWInterface.open_streamc                 K   r   )ú%open a file content for read or writer   r   r   r   r   r   Úopen_content!   r   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 typeNr
   )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   Úclose-   s   zRWInterface.closec                 C   s   | S r	   r   r   r   r   r   Ú	__enter__5   s   zRWInterface.__enter__c                 C   s   |   ¡  d S r	   )r!   )r   Úa_typeÚvalueÚ	tracebackr   r   r   Ú__exit__8   s   zRWInterface.__exit__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú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   A   s
   
zBookReader.__init__c                 K   ó   || _ || _dS )zk
        open a file with unlimited keywords

        keywords are passed on to individual readers
        N)r.   r0   r   r   r   r   r   H   ó   
zBookReader.openc                 K   sŠ   t |ƒrAtrt|dƒr| d¡ n(t| ¡ | jƒ}nddlm} z| d¡ W n |y8   t| ¡ | jƒ}Y nw || _	|| _
dS ttƒ‚)zw
        open a file with unlimited keywords for reading

        keywords are passed on to individual readers
        Úseekr   )ÚUnsupportedOperationN)r   r   Úhasattrr6   Ú_convert_content_to_streamÚreadr   Úior7   r/   r0   ÚIOErrorr   )r   r   r   r7   r   r   r   r   Q   s$   

ÿ
ÿþ
zBookReader.open_streamc                 K   s"   t || jƒ}| j|fi |¤Ž 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)r9   r   r   )r   Úfile_contentr   r   r   r   r   r   r   s   zBookReader.open_contentc                    sD   ‡ fdd„| j D ƒ}t|ƒdkr|d j|  |d ¡iS tdˆ  ƒ‚)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 %s)r1   Úlenr>   Ú
read_sheetÚ
ValueError)r   rB   Únamed_contentsr   rA   r   Úread_sheet_by_name|   s   
þzBookReader.read_sheet_by_namec                 C   s6   z| j | }|j|  |¡iW S  ty   |  ¡  ‚ w )z>
        read an indexed sheet from a excel data book
        )r1   r>   rE   Ú
IndexErrorr!   )r   Ú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   r1   rE   r>   )r   ÚresultrK   r   r   r   Úread_all—   s   
zBookReader.read_allc                 C   s@   t ƒ }|D ]}t|tƒr| |  |¡¡ q| |  |¡¡ q|S rM   )r   Ú
isinstanceÚintÚupdaterL   rH   )r   ÚsheetsrN   rK   r   r   r   Ú	read_many    s   
zBookReader.read_manyc                 C   r   )zE
        Return a context specific sheet from a native sheet
        r   r   )r   Únative_sheetr   r   r   rE   ¬   ó   zBookReader.read_sheet)r'   r(   r)   r*   r   r   r   r   rH   rL   rO   rT   rE   Ú__classcell__r   r   r2   r   r,   <   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-   rX   r   Ú_file_alike_objectr0   r   r2   r   r   r   ¸   s   
zBookWriter.__init__c                 K   r4   )zw
        open a file with unlimited keywords for writing

        keywords are passed on to individual writers
        N)rY   r0   r   r   r   r   r   ½   r5   zBookWriter.openc                 K   s&   t |ƒsttƒ‚| j|fi |¤Ž dS )z~
        open a file stream with unlimited keywords for writing

        keywords are passed on to individual writers
        N)r   r<   r   r   r   r   r   r   r   Æ   s   zBookWriter.open_streamc                 K   r   )r   z+Normal writer would not need this interface)Ú	Exceptionr   r   r   r   r   Ñ   r   zBookWriter.open_contentc                 C   s:   |D ]}|   |¡}|r| || ¡ | ¡  qtdƒ‚dS )z7
        write a dictionary into an excel file
        zCannot create a sheet writer!N)Úcreate_sheetÚwrite_arrayr!   rZ   )r   Úincoming_dictrB   Úsheet_writerr   r   r   ÚwriteÕ   s   

úzBookWriter.writec                 C   r   )z:
        implement this method for easy extension
        zPlease implement create_sheet()r   )r   rB   r   r   r   r[   á   rV   zBookWriter.create_sheet)r'   r(   r)   r*   r   r   r   r   r_   r[   rW   r   r   r2   r   rX   ³   s    	rX   c                 C   s"   t  |¡}| | ¡ | d¡ |S )Nr   )ÚmanagerÚget_ior_   r6   )r=   r   Ústreamr   r   r   r9   è   s   


r9   )r*   Úpyexcel_io.managerr`   Úpyexcel_io._compactr   r   r   Ú	constantsr   r   Úobjectr   r,   rX   r9   r   r   r   r   Ú<module>   s    	-w5