3
8ßL]©2  ã               @   sŽ   d Z ddlmZmZ ddlZddlZddlmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ejeƒZG dd	„ d	eƒZG d
d„ deƒZdS )z‘ Defines the abstract EditorFactory class, which represents a factory for
    creating the Editor objects used in a Traits-based user interface.
é    )Úabsolute_importÚprint_functionN)ÚHasPrivateTraitsÚCallableÚStrÚBoolÚEventÚAnyÚPropertyé   )Úenum_values_changed)Útoolkit_objectc               @   s®   e Zd ZdZeZeZedƒZ	edƒZ
eZeZeZeZeZeZdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚEditorFactoryzd Represents a factory for creating the Editor objects in a Traits-based
        user interface.
    FTc             O   s   t j| f|Ž | j|Ž  dS )z) Initializes the factory object.
        N)r   Ú__init__Úinit)ÚselfÚargsZtraits© r   ú=/tmp/pip-install-5sebvq2j/traitsui/traitsui/editor_factory.pyr   ]   s    zEditorFactory.__init__c             C   s   dS )zd Performs any initialization needed after all constructor traits
            have been set.
        Nr   )r   r   r   r   r   h   s    zEditorFactory.initc             C   sT   |j dƒ}t|ƒdkr"|jddƒ |j|d  }x|dd… D ]}t||ƒ}q>W |S )z} Returns the value of a specified extended name of the form: name or
            context_object_name.name[.name...]:
        Ú.r   r   ÚobjectN)ÚsplitÚlenÚinsertÚcontextÚgetattr)r   ÚnameÚuiÚnamesÚvaluer   r   r   Únamed_values   s    
zEditorFactory.named_valuec             C   s   | j || ||||d�S )z7 Generates an editor using the "simple" style.
        )Úfactoryr   r   r   Údescription)Úsimple_editor_class)r   r   r   r   r"   Úparentr   r   r   Úsimple_editor‹   s    zEditorFactory.simple_editorc             C   s   | j || ||||d�S )z7 Generates an editor using the "custom" style.
        )r!   r   r   r   r"   )Úcustom_editor_class)r   r   r   r   r"   r$   r   r   r   Úcustom_editor•   s    zEditorFactory.custom_editorc             C   s   | j || ||||d�S )z5 Generates an editor using the "text" style.
        )r!   r   r   r   r"   )Útext_editor_class)r   r   r   r   r"   r$   r   r   r   Útext_editorŸ   s    zEditorFactory.text_editorc             C   s   | j || ||||d�S )z2 Generates an "editor" that is read-only.
        )r!   r   r   r   r"   )Úreadonly_editor_class)r   r   r   r   r"   r$   r   r   r   Úreadonly_editor©   s    zEditorFactory.readonly_editorc       	      C   s    dd„ | j ƒ D ƒ}xˆt|ƒD ]|\}}y&|jdƒd	 }dj||gƒ}t|dƒS  tk
r– } z0d}tj|j||ƒƒ |t	|ƒd kr†|‚W Y dd}~X qX qW dS )
zO
        Returns the editor by name class_name in the backend package.
        c             S   s   g | ]}t |tƒr|j‘qS r   )Ú
issubclassr   Ú
__module__)Ú.0Zfactory_classr   r   r   ú
<listcomp>¼   s   z5EditorFactory._get_toolkit_editor.<locals>.<listcomp>r   r   ú:Tz$Can't import toolkit_object '{}': {}Néÿÿÿÿ)
ÚmroÚ	enumerater   Újoinr   ÚRuntimeErrorÚloggerÚdebugÚformatr   )	ÚclsÚ
class_nameZeditor_factory_modulesÚindexZeditor_moduleZeditor_module_nameZ
object_refÚeÚmsgr   r   r   Ú_get_toolkit_editor·   s    
z!EditorFactory._get_toolkit_editorNc             C   sB   | j dk	r| j |ƒS | jdkr(| j| S |dk	r8||ƒS tj|ƒS )a¾   Returns the text representation of a specified object trait value.

        If the **format_func** attribute is set on the editor factory, then
        this method calls that function to do the formatting.  If the
        **format_str** attribute is set on the editor factory, then this
        method uses that string for formatting. If neither attribute is
        set, then this method just calls the appropriate text type to format.
        NÚ )Úformat_funcÚ
