
                             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e
j                        Z G d	 d
e      Zd Zd Zd Zd Zy)zBUtilities for accessing modules by installation independent paths.    )absolute_import)division)unicode_literalsN)
exceptionsc                       e Zd ZdZy)ErrorzExceptions for this module.N__name__
__module____qualname____doc__     &lib/googlecloudsdk/core/module_util.pyr   r      s    #r   r   c                       e Zd ZdZy)ImportModuleErrorzImportModule failed.Nr	   r   r   r   r   r   #   s    r   r   c                    | j                  d      }t        |      dkD  rt        dj                  |             	 t	        j
                  |d         }t        |      dk(  r|S |}|d   j                  d      }|D ]  }	 t        ||      } |S # t        $ r }t        dj                  | |            d}~ww xY w# t        $ r }t        dj                  | |            d}~ww xY w)	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attrs          r   ImportModuler'   '   s
   " 

C
 %Z!^
	66<f[6IK KB$$U1X.F 	Z1_M 	#Qx~~c"*dDCc  
* 
 B
)00a@B BB  D
+
2
2;
BD DDs/   B ?B;	B8B33B8;	C$CC$c                      ~ y)zHMock hook that returns the module path for module that starts with '__'.Nr   )r    s    r   _GetPrivateModulePathr)   Q   s    	r   c                 r   	 | j                   }t        j                  r|dk(  ry|j                  d      rt        |      }|sy	 |dz   | j                  z   S # t        $ r | j                  } | j                   }Y kw xY w# t        $ r0 	 |dz   | j                  j                  z   cY S # t        $ r Y Y yw xY ww xY 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"   s     r   GetModulePathr1   W   s    ^^F
 	WW:%t"6*FC<#,,&& 
 
--C^^F 
 c\CMM2222 sA   A A= !A:9A:=	B6B%"B6%	B2.B61B22B6c                    t         j                  j                  t         j                  j                  |             d   }t        j
                  j                  ||       }|st        dj                  |             t        j
                  j                  |      }|t        j                  |<   	 |j                  j                  |       |S # t        $ r }t        dj                  | |            d}~ww xY 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)r4   module_namespecr"   r#   s        r   
ImportPathrA   z   s      !1!1$!78;+		/	/T	B$	
%,,T24 4>>**40&##++k;KKF# 
- 
 ;
)00q9; ;;s   /C 	C5C00C5)r   
__future__r   r   r   r   importlib.utilr3   r:   googlecloudsdk.corer   r.   r   r   r'   r)   r1   rA   r   r   r   <module>rE      sZ     I &  '   	 
 * 
$J $ 'T Fr   