
    \
jxo                        d Z ddlZddlZddlZddlZddlZddlZ G d de          Zd Z	d Z
 G d d          Z G d	 d
e          Z G d de          Z G d de          Z G d d          Zg a G d de          Z e            Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej        Zej         Z ej!        Z!ej"        Z"dS )a  Load application resources from a known path.

Loading resources by specifying relative paths to filenames is often
problematic in Python, as the working directory is not necessarily the same
directory as the application's script files.

This module allows applications to specify a search path for resources.
Relative paths are taken to be relative to the application's ``__main__``
module. ZIP files can appear on the path; they will be searched inside.  The
resource module also behaves as expected when applications are bundled using
Freezers such as PyInstaller, py2exe, py2app, etc..

In addition to providing file references (with the :py:func:`file` function),
the resource module also contains convenience functions for loading images,
textures, fonts, media and documents.

3rd party modules or packages not bound to a specific application should
construct their own :py:class:`Loader` instance and override the path to use the
resources in the module's directory.

Path format
^^^^^^^^^^^

The resource path :py:attr:`path` (see also :py:meth:`Loader.__init__` and
:py:meth:`Loader.path`)
is a list of locations to search for resources.  Locations are searched in the
order given in the path.  If a location is not valid (for example, if the
directory does not exist), it is skipped.

Locations in the path beginning with an "at" symbol (''@'') specify
Python packages.  Other locations specify a ZIP archive or directory on the
filesystem.  Locations that are not absolute are assumed to be relative to the
script home.  Some examples::

    # Search just the `res` directory, assumed to be located alongside the
    # main script file.
    path = ['res']

    # Search the directory containing the module `levels.level1`, followed
    # by the `res/images` directory.
    path = ['@levels.level1', 'res/images']

Paths are always **case-sensitive** and **forward slashes are always used**
as path separators, even in cases when the filesystem or platform does not do this.
This avoids a common programmer error when porting applications between platforms.

The default path is ``['.']``.  If you modify the path, you must call
:py:func:`reindex`.

.. versionadded:: 1.1
    Nc                       e Zd ZdZd ZdS )ResourceNotFoundExceptionz4The named resource was not found on the search path.c                 F    d|z  }t                               | |           d S )NzbResource "%s" was not found on the path.  Ensure that the filename has the correct captialisation.)	Exception__init__)selfnamemessages      I/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/resource.pyr   z"ResourceNotFoundException.__init__d   s/    NQUV4)))))    N)__name__
__module____qualname____doc__r    r   r   r   r   a   s)        >>* * * * *r   r   c                     t          t          dd          } t          t          dd          }|r|S | dv r)t          j                            t          j                  S | dk    rt          j        d         S t          j        d         }t          |d          rAt          j                            t          j        	                    |j
                            S d	t          j                            t          j                  v rt          j                    S t          j                            t          j                  S )
a2  Get the directory containing the program entry module.

    For ordinary Python scripts, this is the directory containing the
    ``__main__`` module.  For executables created with py2exe the result is
    the directory containing the running executable file.  For OS X bundles
    created using Py2App the result is the Resources directory within the
    running bundle.

    If none of the above cases apply and the file for ``__main__`` cannot
    be determined the working directory is returned.

    When the script is being run by a Python profiler, this function
    may return the directory where the profiler is running instead of
    the directory of the real script. To workaround this behaviour the
    full path to the real script can be specified in :py:attr:`pyglet.resource.path`.

    :rtype: str
    frozenN_MEIPASS)windows_execonsole_exe
