Exemple #1
0
    class SampleListAdapter(Adapter):
        adapts(Sample, IList)

        def get_list(self):
            obj = self.adaptee
            return [getattr(obj, name)
                    for name in obj.trait_names(sample=True)]
Exemple #2
0
class EmptyProjectAdapter(ITreeNodeAdapter):
    """ Adapter for our EmptyProject. """

    adapts(EmptyProject, ITreeNode)

    #-- ITreeNodeAdapter Method Overrides --------------------------------------

    def get_label(self):
        """ Gets the label to display for a specified object.
        """
        return 'No project loaded.'
Exemple #3
0
    class SampleFooAdapter(HasTraits):

        adapts(Sample, IFoo)

        object = Instance(Sample)

        def __init__(self, object):
            self.object = object

        def get_foo(self):
            object = self.object
            return (object.s1 + object.s2 + object.s3)
Exemple #4
0
    class ListAverageAdapter(Adapter):

        adapts(IList, IAverage)

        def get_average(self):
            value = self.adaptee.get_list()
            if len(value) == 0:
                return 0.0

            average = 0.0
            for item in value:
                average += item
            return (average / len(value))
Exemple #5
0
class ParametersTablePlotObjectAdapter(ITreeNodeAdapter):
    adapts(ParametersTablePlot, ITreeNode)

    def allows_children(self):
        return False

    def has_children(self):
        return False

    def get_view(self):
        return self.adaptee.cview()

    def get_label(self):
        return "Parameters"
Exemple #6
0
class DataBlockTablePlotObjectAdapter(ITreeNodeAdapter):

    adapts(DataBlockTablePlot, ITreeNode)

    def allows_children(self):
        return False

    def has_children(self):
        return False

    def get_view(self):
        return self.adaptee.cview()

    def get_label(self):
        return "Raw Data"
class FileAdapter(ITreeNodeAdapter):

    adapts(File, ITreeNode)

    #-- ITreeNodeAdapter Method Overrides ------------------------------------

    def allows_children(self):
        """ Returns whether this object can have children.
        """
        return self.adaptee.is_folder

    def has_children(self):
        """ Returns whether the object has children.
        """
        children = self.adaptee.children
        return ((children is not None) and (len(children) > 0))

    def get_children(self):
        """ Gets the object's children.
        """
        return self.adaptee.children

    def get_label(self):
        """ Gets the label to display for a specified object.
        """
        return self.adaptee.name + self.adaptee.ext

    def get_tooltip(self):
        """ Gets the tooltip to display for a specified object.
        """
        return self.adaptee.absolute_path

    def get_icon(self, is_expanded):
        """ Returns the icon for a specified object.
        """
        if self.adaptee.is_file:
            return '<item>'

        if is_expanded:
            return '<open>'

        return '<open>'

    def can_auto_close(self):
        """ Returns whether the object's children should be automatically
            closed.
        """
        return True
class PersonINameAdapter(HasTraits):

    # Declare what interfaces this adapter implements,
    # and for what class:
    adapts(Person, IName)

    # Declare the type of client it supports:
    client = Instance(Person)

    # Implement the adapter's constructor:
    def __init__(self, client):
        self.client = client

    # Implement the 'IName' interface on behalf of its client:
    def get_name(self):
        return "%s %s" % (self.client.first_name, self.client.last_name)
class IContextAdapter(Adapter):
    """ Defines an adapter from an codetools.contexts.api.IContext
        to an ITemplateDataContext.
    """

    adapts(IContext, ITemplateDataContext)

    #-- ITemplateDataContext Interface Implementation --------------------------

    # The path to this data context (does not include the 'data_context_name'):
    data_context_path = Str

    # The name of the data context:
    data_context_name = Str

    # A list of the names of the data values in this context:
    data_context_values = List(Str)

    # The list of the names of the sub-contexts of this context:
    data_contexts = List(Str)

    def get_data_context_value(self, name):
        """ Returns the data value with the specified *name*. Raises a
            **ITemplateDataContextError** if *name* is not defined as a data
            value in the context.

            Parameters
            ----------
            name : A string specifying the name of the context data value to
                be returned.

            Returns
            -------
            The data value associated with *name* in the context. The type of
            the data is application dependent.

            Raises **ITemplateDataContextError** if *name* is not associated
            with a data value in the context.
        """
        try:
            if name in self.data_context_values:
                return self.adaptee[name]

            raise ITemplateDataContextError("No value named '%s' found." %
                                            name)
        except Exception, excp:
            raise ITemplateDataContextError(str(excp))
