3
:Ad]Û@  ã               @   sV   d Z ddlZddlZddlmZ ddlmZ G dd„ deƒZG dd„ deƒZ	e	ƒ Z	dS )	z1 Enthought Tool Suite configuration information. é    N)Úpath)Úcontextmanagerc               @   s   e Zd ZdZddd„ZdS )ÚETSToolkitErrorz× Error raised by issues importing ETS toolkits

    Attributes
    ----------
    message : str
        The message detailing the error.

    toolkit : str or None
        The toolkit associated with the error.
    Ú Nc             G   sF   | r|rdj |ƒ}|| _|| _|r<|r2|f| }|f| }|| _d S )Nzcould not import toolkit '{0}')ÚformatÚtoolkitÚmessageÚargs)Úselfr   r   r	   © r   ú�/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traits-5.1.2-py3.6-linux-x86_64.egg/traits/etsconfig/etsconfig.pyÚ__init__   s    



zETSToolkitError.__init__)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   
r   c               @   s  e Zd ZdZdd„ Zd1dd„Zdd„ Zd	d
„ Zd2dd„Ze	eeƒZ
dd„ Zdd„ Ze	eeƒZdd„ Zdd„ Ze	eeƒZedd„ ƒZdd„ Zdd„ Ze	eeƒZdd„ Zdd„ Ze	eeƒZdd „ Ze	eƒZd!d"„ Zd#d$„ Ze	eeƒZd%d&„ Zd3d(d)„Zd*d+„ Z d,d-„ Z!d.d/„ Z"d0S )4Ú	ETSConfigzÉ
    Enthought Tool Suite configuration information.

    This class should not use ANY other package in the tool suite so that it
    will always work no matter which other packages are present.

    c             C   s(   d| _ d| _d| _d| _d| _d| _dS )z�
        Constructor.

        Note that this constructor can only ever be called from within this
        module, since we don't expose the class.

        N)Ú_application_dataÚ_application_homeÚ_companyÚ_toolkitÚ_kiva_backendÚ
_user_data)r
   r   r   r   r   2   s    
zETSConfig.__init__Fc             C   s   | j dkr| j|d�| _ | j S )ac   Return the application data directory path.

            Parameters
            ----------
            create: bool
                Create the corresponding directory or not.

            Notes
            -----
            - This is a directory that applications and packages can safely
              write non-user accessible data to i.e. configuration
              information, preferences etc.

            - Do not put anything in here that the user might want to
              navigate to e.g. projects, user data files etc.

            - The actual location differs between operating systems.

       N)Úcreate)r   Ú_initialize_application_data)r
   r   r   r   r   Úget_application_dataK   s    

zETSConfig.get_application_datac             C   s   | j dd�S )z@ Property getter, see get_application_data's docstring.
        T)r   )r   )r
   r   r   r   Ú_get_application_dataf   s    zETSConfig._get_application_datac             C   s
   || _ dS )z#
        Property setter.

        N)r   )r
   Úapplication_datar   r   r   Ú_set_application_datak   s    zETSConfig._set_application_datac             C   s*   | j dkr$tj| j|d�| jƒ ƒ| _ | j S )af   Return the application home directory path.

            Parameters
            ----------
            create: bool
                Create the corresponding directory or not.

            Note
            ----
            - This is a directory named after the current, running
              application that imported this module that applications and
              packages can safely write non-user accessible data to i.e.
              configuration information, preferences etc.  It is a
              sub-directory of self.application_data, named after the
              directory that contains the "main" python script that started
              the process.  For example, if application foo is started with
              a script named "run.py" in a directory named "foo", then the
              application home would be: <ETSConfig.application_data>/foo,
              regardless of if it was launched with "python
              <path_to_foo>/run.py" or "cd <path_to_foo>; python run.py"

            - This is useful for library modules used in apps that need to
              store state, preferences, etc. for the specific app only, and
              not for all apps which use that library module.  If the
              library module uses ETSConfig.application_home, they can
              store prefs for the app all in one place and do not need to
              know the details of where each app might reside.

            - Do not put anything in here that the user might want to
              navigate to e.g. projects, user home files etc.

            - The actual location differs between operating systems.

       N)r   )r   r   Újoinr   Ú_get_application_dirname)r
   r   r   r   r   Úget_application_homeu   s
    #

