3
6ßL]Ä  ã               @   sd   d Z ddlZddlmZmZmZmZmZ ddlm	Z	m
Z
 dd„ Zdd„ Zd	d
„ Zdd„ Zdd„ ZdS )zi Utility functions.

fixme: I don't like random collections of utility functions! Where should
this go?

é    N)ÚbasenameÚdirnameÚisdirÚ
splitdriveÚsplitext)Ú
is_zipfileÚZipFilec             C   sŠ   t jj| ƒr~t jjt jj| ƒƒ\}}|g}t| ƒ}x>t|ƒrpdt j|ƒkrpt|ƒ}|jdt|ƒd ƒ t|ƒ}q4W dj	|ƒ}nt
| ƒ}|S )zß Get the fully qualified module name for a filename.

    For example, if the filename is

    /enthought/envisage/core/core_plugin_definition.py

    this method would return

    envisage.core.core_plugin_definition

    z__init__.pyr   Ú.)ÚosÚpathÚexistsr   r   r   r   ÚlistdirÚinsertÚjoinÚget_module_name_from_zip)ÚfilenameÚmoduleÚextÚmodule_pathÚparentZbnameÚmodule_name© r   ú:/tmp/pip-install-5sebvq2j/apptools/apptools/logger/util.pyÚget_module_name   s    r   c       	      C   sþ   | }d }xXt |ƒ r`t|ƒd dkr`t|ƒd dkr`tjj|ƒ\}}|d k	rZ|d | }q
|}q
W t |ƒsnd S |jdƒs‚|jdƒrŽ|dd … }|jddƒ}tt|ƒƒ\}}|g}t	|ƒ}t
|ƒ}x*t||d ƒrê|jdt|ƒƒ t
|ƒ}qÂW |jƒ  dj|ƒS )Né   ú\ú/z/__init__.pyr   r	   )r   r   r
   r   ÚsplitÚ
startswithÚreplacer   r   r   r   Úpath_exists_in_zipr   Úcloser   )	r   ÚfilepathÚzippathÚtailr   r   r   ÚzZ
parentpathr   r   r   r   5   s.    r   c          	   C   s(   y| j |ƒ d}W n   d}Y nX |S )NTF)Úgetinfo)Úzfiler   r   r   r   r   r    b   s    

r    c             C   sD   | }x6t |ƒ r:t|ƒd dkr:t|ƒd dkr:t|ƒ}qW t |ƒS )z0 Returns True if the path refers to a zip file. r   r   r   )r   r   r   )r   r"   r   r   r   Úis_zip_pathm   s    r(   c             C   sf   | }d}xXt |ƒ r`t|ƒd dkr`t|ƒd dkr`tjj|ƒ\}}|dk	rZ|d | }q
|}q
W |S )zj Returns the path to the zip file contained in the filename.

    fixme: An example here would help.

    Nr   r   r   )r   r   r
   r   r   )r   r"   r#   r$   r   r   r   Úget_zip_pathy   s    r)   )Ú__doc__r
   Úos.pathr   r   r   r   r   Úzipfiler   r   r   r   r    r(   r)   r   r   r   r   Ú<module>   s   &-