U
    [e3                     @   sd  d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZmZ dd	lmZ d(d
dZd)ddZd*ddZd+ddZd,ddZdejfddZeeeejdZeeedejdZeeeejdZeedeejdZeeeeejdZ eeeedejdZ!G dd de"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.internal.meta
    ~~~~~~~~~~~~~~~~~~~~~~

    Annotate sheet and book class' attributes

    :copyright: (c) 2015-2022 by Onni Software Ltd.
    :license: New BSD License
    N)partial)	constants)
docstrings)PY2
append_doc)SOURCE)	save_book
save_sheetget_sheet_stream)make_a_propertyc                    s    fdd}d  |_ |S )z5make a custom presentation method for each file typesc                    sX   t  } ||< f |}||  z| }| }W n tk
rR   d}Y nX |S )/docstring is assigned a few lines down the lineN)r   get_keyword_for_parameter
write_dataget_contentgetvalueAttributeError)selfkeywordskeywordZmemory_sourceZcontent_streamcontent	attributesource_getter W/var/www/html/services/stratfitenv/lib/python3.8/site-packages/pyexcel/internal/meta.pycustom_presenter   s    



z(make_presenter.<locals>.custom_presenterzGet data in %s format__doc__)r   r   r   r   r   r   make_presenter   s    
r   c                 C   s   t j}t|| S )z+make a custom presentation method for sheet)r   Zget_writable_sourcer   r   r   r   r   sheet_presenter+   s    r   c                 C   s   t j}t|| S )z*make a custom presentation method for book)r   Zget_writable_book_sourcer   r   r   r   r   book_presenter1   s    r    c                    s    fdd}d  |_ |S )z$make a custom input method for sheetc                    st   i }t jD ]}||kr
||||< q
t }|dkrJ ||< ||d< n|||< tf |}| j|j|jf| dS )r   	file_typefile_contentN)	r   ZVALID_SHEET_PARAMETERSpopr   r   r
   initpayloadname)r   r   r   Zsheet_paramsfieldr   Znamed_contentr   r   r   custom_importer1:   s    



z"importer.<locals>.custom_importer1Set data in %s formatr   )r   r)   r   r(   r   importer7   s    
r+   c                    s    fdd}d  |_ |S )z#make a custom input method for bookc                    sP   t  }|dkr$ ||< ||d< n|||< tf |\}}}| j|||d dS )r   r!   r"   )sheetsfilenamepathN)r   r   	_get_bookr$   )r   r   r   r   r,   r-   r.   r(   r   r   custom_book_importerP   s    

z+book_importer.<locals>.custom_book_importerr*   r   )r   r0   r   r(   r   book_importerM   s    
r1   ZSheetc                 K   s    | ||}t| ||f| dS )z1
    create custom attributes for each class
    N)formatr   )clsr!   instance_namedescriptionr   Z
doc_stringr   r   r   r   _   s    
r   )getter_funcr5   ZBook)r6   r4   r5   )setter_funcr5   )r4   r7   r5   )r6   r7   r5   )r6   r7   r4   r5   c                   @   s    e Zd ZdZdd Zdd ZdS )StreamAttributez&Provide access to get_*_stream methodsc                 C   s
   || _ d S N)r3   )r   r3   r   r   r   __init__   s    zStreamAttribute.__init__c                 C   s   t | jd}||dS )Nsave_to_memory)r!   )getattrr3   )r   r&   getterr   r   r   __getattr__   s    zStreamAttribute.__getattr__N)__name__
__module____qualname__r   r:   r>   r   r   r   r   r8      s   r8   c                   @   sF   e Zd ZdZedd Zdd ZdddZd	d
 Zdd Z	dd Z
