U
    Y»{jÉF  ã                   @   s¾   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ G dd„ deƒZeƒ Zd	d
„ Zddd„Zddd„Zddd„Zddd„Zd dd„Zd!dd„Zdd„ ZdS )"é    )Úabsolute_importN)ÚPath)Úvalidate_coerce_fig_to_dictÚvalidate_coerce_output_type)Ú
get_module)ÚImageUriValidatorc                   @   s>   e Zd ZdZdd„ Zedd„ ƒZejdd„ ƒZedd„ ƒZ	d	S )
Ú
JsonConfig)ÚjsonÚorjsonÚautoc                 C   s
   d| _ d S )Nr   ©Ú_default_engine©Úself© r   úN/var/www/html/bwave/python/venv/lib/python3.8/site-packages/plotly/io/_json.pyÚ__init__   s    zJsonConfig.__init__c                 C   s   | j S )Nr   r   r   r   r   Údefault_engine   s    zJsonConfig.default_enginec                 C   s8   |t jkrtdjt j|d�ƒ‚|dkr.|  ¡  || _d S )Nz9Supported JSON engines include {valid}
    Received {val})ZvalidÚvalr
   )r   Ú_valid_enginesÚ
ValueErrorÚformatÚvalidate_orjsonr   )r   r   r   r   r   r      s    
 ÿÿc                 C   s   t dƒ}|d krtdƒ‚d S )Nr
   z-The orjson engine requires the orjson package)r   r   )Úclsr
   r   r   r   r   &   s    zJsonConfig.validate_orjsonN)
Ú__name__Ú
__module__Ú__qualname__r   r   Úpropertyr   ÚsetterÚclassmethodr   r   r   r   r   r      s   

r   c                 C   s   | dkrdS | S dS )zM
    This is used to ultimately *encode* into strict JSON, see `encode`

    )ÚInfinityz	-InfinityÚNaNNr   )Úconstr   r   r   Úcoerce_to_strict0   s    r#   Fc                 C   sb  t ddd�}|dkrtj}|dkr6|dk	r0d}qJd}n|dkrJtd| ƒ‚t d	d
d�t dd
d�t dd
d�t dd
d�dœ}|dkr¼i }|r’d|d< nd|d< ddlm} tj| fd|i|—ŽS |dk�r^t 	¡  |j
|jB }|rè||jO }z|  ¡ } W n tk
�r
   Y nX z|j| |d� d¡W S  tk
�r8   Y nX t| dd|d�}|j||d� d¡S dS )aÉ  
    Convert a plotly/Dash object to a JSON string representation

    Parameters
    ----------
    plotly_object:
        A plotly/Dash object represented as a dict, graph_object, or Dash component

    pretty: bool (default False)
        True if JSON representation should be pretty-printed, False if
        representation should be as compact as possible.

    engine: str (default None)
        The JSON encoding engine to use. One of:
          - "json" for an engine based on the built-in Python json module
          - "orjson" for a faster engine that requires the orjson package
          - "auto" for the "orjson" engine if available, otherwise "json"
        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Returns
    -------
    str
        Representation of input object as a JSON string

    See Also
    --------
    to_json : Convert a plotly Figure to JSON with validation
    r
   T©Zshould_loadNr   r	   ©r
   r	   úInvalid json engine: %szsage.allFÚnumpyZpandasz	PIL.Image)Úsage_allÚnpÚpdÚimageé   Úindent)ú,ú:Ú
separatorsr   )ÚPlotlyJSONEncoderr   )ÚoptionÚutf8)Únumpy_allowedÚdatetime_allowedÚmodules)r   Úconfigr   r   Z_plotly_utils.utilsr1   r	   Údumpsr   r   ZOPT_NON_STR_KEYSZOPT_SERIALIZE_NUMPYZOPT_INDENT_2Úto_plotly_jsonÚAttributeErrorÚdecodeÚ	TypeErrorÚclean_to_json_compatible)Zplotly_objectÚprettyÚenginer
   r6   Úoptsr1   Úcleanedr   r   r   Úto_json_plotly<   sR    



ü	


