U
    ccF                     @   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   I/var/www/html/project/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ri }|rd|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rt	
  || }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rt| S | |j
krt| S |d k	rt| |jjjkrt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rd 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"rt| S |d k	rt | |j#rt$%| 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
?