dS )PyexcelObjectz/parent class for pyexcel.Sheet and pyexcel.Bookc                 C   s   t | S )a  Return a stream in which the content is properly encoded

        Example::

            >>> import pyexcel as p
            >>> b = p.get_book(bookdict={"A": [[1]]})
            >>> csv_stream = b.stream.texttable
            >>> print(csv_stream.getvalue())
            A:
            +---+
            | 1 |
            +---+

        Where b.stream.xls.getvalue() is equivalent to b.xls. In some situation
        b.stream.xls is prefered than b.xls.

        Sheet examples::

            >>> import pyexcel as p
            >>> s = p.Sheet([[1]], 'A')
            >>> csv_stream = s.stream.texttable
            >>> print(csv_stream.getvalue())
            A:
            +---+
            | 1 |
            +---+

        Where s.stream.xls.getvalue() is equivalent to s.xls. In some situation
        s.stream.xls is prefered than s.xls.

        It is similar to :meth:`~pyexcel.Book.save_to_memory`.
        )r8   r   r   r   r   stream   s    "zPyexcelObject.streamc                 K   s   t ddS )a  Save the content to memory

        :param file_type: any value of 'csv', 'tsv', 'csvz',
                          'tsvz', 'xls', 'xlsm', 'xlsm', 'ods'
        :param stream: the memory stream to be written to. Note in
                       Python 3, for csv  and tsv format, please
                       pass an instance of StringIO. For xls, xlsx,
                       and ods, an instance of BytesIO.
        z!save to memory is not implementedN)NotImplementedError)r   r!   r   r   r   r   r;      s    
zPyexcelObject.save_to_memorysvgc                 K   s8   | j |f|}|dkr4dd }t|d| t|| |S )a  
        Visualize the data

        Parameters:
        -----------------

        file_type:string
           'svg' by default. 'png', 'jpeg' possible depending on plugins

        chart_type:string
           'bar' by default. other chart types are subjected to plugins.
        )ZpngrF   Zjpegc                 S   s   |   dS )Nutf-8)r   decoderC   r   r   r   r      s    z'PyexcelObject.plot.<locals>.get_contentz	_repr_%s_)r;   setattrr   )r   r!   r   Zmemory_contentr   r   r   r   plot   s    zPyexcelObject.plotc                 C   s   | j S r9   )htmlrC   r   r   r   _repr_html_   s    zPyexcelObject._repr_html_c                 C   s*   t r$t }|dkr$| j}|dS | jS )NasciirG   )r   sysgetdefaultencodingZ	texttableencode)r   Zdefault_encodingresultr   r   r   __repr__   s    
zPyexcelObject.__repr__c                 C   s   |   S r9   )rR   rC   r   r   r   __str__   s    zPyexcelObject.__str__N)rF   )r?   r@   rA   r   propertyrD   r;   rJ   rL   rR   rS   r   r   r   r   rB      s   
#
	rB   c                   @   sX   e Zd ZdZeeZeeZee	Z
eejdd ZdddZdddZdd
dZdS )	SheetMetazAnnotate sheet attributesc                 K   s   t | fd|i|S )z Save the content to a named file	file_namer	   r   r-   r   r   r   r   save_as  s    zSheetMeta.save_asNc                 K   s   t | f||d|}|S )Nr!   Zfile_streamrW   r   r!   rD   r   r   r   r   r;     s     zSheetMeta.save_to_memoryc                 C   s   t | ||||d dS )aY  Save to database table through django model

        :param model: a database model
        :param initializer: a initialization functions for your model
        :param mapdict: custom map dictionary for your data columns
        :param batch_size: a parameter to Django concerning the size
                           for bulk insertion
        )modelinitializermapdict