ürB   Tc                 C   s:   t | |ƒ}|r,| dg ¡D ]}| dd¡ qt|||d�S )a¢  
    Convert a figure to a JSON string representation

    Parameters
    ----------
    fig:
        Figure object or dict representing a figure

    validate: bool (default True)
        True if the figure should be validated before being converted to
        JSON, False otherwise.

    pretty: bool (default False)
        True if JSON representation should be pretty-printed, False if
        representation should be as compact as possible.

    remove_uids: bool (default True)
        True if trace UIDs should be omitted from the JSON representation

    engine: str (default None)
        The JSON encoding engine to use. One of:
          - "json" for an engine based on the built-in Python json module
          - "orjson" for a faster engine that requires the orjson package
          - "auto" for the "orjson" engine if available, otherwise "json"
        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Returns
    -------
    str
        Representation of figure as a JSON string

    See Also
    --------
    to_json_plotly : Convert an arbitrary plotly graph_object or Dash component to JSON
    ÚdataÚuidN)r>   r?   )r   ÚgetÚpoprB   )ÚfigÚvalidater>   Úremove_uidsr?   Úfig_dictÚtracer   r   r   Úto_json˜   s
    '
rL   c                 C   sˆ   t | ||||d�}t|tƒr&t|ƒ}nt|tƒr6|}nd}|dkrzz| |¡ W dS  tk
rf   Y nX tdj|d�ƒ‚n
| |¡ dS )a+  
    Convert a figure to JSON and write it to a file or writeable
    object

    Parameters
    ----------
    fig:
        Figure object or dict representing a figure

    file: str or writeable
        A string representing a local file path or a writeable object
        (e.g. a pathlib.Path object or an open file descriptor)

    pretty: bool (default False)
        True if JSON representation should be pretty-printed, False if
        representation should be as compact as possible.

    remove_uids: bool (default True)
        True if trace UIDs should be omitted from the JSON representation

    engine: str (default None)
        The JSON encoding engine to use. One of:
          - "json" for an engine based on the built-in Python json module
          - "orjson" for a faster engine that requires the orjson package
          - "auto" for the "orjson" engine if available, otherwise "json"
        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.
    Returns
    -------
    None
    )rH   r>   rI   r?   NzX
The 'file' argument '{file}' is not a string, pathlib.Path object, or file descriptor.
)Úfile)	rL   Ú
isinstanceÚstrr   Úwriter:   r   r   Ú
write_text)rG   rM   rH   r>   rI   r?   Újson_strÚpathr   r   r   Ú
write_jsonÊ   s0    $    ÿ



ýÿ
rT   c                 C   s¬   t ddd�}t| ttfƒs0tdjt| ƒ| d�ƒ‚|dkr>tj}|dkrZ|dk	rTd}qnd}n|d	krntd
| ƒ‚|dkrŠt	 
¡  | | ¡}nt| tƒrž|  d¡} t | ¡}|S )aß  
    Parse JSON string using the specified JSON engine

    Parameters
    ----------
    value: str or bytes
        A JSON string or bytes object

    engine: str (default None)
        The JSON decoding engine to use. One of:
          - if "json", parse JSON using built in json module
          - if "orjson", parse using the faster orjson module, requires the orjson
            package
          - if "auto" use orjson module if available, otherwise use the json module

        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Returns
    -------
    dict

    See Also
    --------
    from_json_plotly : Parse JSON with plotly conventions into a dict
    r
   Tr$   zr
from_json_plotly requires a string or bytes argument but received value of type {typ}
    Received value: {value})ÚtypÚvalueNr   r	   r%   r&   zutf-8)r   rN   rO   Úbytesr   r   Útyper7   r   r   r   Úloadsr;   r	   )rV   r?   r
   Z
value_dictr   r   r   Úfrom_json_plotly  s.     ýÿ	


