
    ]h                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJrJ	r	J
r
JrJrJrJr  SSKJr  SSKJr  SSKJr  \R*                  " 5       S:X  a  S	OS
rS\S\4S jr S"S\S\S\S\S\4
S jjr\R6                  S\S\	S   4S j5       rS\\\R:                  \\\4   S4   S\	\\      4S jr\R6                  " \5      r S#S\\\R:                  \\\4   \
\   S4   S\ S\	\\      4S jjr!\R6                  SSS.S\\\R:                  \\\4   \"\ \
\   S4   S\S\ S\	\\\"\ S4      4S jj5       r# " S  S!5      r$g)$zE
Utilities for interacting with the filesystem on multiple platforms
    N)AnyIteratorListMappingOptionalTupleUnion)_TMPDIR   )write_stan_json)
get_loggerWindowsz.exe pathreturnc                 ^   [         R                  " 5       S:w  a  U $ [        R                  R	                  U 5      (       dJ  [        R                  R                  U 5      (       dH  [        R                  R                  U 5      S   S:w  a"  [        R                  R                  U 5      u  pOU Sp![        R                  R                  U5      (       d  [        SR                  U5      5      eSSKnSSKJn  UR                  R                  R                  nUR                   UR"                  UR$                  /Ul        UR$                  Ul        Sn UR+                  U5      nU" XU5      nXh:  a  UR,                  n	OUnM0  U(       a!  [        R                  R/                  X5      n
U
$ U	n
U
$ )z
Gets the short path name of a given long path.
http://stackoverflow.com/a/23598461/200291

On non-Windows platforms, returns the path

If (base)path does not exist, function raises RuntimeError
r   r   r   zXWindows short path function needs a valid directory. Base directory does not exist: "{}"r   N)wintypes)platformsystemosr   isfileisdirsplitextsplitexistsRuntimeErrorformatctypesr   windllkernel32GetShortPathNameWLPCWSTRLPWSTRDWORDargtypesrestypecreate_unicode_buffervaluejoin)r   	base_path	file_namer   r   _GetShortPathNameWoutput_buf_size
output_bufneededshort_base_path
short_paths              mC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\cmdstanpy/utils/filesystem.pywindows_short_pathr3      ss    I%	ww~~dGGMM$BGG$4$4T$:1$=$C!ww}}T2	9#R977>>)$$228&2C
 	

  	00 
 	#
 "*O
11/B
#I?K$(..O$O   	_0 
   
     dirprefixsuffix	name_onlyc                 j    [         R                  " SXXS9nUR                  nUR                  5         U$ )z
Create a named unique file, return filename.
Flag 'name_only' will create then delete the tmp file;
this lets us create filename args for commands which
disallow overwriting existing files (e.g., 'stansummary').
zw+)moder6   r7   r5   delete)tempfileNamedTemporaryFilenameclose)r5   r6   r7   r8   fdr   s         r2   create_named_text_filerA   N   s4     
	$	$&S
B 77DHHJKr4   new_dirc              #      #    [         R                  " 5       n[         R                  " U 5         Sv   [         R                  " U5        g! [         R                  " U5        f = f7f)zActs like pushd/popd.N)r   getcwdchdir)rB   previous_dirs     r2   pushdrG   _   s>      99;LHHW
s   ,A%A
 A%
