
    
dK                        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e          Z d Z!d	 Z"d
 Z#d Z$d Z%ej&        dk    rdZ'ndZ'ddde'z  dfde'z  dfdde'z  dfdddddddZ(d Z)d Z*d Z+d Z,d Z-d Z.d:d!Z/d" Z0 G d# d$e1          Z2 e2            Z3e34                                 d% Z5e6d&k    rddl7Z7d'ej8        9                    d(d(          d'd)gd*Z: e7j;        d+d,d-          Z<d.d/d'd0g d1d2dgd'e<e:gd2dfe:d3ej8        9                    d4d5           ej=        g           e< e7j7        d+d,d-          d6
Z>ddl?Z? e?j?                    Z@ e-e>d7            eAd8 e?j?                    e@z
  z              e?j?                    Z@ e0d7          \  ZBZC ejD        d7            eAd9 e?j?                    e@z
  z             dS dS );z
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                   V    e Zd ZdZd Zd Zd Zej        Z	ej
        Ze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                 X    	 | |         S # t           $ r d|z  }t          |          w xY w)z2Access the dictionary keys for unknown attributes.z)'MatlabStruct' object has no attribute %s)KeyErrorAttributeError)selfattrmsgs      <lib/python3.11/site-packages/spyder_kernels/utils/iofuncs.py__getattr__zMatlabStruct.__getattr__7   sD    	&: 	& 	& 	&=DC %%%	&s   
 )c                    ||                                  v s|                    d          rt                              | |          S t	          j                    }|j        j        rH|                     |j        j                  r)t                              | |t                                 nB|                     |j                  r(t                              | |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   frames      r   r   zMatlabStruct.__getitem__?   s     499;;$//#"6"6##D$///$&&< 	94#3#3EL4G#H#H 	9T48888el++ 	9T4888d+++    c                     t           j        d         t           j        d         t           j                            dd          g}|j        j        }||j        dz            }t          rt          |          n|}||v S )z.Check for allowed op code in the calling frame
STORE_ATTR
LOAD_CONST	STOP_CODEr      )disopmapgetf_codeco_codef_lastir   ord)r   r"   allowedbytecodeinstructions        r   r    zMatlabStruct._is_allowedO   sl    9\*CIl,C9==a002<'u}q01*->c+&&&;g%%r#   c                 *    |                                  S )z#Allow for code completion in a REPL)copyr   s    r   __dict__zMatlabStruct.__dict__[   s     yy{{r#   N)__name__
__module____qualname____doc__r   r   r    r   r!   __setattr____delitem____delattr__propertyr6    r#   r   r   r   &   sv          & & &, , , & & & "K"K  X  r#   r   c                    t          | t                    rd | D             S t          | t          j                  s| S t	          | d          rt                      }| j        j        D ]>}t          | |         	                                
                                          ||<   ?t          | j        t          f|          } |            S | j        j        r\t                      }| j        j        D ]>}t          | |         	                                
                                          ||<   ?|} n| j        j        dk    rN| 	                                
                                } t          | t                    s| g} t          |           } n>| j        dk    r|                                 } n| j        dk    r| j        j        dv rd} ng } | S )z
    Extract a value from a Matlab file

    From the oct2py project, see
    https://pythonhosted.org/oct2py/conversions.html
    c                 ,    g | ]}t          |          S r?   )get_matlab_value).0vs     r   
<listcomp>z$get_matlab_value.<locals>.<listcomp>j   s!    111 ##111r#   	classnameO   r   US )
isinstancelistnpndarrayhasattrr   dtypenamesrB   squeezetolisttyperF   objectr   kindsizeitem)valoutnameclss       r   rB   rB   a   s    #t 211S1111 c2:&& 
 sK   ffIO 	G 	GD(T):):)<)<)C)C)E)EFFCII3=6)S11suu 
 nnIO 	G 	GD(T):):)<)<)C)C)E)EFFCII 
3		kkmm""$$#t$$ 	%Cs## 
Qhhjj 
Q9>T!!CCCJr#   c                 <   t           j        t          u rdS 	 t           j                            | d          }t	                      }|                                D ]\  }}t          |          ||<   |d fS # t          $ r}d t          |          fcY d }~S d }~ww xY w)NNrJ   T)struct_as_record)	spior   loadmatr   itemsrB   	Exceptionstr)filenamerZ   datakeyvalueerrors         r   load_matlabrk      s    	u
x emmHtm<<vvIIKK 	0 	0LS%(//DIITz      SZZ s   AA6 6
B BBBc                     t           j        t          u rd S 	 t           j                            || d           d S # t          $ r}t          |          cY d }~S d }~ww xY w)Nrow)oned_as)r`   ra   r   savematrd   re   )rg   rf   rj   s      r   save_matlabrp      ss    	u

he44444   5zzs   "; 
AAAAc                    t           j        t          u rdS 	 t          j        t          j        |                     d         }t          j        |           }t          |t           j        j        j	                  rt          |          d fS t          |d          r|d fS ||id fS # t          $ r}d t          |          fcY d }~S d }~ww xY w)Nr^   r   r   )rM   loadr   ospsplitextbasenamerK   libnpyioNpzFiler   rO   rd   re   )rf   r[   rg   rj   s       r   
load_arrayry      s    	w*x
 |CL2233A6wx  dBFL011 	&::t##T6"" 	&:$<%%      SZZ s*   A4B& B&  B& &
