o
    ZhK                     @   s  d Z ddlmZ ddlZddlZddlmZ ddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlmZmZmZmZ ddlmZmZmZmZmZ G dd deZ dd	 Z!d
d Z"dd Z#dd Z$dd Z%ej&dkrxdZ'ndZ'ddde' dfde' dfdde' dfdddddddZ(dd Z)d d! Z*d"d# Z+d$d% Z,d&d' Z-d(d) Z.dHd,d-Z/d.d/ Z0G d0d1 d1e1Z2e2 Z3e34  d2d3 Z5e6d4kr?ddl7Z7d5ej89d6d6d5d7gd8Z:e7;d9d:d;Z<d<d=d5d>g d?d@dgd5e<e:gd@dfe:dAej89dBdCe=g e<e77d9d:d;dD
Z>ddl?Z?e?? Z@e-e>dE eAdFe?? e@   e?? Z@e0dE\ZBZCeDdE eAdGe?? e@   dS dS )Iz
Input/Output Utilities

Note: 'load' functions has to return a dictionary from which a globals()
      namespace may be updated
    )print_functionN)getcwdpicklePY2to_text_string)
FakeObjectnumpypandasPILscipyc                   @   s@   e Zd ZdZdd Zdd Zdd ZejZ	ej
Zedd	 Zd
S )MatlabStructa  
    Matlab style struct, enhanced.

    Supports dictionary and attribute style access.  Can be pickled,
    and supports code completion in a REPL.

    Examples
    ========
    >>> from spyder.utils.iofuncs import MatlabStruct
    >>> a = MatlabStruct()
    >>> a.b = 'spam'  # a["b"] == 'spam'
    >>> a.c["d"] = 'eggs'  # a.c.d == 'eggs'
    >>> print(a)
    {'c': {'d': 'eggs'}, 'b': 'spam'}

    c                 C   s*   z| | W S  t y   d| }t|w )z2Access the dictionary keys for unknown attributes.z)'MatlabStruct' object has no attribute %s)KeyErrorAttributeError)selfattrmsg r   S/var/www/html/lang_env/lib/python3.10/site-packages/spyder_kernels/utils/iofuncs.py__getattr__7   s   
zMatlabStruct.__getattr__c                 C   sz   ||   v s|drt| |S t }|jjr)| |jjr)t| |t	  n| |jr7t| |t	  t| |S )z
        Get a dict value; create a MatlabStruct if requesting a submember.

        Do not create a key if the attribute starts with an underscore.
        _)
keys
startswithdict__getitem__inspectcurrentframef_back_is_allowed__setitem__r   )r   r   framer   r   r   r   ?   s   zMatlabStruct.__getitem__c                 C   sN   t jd t jd t jddg}|jj}||jd  }tr!t|n|}||v S )z.Check for allowed op code in the calling frame
STORE_ATTR
LOAD_CONSTZ	STOP_CODEr      )disopmapgetf_codeco_codef_lastir   ord)r   r   allowedbytecodeZinstructionr   r   r   r   O   s   zMatlabStruct._is_allowedc                 C   s   |   S )z#Allow for code completion in a REPL)copyr   r   r   r   __dict__[   s   zMatlabStruct.__dict__N)__name__
__module____qualname____doc__r   r   r   r   r   __setattr____delitem____delattr__propertyr.   r   r   r   r   r   &   s    	r   c                 C   s   t | trdd | D S t | tjs| S t| dr:t }| jjD ]}t| | 	 
 ||< q t| jtf|}| S | jjrXt }| jjD ]}t| | 	 
 ||< qE|} | S | jjdkrr| 	 
 } t | tsl| g} t| } | S | jdkr}|  } | S | jdkr| jjdv rd} | S g } | S )	z
    Extract a value from a Matlab file

    From the oct2py project, see
    https://pythonhosted.org/oct2py/conversions.html
    c                 S   s   g | ]}t |qS r   )get_matlab_value).0vr   r   r   
<listcomp>j   s    z$get_matlab_value.<locals>.<listcomp>	classnameO   r   ZUS )
isinstancelistnpndarrayhasattrr   dtypenamesr7   Zsqueezetolisttyper;   objectr   kindsizeitem)valoutnameclsr   r   r   r7   a   s>   



	
r7   c              
   C   s~   t jtu rdS zt jj| dd}t }| D ]
\}}t|||< q|d fW S  ty> } zd t|fW  Y d }~S d }~ww )NNr>   T)Zstruct_as_record)	spior   Zloadmatr   itemsr7   	Exceptionstr)filenamerM   datakeyvalueerrorr   r   r   load_matlab   s   

r[   c              
   C   sT   t jtu rd S zt jj|| dd W d S  ty) } z
t|W  Y d }~S d }~ww )Nrow)Zoned_as)rQ   rR   r   ZsavematrT   rU   )rW   rV   rZ   r   r   r   save_matlab   s   
r]   c              
   C   s   t jtu rdS z/tt| d }t | }t|t jjj	r&t
