
    ^j!                        d Z ddlZddlZddlZddlmZmZmZ ddlZddl	m
Z
 deee
f         defdZdeeee
f                  defdZd	edefd
Zd	edefdZddedeegef         fdZdedefdZdedefdZ G d d          Z G d d          Z G d d          Z G d de          Z G d de          ZdS )z4Various utility functions used internally by pyglet
    N)OptionalUnionCallable)Buffersreturnc                     t          | t                    r| S t          | t                    rt          d | D                       S t          |           S )Nc              3   4   K   | ]}t          |          V  d S N)ord).0cs     F/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/util.py	<genexpr>zasbytes.<locals>.<genexpr>   s(      ''SVV''''''    )
isinstancebytesstrr   s    r   asbytesr      sS    !U 	As		 ''Q''''''Qxxr   c                 b    | dS t          | t                    r| S |                     d          S )N zutf-8)r   r   decoder   s    r   asstrr      s5    yr!S 88Gr   argc                 $    t          |            dS NT)printr   s    r   _debug_print_realr        s    	#JJJ4r   c                     dS r    r   s    r   _debug_print_dummyr#   %   s    4r   debugpyglet_option_namec                 b    t           j                            | d          }|rt          S t          S )a  Get a debug printer controlled by the given ``pyglet.options`` name.

    This allows repurposing ``assert`` to write cleaner, more efficient
    debug output:

    #. Debug printers fit into a one-line ``assert`` statements instead
       of longer, slower key-lookup ``if`` statements
    #. Running Python with the ``-O`` flag makes pyglet run faster by
       skipping all ``assert`` statements

    Usage example::

        from pyglet.util import debug_print


        _debug_media = debug_print('debug_media')


        def some_func():
            # Python will skip the line below when run with -O
            assert _debug_media('My debug statement')

            # The rest of the function will run as normal
            ...

    For more information, please see `the Python command line
    documentation <https://docs.python.org/3/using/cmdline.html#cmdoption-O>`_.

    Args:
        `pyglet_option_name` :
            The name of a key in :attr:`pyglet.options` to read the
            debug flag's value from.

    Returns:
        A callable which prints a passed string and returns ``True``
        to allow auto-removal when running with ``-O``.

    F)pygletoptionsgetr    r#   )r%   enableds     r   debug_printr+   )   s1    N n  !3U;;G !  r   xc                     | dk    rdS | |                                  dz
  z	  dz  r)dt          j        t          j        |                     z  S dt          j        t          j        |                     z  S )N      )