rZ   ÚFigurec                 C   s$   t | |d�}t|ƒ}|||d�}|S )a®  
    Construct a figure from a JSON string

    Parameters
    ----------
    value: str or bytes
        String or bytes object containing the JSON representation of a figure

    output_type: type or str (default 'Figure')
        The output figure type or type name.
        One of:  graph_objs.Figure, 'Figure', graph_objs.FigureWidget, 'FigureWidget'

    skip_invalid: bool (default False)
        False if invalid figure properties should result in an exception.
        True if invalid figure properties should be silently ignored.

    engine: str (default None)
        The JSON decoding engine to use. One of:
          - if "json", parse JSON using built in json module
          - if "orjson", parse using the faster orjson module, requires the orjson
            package
          - if "auto" use orjson module if available, otherwise use the json module

        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Raises
    ------
    ValueError
        if value is not a string, or if skip_invalid=False and value contains
        invalid figure properties

    Returns
    -------
    Figure or FigureWidget
    )r?   )Úskip_invalid)rZ   r   )rV   Úoutput_typer\   r?   rJ   r   rG   r   r   r   Ú	from_jsonW  s    (r^   c                 C   s\   t | tƒ}t | tƒrt| ƒ}nt | tƒr.| }nd}|dk	rD| ¡ }n|  ¡ }t||||d�S )aŽ  
    Construct a figure from the JSON contents of a local file or readable
    Python object

    Parameters
    ----------
    file: str or readable
       A string containing the path to a local file or a read-able Python
       object (e.g. a pathlib.Path object or an open file descriptor)

    output_type: type or str (default 'Figure')
        The output figure type or type name.
        One of:  graph_objs.Figure, 'Figure', graph_objs.FigureWidget, 'FigureWidget'

    skip_invalid: bool (default False)
        False if invalid figure properties should result in an exception.
        True if invalid figure properties should be silently ignored.

    engine: str (default None)
        The JSON decoding engine to use. One of:
          - if "json", parse JSON using built in json module
          - if "orjson", parse using the faster orjson module, requires the orjson
            package
          - if "auto" use orjson module if available, otherwise use the json module

        If not specified, the default engine is set to the current value of
        plotly.io.json.config.default_engine.

    Returns
    -------
    Figure or FigureWidget
    N)r\   r]   r?   )rN   rO   r   Ú	read_textÚreadr^   )rM   r]   r\   r?   Zfile_is_strrS   rR   r   r   r   Ú	read_json‹  s    %




   ÿra   c              	      s\  t | tttfƒr| S t | tƒr4‡ fdd„|  ¡ D ƒS t | ttfƒrX| rX‡ fdd„| D ƒS ˆ  dd¡}ˆ  dd¡}ˆ  di ¡}|d	 }|d
 }|d }|d }|d k	rÈ| |j	kr¶t| ƒS | |j
krÈt| ƒS |d k	�rt| |jjjkrètdƒS t | |jƒ�r^|�r| jjdk�r| | ¡S | jjdk�r0| | ¡ ¡ S | jjdk�rF|  ¡ S | jjdk�rt|  ¡ } nt | |jƒ�rtt| ƒS |d k	�r(| |jk�rŽd S t | |j|jfƒ�r(|�rÂ| jjdk�rÂ| | j¡S | jjdk�r(t | |jƒ�rî| j ¡  ¡ }	n|  ¡  ¡ }	|�s$tt|	ƒƒD ]}
|	|
  ¡ |	|
< �q|	S z|  ¡ } W n ttfk
�rN   Y nX |�s~z
|  ¡ W S  ttfk
�rz   Y nX nt | t j ƒ�r�| S z
|  ¡ W S  tk
�r°   Y nX t | t!j"ƒ�rÈt| ƒS |d k	�rêt | |j#ƒ�rêt$ %| ¡S z|  &¡ } W n tk
�r   Y nX t | tƒ�r0‡ fdd„|  ¡ D ƒS t | ttfƒ�rX| �rX‡ fdd„| D ƒS | S )Nc                    s   i | ]\}}|t |fˆ Ž“qS r   ©r=   ©Ú.0ÚkÚv©Úkwargsr   r   Ú
<dictcomp>Ó  s      z,clean_to_json_compatible.<locals>.<dictcomp>c                    s   g | ]}t |fˆ Ž‘qS r   rb   ©rd   rf   rg   r   r   Ú
<listcomp>×  s     z,clean_to_json_compatible.<locals>.<listcomp>r4   Fr5   r6   r(   r)   r*   r+   Únan)ÚbÚiÚuÚfÚMÚUÚOc                    s   i | ]\}}|t |fˆ Ž“qS r   rb   rc   rg   r   r   ri   8  s      c                    s   g | ]}t |fˆ Ž‘qS r   rb   rj   rg   r   r   rk   <  s     )'rN   ÚintÚfloatrO   ÚdictÚitemsÚlistÚtuplerE   ZRRZZZÚmaÚcoreZmaskedZndarrayZdtypeÚkindZascontiguousarrayZdatetime_as_stringÚtolistZ
datetime64ZNaTZSeriesZDatetimeIndexÚvaluesÚdtZto_pydatetimeÚrangeÚlenÚ	isoformatr<   r:   ÚdatetimeÚdecimalÚDecimalZImager   Zpil_image_to_urir9   )Úobjrh   r4   r5   r6   r(   r)   r*   r+   Z	dt_valuesrn   r   rg   r   r=   Ê  s˜    









r=   )FN)TFTN)TFTN)N)r[   FN)r[   FN)Ú
__future__r   r	   r„   rƒ   Úpathlibr   Zplotly.io._utilsr   r   Z_plotly_utils.optional_importsr   Z_plotly_utils.basevalidatorsr   Úobjectr   r7   r#   rB   rL   rT   rZ   r^   ra   r=   r   r   r   r   Ú<module>   s"   
\
2
L
A
4
?