a
    ¨ˆc@$  ã                   @   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 )ú9 A representation of files and folders in a file system. é    N)ÚBoolÚHasPrivateTraitsÚInstanceÚListÚProperty)ÚStrc                       s6  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!d"„Z%d#d$„ Z&d%d&„ Z'd'd(„ Z(d)d*„ Z)d+d,„ Z*d-d.„ Z+‡  Z,S )0ÚFiler   c                    s   t t| ƒjf d|i|¤Ž dS )z5 Creates a new representation of the specified path. ÚpathN)Úsuperr	   Ú__init__)Úselfr
   Ztraits©Ú	__class__© úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/io/file.pyr   M   s    zFile.__init__c                 C   s
   d| j  S )z< Returns an 'informal' string representation of the object. zFile(%s))r
   ©r   r   r   r   Ú__str__R   s    zFile.__str__c                 C   s   t j | j¡S )z0 Returns the absolute path of this file/folder. )Úosr
   Úabspathr   r   r   r   Ú_get_absolute_path]   s    zFile._get_absolute_pathc                 C   s@   | j r8g }t | j¡D ]}| ttj | j|¡ƒ¡ qnd}|S )zmReturns 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_childrenb   s    zFile._get_childrenc                 C   s   t j | j¡S )z3 Returns True if the file exists, otherwise False. )r   r
   Úexistsr   r   r   r   Ú_get_existss   s    zFile._get_existsc                 C   s   t j | j¡\}}|S )z Returns the file extension. )r   r
   Úsplitext)r   r   Úextr   r   r   Ú_get_extx   s    zFile._get_extc                 C   s   | j otj | j¡S )z0 Returns True if the path exists and is a file. )r   r   r
   Úisfiler   r   r   r   Ú_get_is_file   s    zFile._get_is_filec                 C   s   | j otj | j¡S )z2 Returns True if the path exists and is a folder. )r   r   r
   Úisdirr   r   r   r   Ú_get_is_folder„   s    zFile._get_is_folderc                 C   s   | j odt | j¡v S )z: Returns True if the path exists and is a Python package. ú__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¡d }||@ dkr,d}qPd}n| jrLt | 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¡\}}|du 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¡}t j |¡\}}|S )z? Returns the last component of the path without the extension. )r   r
   Úbasenamer    )r   r5   r   r!   r   r   r   Ú	_get_name±   s    zFile._get_namec                 C   s   t tj | j¡ƒS )z) Returns the parent of this file/folder. )r	   r   r
   Údirnamer   r   r   r   Ú_get_parentº   s    zFile._get_parentc                 C   s   d| j  d¡ S )z Returns the path as a URL. z
file:///%sú/)Úabsolute_pathÚlstripr   r   r   r   Ú_get_url¿   s    zFile._get_urlc                 C   sD   t |tƒst|ƒ}| jr*t | j|j¡ n| jr@t | j|j¡ dS )z Copies this file/folder. N)Ú
isinstancer	   r   ÚshutilÚcopytreer
   r+   Úcopyfile©r   Zdestinationr   r   r   ÚcopyÇ   s    
z	File.copyÚ c                 C   s6   | j rtd| j ƒ‚t| jdƒ}| |¡ | ¡  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¡ dS )z^Creates a folder at this path.

        All intermediate folders MUST already exist.

        úfolder %s already existsN)r   rE   r
   r   Úmkdirr   r   r   r   Úcreate_folderÞ   s    zFile.create_folderc                 C   s$   | j rtd| j ƒ‚t | j¡ dS )zoCreates a folder at this path.

        This will attempt to create any missing intermediate folders.

        rL   N)r   rE   r
   r   Úmakedirsr   r   r   r   Úcreate_foldersê   s    zFile.create_foldersc                 C   s@   | j rtd| j ƒ‚t | j¡ ttj | jd¡ƒ}| ¡  dS )zhCreates a package at this path.

        All intermediate folders/packages MUST already exist.

        zpackage %s already existsr'   N)r   rE   r
   r   rM   r	   r   rK   )r   Úinitr   r   r   Úcreate_packageö   s
    zFile.create_packagec                 C   s:   | j r|  ¡  t | j¡ n| jr6|  ¡  t | j¡ dS )z\Deletes this file/folder.

        Does nothing if the file/folder does not exist.

        N)r   Úmake_writeabler>   Úrmtreer
   r+   r   Úremover   r   r   r   Údelete  s    zFile.deletec                 C   s‚   | j rXt | j¡D ]B\}}}|| D ].}tj ||¡}t |tj¡s$t |tj	¡ q$qn&| j
r~t | jtj¡s~t | 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     s    zFile.make_writeablec                 C   s.   t |tƒst|ƒ}|  ¡  t | j|j¡ dS )z Moves this file/folder. N)r=   r	   rS   r>   Úmover
   rA   r   r   r   r]   /  s    
z	File.move)rC   )-Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r:   r   r   r!   r   r   r+   r   Ú
is_packageZis_readonlyr2   r   r   Úparentr
   Úurlr   r   r   r   r   r"   r$   r&   r(   r/   r4   r6   r8   r<   rB   rK   rN   rP   rR   rV   rS   r]   Ú__classcell__r   r   r   r   r	      sH   		

r	   )ra   r0   r   r>   r*   Z
traits.apir   r   r   r   r   r   r	   r   r   r   r   Ú<module>
   s   