bit_lengthmathceillog2floorr,   s    r   closest_power_of_twor6   W   si    Avvq	allnnq !Q& -DIdill++++DJty||,,,,r   c                 d    | dk    rdS dt          j        t          j        |                     z  S )Nr/   )r1   r2   r3   r5   s    r   next_or_equal_power_of_twor8   `   s-    Avvq	$)A,,''''r   c                   B    e Zd ZdZd Zd
dZd
dZd Zd Zd Z	d
d	Z
dS )CodecRegistryz9Utility class for handling adding and querying of codecs.c                 >    g | _         g | _        i | _        i | _        d S r   )	_decoders	_encoders_decoder_extensions_encoder_extensionsselfs    r   __init__zCodecRegistry.__init__i   s&    #% #%   r   Nc                     |rUt           j                            |          \  }}|r|n|}| j                            |                                g           S | j        S )zGet a list of all encoders. If a `filename` is provided, only
        encoders supporting that extension will be returned. An empty list
        will be return if no encoders for that extension are available.
        )ospathsplitextr?   r)   lowerr=   rA   filenamerootext	extensions        r   get_encoderszCodecRegistry.get_encoderso   _    
  	G((22ID#",I+//	0A0A2FFF~r   c                     |rUt           j                            |          \  }}|r|n|}| j                            |                                g           S | j        S )zGet a list of all decoders. If a `filename` is provided, only
        decoders supporting that extension will be returned. An empty list
        will be return if no encoders for that extension are available.
        )rD   rE   rF   r>   r)   rG   r<   rH   s        r   get_decoderszCodecRegistry.get_decodersz   rN   r   c                     |                                 D ]f}| j                            |           |                                D ]5}|| j        vr
g | j        |<   | j        |                             |           6gdS )zAdd a decoder module.  The module must define `get_decoders`.  Once
        added, the appropriate decoders defined in the codec will be returned by
        CodecRegistry.get_decoders.
        N)rP   r<   appendget_file_extensionsr>   )rA   moduledecoderrL   s       r   add_decoderszCodecRegistry.add_decoders       
 **,, 	D 	DGN!!'***$88:: D D	D$<<<:<D,Y7(3::7CCCCD	D 	Dr   c                     |                                 D ]f}| j                            |           |                                D ]5}|| j        vr
g | j        |<   | j        |                             |           6gdS )zAdd an encoder module.  The module must define `get_encoders`.  Once
        added, the appropriate encoders defined in the codec will be returned by
        CodecRegistry.get_encoders.
        N)rM   r=   rR   rS   r?   )rA   rT   encoderrL   s       r   add_encoderszCodecRegistry.add_encoders   rW   r   c                    d}|                      |          D ]E}	  |j        ||fi |c S # t          $ r%}|s|}|r|                    d           Y d}~>d}~ww xY w|                                  D ]:}	  |j        ||fi |c S # t          $ r |r|                    d           Y 7w xY w|st          d|           |)zAttempt to decode a file, using the available registered decoders.
        Any decoders that match the file extension will be tried first. If no
        decoders match the extension, all decoders will then be tried in order.
        Nr   z*No decoders available for this file type: )rP   r   DecodeExceptionseek)rA   rI   filekwargsfirst_exceptionrU   es          r   r   zCodecRegistry.decode   s5   
 ((22 	! 	!G!%w~h???????" ! ! !& (&'O !IIaLLL	! ((** 	! 	!G!%w~h???????" ! ! ! !IIaLLL!  	[!"Yx"Y"YZZZs&   ,
AAA5B!B*)B*c                     d}|                      |          D ]/}	  |j        |||fi |c S # t          $ r}|p|}Y d}~(d}~ww xY w|st          d| d          |)zAttempt to encode a pyglet object to a specified format. All registered
        encoders that advertise support for the specific file extension will be tried.
        If no encoders are available, an EncodeException will be raised.
        Nz/No Encoders are available for this extension: '')rM   encodeEncodeException)rA   mediarI   r^   r_   r`   rY   ra   s           r   rd   zCodecRegistry.encode   s     ((22 	7 	7G7%w~eXtFFvFFFFF" 7 7 7"1"6Q7  	a!"_T\"_"_"_```s   -
AA  Ar   )__name__
__module____qualname____doc__rB   rM   rP   rV   rZ   r   rd   r"   r   r   r:   r:   f   s        CC& & &	 	 	 		 	 	 	
D 
D 
D
D 
D 
D  6     r   r:   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )Decoderc                     t                      zkReturn a list or tuple of accepted file extensions, e.g. ['.wav', '.ogg']
        Lower-case only.
        NotImplementedErrorr@   s    r   rS   zDecoder.get_file_extensions        "###r   c                     t                      )zRead and decode the given file object and return an appropriate
        pyglet object. Throws DecodeException if there is an error.
        `filename` can be a file type hint.
        ro   )rA   argsr_   s      r   r   zDecoder.decode   s    
 "###r   c                 4    t          | j        j                  S r   hash	__class__rg   r@   s    r   __hash__zDecoder.__hash__       DN+,,,r   c                 6    | j         j        |j         j        k    S r   rw   rg   rA   others     r   __eq__zDecoder.__eq__       ~&%/*BBBr   c                 f    d                     | j        j        |                                           S Nz{0}{1}formatrw   rg   rS   r@   s    r   __repr__zDecoder.__repr__   '    t~68P8P8R8RSSSr   N)rg   rh   ri   rS   r   rx   r~   r   r"   r   r   rl   rl      sg        $ $ $$ $ $- - -C C CT T T T Tr   rl   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )Encoderc                     t                      rn   ro   r@   s    r   rS   zEncoder.get_file_extensions   rq   r   c                     t                      )zEncode the given media type to the given file.  `filename`
        provides a hint to the file format desired.  options are
        encoder-specific, and unknown options should be ignored or
        issue warnings.
        ro   )rA   rf   rI   r^   s       r   rd   zEncoder.encode   s     "###r   c                 4    t          | j        j                  S r   ru   r@   s    r   rx   zEncoder.__hash__   ry   r   c                 6    | j         j        |j         j        k    S r   r{   r|   s     r   r~   zEncoder.__eq__   r   r   c                 f    d                     | j        j        |                                           S r   r   r@   s    r   r   zEncoder.__repr__   r   r   N)rg   rh   ri   rS   rd   rx   r~   r   r"   r   r   r   r      sg        $ $ $$ $ $- - -C C CT T T T Tr   r   c                       e Zd ZdZdS )r\   r:   Nrg   rh   ri   r"   r   r   r\   r\               JJJr   r\   c                       e Zd ZdZdS )re   r:   Nr   r"   r   r   re   re      r   r   re   )r$   )rj   rD   r1   systypingr   r   r   r'   pyglet.customtypesr   r   r   r   r   boolr    r#   r+   intr6   r8   r:   rl   r   	Exceptionr\   re   r"   r   r   <module>r      sj    
			  



 , , , , , , , , , ,  % % % % % %uS&[! e    XeCK() c    3 4    
C D    * *C *hud{6K * * * *\-C -C - - - -(# (# ( ( ( (b b b b b b b bJT T T T T T T T0T T T T T T T T2! ! ! ! !i ! ! !! ! ! ! !i ! ! ! ! !r   