3
dF                 @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d d	lmZ d
ZG dd dZG dd deZG dd deZG dd deZdd Zd(ddZd)ddZd*ddZd+ddZd,dd Zd-d!d"Zd.d#d$Ze jed%d&d' ZdS )/    N)anytrait_filter)DictItemObserver)FilteredTraitObserver)ListItemObserver)MetadataFilter)NamedTraitObserver)ObserverGraph)SetItemObserver   c               @   sz   e Zd ZdZf Zdd Zdd ZdddZdd	d
ZdddZ	dddZ
dddZd ddZd!ddZdd Zdd ZdS )"ObserverExpressionaS  
    ObserverExpression is an object for describing what traits are being
    observed for change notifications. It can be passed directly to
    ``HasTraits.observe`` method or the ``observe`` decorator.

    An ObserverExpression is typically created using one of the top-level
    functions provided in this module, e.g. ``trait``.
    c             C   s
   t | |S )av   Create a new expression that matches this expression OR
        the given expression.

        e.g. ``trait("age") | trait("number")`` will match either trait
        **age** or trait **number** on an object.

        Parameters
        ----------
        expression : ObserverExpression

        Returns
        -------
        new_expression : ObserverExpression
        )ParallelObserverExpression)self
expression r   ?/tmp/pip-build-7vycvbft/traits/traits/observation/expression.py__or__)   s    zObserverExpression.__or__c             C   s
   t | |S )a   Create a new expression by extending this expression with
        the given expression.

        e.g. ``trait("child").then( trait("age") | trait("number") )``
        on an object matches ``child.age`` or ``child.number`` on the object.

        Parameters
        ----------
        expression : ObserverExpression

        Returns
        -------
        new_expression : ObserverExpression
        )SeriesObserverExpression)r   r   r   r   r   then:   s    zObserverExpression.thenTc             C   s   | j t||dS )a-   Create a new expression for observing traits using the
        given filter.

        Events emitted (if any) will be instances of
        :class:`~traits.observation.events.TraitChangeEvent`.

        Parameters
        ----------
        filter : callable(str, CTrait) -> bool
            A callable that receives the name of a trait and the corresponding
            trait definition. The returned bool indicates whether the trait
            is observed. In order to remove an existing observer with the
            equivalent filter, the filter callables must compare equally. The
            callable must also be hashable.
        notify : bool, optional
            Whether to notify for changes. Default is to notify.

        Returns
        -------
        new_expression : ObserverExpression
        )filternotify)r   match)r   r   r   r   r   r   r   K   s    zObserverExpression.matchc             C   s   | j t|dS )a   Create a new expression for observing all traits.

        Events emitted (if any) will be instances of
        :class:`~traits.observation.events.TraitChangeEvent`.

        Parameters
        ----------
        notify : bool, optional
            Whether to notify for changes. Default is to notify.

        Returns
        -------
        new_expression : ObserverExpression
        )r   r   )r   r   )r   r   r   r   r   anytraitc   s    zObserverExpression.anytraitc             C   s   | j t|d|dS )af   Return a new expression for observing traits where the given
        metadata is not None.

        Events emitted (if any) will be instances of
        :class:`~traits.observation.events.TraitChangeEvent`.

        e.g. ``metadata("age")`` matches traits whose 'age' attribute has a
        non-None value.

        Parameters
        ----------
        metadata_name : str
            Name of the metadata to filter traits with.
        notify : bool, optional
            Whether to notify for changes. Default is to notify.

        Returns
        -------
        new_expression : ObserverExpression
        )metadata_name)r   r   )r   r   )r   r   r   r   r   r   metadatat   s    zObserverExpression.metadataFc             C   s   | j t||dS )a   Create a new expression for observing items inside a dict.

        Events emitted (if any) will be instances of
        :class:`~traits.observation.events.DictChangeEvent`.

        If an expression with ``dict_items`` is further extended, the
        **values** of the dict will be given to the next item in the
        expression. For example, the following observes a trait named "number"
        on any object that is one of the values in the dict named "mapping"::

            trait("mapping").dict_items().trait("number")

        Parameters
        ----------
        notify : bool, optional
            Whether to notify for changes. Default is to notify.
        optional : bool, optional
            Whether to ignore this if the upstream object is not a dict.
            Default is false and an error will be raised if the object is not
            a dict.

        Returns
        -------
        new_expression : ObserverExpression
        )r   optional)r   
