
    gQ                        d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	 erd dl
mZmZ d dlmZ ddZedd	       Zddd
Zy)    )annotationsN)contextmanager)import_module)TYPE_CHECKINGAny)IteratorSequence)Pathc                b    dt         j                  d|d| j                  j                   d|  S )NzWith sys.path = z, accessing z raises z: )syspath	__class____name__)errorobjpaths     E/var/www/openai/venv/lib/python3.12/site-packages/_griffe/importer.py_error_detailsr      s3    chh\g[IaIaHbbdejdkll    c               '     K   | sd yt         j                  }| D cg c]  }t        |       c}t         _        	 d |t         _        yc c}w # |t         _        w xY ww)zRedefine `sys.path` temporarily.

    Parameters:
        *paths: The paths to use when importing modules.
            If no paths are given, keep `sys.path` untouched.

    Yields:
        Nothing.
    N)r   r   str)pathsold_pathr   s      r   sys_pathr      sR      xxH&+,edD	e,CH	 - s&   A"AA"A A"AA"c                x   | j                  d      }g }g }t        |xs d 5  |rdj                  |      }	 t        |      }nt        dj                  |            |}|D ]  }		 t        ||	      } 	 ddd       |S # t        $ rF}|j                  t        ||             |j                  d|j                  d             Y d}~nd}~ww xY w|r# t        $ rO}|j                  t        ||dz   dj                  |      z                t        dj                  |            d}~ww xY w# 1 sw Y   S xY w)a  Dynamically import the specified object.

    It can be a module, class, method, function, attribute,
    nested arbitrarily.

    It works like this:

    - for a given object path `a.b.x.y`
    - it tries to import `a.b.x.y` as a module (with `importlib.import_module`)
    - if it fails, it tries again with `a.b.x`, storing `y`
    - then `a.b`, storing `x.y`
    - then `a`, storing `b.x.y`
    - if nothing worked, it raises an error
    - if one of the iteration worked, it moves on, and...
    - it tries to get the remaining (stored) parts with `getattr`
    - for example it gets `b` from `a`, then `x` from `b`, etc.
    - if a single attribute access fails, it raises an error
    - if everything worked, it returns the last obtained attribute

    Since the function potentially tries multiple things before succeeding,
    all errors happening along the way are recorded, and re-emitted with
    an `ImportError` when it fails, to let users know what was tried.

    IMPORTANT: The paths given through the `import_paths` parameter are used
    to temporarily patch `sys.path`: this function is therefore not thread-safe.

    IMPORTANT: The paths given as `import_paths` must be *correct*.
    The contents of `sys.path` must be consistent to what a user of the imported code
    would expect. Given a set of paths, if the import fails for a user, it will fail here too,
    with potentially unintuitive errors. If we wanted to make this function more robust,
    we could add a loop to "roll the window" of given paths, shifting them to the left
    (for example: `("/a/a", "/a/b", "/a/c/")`, then `("/a/b", "/a/c", "/a/a/")`,
    then `("/a/c", "/a/a", "/a/b/")`), to make sure each entry is given highest priority
    at least once, maintaining relative order, but we deem this unnecessary for now.

    Parameters:
        import_path: The path of the object to import.
        import_paths: The (sys) paths to import the object from.

    Raises:
        ModuleNotFoundError: When the object's module could not be found.
        ImportError: When there was an import error or when couldn't get the attribute.

    Returns:
        The imported object.
    . r   Nz; :)splitr   joinr   BaseExceptionappendr   insertpopImportErrorgetattr)
import_pathimport_pathsmodule_partsobject_partserrorsmodule_pathmoduler   valueparts
             r   dynamic_importr0   +   s7   ^ *//4L LF	L&B	(((<0K&{3 dii/00  D5t, !- 
): L1 ! = nUK@A##A|'7'7';<<	=	 0 ! 5nUK#4EQ]H^4^_`!$))F"34453 
): LsY   D/A>"D/%C1D/>	C<CD/CD/	D,A
D''D,,D//D9)r   r!   r   r   returnr   )r   z
str | Pathr1   zIterator[None])N)r'   r   r(   zSequence[str | Path] | Noner1   r   )
__future__r   r   
contextlibr   	importlibr   typingr   r   collections.abcr   r	   pathlibr
   r   r   r0   r   r   r   <module>r8      s?   
 # 
 % # %2m  *Pr   