
    rhX                    (   S r SSKJ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	r	SSK
r
SSKrSSKJr  / SQr\R                   S 5       rSS jrSSS jjr\R(                  \R*                  S	.rS
 rS rS r\S:X  a  SSKr\R8                  " 5         gg)z
Tools for working with files
    )annotationsN)Music21Exception)readFileEncodingSafereadPickleGzipcdpreparePathClassesForUnpickling!restorePathClassesAfterUnpicklingrunSubprocessCapturingStderrc              #     #    [         R                  " 5       n [         R                  " U 5        Sv   [         R                  " U5        g! [         R                  " U5        f = f7f)z
Useful for a temporary cd for use in a `with` statement:

     with cd('/Library/'):
          os.system(make)

will switch temporarily, and then switch back when leaving.
N)osgetcwdchdir)	targetDircwds     R/home/james-whalen/.local/lib/python3.13/site-packages/music21/common/fileTools.pyr   r   '   s<      ))+C

s   A%A
 A%
A""A%c                H   [        5         [        R                  " U S5       n UR                  5       n[        R
                  " U5      n SSS5        [        5         W$ ! [         a'  n[        5         [        S[        U 5      -   5      UeSnAff = f! , (       d  f       NN= f)zZ
Read a gzip-compressed pickle file, uncompress it, unpickle it, and
return the contents.
rbzCannot load file N)
r   gzipopenreadpickleloads	Exceptionr	   r   str)filePathpickledFileuncompressednewMdbes        r   r   r   :   s    
 $%	8T	"k	O&++-L\\,/F 
# &'M  	O ./"#6X#FGQN	O	 
#	"s(   B&A
B)"BBB
B!c                    [         R                  " U SUS9 nUR                  5       nUsSSS5        $ ! , (       d  f       g= f! [         a}    SSKn[         R                  " U S5       nUR                  5       nUR                  U5      S   =(       d    Sn[        R                  " Xg5      sSSS5        s $ ! , (       d  f        g= ff = f)ax  
Slow, but will read a file of unknown encoding as safely as possible using
the chardet package.

Let's try to load this file as ascii -- it has a copyright symbol at the top,
so it won't load in Python3:

>>> import os
>>> c = str(common.getSourceFilePath() / 'common' / '__init__.py')
>>> #_DOCS_SHOW f = open(c)
>>> #_DOCS_SHOW data = f.read()
Traceback (most recent call last):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position ...:
    ordinal not in range(128)

That won't do! now I know that it is in utf-8, but maybe you don't. Or it could
be an old humdrum or Noteworthy file with unknown encoding.  This will load it safely.

>>> data = common.readFileEncodingSafe(c)
>>> data[0:30]
'# -*- coding: utf-8 -*-\n# ----'

Well, that's nothing, since the first guess here is utf-8, and it's right. So let's
give a worse first guess:

>>> data = common.readFileEncodingSafe(c, firstGuess='SHIFT_JIS')  # old Japanese standard
>>> data[0:30]
'# -*- coding: utf-8 -*-\n# ----'

It worked!

Note that this is slow enough if it gets it wrong that the firstGuess should be set
to something reasonable like 'ascii' or 'utf-8'.
r)encodingNr   r   r"   ascii)ior   r   UnicodeDecodeErrorchardetdetectcodecsdecode)r   
firstGuessthisFiledatar&   thisFileBinary
dataBinaryr"   s           r   r   r   N   s    H	7WWXsZ8H==?D 988  7WWXt$',,.J~~j1*=HH==6 %$$$7sC   A 4	A 
AA A %C*AB9-
C9
C	CC)	posixPathwindowsPathc                     SSK Jn   U " 5       nUS:X  a  [        R                  [        l        g[        R                  [        l        g)z
When we need to unpickle a function that might have relative paths
(like some music21 stream options), Windows chokes if the PosixPath
is not defined, but usually can still unpickle easily.
r   getPlatformwinN)music21.common.miscr3   pathlibWindowsPath	PosixPathr3   platforms     r   r   r      s2     0}H5#//%//    c                 t    SSK Jn   U " 5       nUS:X  a  [        S   [        l        g[        S   [        l        g)z(
After unpickling, leave pathlib alone.
r   r2   r4   r/   r0   N)r5   r3   _storedPathlibClassesr6   r8   r7   r9   s     r   r	   r	      s0     0}H51+>3MBr;   c                   [         R                  " U SSS9nUR                  S:w  a:  UR                  n SSKnUR                  UR                  SS95      n[        U5      eg! [         a    UR                  SSS	9n N(f = f)
z_
Run a subprocess command, capturing stderr and
only show the error if an exception is raised.
TF)capture_outputcheckr   N)do_setlocaler#   ignore)errors)	
subprocessrun
returncodestderrlocaler)   getpreferredencodingr%   IOError)subprocessCommandcompleted_processstderr_bytesrH   
stderr_strs        r   r
   r
      s    
 #'8UZ[##q( )//	G%,,V-H-HV[-H-\]J j!! ) " 	G%,,WX,FJ	Gs   "A" "A?>A?__main__)r   zstr | pathlib.Pathreturnzt.Any)zutf-8)rP   r   )__doc__
__future__r   r(   
contextlibr   r$   r6   r   r   rD   typingtmusic21.exceptions21r   __all__contextmanagerr   r   r   r8   r7   r=   r   r	   r
   __name__music21mainTest r;   r   <module>r]      s    #    	   	   1  $(-7b '.&7&7H[H[\ 0	C"( z r;   