dict_items)r   r   r   r   r   r   r      s    zObserverExpression.dict_itemsc             C   s   | j t||dS )aa   Create a new expression for observing items inside a list.

        Events emitted (if any) will be instances of
        :class:`~traits.observation.events.ListChangeEvent`.

        e.g. ``trait("containers").list_items()`` for observing mutations
        to a list named ``containers``.

        e.g. ``trait("containers").list_items().trait("value")`` for observing
        the trait ``value`` on any items in the list ``containers``.

        Parameters
        ----------
        notify : bool, optional
            Whether to notify for changes. Default is to notify.
        optional : bool, optional
            Whether to ignore this if the upstream object is not a list.
            Default is false and an error will be raised if the object is not
            a list.

        Returns
        -------
        new_expression : ObserverExpression
        )r   r   )r   
list_items)r   r   r   r   r   r   r      s    zObserverExpression.list_itemsc             C   s   | j t||dS )aU   Create a new expression for observing items inside a set.

        Events emitted (if any) will be instances of
        :class:`~traits.observation.events.SetChangeEvent`.

        Parameters
        ----------
        notify : bool, optional
            Whether to notify for changes. Default is to notify.
        optional : bool, optional
            Whether to ignore this if the upstream object is not a set.
            Default is false and an error will be raised if the object is not
            a set.

        Returns
        -------
        new_expression : ObserverExpression
        )r   r   )r   	set_items)r   r   r   r   r   r   r      s    zObserverExpression.set_itemsc             C   s   | j t|||dS )a   Create a new expression for observing a trait with the exact
        name given.

        Events emitted (if any) will be instances of
        :class:`~traits.observation.events.TraitChangeEvent`.

        Parameters
        ----------
        name : str
            Name of the trait to match.
        notify : bool, optional
            Whether to notify for changes. Default is to notify.
        optional : bool, optional
            If true, skip this observer if the requested trait is not found.
            Default is false, and an error will be raised if the requested
            trait is not found.

        Returns
        -------
        new_expression : ObserverExpression
        )namer   r   )r   trait)r   r   r   r   r   r   r   r      s    zObserverExpression.traitc             C   s   | j g dS )a^   Return all the ObserverGraph for the observer framework to attach
        notifiers.

        This is considered private to the users and to modules outside of the
        ``observation`` subpackage, but public to modules within the
        ``observation`` subpackage.

        Returns
        -------
        graphs : list of ObserverGraph
        )branches)_create_graphs)r   r   r   r   
_as_graphs   s    zObserverExpression._as_graphsc             C   s   t ddS )a   Return a list of ObserverGraph with the given branches.

        Parameters
        ----------
        branches : list of ObserverGraph
            Graphs to be used as branches.

        Returns
        -------
        graphs : list of ObserverGraph
        z%'_create_graphs' must be implemented.N)NotImplementedError)r   r    r   r   r   r!      s    z!ObserverExpression._create_graphsN)T)T)T)TF)TF)TF)TF)__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   r   r   r"   r!   r   r   r   r   r      s   






r   c               @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )SingleObserverExpressionzE Container of ObserverExpression for wrapping a single observer.
    	_observerc             C   s
   || _ d S )N)r*   )r   observerr   r   r   __init__  s    z!SingleObserverExpression.__init__c             C   s   t t| j| jfS )N)hashtyper$   r*   )r   r   r   r   __hash__  s    z!SingleObserverExpression.__hash__c             C   s   t | t |ko| j|jkS )N)r.   r*   )r   otherr   r   r   __eq__  s    zSingleObserverExpression.__eq__c             C   s   t | j|dgS )N)nodechildren)r   r*   )r   r    r   r   r   r!   !  s    z'SingleObserverExpression._create_graphsN)r*   )	r$   r%   r&   r'   r(   r,   r/   r1   r!   r   r   r   r   r)     s   r)   c               @   s4   e Zd ZdZdZdd Zdd Zdd	 Zd
