o
    ‹¨Êh«  ã                   @   s\   d Z ddlmZ ddlmZ ddlmZ ddlm	Z	 da
G dd„ deƒZdd	„ Zd
d„ ZdS )z«
    pyexcel.book
    ~~~~~~~~~~~~~~~~~~~

    Excel book

    :copyright: (c) 2014-2017 by Onni Software Ltd.
    :license: New BSD License, see LICENSE for more details
é    N)ÚSheet)ÚBookMeta©ÚSheetIteratorc                   @   s–   e Zd ZdZd%dd„Zd%dd„Zdd	„ Zd
d„ Zdd„ Zd&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d!d"„ Zd#d$„ ZdS )'ÚBookzh
    Read an excel book that has one or more sheets

    For csv file, there will be just one sheet
    NÚmemoryc                 C   s0   d| _ g | _d| _t ¡ | _| j|||d� dS )a4  
        Book constructor

        Selecting a specific book according to filename extension

        :param sheets: a dictionary of data
        :param filename: the physical file
        :param path: the relative path or absolute path
        :param keywords: additional parameters to be passed on
        N)ÚsheetsÚfilenameÚpath)Ú_Book__pathÚ_Book__name_arrayr	   ÚcompactÚOrderedDictÚ_Book__sheetsÚinit©Úselfr   r	   r
   © r   ú>/var/www/html/env/lib/python3.10/site-packages/pyexcel/book.pyÚ__init__   s
   
zBook.__init__c                 C   s   || _ || _|  |¡ dS )zFindpendent function so that it could be called multiple times
        N)r   r	   Úload_from_sheetsr   r   r   r   r   *   s   z	Book.initc                 C   s�   |du rdS |  ¡ }t|tjƒst|ƒ}|D ]'}|| }t|tƒr'|}||_nt||ƒ}| j ||i¡ || j	| 
dd¡< qt| j  ¡ ƒ| _dS )z¢
        Load content from existing sheets

        :param dict sheets: a dictionary of sheets. Each sheet is
                            a list of lists
        Nú Ú_)ÚkeysÚ
isinstancer   r   Úsortedr   Únamer   ÚupdateÚ__dict__ÚreplaceÚlistr   )r   r   r   r   ÚvalueÚsheetr   r   r   r   1   s   

zBook.load_from_sheetsc                 C   s   t | ƒS ©Nr   ©r   r   r   r   Ú__iter__M   s   zBook.__iter__c                 C   ó
   t | jƒS r#   ©Úlenr   r$   r   r   r   Ú__len__P   s   
zBook.__len__Fc                 C   s   t | j||d�| _d S )N)ÚkeyÚreverse)r   r   )r   r*   r+   r   r   r   Úsort_sheetsS   s   zBook.sort_sheetsc                 C   r&   )z-
        Return the number of sheets
        r'   r$   r   r   r   Únumber_of_sheetsV   ó   
zBook.number_of_sheetsc                 C   s   | j S )z(
        Return all sheet names
        )r   r$   r   r   r   Úsheet_names\   s   zBook.sheet_namesc                 C   s
   | j | S )z7
        Get the sheet with the specified name
        )r   )r   r   r   r   r   Úsheet_by_nameb   r.   zBook.sheet_by_namec                 C   s&   |t | jƒk r| j| }|  |¡S dS )z8
        Get the sheet with the specified index
        N)r(   r   r0   )r   ÚindexÚ
sheet_namer   r   r   Úsheet_by_indexh   s   

þzBook.sheet_by_indexc                 C   sz   t |tƒr!|t| jƒk r| j| }| j|= t| j ¡ ƒ| _dS t‚t |tƒr;|| jv r9| j|= t| j ¡ ƒ| _dS t	‚t
‚)z 
        Remove a sheet
        N)r   Úintr(   r   r   r    r   Ú
IndexErrorÚstrÚKeyErrorÚ	TypeError)r   r"   r2   r   r   r   Úremove_sheetp   s   



zBook.remove_sheetc                 C   s   t |tƒr
|  |¡S |  |¡S )zOverride operator[])r   r4   r3   r0   )r   r*   r   r   r   Ú__getitem__„   s   


zBook.__getitem__c                 C   s   |   |¡ | S )z*
        Override del book[index]
        )r9   )r   Úotherr   r   r   Ú__delitem__‹   s   
zBook.__delitem__c           
      C   sú   i }|   ¡ }| ¡ D ]}|}t| ¡ ƒdkrd| j|f }|| ||< q
t|tƒrU|  ¡ }| ¡ D ]"}|}t| ¡ ƒdkr@|j}||v rMtƒ }d||f }|| ||< q1nt|tƒrq|j}||v rktƒ }d|j|f }|j	||< nt
‚tƒ }	|	 |¡ |	S )z‰
        Override operator +

        example::

            book3 = book1 + book2
            book3 = book1 + book2["Sheet 1"]

        é   ú%s_%s)Úto_dictr   r(   r	   r   r   Ú
local_uuidr   r   Úarrayr8   r   )
r   r;   ÚcontentÚcurrent_dictÚkÚnew_keyÚ
other_dictr*   ÚuidÚoutputr   r   r   Ú__add__’   s8   

ù

zBook.__add__c                 C   sÀ   t |tƒr3| ¡ }|D ]&}|}t|ƒdkr|j}|| jv r&tƒ }d||f }t|| j|ƒ| j	|< qn#t |tƒrT|j
}|| jv rJtƒ }d|j
|f }t|j|ƒ| j	|< nt‚t| j	 ¡ ƒ| _| S )z|
        Operator overloading +=

        example::

            book += book2
            book += book2["Sheet1"]

        r=   r>   )r   r   r/   r(   r	   r   r@   r   rA   r   r   r8   r    r   )r   r;   Únamesr   rE   rG   r   r   r   Ú__iadd__¹   s(   


ù

zBook.__iadd__c                 C   s(   t  ¡ }| D ]}| |j|ji¡ q|S )z Convert the book to a dictionary)r   r   r   r   rA   )r   Úthe_dictr"   r   r   r   r?   Ø   s   zBook.to_dict)Nr   N)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r%   r)   r,   r-   r/   r0   r3   r9   r:   r<   rI   rK   r?   r   r   r   r   r      s$    


'r   c                 C   s$   t | tƒr| S t|  ¡ | j| jd�S )z!Convert a bookstream to Book
    )r	   r
   )r   r   r?   r	   r
   )Ú
bookstreamr   r   r   Úto_bookà   s   
ýrR   c                   C   s   t d a t S )zcreate home made uuidr=   )Ú
LOCAL_UUIDr   r   r   r   r@   í   s   r@   )rP   Úpyexcel._compactÚ_compactr   Úpyexcel.sheetr   Úpyexcel.internal.metar   Úpyexcel.internal.commonr   rS   r   rR   r@   r   r   r   r   Ú<module>   s    	 O