o
    w!g?                     @  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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" d dl#m$Z$ d dl%m&Z& dZ'eG dd dZ(edddYddZ)e	dZddd d[d(d)Z*e	dZdddddddd*dd*dd*d+d\d=d>Z+e	dZdddddddddd*dd*d+d\d?d@Z,eddddddddAd]dDdEZ-edddddddddFd^dIdJZ.d*dKd_dOdPZ/d`dQdRZ0dd*dSdadWdXZ1dS )b    )annotationsN)contextmanager)	dataclass)invalidate_caches)Path)dedent)TYPE_CHECKINGAny)inspect)visit)LinesCollection)load)ModuleObject)IteratorMappingSequence)ModulesCollection)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"   O/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/_griffe/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_implicitrX   Extensions | NonerY   DocstringStyle | Parser | NonerZ   dict[str, Any] | Noner[   LinesCollection | Noner\   ModulesCollection | Noner]   r^   r_   r`   bool | Nonera   Iterator[Module]c                c  sb    t | |||d}t|j|jg||||||	|
|||ddV  W d   dS 1 s*w   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: Additional 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.

    Yields:
        A module.
    r8   Fsearch_pathsrX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   force_inspectionN)rW   r   r   r   )r;   r<   r9   r:   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   tmp_packager"   r"   r#   temporary_visited_packagex   s$   /
"rm   c                c  s    t | |||db}z7t|j|jg||||||	|
|||ddV  W ttj D ]}|| ks6||  dr=tj	|d q(t
  n!ttj D ]}|| ksW||  dr^tj	|d qIt
  w W d   dS 1 snw   Y  dS )ab  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: Additional 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.

    Yields:
        A module.
    r8   Tri   rC   N)rW   r   r   r   tuplesysr<   keys
startswithpopr   )r;   r<   r9   r:   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rl   r   r"   r"   r#   temporary_inspected_package   s:   /

"rs   )r,   rX   parentrY   rZ   r[   r\   rt   Module | Nonec                c  s    t | } t| |d+\}}	|pt }|  ||	< t||	| ||||||d	}
|
|
j|< |
V  W d   dS 1 s9w   Y  dS )aS  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: Additional docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.

    Yields:
        The visited module.
    r+   )filepathr-   rX   rt   rY   rZ   r[   r\   N)r   r7   r   
splitlinesr   r\   )r-   r,   rX   rt   rY   rZ   r[   r\   _r   r*   r"   r"   r#   temporary_visited_module   s&   

"ry   )r,   import_pathsrX   rt   rY   rZ   r[   r\   rz   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: Additional docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.

    Yields:
        The inspected module.
    r+   )rv   rz   rX   rt   rY   rZ   r[   r\   N)r7   r   rw   r
   r\   ro   r<   r   )r-   r,   rz   rX   rt   rY   rZ   r[   r\   rx   r   r*   r"   r"   r#   temporary_inspected_module.  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#   vtreeb  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#   htreey  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   )rv   )r   r   ).0indexr   rJ   r"   r#   
<listcomp>  s   2 z module_vtree.<locals>.<listcomp>r}   )split	enumeraterv   	with_stem	_filepathr   )r   r   r}   r<   rv   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   rb   rY   rc   rZ   rd   r[   re   r\   rf   r]   r>   r^   r>   r_   r>   r`   rg   ra   r>   r   rh   )r-   r   r,   r   rX   rb   rt   ru   rY   rc   rZ   rd   r[   re   r\   rf   r   rh   )r-   r   r,   r   rz   r{   rX   rb   rt   ru   rY   rc   rZ   rd   r[   re   r\   rf   r   rh   )r~   r   r}   r>   r   r   )r~   r   r   r   )r   r   r   r>   r}   r>   r   r   )2
__future__r   ro   r2   
contextlibr   dataclassesr   	importlibr   pathlibr   textwrapr   typingr   r	   _griffe.agents.inspectorr
   _griffe.agents.visitorr   _griffe.collectionsr   _griffe.loaderr   _griffe.modelsr   r   collections.abcr   r   r   r   _griffe.docstrings.parsersr   _griffe.enumerationsr   _griffe.extensions.baser   r4   r   r7   rW   rm   rs   ry   r|   r   r   r   r"   r"   r"   r#   <module>   s   0@F-3