zETSConfig.get_application_homec             C   s   | j dd�S )z@ Property getter, see get_application_home's docstring.
        T)r   )r!   )r
   r   r   r   Ú_get_application_home¢   s    zETSConfig._get_application_homec             C   s
   || _ dS )z#
        Property setter.

        N)r   )r
   Úapplication_homer   r   r   Ú_set_application_home§   s    zETSConfig._set_application_homec             C   s   | j dkr| jƒ | _ | j S )z#
        Property getter.

        N)r   Ú_initialize_company)r
   r   r   r   Ú_get_company³   s    

zETSConfig._get_companyc             C   s
   || _ dS )z8
        Property setter for the company name.

        N)r   )r
   Úcompanyr   r   r   Ú_set_company¾   s    zETSConfig._set_companyc          	   c   sD   | j rd}t|j| j ƒƒ‚|| _ y
dV  W n   d| _‚ Y nX dS )a|   Perform an operation with toolkit provisionally set

        This sets the toolkit attribute of the ETSConfig object to the
        provided value. If the operation fails with an exception, the toolkit
        is reset to nothing.

        This method should only be called if the toolkit is not currently set.

        Parameters
        ----------
        toolkit : string
            The name of the toolkit to provisionally use.

        Raises
        ------
        ETSToolkitError
            If the toolkit attribute is already set, then an ETSToolkitError
            will be raised when entering the context manager.
        z)ETSConfig toolkit is already set to '{0}'Nr   )r   r   r   r   )r
   r   Úmsgr   r   r   Úprovisional_toolkitÊ   s    
zETSConfig.provisional_toolkitc             C   s$   | j dkr| jƒ | _ | j jdƒd S )zô
        Property getter for the GUI toolkit.  The value returned is, in order
        of preference: the value set by the application; the value specified by
        the 'ETS_TOOLKIT' environment variable; otherwise the empty string.

        NÚ.r   )r   Ú_initialize_toolkitÚsplit)r
   r   r   r   Ú_get_toolkitê   s    

zETSConfig._get_toolkitc             C   s,   | j r"| j |kr"td|| j f ƒ‚|| _ dS )a)  
        Property setter for the GUI toolkit.  The toolkit can be set more than
        once, but only if it is the same one each time.  An application that is
        written for a particular toolkit can explicitly set it before any other
        module that gets the value is imported.

        z>cannot set toolkit to %s because it has already been set to %sN)r   Ú
ValueError)r
   r   r   r   r   Ú_set_toolkit÷   s    	zETSConfig._set_toolkitc             C   s   ddl m} |dƒ | jS )a/  
        Deprecated: This property is no longer used.

        Property getter for the Enable backend.  The value returned is, in order
        of preference: the value set by the application; the value specified by
        the 'ENABLE_TOOLKIT' environment variable; otherwise the empty string.
        r   )Úwarnz2Use of the enable_toolkit attribute is deprecated.)Úwarningsr1   r   )r
   r1   r   r   r   Ú_get_enable_toolkit  s    zETSConfig._get_enable_toolkitc             C   s   ddl m} |dƒ dS )a@  
        Deprecated.

        Property setter for the Enable toolkit.  The toolkit can be set more than
        once, but only if it is the same one each time.  An application that is
        written for a particular toolkit can explicitly set it before any other
        module that gets the value is imported.
        r   )r1   z2Use of the enable_toolkit attribute is deprecated.N)r2   r1   )r
   r   r1   r   r   r   Ú_set_enable_toolkit  s    	zETSConfig._set_enable_toolkitc             C   s˜   | j dkrtdƒ‚| jdkr’y| j jdƒd | _W n^ tk
r�   | jdkrbtjdkrZdnd| _n*| jd	krtd| _n| jd
kr†d| _nd| _Y nX | jS )aŒ  
        Property getter for the Kiva backend. The value returned is dependent
        on the value of the toolkit property. If toolkit specifies a kiva backend
        using the extended syntax: <enable toolkit>[.<kiva backend>] then the
        value of the property will be whatever was specified. Otherwise the
        value will be a reasonable default for the given enable backend.
        NzKThe kiva_backend attribute is dependent on toolkit, which has not been set.r+   é   ÚwxÚdarwinZquartzÚimageZqt4ZpygletÚgl)r   ÚAttributeErrorr   r-   Ú
