a
    ¨ˆcG  ã                	   @   sD  d Z ddlZddlZddlZddlmZmZ ddl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 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 'e(¡Z)dZ*dZ+dZ,ej-e ƒ eƒ gej.e"ƒ ge,eƒ ge+e*dgiZ/G dd„ dee&ƒZ0dS )z A Python File System context. é    N)ÚjoinÚsplitext)ÚFile)ÚAnyÚDictÚInstanceÚPropertyÚStré   )ÚAddress)ÚBinding)ÚContext)Ú
DirContext)Ú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ÚPyFSContextzdA Python File System context.

    This context represents a directory on a local file system.

    c                    s    t t| ƒjf i |¤Ž i | _dS )z Creates a new context. N)Úsuperr   Ú__init__Ú_cache)ÚselfZtraits©Ú	__class__© ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/naming/pyfs_context.pyr   }   s    zPyFSContext.__init__c                 C   sJ   d| j v r,| j d }| jt|ƒd d… }n| j}d | 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.   |   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¡}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   r4   Ú	referencer   r   r   Ú_get_referenceº   s    þzPyFSContext._get_referencec                 C   s
   || j v 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 v r| j | }n´t| j| j| ƒ}|  ¡ D ]D}| |¡r0z| |¡}W n"   t|ƒ}t 	d| ¡ Y n0  q²q0t
j |¡r�|  ||¡}n"t
j |¡r¦t|ƒ}ntd| ƒ‚t ||| ¡}|| 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  ||| ¡}t|tƒr,|js&| ¡  |}nH|  ¡ D ]2}| |¡r4| t	| j
|ƒ|¡}tj
 |¡} qtq4t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   |   ||¡ 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v r<| j|= || jv rV| 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   Ú_unbind$  s    

zPyFSContext._unbindc                 C   sB  | j | }tt| j|ƒƒ}|  |¡}|jrl|}tt| j|ƒƒ}| |¡ |j|_|| j|< | j|= | ¡  n”t	|tƒr°|}tt| j|ƒƒ}| |¡ |j|_|| j|< | j|= nP||j
 }tt| j|ƒƒ}| ¡  || jv rô| j| | j|< | j|= |  ||¡ | j |= || j |< || jv �r>| j| | j|< | j|= |  ¡  dS )z$ Renames an object in this context. N)r*   r   r   r#   rE   Z	is_folderÚmover   r/   rF   ÚextrM   rL   rN   rO   )r   Zold_nameÚnew_nameZold_filenameZold_filer,   Znew_filenameZnew_filer   r   r   Ú_rename:  s>    



	







zPyFSContext._renamec                 C   s:   t | j|ƒ}t |¡ |  ||¡}|| 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
   |   |¡S )z) Destroys a sub-context of this context. )rQ   r8   r   r   r   Ú_destroy_subcontext   s    zPyFSContext._destroy_subcontextc                 C   s   t | j ¡ ƒS )z( Lists the names bound in this context. )Úlistr*   Úkeysr.   r   r   r   Ú_list_names¥  s    zPyFSContext._list_namesc                    sj   t |ƒd }|dkr$tt| ƒ |¡S t |ƒd }|  ¡ }d}|}||v rf|d t|ƒ d }|d7 }q@|S )Nr
   z.pyr   é   Ú_)r   r   r   Úget_unique_nameZ
list_namesÚstr)r   r+   rS   ÚbodyÚnamesÚiÚuniquer   r   r   r_   ¬  s    
zPyFSContext.get_unique_namec                 C   s   | j  |i ¡}| ¡ 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 |< |  ¡  dS )z3 Sets the attributes of an object in this context. N)rN   rO   rg   r   r   r   Ú_set_attributesÉ  s    
zPyFSContext._set_attributesc                 C   s   | j  | jg ¡S )z' Returns the filters for this context. )r"   ÚgetÚFILTERSr.   r   r   r   Ú_get_filtersÓ  s    zPyFSContext._get_filtersc                 C   s   | j  | jg ¡S )z2 Returns the object serializers for this context. )r"   rk   ÚOBJECT_SERIALIZERSr.   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|d¡ | ¡  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„ˆ   ¡ D ƒ}i }t ˆ j¡D ]d}tˆ j|ƒ}|D ]}|t |¡v r: q&q:ˆ  ¡ D ]"}| |¡rZtj |¡\}} q‚qZ|}|||< q&|S )z, Initializes the '_name_to_filename' trait. c                    s   g | ]}t ˆ j|ƒ‘qS r   )r   r#   )Ú.0Úfilterr.   r   r   Ú
<listcomp>ó  ó    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 |¡}| ¡  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¡}t 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_changed  s    zPyFSContext._path_changed)-r5   Ú
__module__Ú__qualname__Ú__doc__rp   rl   rn   r   ÚENVIRONMENTr"   r   r	   r(   r+   r#   r   r   r6   r*   r   rN   r   r)   r/   r7   r9   rE   rK   rL   rQ   rU   rX   rY   r\   r_   ri   rj   rm   r:   r@   rO   Z*_PyFSContext__name_to_filename_map_defaultZ _PyFSContext__attributes_defaultr   Ú__classcell__r   r   r   r   r   F   sD   
."S
r   )1r‚   rz   Úloggingr&   Úos.pathr   r   rr   Zapptools.io.apir   Z
traits.apir   r   r   r   r	   Úaddressr   Zbindingr   r-   r   Zdir_contextr   Znaming_eventr   r   Zobject_serializerr   Zpyfs_context_factoryr   Zpyfs_object_factoryr   Zpyfs_state_factoryr   r6   r   Zreferenceabler   Ú	getLoggerr5   r=   rp   rl   rn   ZOBJECT_FACTORIESZSTATE_FACTORIESrƒ   r   r   r   r   r   Ú<module>
   s:   

ò