d ZdS )r   a   Container of ObserverExpression for joining expressions in series.

    Parameters
    ----------
    first : ObserverExpression
        Left expression to be joined in series.
    second : ObserverExpression
        Right expression to be joined in series.
    _first_secondc             C   s   || _ || _d S )N)r4   r5   )r   firstsecondr   r   r   r,   4  s    z!SeriesObserverExpression.__init__c             C   s   t t| j| j| jfS )N)r-   r.   r$   r4   r5   )r   r   r   r   r/   8  s    z!SeriesObserverExpression.__hash__c             C   s(   t | t |ko&| j|jko&| j|jkS )N)r.   r4   r5   )r   r0   r   r   r   r1   ;  s    zSeriesObserverExpression.__eq__c             C   s   | j j|d}| jj|dS )N)r    )r5   r!   r4   )r   r    r   r   r   r!   B  s    z'SeriesObserverExpression._create_graphsN)r4   r5   )	r$   r%   r&   r'   r(   r,   r/   r1   r!   r   r   r   r   r   '  s   	r   c               @   s4   e Zd ZdZdZdd Zdd Zdd	 Zd
d ZdS )r   a   Container of ObserverExpression for joining expressions in parallel.

    Parameters
    ----------
    left : ObserverExpression
        Left expression to be joined in parallel.
    right : ObserverExpression
        Right expression to be joined in parallel.
    _left_rightc             C   s   || _ || _d S )N)r8   r9   )r   leftrightr   r   r   r,   T  s    z#ParallelObserverExpression.__init__c             C   s   t t| j| j| jfS )N)r-   r.   r$   r8   r9   )r   r   r   r   r/   X  s    z#ParallelObserverExpression.__hash__c             C   s(   t | t |ko&| j|jko&| j|jkS )N)r.   r8   r9   )r   r0   r   r   r   r1   [  s    z!ParallelObserverExpression.__eq__c             C   s$   | j j|d}| jj|d}|| S )N)r    )r8   r!   r9   )r   r    Zleft_graphsZright_graphsr   r   r   r!   b  s    z)ParallelObserverExpression._create_graphsN)r8   r9   )	r$   r%   r&   r'   r(   r,   r/   r1   r!   r   r   r   r   r   G  s   	r   c              G   s   t jdd | S )a   Convenient function for joining many expressions in series
    using ``ObserverExpression.then``

    Parameters
    ----------
    *expressions : iterable of ObserverExpression

    Returns
    -------
    new_expression : ObserverExpression
        Joined expression.
    c             S   s
   | j |S )N)r   )Ze1e2r   r   r   <lambda>u  s    zjoin.<locals>.<lambda>)	functoolsreduce)Zexpressionsr   r   r   joinh  s    r@   TFc             C   s   t | |d}t|S )aq   Create a new expression for observing items inside a dict.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.DictChangeEvent`.

    If an expression with ``dict_items`` is further extended, the
    **values** of the dict will be given to the next item in the expression.
    For example, the following observes a trait named "number" on any object
    that is one of the values in the dict named "mapping"::

        trait("mapping").dict_items().trait("number")

    Parameters
    ----------
    notify : bool, optional
        Whether to notify for changes. Default is to notify.
    optional : bool, optional
        Whether to ignore this if the upstream object is not a dict.
        Default is false and an error will be raised if the object is not
        a dict.

    Returns
    -------
    new_expression : ObserverExpression
    )r   r   )r   r)   )r   r   r+   r   r   r   r   x  s    r   c             C   s   t | |d}t|S )a   Create a new expression for observing items inside a list.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.ListChangeEvent`.

    e.g. ``trait("containers").list_items()`` for observing mutations
    to a list named ``containers``.

    e.g. ``trait("containers").list_items().trait("value")`` for observing
    the trait ``value`` on any items in the list ``containers``.

    Parameters
    ----------
    notify : bool, optional
        Whether to notify for changes. Default is to notify.
    optional : bool, optional
        Whether to ignore this if the upstream object is not a list.
        Default is false and an error will be raised if the object is not
        a list.

    Returns
    -------
    new_expression : ObserverExpression
    )r   r   )r   r)   )r   r   r+   r   r   r   r     s    r   c             C   s   t || d}t|S )a   Create a new expression for observing traits using the
    given filter.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.TraitChangeEvent`.

    Parameters
    ----------
    filter : callable(str, CTrait) -> bool
        A callable that receives the name of a trait and the corresponding
        trait definition. The returned bool indicates whether the trait is
        observed. In order to remove an existing observer with the equivalent
        filter, the filter callables must compare equally. The callable must
        also be hashable.
    notify : bool, optional
        Whether to notify for changes.

    Returns
    -------
    new_expression : ObserverExpression
    )r   r   )r   r)   )r   r   r+   r   r   r   r     s    r   c             C   s   t | td}t|S )a   Create a new expression for observing all traits on an object.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.TraitChangeEvent`.

    Parameters
    ----------
    notify : bool, optional
        Whether to notify for changes.
    )r   r   )r   r   r)   )r   r+   r   r   r   r     s    r   c             C   s   t t| d|dS )a*   Return a new expression for observing traits where the given metadata
    is not None.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.TraitChangeEvent`.

    e.g. ``metadata("age")`` matches traits whose 'age' attribute has a
    non-None value.

    Parameters
    ----------
    metadata_name : str
        Name of the metadata to filter traits with.
    notify : bool, optional
        Whether to notify for changes. Default is to notify.

    Returns
    -------
    new_expression : ObserverExpression
    )r   )r   r   )r   r   )r   r   r   r   r   r     s    r   c             C   s   t | |d}t|S )a   Create a new expression for observing items inside a set.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.SetChangeEvent`.

    Parameters
    ----------
    notify : bool, optional
        Whether to notify for changes. Default is to notify.
    optional : bool, optional
        Whether to ignore this if the upstream object is not a set.
        Default is false and an error will be raised if the object is not
        a set.

    Returns
    -------
    new_expression : ObserverExpression
    )r   r   )r	   r)   )r   r   r+   r   r   r   r     s    r   c             C   s   t | ||d}t|S )at   Create a new expression for observing a trait with the exact
    name given.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.TraitChangeEvent`.

    Parameters
    ----------
    name : str
        Name of the trait to match.
    notify : bool, optional
        Whether to notify for changes. Default is to notify.
    optional : bool, optional
        If true, skip this observer if the requested trait is not found.
        Default is false, and an error will be raised if the requested
        trait is not found.

    Returns
    -------
    new_expression : ObserverExpression
    )r   r   r   )r   r)   )r   r   r   r+   r   r   r   r     s    r   )maxsizec             C   s   | j  S )z Compile an ObserverExpression to a list of ObserverGraphs.

    Parameters
    ----------
    expr : ObserverExpression

    Returns
    -------
    list of ObserverGraph
    )r"   )exprr   r   r   compile_expr)  s    rC   )TF)TF)T)T)T)TF)TF) r>   Z#traits.observation._anytrait_filterr   Z&traits.observation._dict_item_observerr   Z+traits.observation._filtered_trait_observerr   Z&traits.observation._list_item_observerr   Z#traits.observation._metadata_filterr   Z(traits.observation._named_trait_observerr   Z"traits.observation._observer_graphr   Z%traits.observation._set_item_observerr	   Z"_OBSERVER_EXPRESSION_CACHE_MAXSIZEr   r)   r   r   r@   r   r   r   r   r   r   r   	lru_cacherC   r   r   r   r   <module>   s.    s !