Exemple #10
0
class BoardObjectAdapter(ITreeNodeAdapter):
    adapts(Board, ITreeNode)

    def allows_children(self):
        return True

    def has_children(self):
        return len(self.adaptee.chnls) > 0

    def get_children(self):
        return self.adaptee.chnls

    def get_view(self):
        return self.adaptee.cview

    def get_label(self):
        return self.adaptee.board_id
Exemple #11
0
class ChannelObjectAdapter(ITreeNodeAdapter):
    adapts(Channel, ITreeNode)

    def allows_children(self):
        return True

    def has_children(self):
        return len(self.adaptee.tables) > 0

    def get_children(self):
        return self.adaptee.tables

    def get_view(self):
        return self.adaptee.cview

    def get_label(self):
        return self.adaptee.channel_id
Exemple #12
0
class ProjectAdapter(ITreeNodeAdapter):
    """ Base ProjectAdapter for the root of the tree. """

    adapts(Project, ITreeNode)

    #-- ITreeNodeAdapter Method Overrides --------------------------------------

    def allows_children(self):
        """ Returns whether this object can have children.
        """
        return False

    def has_children(self):
        """ Returns whether the object has children.
        """
        return False

    def get_children(self):
        """ Gets the object's children.
        """
        return []

    def get_label(self):
        """ Gets the label to display for a specified object.
        """
        return self.adaptee.name

    def get_tooltip(self):
        """ Gets the tooltip to display for a specified object.
        """
        return "Project"

    def get_icon(self, is_expanded):
        """ Returns the icon for a specified object.
        """
        return '<open>'

    def can_auto_close(self):
        """ Returns whether the object's children should be automatically
            closed.
        """
        return True
Exemple #13
0
    def compute_step(self, point, time):
        x, y, z = point
        return array([
            self.prandtl * (y - x), x * (self.rayleigh - z) - y,
            x * y - self.beta * z
        ])

    ###########################################################################
    # Protected interface.
    ###########################################################################

    @cached_property
    def _get_points(self):
        return odeint(self.compute_step, self.initial_point, self.times)

    @cached_property
    def _get_times(self):
        return arange(self.time_start, self.time_stop, self.time_step)


class LorenzIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin):

    implements(IPlottable2d)

    adaptee = Instance(Lorenz)

    plot_type = Str('line')


adapts(LorenzIPlottable2dAdapter, Lorenz, IPlottable2d)
Exemple #14
0
    ###########################################################################
    # 'Rossler' interface.
    ###########################################################################

    def compute_step(self, point, time):
        x, y, z = point
        return array([ -y - z, x + self.a * y, self.b + z * (x - self.c) ])

    ###########################################################################
    # Protected interface.
    ###########################################################################

    @cached_property
    def _get_points(self):
        return odeint(self.compute_step, self.initial_point, self.times)

    @cached_property
    def _get_times(self):
        return arange(self.time_start, self.time_stop, self.time_step)


class RosslerIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin):

    implements(IPlottable2d)

    adaptee = Instance(Rossler)

    plot_type = Str('line')

adapts(RosslerIPlottable2dAdapter, Rossler, IPlottable2d)
#  Copyright (c) 2007, Enthought, Inc.
#  License: BSD Style.

# external_adapter.py - Example of declaring a class as an adapter
#                       externally to the class

#--[Imports]-------------------------------------------------------------------
from traits.api import adapts
from interface_definition import IName
from interface_implementation import Person


#--[Code]----------------------------------------------------------------------
class AnotherPersonAdapter(object):

    # Implement the adapter's constructor:
    def __init__(self, person):
        self.person = person

    # Implement the 'IName' interface on behalf of its client:
    def get_name(self):
        return ('%s %s' % (self.person.first_name, self.person.last_name))


adapts(AnotherPersonAdapter, Person, IName)
Exemple #16
0
    # 'Rossler' interface.
    ###########################################################################

    def compute_step(self, point, time):
        x, y, z = point
        return array([-y - z, x + self.a * y, self.b + z * (x - self.c)])

    ###########################################################################
    # Protected interface.
    ###########################################################################

    @cached_property
    def _get_points(self):
        return odeint(self.compute_step, self.initial_point, self.times)

    @cached_property
    def _get_times(self):
        return arange(self.time_start, self.time_stop, self.time_step)


class RosslerIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin):

    implements(IPlottable2d)

    adaptee = Instance(Rossler)

    plot_type = Str('line')