macosx_appRESOURCEPATH__main____file__python)getattrsysospathdirname
executableenvironmoduleshasattrabspathr   basenamegetcwd)r   meipassmains      r   get_script_homer*   j   s    & S(D))Fc:t,,G 7	1	1	1ws~...	<		z.)){:&4$$ 	77??27??4=#A#ABBB27++CN;;;;y{{" ws~666r   c                    t           j        dv r`dt          j        v r0t          j                            t          j        d         |           S t          j                            d| z            S t           j        dk    r"t          j                            d| z            S t           j                            d          r`dt          j        v r0t          j                            t          j        d         |           S t          j                            d| z            S t          j                            d	| z            S )
a  Get a directory to save user preferences.

    Different platforms have different conventions for where to save user
    preferences, saved games, and settings.  This function implements those
    conventions.  Note that the returned path may not exist: applications
    should use ``os.makedirs`` to construct it if desired.

    On Linux, a directory `name` in the user's configuration directory is
    returned (usually under ``~/.config``).

    On Windows (including under Cygwin) the `name` directory in the user's
    ``Application Settings`` directory is returned.

    On Mac OS X the `name` directory under ``~/Library/Application Support``
    is returned.

    :Parameters:
        `name` : str
            The name of the application.

    :rtype: str
    )cygwinwin32APPDATAz~/%sdarwinz ~/Library/Application Support/%slinuxXDG_CONFIG_HOMEz~/.config/%sz~/.%s)pygletcompat_platformr   r"   r   join
expanduser
startswith)r	   s    r   get_settings_pathr7      s    0 !444
""7<<
9 5t<<<7%%ftm444		8	+	+w!!"Dt"KLLL			*	*7	3	3 2
**7<<
+< =tDDD7%%nt&;<<<w!!'D.111r   c                       e Zd ZdZddZdS )Locationa  Abstract resource location.

    Given a location, a file can be loaded from that location with the `open`
    method.  This provides a convenient way to specify a path to load files
    from, and not necessarily have that path reside on the filesystem.
    rbc                      t          d          )a  Open a file at this location.

        :Parameters:
            `filename` : str
                The filename to open.  Absolute paths are not supported.
                Relative paths are not supported by most locations (you
                should specify only a filename with no path component).
            `mode` : str
                The file mode to open with.  Only files opened on the
                filesystem make use of this parameter; others ignore it.

        :rtype: file object
        abstract)NotImplementedErrorr   filenamemodes      r   openzLocation.open   s     "*---r   Nr:   )r   r   r   r   rA   r   r   r   r9   r9      s2         . . . . . .r   r9   c                        e Zd ZdZd ZddZdS )FileLocationz Location on the filesystem.
    c                     || _         dS )zCreate a location given a relative or absolute path.

        :Parameters:
            `path` : str
                Path on the filesystem.
        Nr   )r   r   s     r   r   zFileLocation.__init__   s     			r   r:   c                 h    t          t          j                            | j        |          |          S N)rA   r   r   r4   r>   s      r   rA   zFileLocation.open   s$    BGLLH55t<<<r   NrB   r   r   r   r   r   rA   r   r   r   rD   rD      sA           = = = = = =r   rD   c                        e Zd ZdZd ZddZdS )ZIPLocationz Location within a ZIP file.
    c                 "    || _         || _        dS )ai  Create a location given an open ZIP file and a path within that
        file.

        :Parameters:
            `zip` : ``zipfile.ZipFile``
                An open ZIP file from the ``zipfile`` module.
            `dir` : str
                A path within that ZIP file.  Can be empty to specify files at
                the top level of the ZIP file.

        N)zipdir)r   rM   rN   s      r   r   zZIPLocation.__init__   s     r   r:   c                     | j         r| j         dz   |z   }n|}|                    t          j        d          }| j                            |          }t          j        |          S )N/)rN   replacer   seprM   readioBytesIO)r   r?   r@   r   forward_slash_pathtexts         r   rA   zZIPLocation.open   s]    8 	8c>H,DDD!\\"&#66x}}/00z$r   NrB   rI   r   r   r   rK   rK      sA                      r   rK   c                        e Zd ZdZd ZddZdS )URLLocationzLocation on the network.

    This class uses the ``urlparse`` and ``urllib2`` modules to open files on
    the network given a URL.
    c                     || _         dS )zCreate a location given a base URL.

        :Parameters:
            `base_url` : str
                URL string to prepend to filenames.

        N)base)r   base_urls     r   r   zURLLocation.__init__  s     			r   r:   c                     dd l }dd l}|j                            | j        |          }|j                            |          S )Nr   )urllib.parseurllib.requestparseurljoinr[   requesturlopen)r   r?   r@   urlliburls        r   rA   zURLLocation.open  sI    ++++++++l""49h77~%%c***r   NrB   rI   r   r   r   rY   rY     sA           + + + + + +r   rY   c                       e Zd ZdZddZd Zd Zd Zd Zdd	Z	d
 Z
