
    3j)                        d Z ddlmZ g dZddlmZ ddlZddlZej                  rddl
mZ g dZg dZg d	Zdd
Zd Zedd       Zd Zd Zd Zedk(  rddlZ ej.                          yy)zQ
Utilities for working with file formats.

almost everything here is deprecated.
    )annotations)	findSubConverterForFormat
findFormatfindInputExtensionfindFormatFilefindFormatExtFilefindFormatExtURLVALID_SHOW_FORMATSVALID_WRITE_FORMATSVALID_AUTO_DOWNLOAD)cacheN)SubConverter)musicxmllilypondtexttextlinemidipngpdfsvglily.pdflily.pnglily.svgbraillevexflowvexflow.html
vexflow.jsipythonipython.pngipython.midimusicxml.pngmusicxml.pdf)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   rntxt)askdenyallowc                    | j                         j                         } ddlm} |j	                         j                         }|D ]  }|j                  }| |v s|c S  y)a  
    return a converter.subConverter.SubConverter subclass
    for a given format -- this is a music21 format name,
    not a file extension. Or returns None

    >>> common.findSubConverterForFormat('musicxml')
    <class 'music21.converter.subConverters.ConverterMusicXML'>

    >>> common.findSubConverterForFormat('text')
    <class 'music21.converter.subConverters.ConverterText'>

    >>> common.findSubConverterForFormat('romantext')
    <class 'music21.converter.subConverters.ConverterRomanText'>

    Some subConverters have format aliases

    >>> common.findSubConverterForFormat('t')
    <class 'music21.converter.subConverters.ConverterText'>

    r   	converterN)lowerstripmusic21r)   	ConvertersubConvertersListregisterFormats)fmtr)   sclscformatss        C/DATA/.local/lib/python3.12/site-packages/music21/common/formats.pyr   r   ;   sX    * ))+


C!




1
1
3C$$'>I      c                   ddl m} |j                         }|j                  |       }|y|j	                         }||   }|j
                  rd|j
                  d   z   }||fS |j                  rd|j                  d   z   }||fS d}||fS )a  
    Given a format defined either by a format name, abbreviation, or
    an extension, return the regularized format name as well as
    the output extensions.

    DEPRECATED May 2014 -- moving to converter

    All but the first element of the tuple are deprecated for use, since
    the extension can vary by subConverter (e.g., lily.png)

    >>> common.findFormat('.mxl')
    ('musicxml', '.musicxml')
    >>> common.findFormat('musicxml')
    ('musicxml', '.musicxml')
    >>> common.findFormat('lily')
    ('lilypond', '.ly')
    >>> common.findFormat('lily.png')
    ('lilypond', '.ly')
    >>> common.findFormat('humdrum')
    ('humdrum', '.krn')
    >>> common.findFormat('txt')
    ('text', '.txt')
    >>> common.findFormat('textline')
    ('textline', '.txt')
    >>> common.findFormat('midi')
    ('midi', '.mid')
    >>> common.findFormat('abc')
    ('abc', '.abc')
    >>> common.findFormat('scl')
    ('scala', '.scl')
    >>> common.findFormat('braille')
    ('braille', '.txt')
    >>> common.findFormat('vexflow')
    ('vexflow', '.html')
    >>> common.findFormat('capx')
    ('capella', '.capx')

    Works the same whether you have a leading dot or not:

    >>> common.findFormat('md')
    ('musedata', '.md')
    >>> common.findFormat('.md')
    ('musedata', '.md')

    If you give something we can't deal with, returns a Tuple of None, None:

    >>> common.findFormat('wpd')
    (None, None)
    r   r(   NNN.)r,   r)   r-   regularizeFormatgetSubConverterFormatsregisterOutputExtensionsregisterInputExtensions)r0   r)   c
fileFormatscfr2   firstOutputs          r4   r   r   [   s    ~ "A##C(J

"
"
$C	ZB	""B77:: {"" 
	#	#B66q99 {"" {""r5   c                &   ddl m} | j                         j                         } | j	                  d      r| dd } t        |       }|g }|j                         j                         D ]X  }| |j                  vr|j                  D ])  }|j	                  d      sd|z   }|j                  |       + |sMt        |      c S  t        |      S g }|j                  D ])  }|j	                  d      sd|z   }|j                  |       + t        |      S )ab  
    Will be fully deprecated when there's an exact equivalent in converter.

    Given an input format or music21 format, find and return all possible
    input extensions.

    >>> a = common.findInputExtension('musicxml')
    >>> a
    ('.xml', '.mxl', '.musicxml')
    >>> a = common.findInputExtension('humdrum')
    >>> a
    ('.krn',)
    >>> common.findInputExtension('musedata')
    ('.md', '.musedata', '.zip')

    Leading dots don't matter:

    >>> common.findInputExtension('.mxl')
    ('.xml', '.mxl', '.musicxml')

    Blah is not a format

    >>> common.findInputExtension('blah')
    ()
    r   r(   r8      N)r,   r)   r*   r+   
startswithr   r-   r.   r<   appendtuple)r0   r)   r2   postexts        r4   r   r      s   6 "
))+


