3
AAd]¢&  ã               @   s`   d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
 ddlmZ G dd„ deƒZdS )z9 A representation of files and folders in a file system. é    N)ÚBoolÚHasPrivateTraitsÚInstanceÚListÚProperty)ÚStrc                   s>  e Zd ZdZeeƒZeed ƒƒZeeƒZ	ee
ƒZee
ƒZee
ƒZee
ƒZee
ƒZeeƒZeeƒZeed ƒƒZeZeeƒZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$d d!„ Z%d1d#d$„Z&d%d&„ Z'd'd(„ Z(d)d*„ Z)d+d,„ Z*d-d.„ Z+d/d0„ Z,‡  Z-S )2ÚFilez9 A representation of files and folders in a file system. c                s   t t| ƒjf d|i|—Ž dS )z5 Creates a new representation of the specified path. ÚpathN)Úsuperr   Ú__init__)Úselfr	   Útraits)Ú	__class__© úl/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/io/file.pyr   N   s    zFile.__init__c             C   s   t |tƒrt| j|jƒS dS )z Comparison operators. é   )Ú
isinstancer   Úcmpr	   )r   Úotherr   r   r   Ú__cmp__U   s    
zFile.__cmp__c             C   s
   d| j  S )z< Returns an 'informal' string representation of the object. zFile(%s))r	   )r   r   r   r   Ú__str__\   s    zFile.__str__c             C   s   t jj| jƒS )z0 Returns the absolute path of this file/folder. )Úosr	   Úabspath)r   r   r   r   Ú_get_absolute_pathg   s    zFile._get_absolute_pathc             C   sD   | j r<g }x4tj| jƒD ]}|jttjj| j|ƒƒƒ qW nd}|S )zn Returns the folder's children.

        Returns None if the path does not exist or is not a folder.

        N)Ú	is_folderr   Úlistdirr	   Úappendr   Újoin)r   ÚchildrenÚnamer   r   r   Ú_get_childrenl   s     zFile._get_childrenc             C   s   t jj| jƒS )z3 Returns True if the file exists, otherwise False. )r   r	   Úexists)r   r   r   r   Ú_get_exists}   s    zFile._get_existsc             C   s   t jj| jƒ\}}|S )z Returns the file extension. )r   r	   Úsplitext)r   r   Úextr   r   r   Ú_get_ext‚   s    zFile._get_extc             C   s   | j otjj| jƒS )z0 Returns True if the path exists and is a file. )r!   r   r	   Úisfile)r   r   r   r   Ú_get_is_file‰   s    zFile._get_is_filec             C   s   | j otjj| jƒS )z2 Returns True if the path exists and is a folder. )r!   r   r	   Úisdir)r   r   r   r   Ú_get_is_folderŽ   s    zFile._get_is_folderc             C   s   | j odtj| jƒkS )z: Returns True if the path exists and is a Python package. z__init__.py)r   r   r   r	   )r   r   r   r   Ú_get_is_package“   s    zFile._get_is_packagec             C   sT   | j r2d}tj| jƒd }||@ dkr,d}qPd}n| jrLtj| jtjƒ }nd}|S )z? Returns True if the file/folder is readonly, otherwise False. é’   r   TF)r   r   Ústatr	   Úis_fileÚaccessÚW_OK)r   Z
WRITE_MASKZpermissionsÚreadonlyr   r   r   Ú_get_is_readonly˜   s    zFile._get_is_readonlyc             C   s    t j| jƒ\}}|dkrd}|S )z, Returns the mime-type of this file/folder. Nzcontent/unknown)Ú	mimetypesÚ
guess_typer	   )r   Ú	mime_typeÚencodingr   r   r   Ú_get_mime_type²   s    zFile._get_mime_typec             C   s"   t jj| jƒ}t jj|ƒ\}}|S )z? Returns the last component of the path without the extension. )r   r	   Úbasenamer#   )r   r7   r   r$   r   r   r   Ú	_get_name»   s    zFile._get_namec             C   s   t tjj| jƒƒS )z) Returns the parent of this file/folder. )r   r   r	   Údirname)r   r   r   r   Ú_get_parentÄ   s    zFile._get_parentc             C   s   d| j jdƒ S )z Returns the path as a URL. z
file:///%sú/)Úabsolute_pathÚlstrip)r   r   r   r   Ú_get_urlÉ   s    zFile._get_urlc             C   sD   t |tƒst|ƒ}| jr*tj| j|jƒ n| jr@tj| j|jƒ dS )z Copies this file/folder. N)r   r   r   ÚshutilÚcopytreer	   r-   Úcopyfile)r   Údestinationr   r   r   ÚcopyÑ   s    
z	File.copyÚ c             C   s6   | j rtd| j ƒ‚t| jdƒ}|j|ƒ |jƒ  dS )z Creates a file at this path. zfile %s already existsÚwN)r!   Ú
ValueErrorr	   ÚopenÚwriteÚclose)r   ÚcontentsÚfr   r   r   Úcreate_fileà   s    
zFile.create_filec             C   s$   | j rtd| j ƒ‚tj| jƒ dS )z_ Creates a folder at this path.

        All intermediate folders MUST already exist.

        zfolder %s already existsN)r!   rF   r	   r   Úmkdir)r   r   r   r   Úcreate_folderì   s    zFile.create_folderc             C   s$   | j rtd| j ƒ‚tj| jƒ dS )zp Creates a folder at this path.

        This will attempt to create any missing intermediate folders.

        zfolder %s already existsN)r!   rF   r	   r   Úmakedirs)r   r   r   r   Úcreate_foldersú   s    zFile.create_foldersc             C   s@   | j rtd| j ƒ‚tj| jƒ ttjj| jdƒƒ}|jƒ  dS )zi Creates a package at this path.

        All intermediate folders/packages MUST already exist.

        zpackage %s already existsz__init__.pyN)r!   rF   r	   r   rM   r   r   rL   )r   Úinitr   r   r   Úcreate_package  s    zFile.create_packagec             C   s:   | j r| jƒ  tj| jƒ n| jr6| jƒ  tj| jƒ dS )z] Deletes this file/folder.

        Does nothing if the file/folder does not exist.

        N)r   Úmake_writeabler?   Úrmtreer	   r-   r   Úremove)r   r   r   r   Údelete  s    zFile.deletec             C   sŠ   | j r`x~tj| jƒD ]F\}}}x:|| D ].}tjj||ƒ}tj|tjƒs(tj|tj	ƒ q(W qW n&| j
r†tj| jtjƒs†tj| jtj	ƒ dS )z, Attempt to make the file/folder writeable. N)r   r   Úwalkr	   r   r.   r/   Úchmodr,   ÚS_IWUSRr-   )r   r	   ÚdirnamesÚ	filenamesr   Úfilenamer   r   r   rS   2  s    zFile.make_writeablec             C   s.   t |tƒst|ƒ}| jƒ  tj| j|jƒ dS )z Moves this file/folder. N)r   r   rS   r?   Úmover	   )r   rB   r   r   r   r]   G  s
    
z	File.move)rD   ).Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r<   r   r   r$   r   r!   r-   r   Ú
is_packageZis_readonlyr4   r   r   Úparentr	   Úurlr   r   r   r   r    r"   r%   r'   r)   r*   r1   r6   r8   r:   r>   rC   rL   rN   rP   rR   rV   rS   r]   Ú__classcell__r   r   )r   r   r      sJ   		
r   )ra   r2   r   r?   r,   Z
traits.apir   r   r   r   r   r   r   r   r   r   r   Ú<module>   s    