d Zd Zd Zd dZd!dZd Zd Zd Zd"dZd Zd#dZd Zd Zd Zd ZdS )$Loadera  Load program resource files from disk.

    The loader contains a search path which can include filesystem
    directories, ZIP archives and Python packages.

    :Ivariables:
        `path` : list of str
            List of search locations.  After modifying the path you must
            call the `reindex` method.
        `script_home` : str
            Base resource location, defaulting to the location of the
            application script.

    Nc                 >   |dg}t          |t                    r|g}t          |          | _        |pt	                      | _        d| _        i | _        t          j	                    | _
        t          j	                    | _        t          j	                    | _        dS )a  Create a loader for the given path.

        If no path is specified it defaults to ``['.']``; that is, just the
        program directory.

        See the module documentation for details on the path format.

        :Parameters:
            `path` : list of str
                List of locations to search for resources.
            `script_home` : str
                Base location of relative files.  Defaults to the result of
                `get_script_home`.

        N.)
isinstancestrlistr   r*   _script_home_index_texture_atlas_binsweakrefWeakValueDictionary_cached_textures_cached_images_cached_animations)r   r   script_homes      r   r   zLoader.__init__*  s      <5DdC   	6DJJ	'<?+<+< $&  !( ; = =%9;;")"="?"?r   c                 @    | j         |                                  d S d S rH   )rn   reindexr   s    r   _require_indexzLoader._require_indexJ  s#    ;LLNNNNN r   c                    i | _         | j        D ]l}|                    d          r|dd         }	 t          |          }n#  Y 7xY w|                    d          dd         D ]}t          ||          }t          |d          r%t          j                            |j	                  }nUd}nRt          j        
                    |          s3d|vs
J d            t          j                            | j        |          }t          j                            |          r|                    t          j        j                  }t!          |          }t          j        |          D ]\  }}}|t%          |          dz   d         }|r>d	 |                    t          j                  D             }	d
                    |	          }|D ]%}
|r	|d
z   |
z   }n|
}|                     ||           &d}d}|rt          j                            |          st          j                            |dz             s|}t          j                            |          \  }}||k    rnZd
                    ||f          }|rAt          j                            |          s"t          j                            |dz             ||k    r|                    d
          }|s|                     |          }|rt-          j        |d          }t1          ||          }|                                D ]I}|                    |          r2|r|t%          |          dz   d         }|                     ||           JndS )zRefresh the file index.

        You must call this method if `path` is changed or the filesystem
        layout changes.
        @   Nri   r    z\\z/Backslashes are not permitted in relative pathsc                     g | ]}||S rH   r   ).0parts     r   
<listcomp>z"Loader.reindex.<locals>.<listcomp>s  s*     !6 !6 !6%)$($4 "& %5$4$4r   rP   .001r)rn   r   r6   
__import__splitr   r$   r   r    r   isabsr4   rm   isdirrstriprR   rD   walklen_index_fileisfile_get_streamzipfileZipFilerK   namelist)r   r   r	   module	componentlocationdirpathdirnames	filenamespartsr?   
index_namerN   old_pathtail_dir
zip_streamrM   zip_names                     r   rw   zLoader.reindexN  s    I F	A F	ADs## =ABBx'--FFH!%C!4 8 8I$VY77FF6:.. 7??6?;;DDDDW]]4(( =D(((*[(((w||D$5t<<w}}T"" 0A{{27;//'--46GDMM ? ?0GXy%c$ii!mnn5G 2!6 !6$+MM"&$9$9!6 !6 !6 #&((5//$- ? ?" 2)03)AJJ)1J((X>>>>??"  4BGNN4$8$8 4BGNN4RX=<Y<Y 4#H%'W]]4%8%8ND(x''((Hc?33C  4BGNN4$8$8 4BGNN4RX=<Y<Y 4 8##jjoo  !--d33
 