C0C CCc                 @    |d|z  z   }t          j        ||            |S )zSave numpy arrayz	_%04d.npy)rM   save)rg   ru   indexfnames       r   __save_arrayr~      s(    {U**EGE4Lr#   little<>)z|b1N)|u1Nz%si4z%sf4)z|u2Nz%si2)r   r(   )r      )1LIFzI;16zI;16SPRGBRGBXRGBACMYKYCbCrc                    t           j                            |           }	 t          |j                 \  }}n%# t
          $ r t          d|j        z            w xY w|j        d         |j        d         f}|||fz  }t          j	        |
                                t          j        |                                        |          S )Nz%s mode is not supportedrH   r   )rP   )r   ImageopenDTYPESmoder   RuntimeErrorrW   rM   arraygetdatarP   reshape)rf   imgrP   extrashapes        r   __image_to_arrayr      s    
)..
"
"CBch'uu B B B5@AAABXa[#(1+&E%8CKKMM%999AA%HHHs	   7 "Ac                 "   t           j        t          u st          j        t          u rdS 	 t          j        t          j        |                     d         }|t          |           id fS # t          $ r}d t          |          fcY d }~S d }~ww xY w)Nr^   r   )r   r   r   rM   r   rs   rt   ru   r   rd   re   )rf   r[   rj   s      r   
load_imager      s    
yJ"(j"8"8x |CL2233A6&x001477      SZZ s   >A) )
B3B	B	Bc                 &   	 t           j        t          urt          j        |           dfS t          | d          5 }t	          j        |          }ddd           n# 1 swxY w Y   |dfS # t          $ r}dt          |          fcY d}~S d}~ww xY w)z"Load a pickle file as a dictionaryNrb)pdread_pickler   r   r   rr   rd   re   )rf   fidrg   errs       r   load_pickler      s    >++>(++T11h%% ({3''( ( ( ( ( ( ( ( ( ( ( ( ( ( (:   SXX~sE   (A+ A+ AA+ A  A+ #A $A+ +
B5BBBc                     	 t           rd}nd}t          | |          5 }t          j        |          }ddd           n# 1 swxY w Y   |dfS # t          $ r}dt          |          fcY d}~S d}~ww xY w)z Load a json file as a dictionaryr   rN)r   r   jsonrr   rd   re   )rf   argsr   rg   r   s        r   	load_jsonr      s    	 	DDD(D!! 	"S9S>>D	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"Tz   SXX~s=   A ?A AA AA 
A3A.(A3.A3c           	      
   t          j        |          }t                      }t          j        t          j        |                     d}g }i }	 |                                 D ]l\  }}t          |          sXt          |t          j
                  s>	 t          j        |          ||<   G# t          $ r |                    |           Y hw xY wm|} | st          d          i }t           j        t$          ur.t          j        |          d         }	t)          |                                           D ]}
	 t          | |
         t           j                  re| |
         j        dk    rTt/          | |
         |	t1          |                    }t          j        |          ||
df<   |                     |
           n2t          | |
         t(          t6          f          rt          | |
         t(                    rt9          | |
                   }n4t;          t)          | |
                                                             }g }|D ]v\  }}t          |t           j                  rW|j        dk    rLt/          ||	t1          |                    }t          j        |          ||
|f<   |                    |           wt=          |d          D ]}| |
                             |           # t          t>          j         tB          tD          tF          f$ r Y w xY w|r|| d<   t          j        |          d         dz   }tI          |d          5 }	 t?          j%        | |d	
           n# t>          j         tD          tB          tL          tF          t          f$ r i }|                                 D ]F\  }}	 t?          j'        |d	
           |||<   "# t          $ r |                    |           Y Cw xY w|st          d          t?          j%        ||d	
           Y nw xY wddd           n# 1 swxY w Y   tQ          j$        |dtP          j)                  5 }|gd t)          |*                                          D             z   D ]=}|+                    t          j        |                     t          j,        |           >	 ddd           n# 1 swxY w Y   |r,|-                                 dd.                    |          z   }n8# t          t>          j         tB          f$ r}t_          |          }Y d}~nd}~ww xY wt          j        |           n# t          j        |           w xY w|S )z.Save dictionary in a single file .spydata fileNzNo supported objects to saver   T)reverse__saved_arrays__.picklezw+b   )protocolw)formatc                     g | ]}|S r?   r?   )rC   fns     r   rE   z#save_dictionary.<locals>.<listcomp>e  s    FFFBRFFFr#   z!Some objects could not be saved: z, )0rs   abspathr   oschdirdirnamerc   callablerK   types
