3
AAd]f  ã               @   sH   d Z ddlmZmZmZ ddlmZ dd„ Zdd„ ZG d	d
„ d
eƒZ	dS )zš Defines the Template class, which provides a default implementation of the
    ITemplate interface that can be used when defining templatizable objects.
é    )ÚHasPrivateTraitsÚ	UndefinedÚprovidesé   )Ú	ITemplatec             C   s   | dkS )NÚinstanceÚ
datasource)r   r   © )Úvaluer	   r	   ú{/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/template/template_impl.pyÚinstance_or_datasource    s    r   c             C   s   | d kS )Nr	   )r
   r	   r	   r   Úis_none'   s    r   c               @   s8   e Zd ZdZeeƒ dd„ Zdd„ Zdd„ Zdd	„ Z	d
S )ÚTemplatezª Defines the Template class, which provides a default implementation of
        the ITemplate interface that can be used when defining templatizable
        objects.
    c             C   sÀ   g }xZt | jdd�jƒ ƒD ]B}t|t ƒrFx2|D ]}|j|jƒ ƒ q.W q|dk	r|j|jƒ ƒ qW xZt | jdd�jƒ ƒD ]B}t|t ƒr¢x2|D ]}|j|jƒ ƒ qŠW qv|dk	rv|j|jƒ ƒ qvW |S )a¡   Returns a list of **TemplateDataName** objects, one for each
            bindable data source contained within the template. Each
            **TemplateDataName** object supports unresolved bindings in
            addition to resolved bindings. Also, a **TemplateDataName** may be
            marked as *optional*, meaning the if the name remains unresolved,
            the application can take an alternative action (e.g. omit the data
            from a plot, or substitute default data, etc.).

            Returns
            -------
            A list of **TemplateDataName** objects, one for each bindable data
            source contained in the template.
        r   )ÚtemplateNr   )ÚlistÚgetÚvaluesÚ
isinstanceÚextendÚnames_from_templateÚappendZname_from_data_source)ÚselfZ
data_namesr
   Úvr	   r	   r   r   8   s    



zTemplate.names_from_templatec             C   sÆ   xNt | jdd�jƒ ƒD ]6}t|t ƒr<x&|D ]}|jƒ  q*W q|dk	r|jƒ  qW xNt | jdd�jƒ ƒD ]6}t|t ƒrŒx&|D ]}|jƒ  qzW qf|dk	rf|jƒ  qfW x | jdd�D ]}t| |tƒ q®W | S )z€ Activates the object from its template data.

            Returns
            -------
            The original object.
        r   )r   Nr   Zderived)r   r   r   r   Úobject_from_templateZtrait_namesÚsetattrr   )r   r
   r   Únamer	   r	   r   r   \   s    



zTemplate.object_from_templatec             C   s’   x"t | jtd�jƒ ƒD ]
}d|_qW | jtd�}xBt |jƒ ƒD ]2\}}t|t ƒrddd„ |D ƒ||< q>|j	ƒ ||< q>W |j
| jdd�ƒ | jf |ŽS )zÈ Returns a *templatized* version of the object that is safe for
            serialization.

            Returns
            -------
            A new object of the same class as the original.
        )r   Tc             S   s   g | ]}|j ƒ ‘qS r	   )Útemplate_from_object)Ú.0r   r	   r	   r   ú
<listcomp>�   s    z1Template.template_from_object.<locals>.<listcomp>Úcopy)r   Útraitsr   r   Z	transientr   r   Úitemsr   r   ÚupdateÚ	__class__)r   ZtraitÚcontentsr   r
   r	   r	   r   r   {   s    	

zTemplate.template_from_objectc             C   s   t ‚dS )zì Converts all contained 'TDerived' objects to real objects using the
            template traits of the object. This method must be overridden in
            subclasses.

            Returns
            -------
            None
        N)ÚNotImplementedError)r   r	   r	   r   Úactivate_template˜   s    	zTemplate.activate_templateN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z
implementsr   r   r   r   r&   r	   r	   r	   r   r   .   s   $r   N)
r*   Z
traits.apir   r   r   Z	itemplater   r   r   r   r	   r	   r	   r   Ú<module>   s
   