3
AAd]ÇI  ã            	   @   sT  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 ddlm	Z	 ddl
mZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ej*e+ƒZ,dZ-dZ.dZ/ej0e#ƒ e!ƒ gej1e%ƒ ge/eƒ ge.e-dgiZ2G dd„ dee)ƒZ3dS )z A Python File System context. é    N)ÚjoinÚsplitext)ÚFile)ÚAnyÚDictÚInstanceÚPropertyÚStré   )ÚAddress)ÚBinding)ÚContext)Ú
DirContext)ÚNameNotFoundErrorÚNotContextError)ÚNamingEvent)Únaming_manager)ÚObjectSerializer)ÚPyFSContextFactory)ÚPyFSObjectFactory)ÚPyFSStateFactory)Ú	Reference)ÚReferenceableZ__attributes__zapptools.naming.pyfs.filtersz'apptools.naming.pyfs.object.serializersz.svnc                   s  e Zd ZdZeZeZeZeeƒZ	e
eƒZeZeZe
eeƒƒZeZ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!‡ 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+‡  Z,S )0ÚPyFSContextze A Python File System context.

    This context represents a directory on a local file system.

    c                s   t t| ƒjf |Ž i | _dS )z Creates a new context. N)Úsuperr   Ú__init__Ú_cache)ÚselfÚtraits)Ú	__class__© úx/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/naming/pyfs_context.pyr   „   s    zPyFSContext.__init__c             C   sJ   d| j kr,| j d }| jt|ƒd d… }n| j}dj|jtjjƒƒ}|S )z; Returns the name of the context within its own namespace. Úrootr
   Nú/)ÚenvironmentÚpathÚlenr   ÚsplitÚosÚsep)r   r"   Únamespace_namer    r    r!   Ú_get_namespace_name–   s    

zPyFSContext._get_namespace_namec             C   s.   | j dgƒ i | _tt| j| dd�d�| _dS )z< Refresh the context to reflect changes in the file system. Ú_name_to_filename_mapN)ÚnameÚobjÚcontext)Znew_binding)Zreset_traitsr   r   r   r-   Zcontext_changed)r   r    r    r!   Úrefreshª   s
    zPyFSContext.refreshc             C   s,   t jj| jƒ}t| jjtd|d�gd�}|S )z: Returns a reference to this object suitable for binding. Zpyfs_context)ÚtypeÚcontent)Ú
class_nameÚ	addresses)r(   r%   Úabspathr   r   Ú__name__r   )r   r5   Ú	referencer    r    r!   Ú_get_referenceÅ   s
    zPyFSContext._get_referencec             C   s
   || j kS )z" Is a name bound in this context? )r,   )r   r-   r    r    r!   Ú	_is_boundÕ   s    zPyFSContext._is_boundc          
   C   sÐ   || j kr| j | }n¶t| j| j| ƒ}xŠ| jƒ D ]B}|j|ƒr2y|j|ƒ}W n"   t|ƒ}tj	d| ƒ Y nX P q2W t
jj|ƒr’| j||ƒ}n"t
jj|ƒr¨t|ƒ}ntd| ƒ‚tj||| ƒ}|| j |< |S )z" Looks up a name in this context. zError loading resource at %szunrecognized file for %s)r   r   r%   r,   Ú_get_object_serializersÚcan_loadÚloadr   ÚloggerÚ	exceptionr(   ÚisdirÚ_context_factoryÚisfileÚ
ValueErrorr   Zget_object_instance)r   r-   r.   r%   Ú
serializerÚstater    r    r!   Ú_lookupÚ   s&    



zPyFSContext._lookupc             C   sŽ   t j||| ƒ}t|tƒr,|js&|jƒ  |}nJxH| jƒ D ]0}|j|ƒr6|jt	| j
|ƒ|ƒ}tj
j|ƒ}P q6W td| ƒ‚|| j|< || j|< |S )z, Binds a name to an object in this context. zcannot serialize object %s)r   Zget_state_to_bindÚ
isinstancer   ÚexistsZcreate_filer:   Zcan_saveÚsaver   r%   r(   ÚbasenamerB   r,   r   )r   r-   r.   rD   ÚfilenamerC   r%   r    r    r!   Ú_bind  s    



zPyFSContext._bindc             C   s   | j ||ƒ dS )z. Rebinds a name to an object in this context. N)rK   )r   r-   r.   r    r    r!   Ú_rebind*  s    zPyFSContext._rebindc             C   sZ   t | j| j| ƒ}t|ƒ}|jƒ  | j|= || jkr<| j|= || jkrV| j|= | jƒ  dS )z# Unbinds a name from this context. N)r   r%   r,   r   Údeleter   Ú_attributesÚ_save_attributes)r   r-   r%   Úfr    r    r!   Ú_unbind6  s    

zPyFSContext._unbindc             C   sB  | j | }tt| j|ƒƒ}| j|ƒ}|jrl|}tt| j|ƒƒ}|j|ƒ |j|_|| j|< | j|= |jƒ  n”t	|tƒr°|}tt| j|ƒƒ}|j|ƒ |j|_|| j|< | j|= nP||j
 }tt| j|ƒƒ}|jƒ  || jkrô| j| | j|< | j|= | j||ƒ | j |= || j |< || jk�r>| j| | j|< | j|= | jƒ  dS )z$ Renames an object in this context. N)r,   r   r   r%   rE   Z	is_folderÚmover   r0   rF   ÚextrM   rL   rN   rO   )r   Úold_nameÚnew_nameZold_filenameZold_filer.   Znew_filenameZnew_filer    r    r!   Ú_renameN  s@    



	







