
    3j                       d 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m	Z	 ddlm
Z
 ddlmZ ddlmZ  ej                  d      Zej                   r	 	 ddd	dd
	 	 	 	 	 	 	 ddZddZddZ	 d	 	 	 	 	 ddZ G d dej*                        Zy)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               @   t         d   }|st        d      |j                         st        d| dz         |sd}n	|d   xs d}| j                  d|z         }t	        |      | dt	        |      g}	|r|	j                  d	t	        |      g       ||	j                  d
t	        |      g       t        j                  d      }
t        j                  d      }t        j                         rt        j                         dk(  rS|
dt        j                  d<   |<t	        t        j                         j                               t        j                  d<   |*|	j                  d
t	        t        j                         g       t        j"                  j%                  |	       t        j                         rYt        j                         dk(  rB|
t        j                  j'                  d       |t        j                  j'                  d       |dk(  rt)        |      S |S )a  
    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               H/DATA/.local/lib/python3.12/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                   t        |       } | j                  d      st        d|        | ddt        d      z   }dD ]4  }t	        j
                  |dz   |z   dz         }|j                         s2|c S  t        d|  d| d	d
z         )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#   g   s     'lGF#=gYGHH$%6b3v;&67?ll#9C#?BR#RU[#[\ @
 
7):.D-EEYZ
(	) r/   c                   | j                   }|j                  d      st        d|        |j                  d      }|dk(  rt	        d|        |d| }t        t        | j                  j                  | d      d 	            d   }|S )
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                    | j                   S N)name)ps    r-   <lambda>z!findLastPNGPath.<locals>.<lambda>   s    !&&r/   )key)	rG   r:   r;   rfindr   listsortedparentglob)r@   
inputFpStrdash_location	base_name	last_names        r-   findLastPNGPathrT      s     Jv&=gYGHH$$S)M#@	J
 	
 >M*IVGNN//9+V0DE 02 3357Ir/   c                4   | j                   }|j                  d      st        d|        |t        |       }|j	                  d      }|dk(  rt        d|        d}t        |      |z
  |z
  dz
  }|j                   |dz   |dz   |z    }t        |      }||fS )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;   rT   rK   r<   int)firstFplastFprP   rQ   
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                      e Zd Zd Zy)Testc                @   t        j                         }dD ]  }d|z   dz   }|j                  dd      }|j                  d|      }t	        j
                  ||       t        |      }| j                  t        |      |       t	        j                  |        |j                  dd      }|j                  dd      }t	        j
                  ||       | j                  t              5  t        |       ddd       t	        j                  |       y# 1 sw Y   xY w)z
        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-   pngNumberingz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__rr    r/   r-   ra   ra      s    r/   ra   )rv   )r   z
int | Noner	   boolr
   rX   returnpathlib.Path)r@   zstr | pathlib.Pathrx   ry   )r@   ry   rx   ry   rF   )rY   ry   rZ   zpathlib.Path | Nonerx   ztuple[int, int])__doc__
__future__r   r   r=   typingtunittestmusic21r   r   r   music21.exceptions21r   r   r   TYPE_CHECKINGr.   r#   rT   r_   TestCasera   rv   r/   r-   <module>r      s    # 	       6&{&&'<=?? D D 
	D
 D D DR00 !%%%% %88 r/   