C
~~c!"g	"3	'B	z%%'99;B"44411~~c*)CC  2 T{" < T{ --C>>#&CiKK . T{r5   c                    t        | t        j                        st        j                  |       } t        | j                        \  }}|S )a  
    Given a file path (relative or absolute) return the format

    DEPRECATED May 2014 -- moving to converter

    >>> common.findFormatFile('test.xml')
    'musicxml'
    >>> common.findFormatFile('long/file/path/test-2009.03.02.xml')
    'musicxml'
    >>> common.findFormatFile('long/file/path.intermediate.png/test-2009.03.xml')
    'musicxml'

    On a windows networked filesystem
    >>> common.findFormatFile(r'\\long\file\path\test.krn')
    'humdrum'
    
isinstancepathlibPathr   suffix)fpr0   
unused_exts      r4   r   r      s7    " b',,'\\" +OCJr5   c                    t        | t        j                        st        j                  |       } t        | j                        \  }}|y|| j                  fS )a  
    Given a file path (relative or absolute)
    find format and extension used (not the output extension)

    DEPRECATED May 2014 -- moving to converter

    >>> common.findFormatExtFile('test.mxl')
    ('musicxml', '.mxl')
    >>> common.findFormatExtFile('long/file/path/test-2009.03.02.xml')
    ('musicxml', '.xml')
    >>> common.findFormatExtFile('long/file/path.intermediate.png/test-2009.03.xml')
    ('musicxml', '.xml')

    >>> common.findFormatExtFile('test')
    (None, None)

    Windows drive
    >>> common.findFormatExtFile('d:/long/file/path/test.xml')
    ('musicxml', '.xml')

    On a windows networked filesystem
    >>> common.findFormatExtFile(r'\\long\file\path\test.krn')
    ('humdrum', '.krn')
    r7   rI   )rN   r>   unused_extOuts      r4   r   r      sK    2 b',,'\\" *299 5JBII&&r5   c                   ddl m} d}d| v rd}ncd| v rd}n\d| v sd	| v rd
}nQ|j                         j                         D ]0  }|j                  }|D ]  }| j                  d|z         sd|z   } 0 2 |t        |      \  }}||fS y)aN  
    Given a URL, attempt to find the extension.
    This may scrub arguments in a URL, or simply look at the last characters.

    DEPRECATED May 2014 -- moving to converter

    >>> urlA = 'http://somesite.com/?l=cc/schubert/piano/d0576&file=d0576-06.krn&f=xml'
    >>> urlB = 'http://somesite.com/cgi-bin/ksdata?l=cc/schubert/d0576&file=d0576-06.krn&f=kern'
    >>> urlC = 'http://somesite.com/cgi-bin/ksdata?l=cc/bach/cello&file=bwv1007-01.krn&f=xml'
    >>> urlF = 'https://junk'
    >>> urlM = 'http://somesite.com/files/mid001.mid'

    >>> common.findFormatExtURL(urlA)
    ('musicxml', '.xml')
    >>> common.findFormatExtURL(urlB)
    ('humdrum', '.krn')
    >>> common.findFormatExtURL(urlC)
    ('musicxml', '.xml')
    >>> common.findFormatExtURL(urlF)
    (None, None)
    >>> common.findFormatExtURL(urlM)
    ('midi', '.mid')
    r   r(   Nz=xmlz.xmlz=kernz.krnzformat=stage2zformat=stage1z.mdr8   r7   )r,   r)   r-   r.   r<   endswithr   )urlr)   rG   r2   
inputTypes	extSampler>   unused_junks           r4   r	   r	   %  s    0 "
C}	C	C	?c#9%%'99;B33J'	<<i0	/C ( < ",S/
K3r5   __main__)r0   strreturnztype[SubConverter] | None)r0   rY   rZ   ztuple[str, ...])__doc__
__future__r   __all__	functoolsr   rK   typingtTYPE_CHECKINGmusic21.converter.subConvertersr   r
   r   r   r   r   r   r   r   r	   __name__r,   mainTest r5   r4   <module>rf      s   
 #
    ??<
6    / 
@N#d 4 4r2 'J/d zG r5   