a
    ߙfb                     @   s   d Z ddlZddlZddlZddlZddlZg d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   s|   t | d}| }W d    n1 s(0    Y  t | d,}|tj|d || W d    n1 sn0    Y  d S )Nrw)package)openreadwrite_TAB_HEADERformat)filenamer   fcontents r   4lib/python3.9/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Zoriginalr   r   wrapper4   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p tj|}t	|* |jd| tj||d}W d   n1 s~0    Y  |st
|| |W  d   S 1 s0    Y  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_existsZlexerr   r   r   r   >   s    

$
r   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__l   s    zThreadSafeParser.__init__c                 O   s<   | j " | jj|i |W  d   S 1 s.0    Y  dS )z<Run the wrapped parser, with a lock to ensure serialization.N)r,   r)   parse)r-   argskwargsr   r   r   r/   p   s    zThreadSafeParser.parseN)__name__
__module____qualname____doc__r.   r/   r   r   r   r   r   f   s   r   c              	   C   s   ddl m} |dd }tjtj|| d }tn tj|}t	|, |j| tj|dddd}W d	   n1 s0    Y  |st
|| W d	   n1 s0    Y  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_tablesN)r!   r   r   r"   r#   r$   r%   r&   r'   r   r   r   )r6   r   r   r(   Ztab_filenameZ
tab_existsr)   r   r   r   r   v   s    

$(r   )r5   
contextlibr   rer"   r*   __all__r   r+   r&   r   contextmanagerr   intVERBOSEr   r   r   r   r   r   r   <module>   s   	
(