format_strÚsixÚ	text_type)r   r   r@   r   r   r   Ústring_valueË   s    	



zEditorFactory.string_valuec             C   sV   y| j dƒ}W nB tk
rP } z&d}tj|j| j|ƒƒ tdƒ}W Y dd}~X nX |S )aP   Returns the editor class to use for "simple" style views.
        The default implementation tries to import the SimpleEditor class in the
        editor file in the backend package, and if such a class is not to found
        it returns the SimpleEditor class defined in editor_factory module in
        the backend package.

        ÚSimpleEditorz$Can't import SimpleEditor for {}: {}zeditor_factory:SimpleEditorN)r>   Ú	Exceptionr6   r7   r8   Ú	__class__r   )r   rE   r<   r=   r   r   r   Ú_get_simple_editor_classã   s    z&EditorFactory._get_simple_editor_classc             C   sT   y| j dƒ}W n@ tk
rN } z$d}tj|j| j|ƒƒ | j}W Y dd}~X nX |S )a   Returns the editor class to use for "custom" style views.
        The default implementation tries to import the CustomEditor class in the
        editor file in the backend package, and if such a class is not to found
        it returns simple_editor_class.

        ÚCustomEditorz$Can't import CustomEditor for {}: {}N)r>   rF   r6   r7   r8   rG   r#   )r   rI   r<   r=   r   r   r   Ú_get_custom_editor_classó   s    z&EditorFactory._get_custom_editor_classc             C   sV   y| j dƒ}W nB tk
rP } z&d}tj|j| j|ƒƒ tdƒ}W Y dd}~X nX |S )aL   Returns the editor class to use for "text" style views.
        The default implementation tries to import the TextEditor class in the
        editor file in the backend package, and if such a class is not found
        it returns the TextEditor class declared in the editor_factory module in
        the backend package.

        Ú
TextEditorz"Can't import TextEditor for {}: {}zeditor_factory:TextEditorN)r>   rF   r6   r7   r8   rG   r   )r   rK   r<   r=   r   r   r   Ú_get_text_editor_class  s    z$EditorFactory._get_text_editor_classc             C   sV   y| j dƒ}W nB tk
rP } z&d}tj|j| j|ƒƒ tdƒ}W Y dd}~X nX |S )aX   Returns the editor class to use for "readonly" style views.
        The default implementation tries to import the ReadonlyEditor class in
        the editor file in the backend package, and if such a class is not found
        it returns the ReadonlyEditor class declared in the editor_factory
        module in the backend package.

        ÚReadonlyEditorz&Can't import ReadonlyEditor for {}: {}zeditor_factory:ReadonlyEditorN)r>   rF   r6   r7   r8   rG   r   )r   rM   r<   r=   r   r   r   Ú_get_readonly_editor_class  s    z(EditorFactory._get_readonly_editor_class)N) Ú__name__r-   Ú__qualname__Ú__doc__r   r@   r   rA   r   Zis_grid_cellZenabledÚinvalidZtext_alignmentr
   r#   r&   r(   r*   r   r   r    r%   r'   r)   r+   Úclassmethodr>   rD   rH   rJ   rL   rN   r   r   r   r   r   /   s0   



r   c               @   s,   e Zd ZdZeZedƒZeZe	Z
dd„ ZdS )ÚEditorWithListFactoryzI Base class for factories of editors for objects that contain lists.
    r   c             C   s&   t | j| jd�\| _| _| _d| _dS )zK Recomputes the mappings whenever the **values** trait is changed.
        )ZstrfuncTN)r   ÚvaluesrD   Z_namesÚ_mappingZ_inverse_mappingÚvalues_modified)r   r   r   r   Ú_values_changed@  s    z%EditorWithListFactory._values_changedN)rO   r-   rP   rQ   r	   rU   r   r   r   r   rW   rX   r   r   r   r   rT   '  s   rT   )rQ   Ú
__future__r   r   ÚloggingrB   Z
traits.apir   r   r   r   r   r	   r
   Úhelperr   Ztoolkitr   Ú	getLoggerrO   r6   r   rT   r   r   r   r   Ú<module>   s   $
 y