3
dB                 @   sD   d Z ddlZddlZddlZG dd deZG dd dZe ZdS )z1 Enthought Tool Suite configuration information.     Nc               @   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	   </tmp/pip-build-7vycvbft/traits/traits/etsconfig/etsconfig.py__init__    s    



zETSToolkitError.__init__)r   N)__name__
__module____qualname____doc__r   r	   r	   r	   r
   r      s   
r   c               @   sR  e Zd ZdZdd Zd0ddZedd Zejd	d Zej	d
d Zd1ddZ
edd Zejdd Zej	dd Zedd Zejdd Zej	dd Zejdd Zedd Zejdd Zej	dd Zedd Zejdd Zedd Zed d! Zejd"d! Zej	d#d! Zd$d% Zd2d'd(Zd)d* Zd+d, Zd-d. Zd/S )3ETSConfigTypea  
    Enthought Tool Suite configuration information.

    Instances of this class record state useful for ETS-using applications,
    including the current GUI toolkit in use, and data and home directory
    setttings.

    Users typically shouldn't make use of this class directly. Instead, use the
    module-level :data:`~.ETSConfig` instance of this class, which is shared
    between the various ETS packages.
    c             C   s(   d | _ d | _d | _d | _d | _d | _d S )N)_application_data_application_home_company_toolkitZ_kiva_backend
_user_data)r   r	   r	   r
   r   A   s    zETSConfigType.__init__Fc             C   s   | j dkr| j|d| _ | j S )ad   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_dataS   s    

z"ETSConfigType.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
   application_datan   s    zETSConfigType.application_datac             C   s
   || _ dS )z#
        Property setter.

        N)r   )r   r   r	   r	   r
   r   t   s    c             C   s
   d | _ d S )N)r   )r   r	   r	   r
   r   }   s    c             C   s,   | j dkr&tjj| j|d| j | _ | j S )ag   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   ospathjoinr   _get_application_dirname)r   r   r	   r	   r
   get_application_home   s
    #

z"ETSConfigType.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
   application_home   s    zETSConfigType.application_homec             C   s
   || _ dS )z#
        Property setter.

        N)r   )r   r   r	   r	   r
   r      s    c             C   s
   d | _ d S )N)r   )r   r	   r	   r
   r      s    c             C   s   | j dkr| j | _ | j S )z#
        Property getter.

        N)r   _initialize_company)r   r	   r	   r
   company   s    

zETSConfigType.companyc             C   s
   || _ dS )z8
        Property setter for the company name.

        N)r   )r   r!   r	   r	   r
   r!      s    c             C   s
   d | _ d S )N)r   )r   r	   r	   r
   r!      s    c          	   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!ETSConfigType.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
   r      s    	

zETSConfigType.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
   r     s
    
c             C   s
   d | _ d S )N)r   )r   r	   r	   r
   r     s    c             C   s   ddl m} |d | jS )a7  
        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.)warningsr(   r   )r   r(   r	   r	   r
   enable_toolkit  s    
zETSConfigType.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   )r(   z2Use of the enable_toolkit attribute is deprecated.N)r)   r(   )r   r   r(   r	   r	   r
   r*   ,  s    
c             C   sL   | j dkrtdd| j kr,| j jdd S | jdkrDtjdkrDdS dS d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$      ZwxdarwinZquartzimage)r   AttributeErrorr&   r   sysplatform)r   r	   r	   r
   kiva_backend:  s    


zETSConfigType.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
   	user_dataQ  s    

zETSConfigType.user_datac             C   s
   || _ dS )z#
        Property setter.

        N)r   )r   r3   r	   r	   r
   r3   b  s    c             C   s
   d | _ d S )N)r   )r   r	   r	   r
   r3   k  s    c             C   sJ   d}t jjdd}|dk	rFt|drFtjj|j}tjjtj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   r   abspathr5   basenamedirname)r   r;   Zmain_modZmain_mod_filer	   r	   r
   r   y  s    
z&ETSConfigType._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rddl}ddlm	} |j
 }tjjd	d}|dk	r|d
| 7 }|d||f  tjj||}|rtjj|rtjj|std| n
tj| |S )zH
        Initializes the (default) application data directory.

        win32APPDATAHOMEr$   Nz/rootr   )r(   USERz_%sz?Environment variable "%s" not set, setting home directory to %szFile "%s" already exists)r/   r0   r!   lowerr   environr7   tempfiler)   r(   
gettempdirr   r   existsisdirr'   makedirs)	r   r   Zenvironment_variabledirectory_nameparent_directoryrB   r(   userr   r	   r	   r
   r     s0    

z*ETSConfigType._initialize_application_datac             C   s   dS )z5
        Initializes the (default) company.

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

        NZETS_TOOLKITr   )r   r   rA   r7   )r   r   r	   r	   r
   r%     s    
z!ETSConfigType._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 qX n|j }t jj||}t jj|rt jj|std| n
t j| |S )zA
        Initializes the (default) user data directory.

        ~r<   r   )shellshellconzMy DocumentszFile "%s" already exists)r   r   
expanduserr!   r/   r0   win32com.shellrK   rL   ZCSIDL_PERSONALSHGetFolderPathImportErrorr   rD   r@   rE   r'   rF   )r   rH   rG   rK   rL   ZMY_DOCSZdesired_dirZusr_dirr	   r	   r
   r2     s$    
	
z#ETSConfigType._initialize_user_dataN)F)F)T)r   r   r   r   r   r   propertyr   setterdeleterr   r   r!   
contextlibcontextmanagerr#   r   r*   r1   r3   r   r   r    r%   r2   r	   r	   r	   r
   r   ,   s8   

	
+		 	
/r   )r   rT   r   r/   RuntimeErrorr   r   Z	ETSConfigr	   r	   r	   r
   <module>   s      e