|d fW S t|dr0|d fW S ||id fW S  tyN } zd t|fW  Y d }~S d }~ww )NrP   r   r   )rA   loadr   ospsplitextbasenamer?   libZnpyioZNpzFiler   rC   rT   rU   )rV   rN   rW   rZ   r   r   r   
load_array   s   



rc   c                 C   s   |d|  }t ||  |S )zSave numpy arrayz	_%04d.npy)rA   save)rW   ra   indexfnamer   r   r   __save_array   s   rg   little<>)z|b1N)|u1Nz%si4z%sf4)z|u2Nz%si2)rk   r"   )rk      )1LIFzI;16zI;16SPRGBZRGBXZRGBAZCMYKZYCbCrc                 C   s   t j| }z	t|j \}}W n ty   td|j w |jd |jd f}|d ur1||f7 }tj	|
 t|d|S )Nz%s mode is not supportedr=   r   )rD   )r
   ImageopenDTYPESmoder   RuntimeErrorrJ   rA   arrayZgetdatarD   Zreshape)rV   ZimgrD   extrashaper   r   r   __image_to_array   s   
r{   c              
   C   sp   t jtu s
tjtu rdS ztt| d }|t| id fW S  t	y7 } zd t
|fW  Y d }~S d }~ww )NrP   r   )r
   rs   r   rA   rx   r_   r`   ra   r{   rT   rU   )rV   rN   rZ   r   r   r   
load_image   s   r|   c              
   C   s   z,t jturt | dfW S t| d}t|}W d   n1 s#w   Y  |dfW S  tyD } zdt|fW  Y d}~S d}~ww )z"Load a pickle file as a dictionaryNrb)pdZread_pickler   rt   r   r^   rT   rU   )rV   fidrW   errr   r   r   load_pickle   s   

r   c              
   C   s~   z&t rd}nd}t| |}t|}W d   n1 sw   Y  |dfW S  ty> } zdt|fW  Y d}~S d}~ww )z Load a json file as a dictionaryr}   rN)r   rt   jsonr^   rT   rU   )rV   argsr   rW   r   r   r   r   	load_json   s   
r   c                 C   s  t |}t }tt | d}g }i }z֐z|  D ]&\}}t|sDt|t	j
sDz	t|||< W q tyC   || Y qw q|} | sMtdi }tjturt |d }	t|  D ]}
zt| |
 tjr| |
 jdkrt| |
 |	t|}t |||
df< | |
 n[t| |
 ttfrt| |
 trt| |
 }n
tt| |
  }g }|D ]%\}}t|tjr|jdkrt||	t|}t |||
|f< || qt|ddD ]	}| |
 | qW qa ttj t!t"t#fy   Y qaw |r|| d< t |d d }t$|d]}z
tj%| |d	d
 W nJ tj t"t!t&t#tfya   i }|  D ]#\}}z	tj'|d	d
 W n tyI   || Y q,w |||< q,|sWtdtj%||d	d
 Y nw W d   n	1 smw   Y  t(j$|dt(j)d'}|gdd t|* D  D ]}|+t | t,| qW d   n	1 sw   Y  W n ttj t!fy } z
t-|}W Y d}~n#d}~ww |r|.  dd/| }W t| |S W t| |S W t| |S t| w )z.Save dictionary in a single file .spydata fileNzNo supported objects to saver   T)reverse__saved_arrays__.picklezw+b   )protocolw)formatc                 S   s   g | ]}|qS r   r   )r8   fnr   r   r   r:   e  s    z#save_dictionary.<locals>.<listcomp>z!Some objects could not be saved: z, )0r_   abspathr   oschdirdirnamerS   callabler?   types
ModuleTyper,   deepcopyrT   appendrw   rA   rB   r   r`   r@   r   rJ   rg   lenra   popr   	enumerateitersortedr   PicklingError	TypeErrorr   
IndexErrorrt   dumpImportErrordumpstarfile
PAX_FORMATvaluesaddremover   sortjoin)rW   rV   old_cwderror_messageZskipped_keysZ	data_copyobj_nameZ	obj_valuesaved_arraysZ	arr_fnamerN   rf   iterator	to_removere   rY   pickle_filenamefdescZdata_filteredtarrZ   r   r   r   save_dictionary  s   











r   c                 C   s0   t j| }t j|}t j||g}||kS )z&Check if a file is within a directory.)r   pathr   commonprefix)	directorytargetabs_directory
abs_targetprefixr   r   r   is_within_directoryt  s   r   .Fc                 C   sP   |   D ]}tj||j}t||stdt| jq| j	|||d dS )zSafely extract a tar file.z'Attempted path traversal in tar file {})numeric_ownerN)