A""A%datac              #   &  #    U c  Sv   g[        U [        [        R                  45      (       a  [        U 5      v   g[	        [
        SSS9n[        5       R                  SU5        [        X5         Uv   [        R                  " [        5         [        R                  " U5        SSS5        g! , (       d  f       g= f! [        R                  " [        5         [        R                  " U5        SSS5        f ! , (       d  f       f = f= f7f)zContext manager for json files.Nr   .json)r5   r6   r7   zinput tempfile: %s)
isinstancestrr   PathLikerA   r
   r   debugr   
contextlibsuppressPermissionErrorremove)rH   	data_files     r2   _temp_single_jsonrT   j   s      |
$bkk*++$i&72gNIL+Y7I$!  1IIi  211Z  1IIi  211sN   A0D3C 7DB1(	D1
B?;DDC=4	D=
DDDinputbasec           	   #     #    [        U [        5      (       Ga&  [        [        SSSS9n[	        [        U 5      5       Vs/ s H.  n[        R                  R                  U5      S   SX1-    S3-   PM0     nn[        X5       Hr  u  pV[        U[        5      (       a  [        Xe5        M'  [        U[        5      (       a  [        R                  " XV5        MT  [        S[        [!        U5      5      -   5      e    Uv   U H<  n["        R$                  " [&        5         [        R(                  " U5        S S S 5        M>     g [+        U 5       S h  vN   g s  snf ! , (       d  f       Mi  = f! U HN  n["        R$                  " [&        5         [        R(                  " U5        S S S 5        M>  ! , (       d  f       MM  = f   f = f Nu7f)Nr   rJ   T)r5   r6   r7   r8   r   _z2A list of inits must contain dicts or strings, not)rK   listrA   r
   rangelenr   r   r   zipdictr   rL   shutilcopy
ValueErrortyperO   rP   rQ   rR   rT   )rU   rV   mother_filei	new_filesinitfiles          r2   _temp_multiinputrg      sg     % -7d

 3u:&
& GG[)!,16(%/@@& 	 
 e/JD$%%+D#&&D' H$t*o&  0	$!((9IIdO :9 " %U++++
$ :9 "((9IIdO :999 " 	,se   =G5E4BG8E* <GE2GG
G
E'	"G* G 
F+	!
G +
F:5G  GT)allow_multipleidinitsrh   ri   c             #      #    [        U [        [        45      (       a  U v   g U(       a  [        XS9 S h  vN   g [        U [        5      (       a  [        S5      e[        U 5       S h  vN   g  N9 N7f)N)rV   z(Expected single initialization, got list)rK   floatintrg   rY   r`   rT   )rj   rh   ri   s      r2   
temp_initsrn      sb      %%&&#E333eT""GHH$U+++	 	4 	,s!   4A4A03A4*A2+A42A4c                       \ rS rSrSr\R                  " S5      r\R                  " S5      r\	S\
S\4S j5       rS\
4S jrS\\
\4   4S	 jrSS jrSrg
)SanitizedOrTmpFilePath   zG
Context manager for tmpfiles, handles special characters in filepath.
z[\s~]z\s	file_pathr   c                     [         R                  " 5       S:X  a$  [        U R                  R	                  U5      5      $ [        U R
                  R	                  U5      5      $ )Nr   )r   r   boolWINDOWS_PATTERNsearchUNIXISH_PATTERN)clsrr   s     r2   _has_special_chars)SanitizedOrTmpFilePath._has_special_chars   sI    ??	)++229=>>C''..y9::r4   c                 .   S U l         U R                  [        R                  R	                  U5      5      (       a  [        R                  R                  [        R                  R	                  U5      5      u  p#[        R                  " USS9   [        U5      n[        R                  R                  U5      (       a  [        R                  R                  XC5      nU R                  [        R                  R	                  U5      5      (       ap  [        R                  " 5       nU R                  U5      (       a  [        S5      e[        R                  " SUS9u  pg[        R                  " X5        Xpl        XPl         g Xl        g ! [         a     Nf = f)NT)exist_okzUnable to generate temporary path without spaces or special characters! 
 Please move your stan file to a location without spaces or special characters.z.stan)r7   r5   )_tmpdirry   r   r   abspathr   makedirsr3   r   r)   r   r<   mkdtempmkstempr^   r_   _path)selfrr   r*   r+   r0   tmpdirrX   r   s           r2   __init__SanitizedOrTmpFilePath.__init__   s"   ""277??9#=>>#%77==1K#L IKK	D1"4Y"?77>>/22 "_ HI ""277??9#=>>%%'F&&v.."E  &&g6BGAKK	(J!L"J%   s   AF 
FFc                 6    U R                   U R                  S L4$ )Nr   r}   )r   s    r2   	__enter__ SanitizedOrTmpFilePath.__enter__   s    zz4<<t333r4   Nc                 f    U R                   (       a   [        R                  " U R                   SS9  g g )NT)ignore_errors)r}   r^   rmtree)r   exc_typeexc_valexc_tbs       r2   __exit__SanitizedOrTmpFilePath.__exit__   s     <<MM$,,d; r4   r   )r   N)__name__
__module____qualname____firstlineno____doc__recompilerw   ru   classmethodrL   rt   ry   r   r   r   r   __static_attributes__ r4   r2   rp   rp      sk     jj*Ojj'O;3 ;4 ; ;
## #<45d+ 4<r4   rp   )F)r   )%r   rO   r   r   r   r^   r<   typingr   r   r   r   r   r   r	   	cmdstanpyr
   jsonr   loggingr   r   	EXTENSIONrL   r3   rt   rA   contextmanagerrG   rM   rT   temp_single_jsonrm   rg   rl   rn   rp   r   r4   r2   <module>r      s    	  	   G G G  ! oo'94F"	7S 7S 7v ;@	#&37" 3 8D>  !
R[['#s("3T9
:!hsm!* ,,->? 
 ",bkk738#4d3iEF",
", hsm",J   ,R[['#s(+UCcDH,
 , 	, eCT)*+, ,&1< 1<r4   