ModuleTyper4   deepcopyrd   appendr   rM   rN   r   rt   rL   r   rW   r~   lenru   popr   	enumerateitersortedr   PicklingError	TypeErrorr   
IndexErrorr   dumpImportErrordumpstarfile
PAX_FORMATvaluesaddremovesortjoinr   )rg   rf   old_cwderror_messageskipped_keys	data_copyobj_name	obj_valuesaved_arrays	arr_fnamer[   r}   iterator	to_remover|   ri   pickle_filenamefdescdata_filteredtarrj   s                        r   save_dictionaryr     s:   {8$$HhhGHS[""###MLI\#'::<< 	2 	2Hi
 Y'' 2:i6;6F,H ,H 22*.-	*B*BIh''  2 2 2 ''111112 	?=>>>:Z''X..q1ITYY[[))  "4:rz:: 2 JOa// ,T$Z-0->->!@ !@58\%5H5HdD\2#DJt== 2%d4j$77 F'0d'<'<HH'+Dd1A1A1C1C,D,D'E'EH$&	,4 8 8LE5 *5"* = = 8$)JNN(4UI585F5F)H )H %(L$7$7 !-dE] ; ) 0 0 7 7 7%+It%D%D%D 2 2E JNN51111$f&:I&
4    D	
  8+7'(,x003i? /5)) 	>U>D%!44444(.)\; > > > "+/::<< < <'Hi<Y;;;; 3<h// % 6 6 6$++H555556 % G&'EFFFM51======>	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>& \(C0BCCC 	!s*+FF$|/B/B/D/D*E*EFFFG ! !U++,,,	%    !	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!  	8@#yy667M &.	: . . .&u--. 	s  AT B+*T +C