A!/*c::C*344H$'LLNN A A $..s33 A" C+3CHHqLMM+B ,,Xx@@@MF	A F	As   AAc                    t          j        |          r|S t          j                            |dz             sd S t          |dz   d          5 }t          |                                          }d d d            n# 1 swxY w Y   d}t          j                            |d                    |          z             rt          |d                    |          z   d          5 }|t          |                                          z  }d d d            n# 1 swxY w Y   |dz  }t          j                            |d                    |          z             t          j
        |          }t          j        |          r|S d S )Nr   r:      z.{0:0>3}r|   )r   
is_zipfiler   r   existsrA   bytesrS   formatrT   rU   )r   r   volumebytes_volume_indexr   s         r   r   zLoader._get_stream  s   d## 	Kv.. 	4dVmT** .fv{{}}--. . . . . . . . . . . . . . . L'..
(9(9,(G(G!GHH "$!2!2<!@!@@$GG 36eFKKMM222F3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 !	 '..
(9(9,(G(G!GHH " F++J!*-- !!ts$   "A<<B B $%DDDc                 0    || j         vr|| j         |<   d S d S rH   )rn   )r   r	   r   s      r   r   zLoader._index_file  s)    t{"" (DK #"r   r:   c                     |                                   	 | j        |         }|                    ||          S # t          $ r t	          |          w xY w)aF  Load a resource.

        :Parameters:
            `name` : str
                Filename of the resource to load.
            `mode` : str
                Combination of ``r``, ``w``, ``a``, ``b`` and ``t`` characters
                with the meaning as for the builtin ``open`` function.

        :rtype: file object
        )ry   rn   rA   KeyErrorr   )r   r	   r@   r   s       r   filezLoader.file  sd     		2{4(H==t,,, 	2 	2 	2+D111	2s	   "9 Ac                     |                                   	 | j        |         S # t          $ r t          |          w xY w)a  Get the location of a resource.

        This method is useful for opening files referenced from a resource.
        For example, an HTML file loaded as a resource might reference some
        images.  These images should be located relative to the HTML file, not
        looked up individually in the loader's path.

        :Parameters:
            `name` : str
                Filename of the resource to locate.

        :rtype: `Location`
        )ry   rn   r   r   )r   r	   s     r   r   zLoader.location  sQ     		2;t$$ 	2 	2 	2+D111	2s   # =c                     |                                   ddlm} |                     |          }|                    |           dS )a  Add a font resource to the application.

        Fonts not installed on the system must be added to pyglet before they
        can be used with `font.load`.  Although the font is added with
        its filename using this function, it is loaded by specifying its
        family name.  For example::

            resource.add_font('action_man.ttf')
            action_man = font.load('Action Man')

        :Parameters:
            `name` : str
                Filename of the font resource to add.

        r   )fontN)ry   r2   r   r   add_file)r   r	   r   r   s       r   add_fontzLoader.add_font  sO      	yydr   c                    |                      |          }	 t          j                            ||          }|                                 n# |                                 w xY w|s|                    d          S |                     |j        |j        |          }||                    d          S |	                    ||          S )Nr   T)
r   r2   imageloadcloseget_texture_get_texture_atlas_binwidthheightadd)r   r	   atlasborderr   imgbins          r   _alloc_imagezLoader._alloc_image  s    yy	,##Dt#44CJJLLLLDJJLLLL 	)??4((( ))#)SZHH;??4(((wwsF###s   !A A#c                 6   t           j                                        }t          d|          |z
  }||k    s||k    rdS d}||dz  k    rd}	 | j        |         }n=# t
          $ r0 t           j        j                                        }|| j        |<   Y nw xY w|S )zA heuristic for determining the atlas bin to use for a given image
        size.  Returns None if the image should not be placed in an atlas (too
        big), otherwise the bin (a list of TextureAtlas).
        i   Nr|      r   )r2   r   get_max_texture_sizeminro   r   r   
TextureBin)r   r   r   r   max_texture_sizemax_sizebin_sizetexture_bins           r   r   zLoader._get_texture_atlas_bin  s     "<<<>>t-..78v004 HqL  H	=28<KK 	= 	= 	= ,,7799K1<D$X...	= s   A 7BBFr   Tr|   c                     |                                   || j        v r| j        |         }n!|                     |||          x}| j        |<   |s|s|s|S |                    |||          S )a  Load an image with optional transformation.

        This is similar to `texture`, except the resulting image will be
        packed into a :py:class:`~pyglet.image.atlas.TextureBin` if it is an appropriate size for packing.
        This is more efficient than loading images into separate textures.

        :Parameters:
            `name` : str
                Filename of the image source to load.
            `flip_x` : bool
                If True, the returned image will be flipped horizontally.
            `flip_y` : bool
                If True, the returned image will be flipped vertically.
            `rotate` : int
                The returned image will be rotated clockwise by the given
                number of degrees (a multiple of 90).
            `atlas` : bool
                If True, the image will be loaded into an atlas managed by
                pyglet. If atlas loading is not appropriate for specific
                texturing reasons (e.g. border control is required) then set
                this argument to False.
            `border` : int
                Leaves specified pixels of blank space around each image in
                an atlas, which may help reduce texture bleeding.

        :rtype: `Texture`
        :return: A complete texture if the image is large or not in an atlas,
            otherwise a :py:class:`~pyglet.image.TextureRegion` of a texture atlas.
        )ry   rs   r   get_transform)r   r	   flip_xflip_yrotater   r   identitys           r   r   zLoader.image  s    < 	4&&&*40HH373D3DT5RX3Y3YYHt*40 	f 	V 	O%%fff===r   c                    |                                   	 | j        |         }n# t          $ r t          j                            ||                     |                    }|                     |                                |	                                |          }|r|
                    ||           |x}| j        |<   Y nw xY w|s|s|s|S |                    |||          S )ak  Load an animation with optional transformation.

        Animations loaded from the same source but with different
        transformations will use the same textures.

        :Parameters:
            `name` : str
                Filename of the animation source to load.
            `flip_x` : bool
                If True, the returned image will be flipped horizontally.
            `flip_y` : bool
                If True, the returned image will be flipped vertically.
            `rotate` : int
                The returned image will be rotated clockwise by the given
                number of degrees (a multiple of 90).
            `border` : int
                Leaves specified pixels of blank space around each image in
                an atlas, which may help reduce texture bleeding.
                
        :rtype: :py:class:`~pyglet.image.Animation`
        )ry   rt   r   r2   r   load_animationr   r   get_max_widthget_max_heightadd_to_texture_binr   )	r   r	   r   r   r   r   r   	animationr   s	            r   r   zLoader.animationF  s   , 	
	A.t4HH 	A 	A 	A33D$))D//JJI--i.E.E.G.G.7.F.F.H.H.46 6C  :,,S&9997@@Ht.t444	A  	f 	V 	O%%fff===s   $ BCCc                 v    |                                   t          | j                                                  S )zGet a list of image filenames that have been cached.

        This is useful for debugging and profiling only.

        :rtype: list
        :return: List of str
        )ry   rl   rs   keysrx   s    r   get_cached_image_nameszLoader.get_cached_image_namesn  s3     	D',,..///r   c                 v    |                                   t          | j                                                  S )zGet a list of animation filenames that have been cached.

        This is useful for debugging and profiling only.

        :rtype: list
        :return: List of str
        )ry   rl   rt   r   rx   s    r   get_cached_animation_namesz!Loader.get_cached_animation_namesy  s3     	D+0022333r   c                 v    |                                   t          | j                                                  S )zGet a list of texture bins in use.

        This is useful for debugging and profiling only.

        :rtype: list
        :return: List of :py:class:`~pyglet.image.atlas.TextureBin`
        )ry   rl   ro   valuesrx   s    r   get_texture_binszLoader.get_texture_bins  s3     	D,3355666r   c                    |                                   ddlm} 	 | j        |         }t	          |t
                    r<t          j                            |j        |          }|	                    ||          S |
                    |          }|	                    |||          S # t          $ r t          |          w xY w)a  Load a sound or video resource.

        The meaning of `streaming` is as for `media.load`.  Compressed
        sources cannot be streamed (that is, video and compressed audio
        cannot be streamed from a ZIP archive).

        :Parameters:
            `name` : str
                Filename of the media source to load.
            `streaming` : bool
                True if the source should be streamed from disk, False if
                it should be entirely decoded into memory immediately.

        :rtype: `media.Source`
        r   )media)	streaming)r   r   )ry   r2   r   rn   rj   rD   r   r   r4   r   rA   r   r   )r   r	   r   r   r   r   r   s          r   r   zLoader.media  s      	      
	2{4(H(L11 Hw||HM488zz$)z<<<}}T**zz$TYzGGG 	2 	2 	2+D111	2s   AB' :,B' 'Cc                     |                                   || j        v r| j        |         S |                     |          }t          j                            ||                                          }|| j        |<   |S )a^  Load a texture.

        The named image will be loaded as a single OpenGL texture.  If the
        dimensions of the image are not powers of 2 a :py:class:`~pyglet.image.TextureRegion` will
        be returned.

        :Parameters:
            `name` : str
                Filename of the image resource to load.

        :rtype: `Texture`
        r   )ry   rr   r   r2   r   r   r   )r   r	   r   textures       r   r   zLoader.texture  sy     	4((((..yy,##Dt#44@@BB&-d#r   c                 >   |                                   t          j                            t          j                            |                     |          j                  |          }t          j                            || 	                    |          |          S )zLoad a 3D model.

        :Parameters:
            `name` : str
                Filename of the 3D model to load.
            `batch` : Batch or None
                An optional Batch instance to add this model to.

        :rtype: `Model`
        )r?   r   batch)
ry   r   r   r4   r%   r   r2   modelr   r   )r   r	   r   abspathnames       r   r   zLoader.model  sp     	gll27??4==3F3F3K#L#LdSS|  +DIIdOOSX YYYr   c                     |                                   |                     |          }t          j                            ||d          S )zLoad an HTML document.

        :Parameters:
            `name` : str
                Filename of the HTML resource to load.

        :rtype: `FormattedDocument`
        z	text/htmlry   r   r2   rW   r   r   r	   r   s      r   htmlzLoader.html  s?     	yy{dK888r   c                     |                                   |                     |          }t          j                            ||d          S )a	  Load an attributed text document.

        See `pyglet.text.formats.attributed` for details on this format.

        :Parameters:
            `name` : str
                Filename of the attribute text resource to load.

        :rtype: `FormattedDocument`
        ztext/vnd.pyglet-attributedr   r   s      r   
attributedzLoader.attributed  s@     	yy{d,HIIIr   c                     |                                   |                     |          }t          j                            ||d          S )zLoad a plain text document.

        :Parameters:
            `name` : str
                Filename of the plain text resource to load.

        :rtype: `UnformattedDocument`
        z
text/plainr   r   s      r   rW   zLoader.text  s?     	yy{dL999r   c                 v    |                                   t          | j                                                  S )zQGet the names of textures currently cached.

        :rtype: list of str
        )ry   rl   rr   r   rx   s    r   get_cached_texture_nameszLoader.get_cached_texture_names  s3    
 	D)..00111r   )NNrB   )FFr   Tr|   )FFr   r|   )TrH   )r   r   r   r   r   ry   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r   rg   rg     s        @ @ @ @@  MA MA MA^  ,) ) )2 2 2 2&2 2 2(  *$ $ $"  2'> '> '> '>R&> &> &> &>P	0 	0 	0	4 	4 	4	7 	7 	72 2 2 2<  ,Z Z Z Z9 9 9J J J: : :2 2 2 2 2r   rg   c                   D    e Zd Zed             Zej        d             ZdS )_DefaultLoaderc                     t           S rH   rF   rx   s    r   r   z_DefaultLoader.path  s    r   c                 
    |a d S rH   rF   )r   values     r   r   z_DefaultLoader.path  s     r   N)r   r   r   propertyr   setterr   r   r   r   r     sJ          X 
[  [  r   r   )#r   rT   r   r   r   rp   r2   r   r   r*   r7   r9   rD   rK   rY   rg   r   r   _default_loaderrw   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r   r   r   r   <module>r      s4  H2 2h 
			 				 



   * * * * *	 * * *'7 '7 '7T%2 %2 %2P. . . . . . . .2= = = = =8 = = ="         (      <+ + + + +( + + +.f2 f2 f2 f2 f2 f2 f2 f2b 
	 	 	 	 	V 	 	 	 !.""

!##%	

!'
*C (? ,G "3   r   