batch_sizeNrW   )r   r\   r]   r^   r_   r   r   r   save_to_django_model  s    zSheetMeta.save_to_django_modelTc                 C   s   t | |||||d dS )aI  Save data in sheet to database table

        :param session: database session
        :param table: a database table
        :param initializer: a initialization functions for your table
        :param mapdict: custom map dictionary for your data columns
        :param auto_commit: by default, data is auto committed.

        )sessiontabler]   r^   auto_commitNrW   )r   ra   rb   r]   r^   rc   r   r   r   save_to_database  s    zSheetMeta.save_to_database)N)NNN)NNT)r?   r@   rA   r   classmethodREGISTER_IOregister_ioREGISTER_PRESENTATIONregister_presentationREGISTER_INPUTregister_inputr   docsSAVE_AS_OPTIONSrY   r;   r`   rd   r   r   r   r   rU      s   

     
     rU   c                   @   sX   e Zd ZdZeeZeeZee	Z
eejdd ZdddZdddZdd
dZdS )BookMetazAnnotate book attributesc                 K   s   t | fd|i|S )z0
        Save the content to a new file
        rV   r   rX   r   r   r   rY   <  s    zBookMeta.save_asNc                 K   s   t | f||d|}|S )aC  
        Save the content to a memory stream

        :param file_type: what format the stream is in
        :param stream: a memory stream.  Note in Python 3, for csv and tsv
                       format, please pass an instance of StringIO. For xls,
                       xlsx, and ods, an instance of BytesIO.
        rZ   ro   r[   r   r   r   r;   C  s    	 zBookMeta.save_to_memoryc                 K   s   t | f|||d| dS )a  
        Save to database table through django model

        :param models: a list of database models, that is accepted by
                       :meth:`Sheet.save_to_django_model`. The sequence
                       of tables matters when there is dependencies in
                       between the tables. For example, **Car** is made
                       by **Car Maker**. **Car Maker** table should be
                       specified before **Car** table.
        :param initializers: a list of intialization functions for your
                             tables and the sequence should match tables,
        :param mapdicts: custom map dictionary for your data columns
                         and the sequence should match tables

        optional parameters:
        :param batch_size: django bulk_create batch size
        :param bulk_save: whether to use bulk_create or to use single save
                          per record
        )modelsinitializersmapdictsNro   )r   rp   rq   rr   r   r   r   r   save_to_django_modelsQ  s    zBookMeta.save_to_django_modelsTc                 C   s   t | |||||d dS )aJ  
        Save data in sheets to database tables

        :param session: database session
        :param tables: a list of database tables, that is accepted by
                       :meth:`Sheet.save_to_database`. The sequence of tables
                       matters when there is dependencies in between the
                       tables. For example, **Car** is made by **Car Maker**.
                       **Car Maker** table should
                       be specified before **Car** table.
        :param initializers: a list of intialization functions for your
                             tables and the sequence should match tables,
        :param mapdicts: custom map dictionary for your data columns
                         and the sequence should match tables
        :param auto_commit: by default, data is committed.

        )ra   tablesrq   rr   rc   Nro   )r   ra   rt   rq   rr   rc   r   r   r   rd   o  s    zBookMeta.save_to_database)N)NN)NNT)r?   r@   rA   r   re   REGISTER_BOOK_IOrg   REGISTER_BOOK_PRESENTATIONri   REGISTER_BOOK_INPUTrk   r   rl   rm   rY   r;   rs   rd   r   r   r   r   rn   5  s   

   
"   rn   c                  K   s*   t jf | }| }| \}}|||fS )zGet an instance of :class:`Book` from an excel source

    Where the dictionary should have text as keys and two dimensional
    array as values.
    )r   Zget_book_sourceget_dataZget_source_info)r   Za_sourcer,   r-   r.   r   r   r   r/     s    r/   )N)N)N)N)N)(r   rN   	functoolsr   Zpyexcelr   r   rl   Zpyexcel._compactr   r   Zpyexcel.internalr   Zpyexcel.internal.corer   r	   r
   Zpyexcel.internal.utilsr   r   r   r    r+   r1   ZOUT_FILE_TYPE_DOC_STRINGr   rh   rv   ZIN_FILE_TYPE_DOC_STRINGrj   rw   ZIO_FILE_TYPE_DOC_STRINGrf   ru   objectr8   rB   rU   rn   r/   r   r   r   r   <module>   sp   	





	];]