T CA+T 9F7K20T 2(LT L7T P4M/.P4/A	P%9OP%O7	4P%6O7	7+P%"P4$P%%P4(T 4P88T ;P8<$T  A.ST ST "S#T &/U" U
1U U" U

U" "U8c                     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prefixs        r   is_within_directoryr   t  sM    GOOI..M((JW!!=*"=>>F]""r#   .Fc                 6   |                                  D ]k}t          j                            ||j                  }t          ||          s4t          d                    t          | j                                      l| 	                    |||           dS )zSafely extract a tar file.z'Attempted path traversal in tar file {})numeric_ownerN)

getmembersr   r   r   r[   r   rd   r   repr
extractall)r   r   membersr   membermember_paths         r   safe_extractr   |  s    ..""  gll455"455 	9@@NN   	 NN4N>>>>>r#   c                    t          j        |           } t                      }t          j                    }t          j        |           d}d}	 t          j        | d          5 }t          r|
                                 nt          |           ddd           n# 1 swxY w Y   t          j        d          d         }t          |d          5 }t          j        |                                          }ddd           n# 1 swxY w Y   i }t           j        t$          ur	 |                    d          }t)          |                                          D ]|\  \  }	}
}t!          j        t          j        ||          d          }|
|||	<   9t/          ||	         t0                    r|||	         |
<   `||	                             |
|           }n# t4          $ r Y nw xY wn3# t6          t8          t:          f$ r}t=          |          }Y d}~nd}~ww xY wt          j        |           	 t?          j         |           nz# tB          $ r}t=          |          }Y d}~n\d}~ww xY w# t          j        |           	 t?          j         |           w # tB          $ r}t=          |          }Y d}~w d}~ww xY wxY w||fS )	z"Load dictionary from .spydata fileNr   z*.pickler   r   r   T)allow_pickle)"rs   r   r   tempfilemkdtempr   r   r   r   r   r   r   globr   loadsreadrM   rr   r   r   rL   rc   r   rK   r   insertr   r   EOFError
ValueErrorr   shutilrmtreeOSError)rf   r   
tmp_folderrg   r   r   r   r   r   r[   r|   r}   arrrj   s                 r   load_dictionaryr     sb   {8$$HhhG!##JHZDM#2\(C(( 	"C "    S!!!		" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" )J//2/4(( 	.E<

--D	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.7*$$#xx(:;;,01C1C1E1E,F,F 6 6(MT55'#(:u"="=DQQQC}%(T

#DJ55 6,/T
5))T
))%55556     Hj1 . . .&u--. 		2M*%%%% 	2 	2 	2*511MMMMMM	2 		2M*%%%% 	2 	2 	2*511MMMMMM	2s   G* $,BG* B  G* #B $-G* 'D8G* DG* DG* %B3G G* 
G&#G* %G&&G* )I- *HHI- HI- 2I 
I*I%%I*-J>JJ>
J;"J61J>6J;;J>c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )IOFunctionsc                 Z    d | _         d | _        d | _        d | _        d | _        d | _        d S )N)load_extensionssave_extensionsload_filterssave_filters
load_funcs
save_funcsr5   s    r   __init__zIOFunctions.__init__  s4    ##  r#   c           	         |                                  |                                 z   }i }i }i }i }g }g }g }|D ]s\  }	}
}}t          |
d|	z  z             }|4|                    |           |	||<   |||	<   |                    |	           ||	||<   |                    |           |||	<   t|                    dt          dd                    |          z   dz                        |                    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   iofuncsr  r  r  r  r  r  load_extextr[   loadfuncsavefunc
filter_strs                 r   setupzIOFunctions.setup  s   ))++D,D,D,F,FF

-4 
	+ 
	+)Cx'x#~(=>>J###J///.1
+"*
3$$$#.1
+##J///"*
3A~.E.2ii.A.A/BBE/F  G  G 	H 	H 	HN+<==>>> IIl33 IIl33$$..r#   c                     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   ry   rk   rp   r   r   r   r5   s    r   r  zIOFunctions.get_internal_funcs  s    0,o?T:+Z>kBA=
D9z48z48
D9d;NK>,	48 	r#   c           	      <   g }	 ddl m}  |d          D ]t}	 |                    |j        |j        |j        |j        f           1# t          $ r7}t          |dt          |          t          j                   Y d }~md }~ww xY wn# t          $ r Y nw xY w|S )Nr   )get_spyderplugins_modsT)ra   z: )file)spyder.otherpluginsr  r   
FORMAT_EXTFORMAT_NAMEFORMAT_LOADFORMAT_SAVEr   printre   sysstderrr   )r   other_funcsr  modrj   s        r   r  zIOFunctions.get_3rd_party_funcs  s   		BBBBBB--666 I II&&(+(J K K K K% I I Iccc3u:::6SZHHHHHHHHHI	I  	 	 	D	s:   B -AB 
B-B>B BB 
BBc                     t          j        |          d                                         }|| j        v r | j        |         ||          S d|z  S NrH   z!<b>Unsupported file type '%s'</b>)rs   rt   lowerr  )r   rg   rf   r  s       r   r{   zIOFunctions.save  sS    l8$$Q'--//$/!!'4?3'h7776<<r#   c                     t          j        |          d                                         }|| j        v r | j        |         |          S d d|z  fS r&  )rs   rt   r'  r  )r   rf   r  s      r   rr   zIOFunctions.load  sW    l8$$Q'--//$/!!'4?3'111<sBBBr#   N)	r7   r8   r9   r	  r  r  r  r{   rr   r?   r#   r   r  r    ss          / / /<  $  = = =C C C C Cr#   r  c                     dS )z4Save data into filename, depending on file extensionNr?   )rg   rf   s     r   	save_autor*    s    Dr#   __main__rH   
   r   )dabi        zkjkj kj k j j kj k jkju   éùr(   )r   r0     kjkjg&S?i  i  )
re   unicoderL   tupler   floatr   empty_arraydatedatetimeztest.spydataz Data saved in %.3f secondszData loaded in %.3f seconds)r   NF)Er:   
__future__r   r!  r   os.pathr   rs   r   r   r   r   r   r   r)   r4   r   spyder_kernels.py3compatr   r   r   r    spyder_kernels.utils.lazymodulesr   r	   rM   r
   r   r   r   r`   r   r   rB   rk   rp   ry   r~   	byteorder_ENDIANr   r   r   r   r   r   r   r   r   rU   r  iofunctionsr  r*  r7   r9  randomrandtestdictr8  testdater   exampletimet0r   example2okr   r?   r#   r   <module>rJ     sE    & % % % % % 


 				             



   I H H H H H H H H H H H= = = = = = = = = = = = = =8 8 8 8 84 8 8 8v0 0 0f            "   =HGGG 
	
7
D	!
7
D	!w%	
 
 	I 	I 	I     
 
 
  f f fR# # #
? 
? 
? 
?, , ,^SC SC SC SC SC& SC SC SCj kmm      	 	 	
 zOOORY^^B331a&AAHx}T1a((H.!1iii6Xx0&$?	tS11&bhrll,8,T1a88
 
G KKK	BOG^,,,	E
'949;;r>
:;;;	B"?>22LHbBIn	E
'949;;r>
:;;;;;/ r#   