o
    hYA                     @  s  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZmZ erxd dlmZmZmZ d dlmZ d dl m!Z!m"Z" d dl#m$Z$ d dl%m&Z& dZ'eG dd dZ(edddZddZ)e	d[ddd d\d(d)Z*e	d[dddddddd*dd*dd*d*d+d]d>d?Z+e	d[dddddddddd*dd*d*d+d]d@dAZ,eddddddddBd^dEdFZ-edddddddddGd_dJdKZ.d*dLd`dPdQZ/dadRdSZ0dd*dTdbdXdYZ1dS )c    )annotationsN)contextmanager)	dataclass)invalidate_caches)Path)dedent)TYPE_CHECKING)inspect)visit)LinesCollection)load)ModuleObject)IteratorMappingSequence)ModulesCollection)DocstringOptionsDocstringStyle)Parser)
Extensionsgriffe_c                   @  s:   e Zd ZU dZded< 	 ded< 	 ded< 	 dd	d
ZdS )
TmpPackagezA temporary package.

    The `tmpdir` and `path` parameters can be passed as relative path.
    They will be resolved to absolute paths after initialization.
    r   tmpdirstrnamepathreturnNonec                 C  s   | j  | _ | j | _d S N)r   resolver   )self r"   W/var/www/html/openai_agents/venv/lib/python3.10/site-packages/griffe/_internal/tests.py__post_init__0   s   zTmpPackage.__post_init__N)r   r   )__name__
__module____qualname____doc____annotations__r$   r"   r"   r"   r#   r   !   s   
 r   modulemodule_namecoder   r,   r   Iterator[tuple[str, Path]]c                c  s^    t jtd}t|| d }|t|  ||fV  W d   dS 1 s(w   Y  dS )aB  Create a Python file containing the given code in a temporary directory.

    Parameters:
        code: The code to write to the temporary file.
        module_name: The name of the temporary module.

    Yields:
        module_name: The module name, as to dynamically import it.
        module_path: The module path.
    prefix.pyN)tempfileTemporaryDirectory_TMPDIR_PREFIXr   
write_textr   )r-   r,   r   tmpfiler"   r"   r#   temporary_pyfile5   s   "r7   Tinitinitspackagemodules(Sequence[str] | Mapping[str, str] | Noner9   boolr:   Iterator[TmpPackage]c                c  s   |pi }t |trt|d}ddd}tjtdi}t|}dt| j	}||  }|j
d	i | |r=|d  | D ]3\}	}
|}t|	j	D ]'}|dr^||t|
 qL|| }|j
d	i | |rs|d  qLqAt|||V  W d   dS 1 sw   Y  dS )
a(  Create a package containing the given modules in a temporary directory.

    Parameters:
        package: The package name. Example: `"a"` gives
            a package named `a`, while `"a/b"` gives a namespace package
            named `a` with a package inside named `b`.
            If `init` is false, then `b` is also a namespace package.
        modules: Additional modules to create in the package.
            If a list, simply touch the files: `["b.py", "c/d.py", "e/f"]`.
            If a dict, keys are the file names and values their contents:
            `{"b.py": "b = 1", "c/d.py": "print('hey from c')"}`.
        init: Whether to create an `__init__` module in the top package.
        inits: Whether to create `__init__` modules in subpackages.

    Yields:
        A temporary package.
     T)parentsexist_okr/   .__init__.py)r1   z.pyiNr"   )
isinstancelistdictfromkeysr2   r3   r4   r   joinpartsmkdirjoinpathtouchitemsendswithr5   r   r   )r;   r<   r9   r:   mkdir_kwargsr   
tmpdirpathpackage_namepackage_pathr,   module_contentscurrent_pathpartr"   r"   r#   temporary_pypackageG   s2   