adapts(RosslerIPlottable2dAdapter, Rossler, IPlottable2d)
Exemple #17
0
class EnvProjectAdapter(ITreeNodeAdapter):
    """ EnvProjectAdapter for our custom project. """

    adapts(EnvProject, ITreeNode)

    #-- ITreeNodeAdapter Method Overrides --------------------------------------

    def allows_children(self):
        """ Returns whether this object can have children.
        """
        return True

    def has_children(self):
        """ Returns whether the object has children.
        """
        return (len(self.adaptee.list_items) > 0)

    def get_children(self):
        """ Gets the object's children.
        """
        return self.adaptee.list_items

    def get_children_id(self):
        """ Gets the object's children identifier.
        """
        return 'list_items'

    def append_child(self, child=None):
        """ Appends a child to the object's children.
        """
        data = self.adaptee.create_data()

    def confirm_delete(self):
        """ Checks whether a specified object can be deleted.

        Returns
        -------
        * **True** if the object should be deleted with no further prompting.
        * **False** if the object should not be deleted.
        * Anything else: Caller should take its default action (which might
          include prompting the user to confirm deletion).
        """
        return False

    def delete_child(self, index):
        """ Deletes a child at a specified index from the object's children.
        """
        # Remove the child at the specified index.
        child = self.adaptee.list_items[index]
        self.adaptee._unbind_dynamic(child, 'context_name')

    def when_children_replaced(self, listener, remove):
        """ Sets up or removes a listener for children being replaced on a
            specified object.
        """
        self.adaptee.on_trait_change(listener,
                                     'list_items',
                                     remove=remove,
                                     dispatch='ui')

    def get_label(self):
        """ Gets the label to display for a specified object.
        """
        return self.adaptee.name

    def get_menu(self):
        """ Returns the right-click context menu for an object.
        """
        return Menu(
            *
            [Action(
                name='Create Data',
                action='node.adapter.append_child',
            )])

    def get_tooltip(self):
        """ Gets the tooltip to display for a specified object.
        """
        return "Project"

    def get_icon(self, is_expanded):
        """ Returns the icon for a specified object.
        """
        return '<open>'

    def can_rename(self):
        """ Returns whether the object's children can be renamed.
        """
        return True

    def can_copy(self):
        """ Returns whether the object's children can be copied.
        """
        return True

    def can_delete(self):
        """ Returns whether the object's children can be deleted.
        """
        return True

    def can_auto_open(self):
        """ Returns whether the object's children should be automatically
            opened.
        """
        return True

    def can_auto_close(self):
        """ Returns whether the object's children should be automatically
            closed.
        """
        return False
Exemple #18
0
            object = self.object
            return (object.s1 + object.s2 + object.s3)


    class FooPlusAdapter(object):

        def __init__(self, obj):
            self.obj = obj

        def get_foo(self):
            return self.obj.get_foo()

        def get_foo_plus(self):
            return (self.obj.get_foo() + 1)

    adapts(FooPlusAdapter, IFoo, IFooPlus)


@unittest.skipUnless(sys.version_info < (3,),
                     "The 'adapts' and 'implements' class advisors "
                     "are not supported in Python 3.")
class InterfacesTest(unittest.TestCase):

    #### 'TestCase' protocol ##################################################

    def setUp(self):
        set_global_adaptation_manager(_adaptation_manager)

    #### Tests ################################################################

    def test_implements_none(self):
Exemple #19
0
#  Copyright (c) 2007, Enthought, Inc.
#  License: BSD Style.

# external_adapter.py - Example of declaring a class as an adapter
#                       externally to the class

#--[Imports]-------------------------------------------------------------------
from traits.api import adapts
from interface_definition import IName
from interface_implementation import Person


#--[Code]----------------------------------------------------------------------
class AnotherPersonAdapter(object):

    # Implement the adapter's constructor:
    def __init__(self, person):
        self.person = person

    # Implement the 'IName' interface on behalf of its client:
    def get_name(self):
        return ('%s %s' % (self.person.first_name,
                           self.person.last_name))

adapts(AnotherPersonAdapter, Person, IName)
    def get_foo ( self ):
        object = self.object
        return (object.s1 + object.s2 + object.s3)

class FooPlusAdapter ( object ):

    def __init__ ( self, obj ):
        self.obj = obj

    def get_foo ( self ):
        return self.obj.get_foo()

    def get_foo_plus ( self ):
        return (self.obj.get_foo() + 1)

adapts( FooPlusAdapter, IFoo, IFooPlus )

#-------------------------------------------------------------------------------
#  'InterfacesTest' unit test class:
#-------------------------------------------------------------------------------

class InterfacesTest ( unittest.TestCase ):

    #---------------------------------------------------------------------------
    #  Individual unit test methods:
    #---------------------------------------------------------------------------

    def test_implements_none ( self ):
        class Test ( HasTraits ):
            implements()
Exemple #21
0
 class BazToIFooAdapter(Adapter):
     adapts(Baz, IFoo)