IndexErrorr   ÚsysÚplatform)r
   r   r   r   Ú_get_kiva_backend+  s    




zETSConfig._get_kiva_backendc             C   s   | j dkr| jƒ | _ | j S )zð
        Property getter.

        This is a directory that users can safely write user accessible data
        to i.e. user-defined functions, edited functions, etc.

        The actual location differs between operating systems.

        N)r   Ú_initialize_user_data)r
   r   r   r   Ú_get_user_dataL  s    

zETSConfig._get_user_datac             C   s
   || _ dS )z#
        Property setter.

        N)r   )r
   Ú	user_datar   r   r   Ú_set_user_data\  s    zETSConfig._set_user_datac             C   sD   d}t jjddƒ}|dk	r@t|dƒr@tj|jƒ}tjtj|ƒƒ}|S )a  
        Return the name of the directory (not a path) that the "main"
        Python script which started this process resides in, or "" if it could
        not be determined or is not appropriate.

        For example, if the script that started the current process was named
        "run.py" in a directory named "foo", and was launched with "python
        run.py", the name "foo" would be returned (this assumes the directory
        name is the name of the app, which seems to be as good of an assumption
        as any).

        r   Ú__main__NÚ__file__)	r<   ÚmodulesÚgetÚhasattrr   ÚabspathrD   ÚbasenameÚdirname)r
   rJ   Zmain_modZmain_mod_filer   r   r   r    r  s    
z"ETSConfig._get_application_dirnameTc       	      C   sÚ   t jdkrd}| j}nd}d| jjƒ  }tjj|dƒ}|dksF|dkr”ddl}ddlm	} |j
ƒ }tjjd	dƒ}|dk	r„|d
| 7 }|d||f ƒ tjj||ƒ}|rÖtjj|ƒrÌtjj|ƒsÖtd| ƒ‚n
tj|ƒ |S )zH
        Initializes the (default) application data directory.

        Úwin32ÚAPPDATAÚHOMEr+   Nz/rootr   )r1   ÚUSERz_%sz?Environment variable "%s" not set, setting home directory to %szFile "%s" already exists)r<   r=   r'   ÚlowerÚosÚenvironrF   Útempfiler2   r1   Ú
gettempdirr   r   ÚexistsÚisdirr/   Úmakedirs)	r
   r   Zenvironment_variableÚdirectory_nameÚparent_directoryrR   r1   Úuserr   r   r   r   r   Š  s0    

z&ETSConfig._initialize_application_datac             C   s   dS )z5
        Initializes the (default) company.

        Z	Enthoughtr   )r
   r   r   r   r%   ¸  s    zETSConfig._initialize_companyc             C   s$   | j dk	r| j }ntjjddƒ}|S )z+
        Initializes the toolkit.

        NZETS_TOOLKITr   )r   rP   rQ   rF   )r
   r   r   r   r   r,   À  s    
zETSConfig._initialize_toolkitc             C   sÆ   t jjdƒ}| j}tjdkr|y*ddlm}m} |j	}|j
d|ddƒ}W q„ tk
rx   t jj|dƒ}t jj|ƒrt|}Y q„X n|jƒ }t jj||ƒ}t jj|ƒr¸t jj|ƒsÂtd| ƒ‚n
t j|ƒ |S )zA
        Initializes the (default) user data directory.

        ú~rK   r   )ÚshellÚshellconzMy DocumentszFile "%s" already exists)rP   r   Ú
expanduserr'   r<   r=   Úwin32com.shellr[   r\   ZCSIDL_PERSONALÚSHGetFolderPathÚImportErrorr   rT   rO   rU   r/   rV   )r
   rX   rW   r[   r\   ZMY_DOCSZdesired_dirZusr_dirr   r   r   r?   Ì  s$    

zETSConfig._initialize_user_dataN)F)F)T)#r   r   r   r   r   r   r   r   r!   Úpropertyr   r"   r$   r#   r&   r(   r'   r   r*   r.   r0   r   r3   r4   Zenable_toolkitr>   Zkiva_backendr@   rB   rA   r    r   r%   r,   r?   r   r   r   r   r   #   s<   


+




 




.r   )
r   r<   rP   r   Ú
contextlibr   ÚRuntimeErrorr   Úobjectr   r   r   r   r   Ú<module>   s      b