"rW   F)r9   r:   
extensionsdocstring_parserdocstring_optionslines_collectionmodules_collectionallow_inspectionstore_sourceresolve_aliasesresolve_externalresolve_implicitsearch_sys_pathrX   Extensions | NonerY   DocstringStyle | Parser | NonerZ   DocstringOptions | Noner[   LinesCollection | Noner\   ModulesCollection | Noner]   r^   r_   r`   bool | Nonera   rb   Iterator[Module]c                c  st    |rt jng }t| |||d }t|j|jg|||||||	|
|||ddV  W d   dS 1 s3w   Y  dS )a  Create and visit a temporary package.

    Parameters:
        package: The package name. Example: `"a"` gives
            a package named `a`, while `"a/b"` gives a namespace package
            named `a` with a package inside named `b`.
            If `init` is false, then `b` is also a namespace package.
        modules: Additional modules to create in the package.
            If a list, simply touch the files: `["b.py", "c/d.py", "e/f"]`.
            If a dict, keys are the file names and values their contents:
            `{"b.py": "b = 1", "c/d.py": "print('hey from c')"}`.
        init: Whether to create an `__init__` module in the top package.
        inits: Whether to create `__init__` modules in subpackages.
        extensions: The extensions to use.
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.
        allow_inspection: Whether to allow inspecting modules when visiting them is not possible.
        store_source: Whether to store code source in the lines collection.
        resolve_aliases: Whether to resolve aliases.
        resolve_external: Whether to try to load unspecified modules to resolve aliases.
            Default value (`None`) means to load external modules only if they are the private sibling
            or the origin module (for example when `ast` imports from `_ast`).
        resolve_implicit: When false, only try to resolve an alias if it is explicitly exported.
        search_sys_path: Whether to search the system paths for the package.

    Yields:
        A module.
    r8   Fsearch_pathsrX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   force_inspectionN)sysr   rW   r   r   r   )r;   r<   r9   r:   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rk   tmp_packager"   r"   r#   temporary_visited_packagex   s&   1

"ro   c                c  s    |rt jng }t| |||dd}z9t|j|jg|||||||	|
|||ddV  W tt j D ]}|| ks?|	|  drFt j
|d q1t  n!tt j D ]}|| ks`|	|  drgt j
|d qRt  w W d   dS 1 sww   Y  dS )a  Create and inspect a temporary package.

    Parameters:
        package: The package name. Example: `"a"` gives
            a package named `a`, while `"a/b"` gives a namespace package
            named `a` with a package inside named `b`.
            If `init` is false, then `b` is also a namespace package.
        modules: Additional modules to create in the package.
            If a list, simply touch the files: `["b.py", "c/d.py", "e/f"]`.
            If a dict, keys are the file names and values their contents:
            `{"b.py": "b = 1", "c/d.py": "print('hey from c')"}`.
        init: Whether to create an `__init__` module in the top package.
        inits: Whether to create `__init__` modules in subpackages.
        extensions: The extensions to use.
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.
        allow_inspection: Whether to allow inspecting modules.
        store_source: Whether to store code source in the lines collection.
        resolve_aliases: Whether to resolve aliases.
        resolve_external: Whether to try to load unspecified modules to resolve aliases.
            Default value (`None`) means to load external modules only if they are the private sibling
            or the origin module (for example when `ast` imports from `_ast`).
        resolve_implicit: When false, only try to resolve an alias if it is explicitly exported.
        search_sys_path: Whether to search the system paths for the package.

    Yields:
        A module.
    r8   Trj   rC   N)rm   r   rW   r   r   r   tupler<   keys
startswithpopr   )r;   r<   r9   r:   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rk   rn   r   r"   r"   r#   temporary_inspected_package   s<   1


"rt   )r,   rX   parentrY   rZ   r[   r\   ru   Module | Nonec                c  s    t | } t| |d+\}}	|pt }|  ||	< t||	| ||||||d	}
|
|
j|< |
V  W d   dS 1 s9w   Y  dS )aH  Create and visit a temporary module with the given code.

    Parameters:
        code: The code of the module.
        module_name: The name of the temporary module.
        extensions: The extensions to use when visiting the AST.
        parent: The optional parent of this module.
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.

    Yields:
        The visited module.
    r+   )filepathr-   rX   ru   rY   rZ   r[   r\   N)r   r7   r   
