3
6ßL]u\  ã               @   sª   d Z ddlZddlZddlZddlmZ ddlZddlm	Z	 ddl
mZmZmZmZmZmZmZmZ ddlmZmZ ddlmZ ddlmZ ejeƒZG d	d
„ d
eƒZdS )zP
A base class for projects that can be displayed by the single_project
plugin.

é    N)Ú	ETSConfig)ÚFile)ÚAnyÚBoolÚDictÚ	DirectoryÚ	HasTraitsÚInstanceÚPropertyÚStr)ÚGroupÚView)Úclean_filename)ÚApplicationc            	       s€  e Zd ZdZdZeedd�ZdZe	ddd�Z
e	ddd�Ze	ddd�ZeZeeƒZeedƒddd	d
gdddd�Zedd�Z‡ fdd„Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ ZeeƒZdd„ Zdd„ ZeeƒZdd„ ZeeƒZdd„ ZeeƒZd>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'd0d1„ Z(ee(ƒZ(d2d3„ Z)d4d5„ Z*d6d7„ Z+ee+ƒZ+d8d9„ Z,d:d;„ Z-d<d=„ Z.‡  Z/S )@ÚProjectz\
    A base class for projects that can be displayed by the single_project
    plugin.

    T)Ú	transientz%s_%sFÚlocationzNew Projectz'envisage.single_project.project.ProjectÚOKZCancelg…ëQ¸Õ?)ÚtitleÚidZbuttonsÚwidthZclose_resultZ	resizablec                sH   t t| ƒjƒ jƒ }x | jdd�D ]}|j|dƒ q W d|d< d|d< |S )a9  
        Get the state of this object for pickling.

        Extended to limit which attributes get pickled and also to add
        version numbers to our pickle.

        It is STRONGLY recommended that derived classes not leverage the
        pickling mechanism if they wish to store additional data in OTHER
        files as part of their project.  Instead, they should override the
        *_save* method.

        Extending this method in derived classes IS appropriate if you want
        to store additional data in the SAME pickle file as the project.

        T)r   Né   Ú_project_version_majorr   Ú_project_version_minor)Úsuperr   Ú__getstate__ÚcopyZtrait_namesÚpop)ÚselfÚstateZ
trait_name)Ú	__class__© úH/tmp/pip-install-5sebvq2j/envisage/envisage/ui/single_project/project.pyr   �   s    zProject.__getstate__c                sJ   |j ddƒ}|j ddƒ}|dk r:xdD ]}|j |dƒ q&W tt| ƒj|ƒS )aû  
        Restore the state of this object during unpickling.

        Extended to upgrade pickles to the current Project version.

        It is STRONGLY recommended that derived classes not leverage the
        unpickling mechanism if they wish to load data from additional pickle
        files. Instead, they should override the *_load* method.

        Extending this method in derived classes IS appropriate if you want
        to load additional data from the SAME pickle file as the project.

        r   r   r   r   ÚnameN)r#   )r   r   r   Ú__setstate__)r   r   ÚmajorÚminorÚkey)r    r!   r"   r$   ¢   s    
zProject.__setstate__c                s   dt t| ƒjƒ | jf }|S )zN
        Return the unofficial string representation of this object.

        z%s(name=%s))r   r   Ú__str__r#   )r   Úresult)r    r!   r"   r(   Á   s    zProject.__str__c             C   s2   | j |ƒ}t| jƒ ƒ}tjj||ƒ}| j|ƒ}|S )zA
        Return the default location for a new project.

        )Úget_default_pathr   Úget_default_nameÚosÚpathÚjoinÚ_make_location_unique)r   Úapplicationr-   r#   r   r!   r!   r"   Úget_default_project_locationÑ   s
    

z$Project.get_default_project_locationc             C   sH   |dkrt jS |j}d}|j|ƒ}t|ƒdkrDt j}|j||ƒ |S |S )zU
        Return the default path to the parent directory for a new project.

        Nz)envisage.ui.single_project.preferred_pathr   )r   Zapplication_homeZpreferencesÚgetÚlenÚset)Úclsr0   Zapp_preferencesZpath_idr-   Zapp_homer!   r!   r"   r*   Þ   s    
zProject.get_default_pathc             C   s   dS )z=
        Return the default name for a new project.

        zNew Projectr!   )r   r!   r!   r"   r+   ù   s    zProject.get_default_namec             C   s   | j r|}ntjj|dƒ}|S )aƒ  
        Generate the project's pickle filename given a source location.

        By default, the filename IS the specified location or, when saving
        projects as directories, a file called 'project' within a directory
        that is the specified location,

        Derived classes may wish to use the location as the basis for
        identifying the real pickle file.

        Úproject)ÚPROJECTS_ARE_FILESr,   r-   r.   )r5   r   r)   r!   r!   r"   Úget_pickle_filename  s    zProject.get_pickle_filenamec             C   s   t jS )aY  
        Returns the pickle package to use for pickling and unpickling
        projects.

        Implementors can override this to customize the way in which
        projects are pickled and unpickled.

        This implementation returns the apptools.sweet_pickle package which
        supports versioning and refactoring of classes.

        )ÚapptoolsZsweet_pickle)r5   r!   r!   r"   Úget_pickle_package  s    zProject.get_pickle_packagec             C   s    | j |ƒ}||_d|_||_|S )a3  
        Load a project from a specified location.

        The loaded project's location is always set to the location the project
        was actually loaded from.  Additionally, the dirty flag is cleared
        on the loaded project.

        An exception will be raised to indicate a failure.

        F)Ú_loadr   Údirtyr0   )r5   r   r0   r6   r!   r!   r"   Úload(  s
    
