o
    NhI                     @   s   d dl 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
Zed 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 )#    N)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   P/var/www/html/Persson_Maskin/env/lib/python3.10/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vrtdjt j|d|dkr|   || _d S )Nz9Supported JSON engines include {valid}
    Received {val})validvalr	   )r   _valid_engines
ValueErrorformatvalidate_orjsonr   )r   r   r   r   r   r      s   

c                 C   s   t d}|d u 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v rdS | S )zM
    This is used to ultimately *encode* into strict JSON, see `encode`

    )Infinityz	-InfinityNaNNr   )constr   r   r   coerce_to_strict0   s   r$   ))<z\u003c)>z\u003e)/z\u002f))u    z\u2028)u    z\u2029c                 C   s*   | }|D ]\}}||v r| ||}q|S r   )replace)json_str_swapoutunsafe_char	safe_charr   r   r   _safeG   s   r.   Fc                 C   sj  t ddd}|du rtj}|dkr|durd}nd}n
|dvr%td| t d	d
dt dd
dt dd
dt dd
dd}|dkrai }|rId|d< nd|d< ddlm} ttj| fd|i|t	S |dkrt
  |j|jB }|rv||jO }z|  } W n	 ty   Y nw zt|j| |ddtW S  ty   Y nw t| dd|d}t|j||ddt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should_loadNr
   r   r	   r   Invalid json engine: %szsage.allFnumpypandasz	PIL.Image)sage_allnppdimage   indent),:
separatorsr   )PlotlyJSONEncoderr   )optionutf8)numpy_alloweddatetime_allowedmodules)r   configr   r   _plotly_utils.utilsr>   r.   r   dumps
_swap_jsonr   r   OPT_NON_STR_KEYSOPT_SERIALIZE_NUMPYOPT_INDENT_2to_plotly_jsonAttributeErrordecode_swap_orjson	TypeErrorclean_to_json_compatible)plotly_objectprettyenginer	   rC   optsr>   cleanedr   r   r   to_json_plotlyO   s`   



	

rV   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)rR   rS   )r   getpoprV   )figvalidaterR   remove_uidsrS   fig_dicttracer   r   r   to_json   s
   
'r`   c                 C   s   t | ||||d}t|trt|}n
t|tr|}nd}|du r;z|| W dS  ty2   Y nw tdj|d|| dS )a  
    Convert a figure to JSON and write it to a file or writeable
    object.

    Note: A figure converted to JSON with one version of Plotly.py may not be compatible with another version.

    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
    )r\   rR   r]   rS   NzX
The 'file' argument '{file}' is not a string, pathlib.Path object, or file descriptor.
)file)	r`   
isinstancestrr   writerL   r   r   
write_text)r[   ra   r\   rR   r]   rS   r)   pathr   r   r   
write_json   s*   &




rg   c                 C   s   t ddd}t| ttfstdjt| | d|du rtj}|dkr-|dur*d}nd}n
|d	vr7td
| |dkrFt	
  || }|S t| trP| 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   r1   r2   zutf-8)r   rb   rc   bytesr   r   typerD   r   r   r   loadsrM   r   )ri   rS   r	   
value_dictr   r   r   from_json_plotly-  s.   




rn   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
    )rS   )skip_invalid)rn   r   )ri   output_typerp   rS   r^   r   r[   r   r   r   	from_jsonl  s   (rr   c                 C   sR   t | tr
t| }n
t | tr| }nd}|dur| }n|  }t||||dS )a  
    Construct a figure from the JSON contents of a local file or readable
    Python object.

    Note: A figure converted to JSON with one version of Plotly.py may not be compatible with another version.

    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)rp   rq   rS   )rb   rc   r   	read_textreadrr   )ra   rq   rp   rS   rf   r)   r   r   r   	read_json  s   
%


ru   c              	      s  t | tttfr
| S t | tr fdd|  D S t | ttfr,| r, fdd| D S  dd} dd} di }|d	 }|d
 }|d }|d }|d urd| |j	v r[t| S | |j
v rdt| S |d ur| |jjju rstdS t | |jr|r| jjdv r|| S | jjdkr||  S | jjdkr|  S | jjdkr|  } n
t | |jrt| S |d ur,| |ju s| |ju rd S t | |j|jfr,|r| jjdv r|| jS | jjdkr,t | |jrt  tdt || j   }	W d    n	1 s	w   Y  n|    }	|s*t!t"|	D ]}
|	|
 # |	|
< q|	S z|   } W n t$t%fy>   Y nw |sTz| # W S  t$t%fyS   Y n
w t | t&j&r]| S z|  W S  t%yl   Y nw t | t'j(rxt| S |d urt | |j)rt*+| S z| , } W n
 t%y   Y nw t | tr fdd|  D S t | ttfr| r fdd| D S | S )Nc                    "   i | ]\}}|t |fi  qS r   rP   .0kvkwargsr   r   
<dictcomp>     " z,clean_to_json_compatible.<locals>.<dictcomp>c                       g | ]
}t |fi  qS r   rw   ry   r{   r|   r   r   
<listcomp>      z,clean_to_json_compatible.<locals>.<listcomp>rA   FrB   rC   r5   r6   r7   r8   nan)biufMUOignorec                    rv   r   rw   rx   r|   r   r   r~   P  r   c                    r   r   rw   r   r|   r   r   r   T  r   )-rb   intfloatrc   dictitemslisttuplerY   RRZZmacoremaskedndarraydtypekindascontiguousarraydatetime_as_stringtolist
datetime64NaTNASeriesDatetimeIndexvalueswarningscatch_warningssimplefilterFutureWarningarraydtto_pydatetimerangelen	isoformatrO   rL   datetimedecimalDecimalImager   pil_image_to_urirK   )objr}   rA   rB   rC   r5   r6   r7   r8   	dt_valuesr   r   r|   r   rP     s   








rP   )FN)TFTNr   )ro   FN)r   r   r   r   pathlibr   plotly.io._utilsr   r   _plotly_utils.optional_importsr   _plotly_utils.basevalidatorsr   objectr   rD   r$   rG   rN   r.   rV   r`   rg   rn   rr   ru   rP   r   r   r   r   <module>   s*    

`
2
L
?
4>