
    rh                    .   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J	r	  SSKJ
r
  SSKJr  SSKJr  \R                  " S5      r\R                   (       a    SSS	SS
.       SS jjjrSS jrSS jr S     SS jjr " S S\R*                  5      rg)zz
This module contains tools for using MuseScore to do conversions
from music21.  It was formerly mostly in subConverters.
    )annotationsN)common)defaults)environment)SubConverterExceptionzconverter.museScoreT)dpi	trimEdgesleaveMarginc                  [         S   nU(       d  [        S5      eUR                  5       (       d  [        SU S3-   5      eU(       d  SnOUS   =(       d    SnU R                  SU-   5      n[	        U5      U S[	        U5      /n	U(       a  U	R                  S	[	        U5      /5        Ub  U	R                  S
[	        U5      /5        [        R                  " S5      n
[        R                  " S5      n[        R                  " 5       (       a  [        R                  " 5       S:X  aV  U
c  S[        R                  S'   Uc=  [	        [        R                  " 5       R                  5       5      [        R                  S'   Uc*  U	R                  S
[	        [        R                   5      /5        [        R"                  R%                  U	5        [        R                  " 5       (       a]  [        R                  " 5       S:X  aD  U
c  [        R                  R'                  S5        Uc  [        R                  R'                  S5        US:X  a  [)        U5      $ U$ )ae  
Take the output of the conversion process and run it through MuseScore to convert it
to a png.

* dpi: specifies the dpi of the output file.  If None, then the default is used.
* trimEdges: if True (default) the image is trimmed to the edges of the music.
* leaveMargin: if trimEdges is True, then this number of pixels is left around the
  trimmed image.
musescoreDirectPNGPathzTo create PNG files directly from MusicXML you need to download MuseScore and put a link to it in your .music21rc via Environment.z+Cannot find a path to the 'mscore' file at z -- download MuseScorepngr   .z-oz-Tz-rQT_QPA_PLATFORMXDG_RUNTIME_DIRnix	offscreen)environLocalr   existswith_suffixstrextendosgetenvr   runningInNotebookgetPlatformenvironr   EnvironmentgetRootTempDirr   jupyterImageDpi	fileToolsrunSubprocessCapturingStderrpopfindNumberedPNGPath)fp
subformatsr   r	   r
   keywordsmuseScorePathsubformatExtensionfpOutmuseScoreRunprior_qt	prior_xdgs               U/home/james-whalen/.local/lib/python3.13/site-packages/music21/converter/museScore.pyrunThroughMuseScorer.      s   $ !!9:M#EF 	F !!#95678 	8 "']3eNN3!334E&D#e*=LT3{#345
T3s8,-yy*+H		+,I!!5( 0;

,- 03K4K4K4M4\4\4^0_

,-;s8+C+C'D EF
11,?!!f&8&8&:e&CJJNN,-JJNN,-U""5))    c                .   [        U 5      n U R                  S5      (       d  [        SU  35      eU SS[        S5      -   nS H;  n[        R
                  " US-   U-   S-   5      nUR                  5       (       d  M9  Us  $    [        SU  SU S	3S
-   5      e)z
Find the first numbered file path corresponding to the provided unnumbered file path
ending in ".png". Raises an exception if no file can be found.

Renamed in v7.  Returns a pathlib.Path
.png"inputFp must end with ".png"; got N)101001000100001-zNo png file for z
 (such as z-1.png) was found.  zThe conversion to png failed)r   endswith
ValueErrorlenpathlibPathr   IOError)inputFppath_without_extensionsearch_extensionsearch_paths       r-   r#   r#   h   s     'lGF##=gYGHH$%6b3v;&67?ll#9C#?BR#RU[#[\ @
 
7):.D-EEYZ
(	) r/   c                    U R                   nUR                  S5      (       d  [        SU  35      eUR                  S5      nUS:X  a  [	        SU  35      eUSU n[        [        U R                  R                  U S35      S S	95      S   nU$ )
z
Find the last numbered file path corresponding to the provided NUMBERED file path
ending in ".png". Raises an exception if no file can be found.

For instance, if there was a file named abc-01.png it might find abc-22.png.
r1   r2   r9   r3   z2inputFp must end with "-0001.png" or similar; got Nz-*.pngc                    U R                   $ N)name)ps    r-   <lambda>!findLastPNGPath.<locals>.<lambda>   s    !&&r/   )key)	rG   r:   r;   rfindr   listsortedparentglob)r@   
inputFpStrdash_location	base_name	last_names        r-   findLastPNGPathrU      s     Jv&&=gYGHH$$S)M#@	J
 	
 >M*IVGNN//9+V0DE 02 3357Ir/   c                @   U R                   nUR                  S5      (       d  [        SU  35      eUc  [        U 5      nUR	                  S5      nUS:X  a  [        SU  35      eSn[        U5      U-
  U-
  S-
  nUR                   US-   US-   U-    n[        U5      nXu4$ )z
Return a 2-tuple of the maximum PNG number and the number of digits used to
specify the PNG (for MuseScore generated PNGs, for use in the widget)
r1   z"firstFp must end with ".png"; got r9   r3   z2firstFp must end with "-0001.png" or similar; got       )rG   r:   r;   rU   rL   r<   int)firstFplastFprQ   rR   
suffix_len
num_digits
num_stringlast_numbers           r-   findPNGRanger`      s     Jv&&=gYGHH~ )$$S)M@	J
 	
 JZ=0:=AJ]Q.}q/@:/MNJj/K$$r/   c                      \ rS rSrS rSrg)Test   c                H   [         R                  " 5       nS H  nSU-   S-   nUR                  SSS9nUR                  SU5      n[        R
                  " XE5        [        U5      nU R                  [        U5      U5        [        R                  " U5        M     UR                  SSS9nUR                  SS5      n[        R
                  " XE5        U R                  [        5         [        U5        SSS5        [        R                  " U5        g! , (       d  f       N%= f)zt
Testing findNumberedPNGPath() with files of lengths
that create .png files with -1, -01, -001, and -0001 in the fp
)r4   r5   r6   r7   r9   r1   F)suffixreturnPathlibz-0000001.pngN)r   r   getTempFilereplacer   renamer#   assertEqualr   removeassertRaisesr?   )selfenvext_basepng_exttmptmpNumberedpngFp1s          r-   pngNumberingTest.pngNumbering   s    
 %%'0HHnv-G//u/EC++fg6KIIc'(-FS[+6IIk" 1 ooV5oAkk&.9
		##w', (
		+ ('s   (D
D! N)__name__
__module____qualname____firstlineno__rt   __static_attributes__rv   r/   r-   rb   rb      s    r/   rb   )rv   )r   z
int | Noner	   boolr
   rY   returnpathlib.Path)r@   zstr | pathlib.Pathr}   r~   )r@   r~   r}   r~   rF   )rZ   r~   r[   zpathlib.Path | Noner}   ztuple[int, int])__doc__
__future__r   r   r=   typingtunittestmusic21r   r   r   music21.exceptions21r   r   r   TYPE_CHECKINGr.   r#   rU   r`   TestCaserb   rv   r/   r-   <module>r      s    # 	       6&&'<=?? D D 
	D
 D D DR00 !%%%% %88 r/   