getmembersr   r   r   rN   r   rT   r   repr
extractall)r   r   membersr   memberZmember_pathr   r   r   safe_extract|  s   
r   c                 C   s&  t | } t }t }t| d}d}zzt| d}t	r%|
  nt| W d   n1 s3w   Y  tdd }t|d}t| }W d   n1 sVw   Y  i }tjturz>|d}t| D ]0\\}	}
}tjt ||dd}|
du r|||	< qnt||	 tr|||	 |
< qn||	 |
| qnW n	 ty   Y nw W n tttfy } z
t|}W Y d}~nd}~ww W t| z
t | W ||fS  t!y } zt|}W Y d}~||fS d}~ww t| zt | W w  t!y } z
t|}W Y d}~w d}~ww )	z"Load dictionary from .spydata fileNr   z*.pickler   r}   r   T)Zallow_pickle)"r_   r   r   tempfilemkdtempr   r   r   rt   r   r   r   globr   loadsreadrA   r^   r   r   r@   rS   r   r?   r   insertr   r   EOFError
ValueErrorr   shutilrmtreeOSError)rV   r   Z
tmp_folderrW   r   r   r   r   r   rN   re   rf   ZarrrZ   r   r   r   load_dictionary  sl   







r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )IOFunctionsc                 C   s(   d | _ d | _d | _d | _d | _d | _d S )N)load_extensionssave_extensionsload_filterssave_filters
load_funcs
save_funcsr-   r   r   r   __init__  s   
zIOFunctions.__init__c                 C   s   |   |   }i }i }i }i }g }g }g }|D ]5\}	}
}}t|
d|	  }|d ur<|| |	||< |||	< ||	 |d urM|	||< || |||	< q|dtdd| d  |td d|| _d|| _|| _|| _	|| _
|| _d S )Nz (*%s)r   zSupported files (*z *)zAll files (*.*)
)get_internal_funcsget_3rd_party_funcsr   r   r   r   r   r   r   r   r   r   )r   Ziofuncsr   r   r   r   r   r   Zload_extextrN   ZloadfuncZsavefuncZ
filter_strr   r   r   setup  s@   




zIOFunctions.setupc                 C   sv   ddt tfddtd fddtd fddttfd	d
ddtd fddtd fddtd fddtd fddtd fddtd fddtd fgS )Nz.spydatazSpyder data filesz.npyzNumPy arraysz.npzzNumPy zip arraysz.matzMatlab files)z.csvzCSV text filesimport_wizardN)z.txtz
Text filesr   Nz.jpgzJPEG imagesz.pngz
PNG imagesz.gifz
GIF imagesz.tifzTIFF imagesz.pklzPickle filesr   z.jsonz
JSON files)r   r   rc   r[   r]   r|   r   r   r-   r   r   r   r     s    









zIOFunctions.get_internal_funcsc                 C   s   g }z?ddl m} |ddD ]0}z||j|j|j|jf W q ty> } ztd|t	|f t
jd W Y d }~qd }~ww W |S  tyK   Y |S w )Nr   )get_spyderplugins_modsT)rR   z%s: %s)file)Zspyder.otherpluginsr   r   Z
FORMAT_EXTZFORMAT_NAMEZFORMAT_LOADZFORMAT_SAVEr   printrU   sysstderrr   )r   Zother_funcsr   modrZ   r   r   r   r     s$   
&zIOFunctions.get_3rd_party_funcsc                 C   s4   t |d  }|| jv r| j| ||S d| S Nr=   z!<b>Unsupported file type '%s'</b>)r_   r`   lowerr   )r   rW   rV   r   r   r   r   rd     s   
zIOFunctions.savec                 C   s6   t |d  }|| jv r| j| |S d d| fS r   )r_   r`   r   r   )r   rV   r   r   r   r   r^     s   
zIOFunctions.loadN)	r/   r0   r1   r   r   r   r   rd   r^   r   r   r   r   r     s    r   c                 C   s   dS )z4Save data into filename, depending on file extensionNr   )rW   rV   r   r   r   	save_auto  s   r   __main__r=   
   r   )dabi        zkjkj kj k j j kj k jkju   éùr"   )rl   r      Zkjkjg&S?i  i  )
rU   unicoder@   tupler   floatrx   Zempty_arraydatedatetimeztest.spydataz Data saved in %.3f secondszData loaded in %.3f seconds)r   NF)Er2   
__future__r   r   r   Zos.pathr   r_   r   r   r   r   r   r   r#   r,   r   Zspyder_kernels.py3compatr   r   r   r   Z spyder_kernels.utils.lazymodulesr   r   rA   r	   r~   r
   r   rQ   r   r   r7   r[   r]   rc   rg   	byteorderZ_ENDIANru   r{   r|   r   r   r   r   r   r   rH   r   Ziofunctionsr   r   r/   r   randomZrandZtestdictr   Ztestdaterx   exampletimet0r   Zexample2okr   r   r   r   r   <module>   s   ;3




i
/U