zPyFSContext._renamec             C   s:   t | j|ƒ}tj|ƒ | j||ƒ}|| j|< || j|< |S )z( Creates a sub-context of this context. )r   r%   r(   Úmkdirr@   r,   r   )r   r-   r%   Úsubr    r    r!   Ú_create_subcontext£  s    


zPyFSContext._create_subcontextc             C   s
   | j |ƒS )z) Destroys a sub-context of this context. )rQ   )r   r-   r    r    r!   Ú_destroy_subcontext¶  s    zPyFSContext._destroy_subcontextc             C   s   t | jjƒ ƒS )z( Lists the names bound in this context. )Úlistr,   Úkeys)r   r    r    r!   Ú_list_names»  s    zPyFSContext._list_namesc                sn   t |ƒd }|dkr$tt| ƒj|ƒS t |ƒd }| jƒ }d}|}x(||krh|d t|ƒ d }|d7 }qBW |S )Nr
   z.pyr   é   Ú_)r   r   r   Úget_unique_nameZ
list_namesÚstr)r   r-   rS   ÚbodyÚnamesÚiÚunique)r   r    r!   r`   Â  s    
zPyFSContext.get_unique_namec             C   s   | j j|i ƒ}|jƒ S )z6 Returns the attributes of an object in this context. )rN   Ú
setdefaultÚcopy)r   r-   Ú
attributesr    r    r!   Ú_get_attributesØ  s    zPyFSContext._get_attributesc             C   s   || j |< | jƒ  dS )z3 Sets the attributes of an object in this context. N)rN   rO   )r   r-   rh   r    r    r!   Ú_set_attributesß  s    
zPyFSContext._set_attributesc             C   s   | j j| jg ƒS )z' Returns the filters for this context. )r$   ÚgetÚFILTERS)r   r    r    r!   Ú_get_filtersë  s    zPyFSContext._get_filtersc             C   s   | j j| jg ƒS )z2 Returns the object serializers for this context. )r$   rk   ÚOBJECT_SERIALIZERS)r   r    r    r!   r:   ð  s    z#PyFSContext._get_object_serializersc             C   s   | j || jd�S )z Create a sub-context. )r%   r$   )r   r$   )r   r-   r%   r    r    r!   r@   õ  s    zPyFSContext._context_factoryc             C   s4   t | j| jƒ}t|dƒ}tj| j|dƒ |jƒ  dS )z. Saves all attributes to the attributes file. Úwbr
   N)r   r%   ÚATTRIBUTES_FILEÚopenÚpickleÚdumprN   Úclose)r   r%   rP   r    r    r!   rO   ú  s
    
zPyFSContext._save_attributesc       	         s˜   ‡ fdd„ˆ j ƒ D ƒ}i }xxtjˆ jƒD ]h}tˆ j|ƒ}xV|D ]}|tj|ƒkr>P q>W x0ˆ jƒ D ] }|j|ƒr`tjj|ƒ\}}P q`W |}|||< q(W |S )z, Initializes the '_name_to_filename' trait. c                s   g | ]}t ˆ j|ƒ‘qS r    )r   r%   )Ú.0Úfilter)r   r    r!   ú
<listcomp>  s    z>PyFSContext.__name_to_filename_map_default.<locals>.<listcomp>)	rm   r(   Úlistdirr%   r   Úglobr:   r;   r   )	r   ÚpatternsZname_to_filename_maprJ   r%   ÚpatternrC   r-   rS   r    )r   r!   Z__name_to_filename_map_default  s    

z*PyFSContext.__name_to_filename_map_defaultc             C   s@   t t| j| jƒƒ}|jr8t|jdƒ}tj|ƒ}|jƒ  ni }|S )z& Initializes the '_attributes' trait. Úrb)	r   r   r%   rp   Úis_filerq   rr   r<   rt   )r   Zattributes_filerP   rh   r    r    r!   Z__attributes_default&  s    

z PyFSContext.__attributes_defaultc             C   s$   t jj| jƒ}t jj|ƒ\| _}dS )z- Called when the context's path has changed. N)r(   r%   rI   r   r-   )r   rI   rS   r    r    r!   Ú_path_changed6  s    zPyFSContext._path_changed)-r6   Ú
__module__Ú__qualname__Ú__doc__rp   rl   rn   r   ÚENVIRONMENTr$   r   r	   r*   r-   r%   r   r   r7   r,   r   rN   r   r+   r0   r8   r9   rE   rK   rL   rQ   rV   rY   rZ   r]   r`   ri   rj   rm   r:   r@   rO   Z*_PyFSContext__name_to_filename_map_defaultZ _PyFSContext__attributes_defaultr~   Ú__classcell__r    r    )r   r!   r   M   sD   
."Ur   )4r�   rr   ry   Úloggingr(   Úos.pathr   r   Zapptools.io.apir   Z
traits.apir   r   r   r   r	   Úaddressr   Úbindingr   r/   r   Zdir_contextr   r>   r   r   Znaming_eventr   r   Zobject_serializerr   Zpyfs_context_factoryr   Zpyfs_object_factoryr   Zpyfs_state_factoryr   r7   r   Zreferenceabler   Ú	getLoggerr6   r=   rp   rl   rn   ZOBJECT_FACTORIESZSTATE_FACTORIESr‚   r   r    r    r    r!   Ú<module>   s4    

