
    Efq                         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e          Z G d	 d
e          Z G d de          Z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                   F    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZdS )RWInterfacez7
    The common methods for book reader and writer
    Nc                 F    t          j        t                     d | _        d S N)warningswarnDEPRECATED_SINCE_0_6_0
_file_typeselfs    Q/var/www/ffwdstore.be/apps/py-env/lib/python3.11/site-packages/pyexcel_io/book.py__init__zRWInterface.__init__   s    ,---    c                      t          d          )zopen a file for read or writePlease implement this methodNotImplementedErrorr   	file_namekeywordss      r   openzRWInterface.open#       !"@AAAr   c                      t          d          )z$open a file stream for read or writer   r   r   file_streamr   s      r   open_streamzRWInterface.open_stream'   r   r   c                      t          d          )%open a file content for read or writer   r   r   s      r   open_contentzRWInterface.open_content+   r   r   c                     || _         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_types     r   set_typezRWInterface.set_type/   s     $r   c                     dS )z4
        close the file handle if necessary
        N r   s    r   closezRWInterface.close7   s	     	r   c                     | S r   r)   r   s    r   	__enter__zRWInterface.__enter__?   s    r   c                 .    |                                   d S r   )r*   )r   a_typevalue	tracebacks       r   __exit__zRWInterface.__exit__B   s    

r   )__name__
__module____qualname____doc__stream_typer   r   r!   r$   r'   r*   r,   r1   r)   r   r   r	   r	      s          K  B B BB B BB B B$ $ $        r   r	   c                   R     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Z xZS )
BookReaderz
    Standard book reader
    c                     t          t          |                                            d | _        d | _        d | _        d | _        d S r   )superr8   r   
_file_name_file_stream	_keywords_native_bookr   	__class__s    r   r   zBookReader.__init__K   sC    j$((***  r   c                 "    || _         || _        dS )zk
        open a file with unlimited keywords

        keywords are passed on to individual readers
        N)r;   r=   r   s      r   r   zBookReader.openR   s     $!r   c                    t          |          r_ddlm} 	 |                    d           n2# |$ r* t	          |                                | j                  }Y nw xY w|| _        || _        dS t          t                    )zw
        open a file with unlimited keywords for reading

        keywords are passed on to individual readers
        r   )UnsupportedOperationN)r   iorC   seek_convert_content_to_streamreadr   r<   r=   IOErrorr   )r   r    r   rC   s       r   r!   zBookReader.open_stream[   s     K   	5//////  ####'   8$$&&  !,D%DNNN3444s   - ,AAc                 L    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)rF   r   r!   )r   file_contentr   r    s       r   r$   zBookReader.open_contentq   s6     1tOO1111111r   c                     fd| j         D             }t          |          dk    r(|d         j        |                     |d                   iS t	          dz            )z;
        read a named sheet from a excel data book
        c                 *    g | ]}|j         k    |S r)   )name).0content
sheet_names     r   
<listcomp>z1BookReader.read_sheet_by_name.<locals>.<listcomp>   s0     
 
 
|z)) )))r   r   r   zCannot find sheet %s)r>   lenrM   
read_sheet
ValueError)r   rP   named_contentss    ` r   read_sheet_by_namezBookReader.read_sheet_by_name{   s}    
 
 
 
,
 
 

 ~!##"1%*DOON1<M,N,NOO 3j@AAAr   c                     	 | j         |         }|j        |                     |          iS # t          $ r |                                   w xY w)z>
        read an indexed sheet from a excel data book
        )r>   rM   rS   
IndexErrorr*   )r   sheet_indexsheets      r   read_sheet_by_indexzBookReader.read_sheet_by_index   sX    	%k2EJ 6 677 	 	 	JJLLL	s	   (+  Ac                 p    t                      }| j        D ]}|                     |          ||j        <    |S z8
        read everything from a excel data book
        )r   r>   rS   rM   )r   resultrZ   s      r   read_allzBookReader.read_all   s?     & 	8 	8E!%!7!7F5:r   c                     t                      }|D ]h}t          |t                    r)|                    |                     |                     @|                    |                     |                     i|S r]   )r   
isinstanceintupdater[   rV   )r   sheetsr^   rZ   s       r   	read_manyzBookReader.read_many   sy      	> 	>E%%% >d66u==>>>>d55e<<====r   c                      t          d          )zE
        Return a context specific sheet from a native sheet
        r   r   )r   native_sheets     r   rS   zBookReader.read_sheet   s     ""@AAAr   )r2   r3   r4   r5   r   r   r!   r$   rV   r[   r_   re   rS   __classcell__r@   s   @r   r8   r8   F   s         ! ! ! ! !" " "5 5 5,2 2 2B B B
 
 
  
 
 
B B B B B B Br   r8   c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )
BookWriterz
    Standard book writer
    c                 r    t          t          |                                            d | _        d | _        d S r   )r:   rk   r   _file_alike_objectr=   r?   s    r   r   zBookWriter.__init__   s2    j$((***"&r   c                 "    || _         || _        dS )zw
        open a file with unlimited keywords for writing

        keywords are passed on to individual writers
        N)rm   r=   r   s      r   r   zBookWriter.open   s     #,!r   c                 h    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   rH   r   r   r   s      r   r!   zBookWriter.open_stream   sC     $$ 	,*+++	+*******r   c                      t          d          )r#   z+Normal writer would not need this interface)	Exceptionr   s      r   r$   zBookWriter.open_content   s    EFFFr   c                     |D ]W}|                      |          }|r0|                    ||                    |                                 It          d          dS )z7
        write a dictionary into an excel file
        zCannot create a sheet writer!N)create_sheetwrite_arrayr*   rq   )r   incoming_dictrP   sheet_writers       r   writezBookWriter.write   sz     ( 	A 	AJ,,Z88L A((z)BCCC""$$$$ ?@@@	A 	Ar   c                      t          d          )z:
        implement this method for easy extension
        zPlease implement create_sheet()r   )r   rP   s     r   rs   zBookWriter.create_sheet   s     ""CDDDr   )r2   r3   r4   r5   r   r   r!   r$   rw   rs   rh   ri   s   @r   rk   rk      s             
" " "	+ 	+ 	+G G G
A 
A 
AE E E E E E Er   rk   c                 v   t          j        |          }t          j        |          }|dk    ot          | t                     }|dk    ot          | t                    }|r|                     d          } n|r|                     d          } |                    |            |                    d           |S )Nbytesstringzutf-8r   )	managerget_ioget_io_typera   rz   encodedecoderw   rE   )rJ   r&   streamtarget_content_typeneeds_encodeneeds_decodes         r   rF   rF      s    ^I&&F!-i88&'1 *e; ; 7L '(2 ze8 8L  4#**733	 4#**733
LL
KKNNNMr   )r5   r   pyexcel_io.managerr|   pyexcel_io._compactr   r   	constantsr   r   r   objectr	   r8   rk   rF   r)   r   r   <module>r      s     $ $ $ $ $ $ 5 5 5 5 5 5 5 5 B B B B B B B B: + + + + +& + + +\iB iB iB iB iB iB iB iBX2E 2E 2E 2E 2E 2E 2E 2Ej    r   