o
    €Ï¿  ã                   @   s¢   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	m
Z
 ddlZG dd„ de
jƒZG d	d
„ d
eƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )zBUtilities for accessing modules by installation independent paths.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Ú
exceptionsc                   @   ó   e Zd ZdZdS )ÚErrorzExceptions for this module.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú</tmp/google-cloud-sdk/lib/googlecloudsdk/core/module_util.pyr      ó    r   c                   @   r   )ÚImportModuleErrorzImportModule failed.Nr   r   r   r   r   r   #   r   r   c                 C   sÆ   |   d¡}t|ƒdkrtd | ¡ƒ‚z	t |d ¡}W n ty/ } ztd | |¡ƒ‚d}~ww t|ƒdkr8|S |}|d   d¡}|D ]}zt||ƒ}W qC ty` } ztd | |¡ƒ‚d}~ww |S )	aŽ  Imports a module object given its ModulePath and returns it.

  A module_path from GetModulePath() from any valid installation is importable
  by ImportModule() in another installation of same release.

  Args:
    module_path: The module path to import.

  Raises:
    ImportModuleError: Malformed module path or any failure to import.

  Returns:
    The Cloud SDK object named by module_path.
  ú:é   zQModule path [{}] must be in the form: package(.module)+(:attribute(.attribute)*)?r   zModule path [{}] not found: {}.Né   Ú.)	ÚsplitÚlenr   ÚformatÚ	importlibÚimport_moduleÚImportErrorÚgetattrÚAttributeError)Úmodule_pathÚpartsÚmoduleÚeÚobjÚ
attributesÚattrr   r   r   ÚImportModule'   s8   
þ
ÿ€ÿ
ÿ€ÿr$   c                 C   s   ~ dS )zHMock hook that returns the module path for module that starts with '__'.Nr   )r   r   r   r   Ú_GetPrivateModulePathQ   s   r%   c                 C   s¤   z| j }W n ty   | j} | j }Y nw tjr|dkrdS | d¡r+t|ƒ}|s+dS z|d | j W S  tyQ   z|d | jj W  Y S  tyP   Y Y dS w w )a1  Returns the module path string for obj, None if it's builtin.

  The module path is relative and importable by ImportModule() from any
  installation of the current release.

  Args:
    obj: The object to get the module path from.

  Returns:
    The module path name for obj if not builtin else None.
  ÚbuiltinsNÚ__r   )r
   r   Ú	__class__ÚsixÚPY3Ú
startswithr%   r	   )r!   r   r   r   r   ÚGetModulePathW   s*   

ý
ÿýr,   c              
   C   sŠ   t j t j | ¡¡d }tj || ¡}|std | ¡ƒ‚tj 	|¡}|t
j|< z	|j |¡ W |S  tyD } ztd | |¡ƒ‚d}~ww )z?Imports and returns the module given a python source file path.r   zModule file [{}] not found.zModule file [{}] not found: {}.N)ÚosÚpathÚsplitextÚbasenamer   ÚutilÚspec_from_file_locationr   r   Úmodule_from_specÚsysÚmodulesÚloaderÚexec_moduleÚFileNotFoundError)r.   Úmodule_nameÚspecr   r    r   r   r   Ú
ImportPathz   s"   ÿ
ý
ÿ€ÿr;   )r   Ú
__future__r   r   r   r   Úimportlib.utilr-   r4   Úgooglecloudsdk.corer   r)   r   r   r$   r%   r,   r;   r   r   r   r   Ú<module>   s    *#