splitlinesr
   r\   )r-   r,   rX   ru   rY   rZ   r[   r\   _r   r*   r"   r"   r#   temporary_visited_module  s&   

"rz   )r,   import_pathsrX   ru   rY   rZ   r[   r\   r{   list[Path] | Nonec                c  s    t | |dG\}	}
|pt }|  ||
< z#t||
|||||||d	}||j|< |V  W |tjv r5tj|= t  n|tjv rBtj|= t  w W d   dS 1 sQw   Y  dS )a  Create and inspect a temporary module with the given code.

    Parameters:
        code: The code of the module.
        module_name: The name of the temporary module.
        import_paths: Paths to import the module from.
        extensions: The extensions to use when visiting the AST.
        parent: The optional parent of this module.
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.

    Yields:
        The inspected module.
    r+   )rw   r{   rX   ru   rY   rZ   r[   r\   N)r7   r   rx   r	   r\   rm   r<   r   )r-   r,   r{   rX   ru   rY   rZ   r[   r\   ry   r   r*   r"   r"   r#   temporary_inspected_module4  s2   




"r}   )return_leafobjectsr   r~   c                 G  sH   |st d|d }|}|dd D ]}||j| |}q| r"|S |S )a<  Link objects together, vertically.

    Parameters:
        *objects: A sequence of objects. The first one is at the top of the tree.
        return_leaf: Whether to return the leaf instead of the root.

    Raises:
        ValueError: When no objects are provided.

    Returns:
        The top or leaf object.
    $At least one object must be providedr      N
ValueError
set_memberr   )r~   r   topleafobjr"   r"   r#   vtreeh  s   r   c                  G  s8   | st d| d }| dd D ]	}||j| q|S )a=  Link objects together, horizontally.

    Parameters:
        *objects: A sequence of objects. All objects starting at the second become members of the first.

    Raises:
        ValueError: When no objects are provided.

    Returns:
        The first given object, with all the other objects as members of it.
    r   r   r   Nr   )r   r   r   r"   r"   r#   htree  s   r   )leaf_packager~   r   r   r   c                  sP   |  d  fddt D }|s!|d j d }||d _t|d|iS )ao  Link objects together, vertically.

    Parameters:
        path: The complete module path, like `"a.b.c.d"`.
        leaf_package: Whether the deepest module should also be a package.
        return_leaf: Whether to return the leaf instead of the root.

    Raises:
        ValueError: When no objects are provided.

    Returns:
        The top or leaf module.
    rC   c              	     s2   g | ]\}}t |tg  d | dR  dqS )NrD   )rw   )r   r   ).0indexr   rJ   r"   r#   
<listcomp>  s   2 z module_vtree.<locals>.<listcomp>r~   )split	enumeraterw   	with_stem	_filepathr   )r   r   r~   r<   rw   r"   r   r#   module_vtree  s   

r   )r-   r   r,   r   r   r.   r   )
r;   r   r<   r=   r9   r>   r:   r>   r   r?   ) r;   r   r<   r=   r9   r>   r:   r>   rX   rc   rY   rd   rZ   re   r[   rf   r\   rg   r]   r>   r^   r>   r_   r>   r`   rh   ra   r>   rb   r>   r   ri   )r-   r   r,   r   rX   rc   ru   rv   rY   rd   rZ   re   r[   rf   r\   rg   r   ri   )r-   r   r,   r   r{   r|   rX   rc   ru   rv   rY   rd   rZ   re   r[   rf   r\   rg   r   ri   )r   r   r~   r>   r   r   )r   r   r   r   )r   r   r   r>   r~   r>   r   r   )2
__future__r   rm   r2   
contextlibr   dataclassesr   	importlibr   pathlibr   textwrapr   typingr   !griffe._internal.agents.inspectorr	   griffe._internal.agents.visitorr
   griffe._internal.collectionsr   griffe._internal.loaderr   griffe._internal.modelsr   r   collections.abcr   r   r   r   #griffe._internal.docstrings.parsersr   r   griffe._internal.enumerationsr    griffe._internal.extensions.baser   r4   r   r7   rW   ro   rt   rz   r}   r   r   r   r"   r"   r"   r#   <module>   s   0CI-3