Exemple #22
0
class IContextAdapter(Adapter):
    """ Defines an adapter from an codetools.contexts.api.IContext
        to an ITemplateDataContext.
    """

    adapts(IContext, ITemplateDataContext)

    #-- ITemplateDataContext Interface Implementation --------------------------

    # The path to this data context (does not include the 'data_context_name'):
    data_context_path = Str

    # The name of the data context:
    data_context_name = Str

    # A list of the names of the data values in this context:
    data_context_values = List(Str)

    # The list of the names of the sub-contexts of this context:
    data_contexts = List(Str)

    def get_data_context_value(self, name):
        """ Returns the data value with the specified *name*. Raises a
            **ITemplateDataContextError** if *name* is not defined as a data
            value in the context.

            Parameters
            ----------
            name : A string specifying the name of the context data value to
                be returned.

            Returns
            -------
            The data value associated with *name* in the context. The type of
            the data is application dependent.

            Raises **ITemplateDataContextError** if *name* is not associated
            with a data value in the context.
        """
        try:
            if name in self.data_context_values:
                return self.adaptee[name]

            raise ITemplateDataContextError("No value named '%s' found." %
                                            name)
        except Exception as excp:
            raise ITemplateDataContextError(str(excp))

    def get_data_context(self, name):
        """ Returns the **ITemplateDataContext** value associated with the
            specified *name*. Raises **ITemplateDataContextError** if *name* is
            not defined as a data context in the context.

            Parameters
            ----------
            name : A string specifying the name of the data context to be
                returned.

            Returns
            -------
            The **ITemplateDataContext** associated with *name* in the context.

            Raises **ITemplateDataContextError** if *name* is not associated
            with a data context in the context.
        """
        try:
            if name in self.data_contexts:
                bdca = IContextAdapter(self.adaptee[name])
                bdca.data_context_path = path_for(self.data_context_path,
                                                  self.data_context_name)
                return bdca

            raise ITemplateDataContextError("No context named '%s' found." %
                                            name)
        except Exception as excp:
            raise ITemplateDataContextError(str(excp))

    #-- Traits Event Handlers --------------------------------------------------

    def _adaptee_changed(self, context):
        """ Handles being bound to a IContext object.
        """
        self.data_context_name = context.name
        values = []
        contexts = []
        for name in list(context.keys()):
            value = context[name]
            try:
                adapt(value, IContext)
            except AdaptationError:
                # Is not a subcontext.
                values.append(name)
            else:
                # Is a subcontext.
                contexts.append(name)

        self.data_context_values = values
        self.data_contexts = contexts
Exemple #23
0
    ###########################################################################

    def compute_step(self, point, time):
        x, y, z = point
        return array([ self.prandtl * (y - x),
                       x * (self.rayleigh - z) - y,
                       x * y - self.beta * z ])

    ###########################################################################
    # Protected interface.
    ###########################################################################

    @cached_property
    def _get_points(self):
        return odeint(self.compute_step, self.initial_point, self.times)

    @cached_property
    def _get_times(self):
        return arange(self.time_start, self.time_stop, self.time_step)


class LorenzIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin):

    implements(IPlottable2d)

    adaptee = Instance(Lorenz)

    plot_type = Str('line')

adapts(LorenzIPlottable2dAdapter, Lorenz, IPlottable2d)
Exemple #24
0
class DataAdapter(ITreeNodeAdapter):
    """ ITreeNodeAdapter for our custom Data object. """

    adapts(Data, ITreeNode)

    # -- ITreeNodeAdapter Method Overrides --------------------------------------

    def allows_children(self):
        """ Returns whether this object can have children.
        """
        return False

    def get_label(self):
        """ Gets the label to display for a specified object.
        """
        return self.adaptee.name

    def confirm_delete(self):
        """ Checks whether a specified object can be deleted.

        Returns
        -------
        * **True** if the object should be deleted with no further prompting.
        * **False** if the object should not be deleted.
        * Anything else: Caller should take its default action (which might
          include prompting the user to confirm deletion).
        """
        return None

    def when_label_changed(self, listener, remove):
        """ Sets up or removes a listener for the label being changed on a
            specified object.
        """
        self.adaptee.on_trait_change(
            listener, "list_items", remove=remove, dispatch="ui"
        )

    def get_tooltip(self):
        """ Gets the tooltip to display for a specified object.
        """
        return "Data"

    def get_icon(self, is_expanded):
        """ Returns the icon for a specified object.
        """
        return "<item>"

    def can_auto_close(self):
        """ Returns whether the object's children should be automatically
            closed.
        """
        return True

    def can_rename_me(self):
        """ Returns whether the object can be renamed.
        """
        return True

    def can_delete_me(self):
        """ Returns whether the object can be deleted.
        """
        return True