zProject.loadc             C   s8   | j |ƒstjd| |f ƒ |s,|| j|< n| j|= dS )a  
        Inform us that an editor has been opened for what is believed to be
        a resource in this project.

        Note that if this project can be represented as a hierarchy, the
        resource may not be a top level object in that hierarchy!

        z0This Project [%s] does not contain resource [%s]N)Ú_contains_resourceÚloggerÚwarnÚ_editors)r   ÚresourceÚeditorÚremover!   r!   r"   Úregister_editorC  s    
zProject.register_editorNc             C   sô   |dks|| j kr"| js:tdƒ‚n|| j kr:| js:tdƒ‚| j }|dk	rŽ|jƒ }t|ƒdkrŽ|| j krŽtjj|ƒrŠ|dkrŠtdd|  ƒ‚|}| j	r¦tjj
|ƒ\}}n|}t|ƒdkrÚt|ƒ}|jrÌ|jƒ  |jsÚ|jƒ  | j|ƒ || _ d| _dS )a  
        Save this project.

        The project is saved to its current location, identified by the value
        of the *location* trait, unless a new location is explicitly provided.
        The specification of a new location is used to do a 'save as'
        operation.

        If a new location is provided, and a file or directory already exists
        at that location, then an *AssertionError* exception is raised unless
        the overwrite flag is True.  This ensures that users won't accidentally
        overwrite existing data.

        This method requires the overwrite flag because prompting the user to
        confirm the overwrite requires GUI interaction and thus should not be
        done at the model level.

        Note that, because we can rely on the location of a loaded project
        always being set to the location it was loaded from, there is no reason
        to try to force the *location* trait within a saved project to the
        location we are trying to save to.  Instead, we update the value in
        the in-memory version of the project only if the save completed
        successfully.

        The dirty flag is always cleared upon a succesfull save of the project.

        An exception will be raised to indicate a failure.

        Nz Saving is currently not allowed.z!Save as is currently not allowed.r   FzCan not overwrite existing zlocation [%s])r   Úis_save_allowedÚAssertionErrorÚis_save_as_allowedÚstripr3   r,   r-   Úexistsr7   Úsplitr   Úis_fileÚdeleteZcreate_foldersÚ_saver<   )r   r   Ú	overwriteÚlocr-   ÚfilenameÚfr!   r!   r"   ÚsaveY  s6     


zProject.savec             C   s   t jd| ƒ i | _dS )a¸  
        Notify this project that it is now the 'current' project.

        This call should only be made by the project plugin framework!

        This call *could* happen multiple times to a project, but
        only if interwoven with paired calls to the 'stop' method.

        Derived classes should override this, and chain the base
        implementation, if they need to do anything when a project
        becomes current.

        zProject [%s] startedN)r?   ÚdebugrA   )r   r!   r!   r"   Ústart­  s    zProject.startc             C   s   | j ƒ  tjd| ƒ dS )a«  
        Called only by the project plugin framework to notify this
        project it is no longer the current project.

        This call *could* happen multiple times to a project, but
        only if interwoven with paired calls to the 'start' method.

        Derived classes should override this, and chain the base
        implementation, if they need to do anything when a project
        stops being current.

        zProject [%s] stoppedN)Ú_close_all_editorsr?   rT   )r   r!   r!   r"   ÚstopÂ  s    zProject.stopc             C   s,   x&| j jƒ D ]}tjd|ƒ |jƒ  qW dS )zL
        Called to close all editors associated with this project.

        z.Project requesting close of ProjectEditor [%s]N)rA   Úvaluesr?   rT   Úclose)r   rC   r!   r!   r"   rV   Ø  s    	zProject._close_all_editorsc             C   sN   t |tƒs|g}x8|D ]0}| jj|dƒ}|dk	rtjd|| ƒ |jƒ  qW dS )a=  
        Close any editors associated with the specified resource(s).

        The passed value may be a single resource or a list of resources.  The
        resources should be parts of this project but no error is generated if
        they are not, nor if they are not currently associated with an editor.

        Nz,Requesting close of ProjectEditor [%s] from zProject [%s]z8Requesting close of ProjectEditor [%s] from Project [%s])Ú
isinstanceÚlistrA   r2   r?   rT   rY   )r   rB   ÚrrC   r!   r!   r"   Ú_close_resource_editorsç  s    


