3
6ßL]Ç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)ÚselfZtraits)Ú	__class__© úB/tmp/pip-install-5sebvq2j/apptools/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   r4   Ú	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_filer9   Zcan_saveÚsaver   r$   r'   ÚbasenamerA   r+   r   )r   r,   r-   rC   ÚfilenamerB   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)rJ   )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$   rD   Z	is_folderÚmover   r/   rE   ÚextrL   rK   rM   rN   )r   Z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. )rP   )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,   rR   Ú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. )rM   Ú
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)rM   rN   )r   r,   rf   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#   ri   ÚOBJECT_SERIALIZERS)r   r   r   r    r9   ð  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ÚdumprM   Úclose)r   r$   rO   r   r   r    rN   ú  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>)	rk   r'   Úlistdirr$   r   Úglobr9   r:   r   )	r   ÚpatternsZname_to_filename_maprI   r$   ÚpatternrB   r,   rR   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$   rn   Úis_filero   rp   r;   rr   )r   Zattributes_filerO   rf   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$   rH   r   r,   )r   rH   rR   r   r   r    Ú_path_changed6  s    zPyFSContext._path_changed)-r5   Ú
__module__Ú__qualname__Ú__doc__rn   rj   rl   r   ÚENVIRONMENTr#   r   r	   r)   r,   r$   r   r   r6   r+   r   rM   r   r*   r/   r7   r8   rD   rJ   rK   rP   rT   rW   rX   r[   r^   rg   rh   rk   r9   r?   rN   Z*_PyFSContext__name_to_filename_map_defaultZ _PyFSContext__attributes_defaultr|   Ú__classcell__r   r   )r   r    r   M   sD   
."Ur   )4r   rp   rw   Úloggingr'   Úos.pathr   r   Zapptools.io.apir   Z
traits.apir   r   r   r   r	   Úaddressr   Z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   r6   r   Zreferenceabler   Ú	getLoggerr5   r<   rn   rj   rl   ZOBJECT_FACTORIESZSTATE_FACTORIESr€   r   r   r   r   r    Ú<module>   s4    

