U
    e»{j¶  ã                   @   s„   d Z ddlZddlZddlZddlZddlZdddgZdZe ¡ Z	dd„ Z
ejd	d
„ ƒZeejƒfdd„ZG dd„ dƒZdd„ ZdS )z,
Wrappers for PLY to provide thread safety.
é    NÚlexÚThreadSafeParserÚyaccad  # -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst

# This file was automatically generated from ply. To re-generate this file,
# remove it from this folder, then build astropy and run the tests in-place:
#
#   python setup.py build_ext --inplace
#   pytest {package}
#
# You can then commit the changes to this file.

c              	   C   sR   t | ƒ�}| ¡ }W 5 Q R X t | dƒ�"}| tj|d�¡ | |¡ W 5 Q R X d S )NÚw)Úpackage)ÚopenÚreadÚwriteÚ_TAB_HEADERÚformat)Úfilenamer   ÚfÚcontents© r   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/utils/parsing.pyÚ_add_tab_header   s
    
r   c                 #   s2   | j ‰ t ˆ ¡‡ fdd„ƒ}|| _ dV  ˆ | _ dS )a<  Temporarily replace the module's get_caller_module_dict.

    This is a function inside ``ply.lex`` and ``ply.yacc`` (each has a copy)
    that is used to retrieve the caller's local symbols. Here, we patch the
    function to instead retrieve the grandparent's local symbols to account
    for a wrapper layer.
    c                    s   ˆ | d ƒS )Né   r   )Zlevels©Úoriginalr   r   Úwrapper2   s    z._patch_get_caller_module_dict.<locals>.wrapperN)Úget_caller_module_dictÚ	functoolsÚwraps)Úmoduler   r   r   r   Ú_patch_get_caller_module_dict'   s    	r   c              
   C   sœ   ddl m} | d¡d }tj tj |¡| d ¡}t�Z tj |¡}t	|ƒ�  |jd| tj |¡|d�}W 5 Q R X |s‚t
||ƒ |W  5 Q R £ S Q R X dS )	aq  Create a lexer from local variables.

    It automatically compiles the lexer in optimized mode, writing to
    ``lextab`` in the same directory as the calling file.

    This function is thread-safe. The returned lexer is *not* thread-safe, but
    if it is used exclusively with a single parser returned by :func:`yacc`
    then it will be safe.

    It is only intended to work with lexers defined within the calling
    function, rather than at class or module scope.

    Parameters
    ----------
    lextab : str
        Name for the file to write with the generated tables, if it does not
        already exist (without ``.py`` suffix).
    package : str
        Name of a test package which should be run with pytest to regenerate
        the output file. This is inserted into a comment in the generated
        file.
    reflags : int
        Passed to ``ply.lex``.
    r   )r   r   Ú__file__ú.pyT)ÚoptimizeÚlextabÚ	outputdirÚreflagsN)Úastropy.extern.plyr   r   ÚosÚpathÚjoinÚdirnameÚ_LOCKÚexistsr   r   )r   r   r    r   Úcaller_fileZlextab_filenameZlextab_existsÚlexerr   r   r   r   <   s    

ü
c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   znWrap a parser produced by ``ply.yacc.yacc``.

    It provides a :meth:`parse` method that is thread-safe.
    c                 C   s   || _ t ¡ | _d S )N)ÚparserÚ	threadingÚRLockÚ_lock)Úselfr*   r   r   r   Ú__init__m   s    zThreadSafeParser.__init__c              
   O   s,   | j � | jj||ŽW  5 Q R £ S Q R X dS )z<Run the wrapped parser, with a lock to ensure serialization.N)r-   r*   Úparse)r.   ÚargsÚkwargsr   r   r   r0   q   s    zThreadSafeParser.parseN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r0   r   r   r   r   r   g   s   c              
   C   s–   ddl m} | d¡d }tj tj |¡| d ¡}t�P tj |¡}t	|ƒ�" |j| tj |¡dddd�}W 5 Q R X |s„t
||ƒ W 5 Q R X t|ƒS )	a&  Create a parser from local variables.

    It automatically compiles the parser in optimized mode, writing to
    ``tabmodule`` in the same directory as the calling file.

    This function is thread-safe, and the returned parser is also thread-safe,
    provided that it does not share a lexer with any other parser.

    It is only intended to work with parsers defined within the calling
    function, rather than at class or module scope.

    Parameters
    ----------
    tabmodule : str
        Name for the file to write with the generated tables, if it does not
        already exist (without ``.py`` suffix).
    package : str
        Name of a test package which should be run with pytest to regenerate
        the output file. This is inserted into a comment in the generated
        file.
    r   )r   r   r   r   FT)Ú	tabmoduler   Údebugr   Zwrite_tables)r!   r   r   r"   r#   r$   r%   r&   r'   r   r   r   )r7   r   r   r(   Ztab_filenameZ
tab_existsr*   r   r   r   r   w   s     

û)r6   Ú
contextlibr   r"   Úrer+   Ú__all__r
   r,   r&   r   Úcontextmanagerr   ÚintÚVERBOSEr   r   r   r   r   r   r   Ú<module>   s   
	
+