zProject._close_resource_editorsc             C   s   dS )a  
        Called to determine if this project contains the specified
        resource.

        Note that if this project can be represented as a hierarchy, the
        resource may not be a top level object in that hierarchy!

        Derived classes must implement this!

        Fr!   )r   rB   r!   r!   r"   r>      s    zProject._contains_resourcec             C   sH   | j dk	r t| j ƒdkr | j }n$| jjtjjƒ}tjj|ƒ| _ | j }|S )zÕ
        Returns the current name for this project.

        The name is always the last part of the path that is the project's
        location.  If we have no location, then a default name is returned.

        Nr   )Ú_namer3   r   Úrstripr,   r-   ÚsepÚbasename)r   r)   r   r!   r!   r"   Ú	_get_name  s    
zProject._get_namec             C   s†   | j |ƒ}tjd| |ƒ d}z8t|dƒ}| jƒ }|j|ƒ}|j|ƒ tjd||ƒ W d|r€y|jƒ  W n   tjd|ƒ Y nX X |S )am  
        Load a project from the specified location.

        This method exists purely to allow derived classes to have an easy way
        to override or extend the loading of a project.  The default behavior
        is to load the project from a file using the unpickle mechanism.

        The caller is notified of loading errors by raised exceptions.

        z'Loading Project of class [%s] from [%s]NÚrbz&Loaded Project [%s] from location [%s]z!Unable to close project file [%s])	r8   r?   rT   Úopenr:   r=   Ú
_load_hookrY   Ú	exception)r5   r   rQ   ÚfhÚpickle_packager6   r!   r!   r"   r;   *  s$    




zProject._loadc             C   s   dS )aÄ  
        Finish loading of a project.

        This method exists purely to allow derived classes to customize the
        steps that finish the loading of a project.

        Note that the project's internal location value does not yet reflect
        the location the project was loaded from, nor do we guarantee the dirty
        flag isn't set.  (Doing the right thing to both of these is done by the
        framework after this method!)

        Nr!   )r   r   r!   r!   r"   re   X  s    zProject._load_hookc             C   s   | j | jƒS )zF
        Generates the default value for our location trait.

        )r1   r0   )r   r!   r!   r"   Ú_location_defaulti  s    zProject._location_defaultc             C   s4   |}d}x&t jj|ƒr.| j||f }|d7 }q
W |S )z~
        Return a location, based off the specified location, that does
        not already exist on the filesystem.

        r   )r,   r-   rJ   Ú_unique_name_format)r5   r   r)   Úcounterr!   r!   r"   r/   r  s    zProject._make_location_uniquec             C   sŽ   | j |ƒ}tjd| |ƒ d}z<| j|ƒ t|dƒ}| jƒ }|j| |dƒ tjd| |ƒ W dy|dk	rl|jƒ  W n   tjd|ƒ Y nX X dS )ai  
        Save this project to the specified location.

        This method exists purely to allow derived classes to have an easy way
        to override or extend the saving of a project.  The default behavior is
        to save this project to a file using the pickle mechanism.

        The caller is notified of saving errors by raised exceptions.

        zSaving Project [%s] to [%s]NÚwbr   zSaved Project [%s] to [%s]z(Unable to close project pickle file [%s])	r8   r?   rT   Ú
_save_hookrd   r:   ÚdumprY   rf   )r   r   rQ   rg   rh   r!   r!   r"   rN   ƒ  s     


zProject._savec             C   s   dS )a   
        Start saving a project.

        This method exists purely to allow derived classes to customize the
        steps that initiate the saving of a project.

        Note that the project's internal location value does not reflect the
        target location for the save.

        Nr!   )r   r   r!   r!   r"   rm   ®  s    zProject._save_hookc             C   s6   t jd||| ƒ | j}d| _| jd|| jƒ d| _dS )zB
        Called whenever the project's location changes.

        z3Location changed from [%s] to [%s] for Project [%s]Ú r#   TN)r?   rT   r^   Ztrait_property_changedr#   r<   )r   ÚoldÚnewZold_namer!   r!   r"   Ú_location_changed¿  s    
zProject._location_changed)F)NF)0Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   r	   r   r0   rj   r   r<   rF   rH   r   r   r
   r   r#   r   r   Ztraits_viewr^   r   r$   r(   r1   r*   Úclassmethodr+   r8   r:   r=   rE   rS   rU   rW   rV   r]   r>   rb   r;   re   ri   r/   rN   rm   rr   Ú__classcell__r!   r!   )r    r"   r   (   s`   

	!

T+	+r   )rv   Úloggingr,   ÚsysZtraits.etsconfig.apir   Zapptools.sweet_pickler9   Zapptools.io.apir   Z
traits.apir   r   r   r   r   r	   r
   r   Ztraitsui.apir   r   Ztraits.util.clean_stringsr   Zenvisage.apir   Ú	getLoggerrs   r?   r   r!   r!   r!   r"   Ú<module>   s   (
