From fc8b8f8b83fed81897ae6189a604fdf1b8fdad3a Mon Sep 17 00:00:00 2001 From: Aaron Ayres Date: Tue, 3 Nov 2020 13:56:44 -0600 Subject: [PATCH 1/2] remove template submodule and any references to it --- README.rst | 1 - TODO.txt | 4 - apptools/preferences/tests/example.ini | 2 +- apptools/template/__init__.py | 7 - apptools/template/api.py | 47 --- apptools/template/impl/__init__.py | 1 - .../impl/any_context_data_name_item.py | 138 ------- apptools/template/impl/any_data_name_item.py | 205 ---------- apptools/template/impl/api.py | 34 -- .../template/impl/context_data_name_item.py | 98 ----- apptools/template/impl/helper.py | 31 -- apptools/template/impl/i_context_adapter.py | 140 ------- .../template/impl/template_data_context.py | 124 ------ .../template/impl/template_data_source.py | 52 --- .../template/impl/value_data_name_item.py | 157 -------- .../template/impl/value_nd_data_name_item.py | 99 ----- apptools/template/imutable_template.py | 41 -- apptools/template/itemplate.py | 74 ---- apptools/template/itemplate_choice.py | 36 -- apptools/template/itemplate_data_context.py | 94 ----- apptools/template/itemplate_data_name_item.py | 71 ---- apptools/template/itemplate_data_source.py | 43 -- apptools/template/mutable_template.py | 42 -- apptools/template/template_choice.py | 32 -- apptools/template/template_data_name.py | 137 ------- apptools/template/template_data_names.py | 255 ------------ apptools/template/template_impl.py | 160 -------- apptools/template/template_traits.py | 95 ----- apptools/template/test/__init__.py | 1 - apptools/template/test/enable_editor.py | 78 ---- apptools/template/test/scatter_plot.py | 212 ---------- apptools/template/test/scatter_plot_2.py | 197 ---------- apptools/template/test/scatter_plot_nm.py | 236 ----------- apptools/template/test/template_view.py | 370 ------------------ 34 files changed, 1 insertion(+), 3313 deletions(-) delete mode 100644 apptools/template/__init__.py delete mode 100644 apptools/template/api.py delete mode 100644 apptools/template/impl/__init__.py delete mode 100644 apptools/template/impl/any_context_data_name_item.py delete mode 100644 apptools/template/impl/any_data_name_item.py delete mode 100644 apptools/template/impl/api.py delete mode 100644 apptools/template/impl/context_data_name_item.py delete mode 100644 apptools/template/impl/helper.py delete mode 100644 apptools/template/impl/i_context_adapter.py delete mode 100644 apptools/template/impl/template_data_context.py delete mode 100644 apptools/template/impl/template_data_source.py delete mode 100644 apptools/template/impl/value_data_name_item.py delete mode 100644 apptools/template/impl/value_nd_data_name_item.py delete mode 100644 apptools/template/imutable_template.py delete mode 100644 apptools/template/itemplate.py delete mode 100644 apptools/template/itemplate_choice.py delete mode 100644 apptools/template/itemplate_data_context.py delete mode 100644 apptools/template/itemplate_data_name_item.py delete mode 100644 apptools/template/itemplate_data_source.py delete mode 100644 apptools/template/mutable_template.py delete mode 100644 apptools/template/template_choice.py delete mode 100644 apptools/template/template_data_name.py delete mode 100644 apptools/template/template_data_names.py delete mode 100644 apptools/template/template_impl.py delete mode 100644 apptools/template/template_traits.py delete mode 100644 apptools/template/test/__init__.py delete mode 100644 apptools/template/test/enable_editor.py delete mode 100644 apptools/template/test/scatter_plot.py delete mode 100644 apptools/template/test/scatter_plot_2.py delete mode 100644 apptools/template/test/scatter_plot_nm.py delete mode 100644 apptools/template/test/template_view.py diff --git a/README.rst b/README.rst index 101a86ded..438821e56 100644 --- a/README.rst +++ b/README.rst @@ -41,7 +41,6 @@ that is commonly needed by many applications - **apptools.sweet_pickle**: Handles class-level versioning, to support loading of saved data that exist over several generations of internal class structures. -- **apptools.template**: Supports creating templatizable object hierarchies. - **apptools.type_manager**: Manages type extensions, including factories to generate adapters, and hooks for methods and functions. - **apptools.undo**: Supports undoing and scripting application commands. diff --git a/TODO.txt b/TODO.txt index 65be9ffec..fb6a518a1 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,7 +1,3 @@ -* Take a critical look at whether apptools.template should be in this project. - It appears to be adding a lot of dependencies the other packages do not need. - For now, its use is controlled through an extra. - * Refactor the state_pickler.py module to break the need to import tvtk at all. diff --git a/apptools/preferences/tests/example.ini b/apptools/preferences/tests/example.ini index 100dc6e16..c535635ce 100644 --- a/apptools/preferences/tests/example.ini +++ b/apptools/preferences/tests/example.ini @@ -3,7 +3,7 @@ bgcolor = blue width = 50 ratio = 1.0 visible = True -description = 'acme ui' +description = acme ui offsets = "[1, 2, 3, 4]" names = "['joe', 'fred', 'jane']" diff --git a/apptools/template/__init__.py b/apptools/template/__init__.py deleted file mode 100644 index d0fb96561..000000000 --- a/apptools/template/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -#------------------------------------------------------------------------------ -# Copyright (c) 2007 by Enthought, Inc. -# All rights reserved. -#------------------------------------------------------------------------------ -""" Supports creating templatizable object hierarchies. - Part of the AppTools project of the Enthought Tool Suite. -""" diff --git a/apptools/template/api.py b/apptools/template/api.py deleted file mode 100644 index db5d5942a..000000000 --- a/apptools/template/api.py +++ /dev/null @@ -1,47 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the external API for the template package. -# -# Written by: David C. Morrill -# -# Date: 07/26/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the external API for the template package. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from .template_traits \ - import TDataSource, TInstance, TList, TInt, TFloat, TStr, TBool, TRange, \ - TEnum, TDerived - -from .itemplate \ - import ITemplate - -from .imutable_template \ - import IMutableTemplate - -from .itemplate_data_context \ - import ITemplateDataContext, ITemplateDataContextError - -from .template_data_name \ - import TemplateDataName - -from .template_data_names \ - import TemplateDataNames - -from .template_impl \ - import Template - -from .mutable_template \ - import MutableTemplate - -from .template_choice \ - import TemplateChoice - diff --git a/apptools/template/impl/__init__.py b/apptools/template/impl/__init__.py deleted file mode 100644 index 8b1378917..000000000 --- a/apptools/template/impl/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/apptools/template/impl/any_context_data_name_item.py b/apptools/template/impl/any_context_data_name_item.py deleted file mode 100644 index 4564ddbba..000000000 --- a/apptools/template/impl/any_context_data_name_item.py +++ /dev/null @@ -1,138 +0,0 @@ -#------------------------------------------------------------------------------- -# -# An abstract base class implementation of the ITemplateDataNameItem interface -# that looks for specified sub-contexts in its input context and if one match -# is found, outputs that context; otherwise if more than one match is found it -# outputs a context containing all matching sub-contexts found. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" An abstract base class implementation of the ITemplateDataNameItem interface - that looks for specified sub-contexts in its input context and if one match - is found, outputs that context; otherwise if more than one match is found it - outputs a context containing all matching sub-contexts found. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import HasPrivateTraits, Instance, Property, provides, on_trait_change - -from apptools.template.itemplate_data_context \ - import ITemplateDataContext - -from apptools.template.itemplate_data_name_item \ - import ITemplateDataNameItem - -from apptools.template.template_impl \ - import Template - -from .template_data_context \ - import TemplateDataContext - -#------------------------------------------------------------------------------- -# 'AnyContextDataNameItem' class: -#------------------------------------------------------------------------------- - -@provides(ITemplateDataNameItem) -class AnyContextDataNameItem ( Template ): - """ An abstract base class implementation of the ITemplateDataNameItem - interface that looks for specified sub-contexts in its input context - and if one match is found, outputs that context; otherwise if more than - one match is found it outputs a context containing all matching - sub-contexts found. - """ - - #-- 'ITemplateDataNameItem' Interface Implementation ----------------------- - - # The data context which this data name item should match against: - input_data_context = Instance( ITemplateDataContext ) - - # The data context containing the data values and/or contexts this data - # name item matches: - output_data_context = Instance( ITemplateDataContext ) - - # The ITemplateChoice instance representing the current settings of the - # data name item. This value must be read/write, and must be overridden by - # sublasses. - data_name_item_choice = Property - - # The alternative choices the user has for the data name item settings for - # the current input data context. The list may be empty, in which case the - # user cannot change the settings of the data name item. This value can be - # read only, and must be overridden by subclasses. - data_name_item_choices = Property - - #-- Private Traits --------------------------------------------------------- - - # The current input data context: - current_input_data_context = Property - - #-- Partially Abstract Methods (Can be overridden in subclasses) ----------- - - def filter ( self, name, context ): - """ Returns **True** if the specified *context* called *name* should be - included in the output context; and **False** otherwise. - """ - return False - - #-- Property Implementations ----------------------------------------------- - - def _get_data_name_item_choice ( self ): - raise NotImplementedError - - def _set_data_name_item_choice ( self, value ): - raise NotImplementedError - - def _get_data_name_item_choices ( self ): - raise NotImplementedError - - def _get_current_input_data_context ( self ): - return self.input_data_context - - #-- Trait Event Handlers --------------------------------------------------- - - def _input_data_context_changed ( self ): - """ Handles the 'input_data_context' trait being changed. - """ - self.inputs_changed() - - #-- Private Methods -------------------------------------------------------- - - def inputs_changed ( self ): - """ Handles any of the input values being changed. May be called by - subclasses. - """ - output_context = None - input_context = self.input_data_context - if input_context is not None: - contexts = {} - - # Process each name/context in the input data contexts, and only add - # those that match the subclass's filter to the output context: - filter = self.filter - gdc = input_context.get_data_context - for name in input_context.data_contexts: - if filter( name, gdc( name ) ): - contexts[ name ] = context - - # If the result set is not empty, create an output context for it: - n = len( contexts ) - if n == 1: - output_context = list(values.values())[0] - elif n > 1: - output_context = TemplateDataContext( - data_context_path = input_context.data_context_path, - data_context_name = input_context.data_context_name, - contexts = contexts ) - - # Set the new output context: - self.output_data_context = output_context diff --git a/apptools/template/impl/any_data_name_item.py b/apptools/template/impl/any_data_name_item.py deleted file mode 100644 index bbd78bf3c..000000000 --- a/apptools/template/impl/any_data_name_item.py +++ /dev/null @@ -1,205 +0,0 @@ -#------------------------------------------------------------------------------- -# -# An abstract base class implementation of the ITemplateDataNameItem interface -# that looks for all specified values in its input context or optionally any of -# its sub-contexts and outputs a context containing all such values found. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" An abstract base class implementation of the ITemplateDataNameItem interface - that looks for all specified values in its input context or optionally any - of its sub-contexts and outputs a context containing all such values found. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import HasPrivateTraits, Instance, Property, provides - -from apptools.template.template_traits \ - import TBool - -from apptools.template.itemplate_data_context \ - import ITemplateDataContext - -from apptools.template.itemplate_data_name_item \ - import ITemplateDataNameItem - -from apptools.template.template_impl \ - import Template - -from .template_data_context \ - import TemplateDataContext - -from .helper \ - import path_for - -#------------------------------------------------------------------------------- -# 'AnyDataNameItem' class: -#------------------------------------------------------------------------------- - -@provides(ITemplateDataNameItem) -class AnyDataNameItem ( Template ): - """ An abstract base class implementation of the ITemplateDataNameItem - interface that looks for all specified values in its input context or - optionally any of its sub-contexts and outputs a context containing all - such values found. - """ - - #-- 'ITemplateDataNameItem' Interface Implementation ----------------------- - - # The data context which this data name item should match against: - input_data_context = Instance( ITemplateDataContext ) - - # The data context containing the data values and/or contexts this data - # name item matches: - output_data_context = Instance( ITemplateDataContext ) - - # The ITemplateChoice instance representing the current settings of the - # data name item. This value must be read/write, and must be overridden by - # sublasses. - data_name_item_choice = Property - - # The alternative choices the user has for the data name item settings for - # the current input data context. The list may be empty, in which case the - # user cannot change the settings of the data name item. This value can be - # read only, and must be overridden by subclasses. - data_name_item_choices = Property - - #-- Public Traits ---------------------------------------------------------- - - # Should all sub-contexts be included in the search: - recursive = TBool( False ) - - # Should included sub-contexts be flattened into a single context? - flatten = TBool( False ) - - #-- Private Traits --------------------------------------------------------- - - # The current recursive setting: - current_recursive = TBool( False ) - - # The current input data context: - current_input_data_context = Property - - #-- Abstract Methods (Must be overridden in subclasses) -------------------- - - def filter ( self, name, value ): - """ Returns **True** if the specified context data *name* and *value* - should be included in the output context; and **False** otherwise. - """ - raise NotImplementedError - - #-- Property Implementations ----------------------------------------------- - - def _get_data_name_item_choice ( self ): - raise NotImplementedError - - def _set_data_name_item_choice ( self, value ): - raise NotImplementedError - - def _get_data_name_item_choices ( self ): - raise NotImplementedError - - def _get_current_input_data_context ( self ): - return self.input_data_context - - #-- Trait Event Handlers --------------------------------------------------- - - def _recursive_changed ( self, value ): - """ Handles the primary recursive setting being changed. - """ - self.current_recursive = value - - def _input_data_context_changed ( self ): - """ Handles the 'input_data_context' trait being changed. - """ - self.inputs_changed() - - #-- Private Methods -------------------------------------------------------- - - def inputs_changed ( self ): - """ Handles any input value being changed. This method should be called - by subclasses when any of their input values change. - """ - output_context = None - input_context = self.current_input_data_context - if input_context is not None: - values = {} - - if self.current_recursive: - if self.flatten: - self._add_context( input_context, values ) - else: - self._copy_context( input_context, values ) - else: - self._add_values( input_context, values, '' ) - - if len( values ) > 0: - output_context = TemplateDataContext( - data_context_path = input_context.data_context_path, - data_context_name = input_context.data_context_name, - values = values ) - - self.output_data_context = output_context - - def _add_values ( self, input_context, values, path = '' ): - """ Adds all of the matching values in the specified *input_context* to - the specified *values* dictionary. - """ - # Filter each name/value in the current input context to see if it - # should be added to the output values: - filter = self.filter - gdcv = input_context.get_data_context_value - for name in input_context.data_context_values: - value = gdcv( name ) - if self.filter( name, value ): - values[ path_for( path, name ) ] = value - - def _add_context ( self, input_context, values, path = '' ): - """ Adds all of the matching values in the specified *input_context* to - the specified *output_context*, and then applies itself recursively - to all contexts contained in the specified *input_context*. - """ - # Add all of the filtered values in the specified input context: - self._add_values( input_context, values, path ) - - # Now process all of the input context's sub-contexts: - gdc = input_context.get_data_context - for name in input_context.data_contexts: - self._add_context( gdc( name ), values, path_for( path, - input_context.data_context_name ) ) - - def _copy_context ( self, input_context ): - """ Clone the input context so that the result only contains values and - contexts which contain valid values and are not empty. - """ - values = {} - contexts = {} - - # Add all of the filtered values in the specified input context: - self._add_values( input_context, values ) - - # Now process all of the input context's sub-contexts: - gdc = input_context.get_data_context - for name in input_context.data_contexts: - context = self._copy_context( gdc( name ) ) - if context is not None: - contexts[ name ] = context - - if (len( values ) == 0) and (len( contexts ) == 0): - return None - - return TemplateDataContext( - data_context_path = input_context.data_context_path, - data_context_name = input_context.data_context_name, - values = values, - contexts = contexts ) diff --git a/apptools/template/impl/api.py b/apptools/template/impl/api.py deleted file mode 100644 index 62bb6f677..000000000 --- a/apptools/template/impl/api.py +++ /dev/null @@ -1,34 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Publically exported symbols for the template.impl package. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -from .any_context_data_name_item \ - import AnyContextDataNameItem - -from .any_data_name_item \ - import AnyDataNameItem - -from .context_data_name_item \ - import ContextDataNameItem - -from .template_data_context \ - import TemplateDataContext - -from .template_data_source \ - import TemplateDataSource - -from .value_data_name_item \ - import ValueDataNameItem - -from .value_nd_data_name_item \ - import ValueNDDataNameItem, Value1DDataNameItem, Value2DDataNameItem, \ - Value3DDataNameItem - diff --git a/apptools/template/impl/context_data_name_item.py b/apptools/template/impl/context_data_name_item.py deleted file mode 100644 index 0e4c52b7e..000000000 --- a/apptools/template/impl/context_data_name_item.py +++ /dev/null @@ -1,98 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A concrete implementation of the ITemplateDataNameItem interface that looks -# for a specified sub-context in its input context and outputs that as its -# output context if it is found. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A concrete implementation of the ITemplateDataNameItem interface that looks - for a specified sub-context in its input context and outputs that as its - output context if it is found. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from apptools.template.template_traits \ - import TStr - -from .any_context_data_name_item \ - import AnyContextDataNameItem - -from .helper \ - import path_for, parse_name - -#------------------------------------------------------------------------------- -# 'ContextDataNameItem' class: -#------------------------------------------------------------------------------- - -class ContextDataNameItem ( AnyContextDataNameItem ): - """ A concrete implementation of the ITemplateDataNameItem interface that - looks for a specified sub-context in its input context and outputs that - as its output context if it is found. - """ - - #-- Public Traits ---------------------------------------------------------- - - # The name of the context to be matched: - name = TStr - - #-- Abstract Method Implementations ---------------------------------------- - - def filter ( self, name, context ): - """ Returns **True** if the specified *context* called *name* should be - included in the output context; and **False** otherwise. - """ - return (name == self.name_last) - - #-- AnyDataNameItem Property Implementation Overrides ---------------------- - - def _get_data_name_item_choice ( self ): - return TemplateChoice( choice_value = self.name ) - - def _set_data_name_item_choice ( self, value ): - self.name = value.choice_value - - def _get_data_name_item_choices ( self ): - return self._get_choices( self.input_data_context ) - - def _get_current_input_data_context ( self ): - context = self.input_data_context - for name in parse_name( self.name )[:-1]: - if name not in context.data_contexts: - return None - - context = context.get_data_context( name ) - - return context - - #-- Trait Event Handlers --------------------------------------------------- - - def _name_changed ( self, name ): - """ Handles the 'name' trait being changed. - """ - self.name_last = parse_name( name )[-1] - self.inputs_changed() - - #-- Private Methods -------------------------------------------------------- - - def _get_choices ( self, context, path = '' ): - """ Returns all of the valid TemplateChoices for this item. - """ - choices = [] - gdc = context.get_data_context - for name in context.data_contexts: - next_path = path_for( path, name ) - choices.append( TemplateChoice( choice_value = next_path ) ) - choices.extend( self._get_choices( gdc( name ), next_path ) ) - - return choices - diff --git a/apptools/template/impl/helper.py b/apptools/template/impl/helper.py deleted file mode 100644 index e86fe37b3..000000000 --- a/apptools/template/impl/helper.py +++ /dev/null @@ -1,31 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Helper functions/classes useful for implementing various template interfaces. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -#------------------------------------------------------------------------------- -# Returns a properly joined path name: -#------------------------------------------------------------------------------- - -def path_for ( *names ): - """ Returns a properly joined path name (i.e. the elements of the name are - separated by '.'). - """ - return '.'.join( [ name for name in names if name != '' ] ) - -#------------------------------------------------------------------------------- -# Parses a possible compound data context name: -#------------------------------------------------------------------------------- - -def parse_name ( name ): - """ Parses a possible compound data context name. - """ - return name.split( '.' ) - diff --git a/apptools/template/impl/i_context_adapter.py b/apptools/template/impl/i_context_adapter.py deleted file mode 100644 index 21514b16e..000000000 --- a/apptools/template/impl/i_context_adapter.py +++ /dev/null @@ -1,140 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines an adapter from an codetools.contexts.api.IContext -# to an ITemplateDataContext. -# -# Written by: David C. Morrill -# Modified by: Robert Kern -# -# Date: 11/16/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines an adapter from an codetools.contexts.api.IContext to an - ITemplateDataContext. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Adapter, Str, List, adapts - -from traits.protocols.api \ - import AdaptationError, adapt - -from codetools.contexts.api \ - import IContext - -from apptools.template.itemplate_data_context \ - import ITemplateDataContext, ITemplateDataContextError - -from .helper \ - import path_for - -#------------------------------------------------------------------------------- -# 'IContextAdapter' class: -#------------------------------------------------------------------------------- - -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 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 - diff --git a/apptools/template/impl/template_data_context.py b/apptools/template/impl/template_data_context.py deleted file mode 100644 index a484d640d..000000000 --- a/apptools/template/impl/template_data_context.py +++ /dev/null @@ -1,124 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A concrete implementation of the ITemplateDataContext interface intended to -# be used for creating the *output_data_context* value of an -# **ITemplateDataNameItem** implementation (although they are not required to -# use it). -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A concrete implementation of the ITemplateDataContext interface intended to - be used for creating the *output_data_context* value of an - **ITemplateDataNameItem** implementation (although they are not required to - use it). -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api\ - import HasPrivateTraits, Dict, Str, Any, Property, provides, \ - cached_property - -from apptools.template.itemplate_data_context \ - import ITemplateDataContext, ITemplateDataContextError - -#------------------------------------------------------------------------------- -# 'TemplateDataContext' class: -#------------------------------------------------------------------------------- - -@provides(ITemplateDataContext) -class TemplateDataContext ( HasPrivateTraits ): - """ A concrete implementation of the ITemplateDataContext interface - intended to be used for creating the *output_data_context* value of an - **ITemplateDataNameItem** implementation (although they are not - required to use it). - """ - - #-- 'ITemplateDataContext' Interface Traits -------------------------------- - - # 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 = Property # List( Str ) - - # The list of the names of the sub-contexts of this context: - data_contexts = Property # List( Str ) - - #-- Public Traits --------------------------------------------------------- - - # The data context values dictionary: - values = Dict( Str, Any ) - - # The data contexts dictionary: - contexts = Dict( Str, ITemplateDataContext ) - - #-- 'ITemplateDataContext' Property Implementations ------------------------ - - @cached_property - def _get_data_context_values ( self ): - values = sorted(self.values.keys()) - return values - - @cached_property - def _get_data_contexts ( self ): - contexts = sorted(self.contexts.keys()) - return contexts - - #-- 'ITemplateDataContext' Interface Implementation ------------------------ - - 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: - return self.values[ name ] - except: - raise ITemplateDataContextError( "Value '%s' not found." % name ) - - 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: - return self.context[ name ] - except: - raise ITemplateDataContextError( "Context '%s' not found." % name ) diff --git a/apptools/template/impl/template_data_source.py b/apptools/template/impl/template_data_source.py deleted file mode 100644 index c6e416e3f..000000000 --- a/apptools/template/impl/template_data_source.py +++ /dev/null @@ -1,52 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A concrete implementation of the ITemplateDataSource interface based on the -# implementation of the TemplateDataName class. -# -# Write by: David C. Morrill -# -# Date: 07/30/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A concrete implementation of the ITemplateDataSource interface based on the - implementation of the TemplateDataName class. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import provides - -from apptools.template.template_data_name \ - import TemplateDataName - -from apptools.template.itemplate_data_source \ - import ITemplateDataSource - -#------------------------------------------------------------------------------- -# 'TemplateDataSource' class: -#------------------------------------------------------------------------------- - -@provides(ITemplateDataSource) -class TemplateDataSource ( TemplateDataName ): - """ A concrete implementation of the ITemplateDataSource interface based on - the implementation of the TemplateDataName class. - """ - - #-- ITemplateDataSource Interface Implementation --------------------------- - - def name_from_data_source ( self ): - """ Allows the object to provide a description of the possibly optional - data binding it requires. - - Returns - ------- - A **TemplateDataName** object describing the binding the data - source object requires. - """ - return self diff --git a/apptools/template/impl/value_data_name_item.py b/apptools/template/impl/value_data_name_item.py deleted file mode 100644 index 7456c1c49..000000000 --- a/apptools/template/impl/value_data_name_item.py +++ /dev/null @@ -1,157 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A concrete implementation of the ITemplateDataNameItem interface that looks -# for a specified named value in its input context or optionally in any of its -# sub-contexts and outputs a context containing only those values that match -# the specified name. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A concrete implementation of the ITemplateDataNameItem interface that looks - for a specified named value in its input context or optionally in any of its - sub-contexts and outputs a context containing only those values that match - the specified name. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Bool - -from apptools.template.template_traits \ - import TStr, TBool - -from apptools.template.template_choice \ - import TemplateChoice - -from apptools.template.itemplate_data_context \ - import ITemplateDataContext - -from .any_data_name_item \ - import AnyDataNameItem - -from .helper \ - import parse_name, path_for - -#------------------------------------------------------------------------------- -# Constants: -#------------------------------------------------------------------------------- - -# Value used to reset to the factory settings: -ResetChoice = '----' - -#------------------------------------------------------------------------------- -# 'ValueDataNameItem' class: -#------------------------------------------------------------------------------- - -class ValueDataNameItem ( AnyDataNameItem ): - """ A concrete implementation of the ITemplateDataNameItem interface that - looks for a specified named value in its input context or optionally in - any of its sub-contexts and outputs a context containing only those - values that match the specified name. - """ - - #-- Public Traits ---------------------------------------------------------- - - # The name of the value to be matched: - name = TStr - - # (Override) Should included sub-contexts be flattened into a single - # context? - flatten = True - - #-- Private Traits --------------------------------------------------------- - - # The current name of the value to be matched: - current_name = TStr - - # The current name's last component: - current_name_last = TStr - - # Should all possible choices be included? - all_choices = Bool( True ) - - #-- AnyDataNameItem Property Implementation Overrides ---------------------- - - def _get_data_name_item_choice ( self ): - return TemplateChoice( choice_value = self.current_name ) - - def _set_data_name_item_choice ( self, choice ): - if choice.choice_value == ResetChoice: - self.current_recursive = self.recursive - self.current_name = self.name - self.all_choices = True - else: - self.current_recursive = False - self.current_name = choice.choice_value - self.all_choices = False - - def _get_data_name_item_choices ( self ): - context = self.input_data_context - if context is None: - return [] - - if not self.all_choices: - output_context = self.output_data_context - if output_context is not None: - return [ TemplateChoice( choice_value = name ) - for name in output_context.data_context_values ] - - return ([ TemplateChoice( choice_value = ResetChoice ) ] + - self._get_choices( context )) - - def _get_current_input_data_context ( self ): - context = self.input_data_context - for name in parse_name( self.current_name )[:-1]: - if name not in context.data_contexts: - return None - - context = context.get_data_context( name ) - - return context - - #-- Abstract Method Implementations ---------------------------------------- - - def filter ( self, name, value ): - """ Returns **True** if the specified context data *name* and *value* - should be included in the output context; and **False** otherwise. - """ - return (name == self.current_name_last) - - #-- Trait Event Handlers --------------------------------------------------- - - def _name_changed ( self, name ): - """ Handles the 'name' trait being changed. - """ - self.current_name = name - - def _current_name_changed ( self, name ): - self.current_name_last = parse_name( name )[-1] - self.inputs_changed() - - #-- Private Methods -------------------------------------------------------- - - def _get_choices ( self, context, path = '' ): - """ Returns the list of available user settings choices for the - specified context. - """ - choices = [ TemplateChoice( choice_value = path_for( path, name ) ) - for name in context.data_context_values ] - - if self.recursive: - # Now process all of the context's sub-contexts: - gdc = context.get_data_context - for name in context.data_contexts: - choices.extend( self._get_choices( gdc( name ), path_for( path, - context.data_context_name ) ) ) - - return choices - diff --git a/apptools/template/impl/value_nd_data_name_item.py b/apptools/template/impl/value_nd_data_name_item.py deleted file mode 100644 index bfd5d619f..000000000 --- a/apptools/template/impl/value_nd_data_name_item.py +++ /dev/null @@ -1,99 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A concrete implementation of the ITemplateDataNameItem interface that looks -# for array values of a specified dimensionality in its input context or -# optionally in any of its sub-contexts and outputs a context containing -# only those values that it found. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A concrete implementation of the ITemplateDataNameItem interface that looks - for array values of a specified dimensionality in its input context or - optionally in any of its sub-contexts and outputs a context containing - only those values that it found. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from numpy \ - import array - -from apptools.template.template_traits \ - import TRange - -from apptools.template.template_choice \ - import TemplateChoice - -from .any_data_name_item \ - import AnyDataNameItem - -#------------------------------------------------------------------------------- -# 'ValueNDDataNameItem' class: -#------------------------------------------------------------------------------- - -class ValueNDDataNameItem ( AnyDataNameItem ): - """ A concrete implementation of the ITemplateDataNameItem interface that - looks for array values of a specified dimensionality in its input - context or optionally in any of its sub-contexts and outputs a context - containing only those values that it found. - """ - - #-- Public Traits ---------------------------------------------------------- - - # The dimensionality of the array values to be accepted: - dimensions = TRange( 0, 1000 ) - - #-- AnyDataNameItem Property Implementation Overrides ---------------------- - - def _get_data_name_item_choice ( self ): - return TemplateChoice( choice_value = '%dD array' % self.dimensions ) - - def _set_data_name_item_choice ( self, value ): - pass - - def _get_data_name_item_choices ( self ): - return [] - - #-- Abstract Method Implementations ---------------------------------------- - - def filter ( self, name, value ): - """ Returns **True** if the specified context data *name* and *value* - should be included in the output context; and **False** otherwise. - """ - return (isinstance( value, array ) and - (len( value.shape ) == self.dimensions )) - - #-- Trait Event Handlers --------------------------------------------------- - - def _name_changed ( self ): - """ Handles the 'name' trait being changed. - """ - self.inputs_changed() - -#------------------------------------------------------------------------------- -# Define a few common sub-classes for 1D, 2D and 3D arrays: -#------------------------------------------------------------------------------- - -class Value1DDataNameItem ( ValueNDDataNameItem ): - - # Override the dimensionaly of the array values to be accepted: - dimensions = 1 - -class Value2DDataNameItem ( ValueNDDataNameItem ): - - # Override the dimensionaly of the array values to be accepted: - dimensions = 2 - -class Value3DDataNameItem ( ValueNDDataNameItem ): - - # Override the dimensionaly of the array values to be accepted: - dimensions = 3 - diff --git a/apptools/template/imutable_template.py b/apptools/template/imutable_template.py deleted file mode 100644 index 60a9d3237..000000000 --- a/apptools/template/imutable_template.py +++ /dev/null @@ -1,41 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the IMutableTemplate interface used to define modifiable templates. -# -# Written by: David C. Morrill -# -# Date: 08/01/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the IMutableTemplate interface used to define modifiable templates. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Event - -from .itemplate \ - import ITemplate - -#------------------------------------------------------------------------------- -# 'IMutableTemplate' interface: -#------------------------------------------------------------------------------- - -class IMutableTemplate ( ITemplate ): - """ Defines the IMutableTemplate interface used to define modifiable - templates. - - Extends the ITemplate interface by supporting the ability for the - template to modify itself. - """ - - # An event fired when the template mutates (i.e. changes in some way that - # may affect the number of data sources it exposes, and so on): - template_mutated = Event - diff --git a/apptools/template/itemplate.py b/apptools/template/itemplate.py deleted file mode 100644 index 4f4de7956..000000000 --- a/apptools/template/itemplate.py +++ /dev/null @@ -1,74 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the ITemplate interface used to create templatizable object -# hierarchies. -# -# Written by: David C. Morrill -# -# Date: 07/26/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the ITemplate interface used to create templatizable object - hierarchies. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Interface - -#------------------------------------------------------------------------------- -# 'ITemplate' interface: -#------------------------------------------------------------------------------- - -class ITemplate ( Interface ): - """ Defines the ITemplate interface used to create templatizable object - hierarchies. - """ - - def names_from_template ( self ): - """ 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. - """ - - def object_from_template ( self ): - """ Activates the object from its template data. - - Returns - ------- - The original object. - """ - - def template_from_object ( self ): - """ Returns a *templatized* version of the object that is safe for - serialization. - - Returns - ------- - A new object of the same class as the original. - """ - - def activate_template ( self ): - """ Converts all contained 'TDerived' objects to real objects using the - template traits of the object. - - Returns - ------- - None - """ - diff --git a/apptools/template/itemplate_choice.py b/apptools/template/itemplate_choice.py deleted file mode 100644 index 06f08eb4d..000000000 --- a/apptools/template/itemplate_choice.py +++ /dev/null @@ -1,36 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the ITemplateChoice interface used by ITemplateDataNameItem -# interface. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the ITemplateChoice interface used by ITemplateDataNameItem - interface. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Interface, Str - -#------------------------------------------------------------------------------- -# 'ITemplateChoice' interface: -#------------------------------------------------------------------------------- - -class ITemplateChoice ( Interface ): - """ Defines the ITemplateChoice interface used by ITemplateDataNameItem - interface. - """ - - # The user interface string for this choice: - choice_value = Str - diff --git a/apptools/template/itemplate_data_context.py b/apptools/template/itemplate_data_context.py deleted file mode 100644 index edd13f02d..000000000 --- a/apptools/template/itemplate_data_context.py +++ /dev/null @@ -1,94 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the ITemplateDataContext interface for accessing a named collection -# of data that can be bound to a templatized object when converting it to a -# 'live' set of objects. -# -# Written by: David C. Morrill -# -# Date: 07/26/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the ITemplateDataContext interface for accessing a named collection - of data that can be bound to a templatized object when converting it to a - 'live' set of objects. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api\ - import Interface, List, Str - -#------------------------------------------------------------------------------- -# 'ITemplateDataContext' interface: -#------------------------------------------------------------------------------- - -class ITemplateDataContext ( Interface ): - """ Defines the ITemplateDataContext interface for accessing a named - collection of data that can be bound to a templatized object when - converting it to a 'live' set of objects. - """ - - # 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. - """ - - 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. - """ - -#------------------------------------------------------------------------------- -# 'ITemplateDataContextError' exception: -#------------------------------------------------------------------------------- - -class ITemplateDataContextError ( Exception ): - """ The exception class associated with the **ITemplateDataContext** - interface. - """ - diff --git a/apptools/template/itemplate_data_name_item.py b/apptools/template/itemplate_data_name_item.py deleted file mode 100644 index c58ad9db8..000000000 --- a/apptools/template/itemplate_data_name_item.py +++ /dev/null @@ -1,71 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the ITemplateDataNameItem interface used by elements of a -# TemplateDataName. -# -# Written by: David C. Morrill -# -# Date: 07/27/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the ITemplateDataNameItem interface used by elements of a - TemplateDataName. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Interface, Instance, List - -from .itemplate_data_context \ - import ITemplateDataContext - -from .itemplate_choice \ - import ITemplateChoice - -#------------------------------------------------------------------------------- -# 'ITemplateDataNameItem' interface: -#------------------------------------------------------------------------------- - -class ITemplateDataNameItem ( Interface ): - """ Defines the ITemplateDataNameItem interface used by elements of a - TemplateDataName. - - The contents of the *input_data_context* and *output_data_context* - are assumed to be immutable. That is, no changes should occur to the - data contexts once they have been assigned to the - **ITemplateDataNameItem** traits. - - However, new input contexts can be assigned, in which case a new - output context should be created and assigned to the output context - (once it has been fully populated). - - Also, the *output_data_context* should be **None** if the - *input_data_context* value is **None** or the object cannot match any - values in the *input_data_context*. - """ - - # The data context which this data name item should match against. This - # value must be read/write. - input_data_context = Instance( ITemplateDataContext ) - - # The data context containing the data values and/or contexts this data - # name item matches. This value must be read/write. - output_data_context = Instance( ITemplateDataContext ) - - # The ITemplateChoice instance representing the current settings of the - # data name item. This value must be read/write. - data_name_item_choice = Instance( ITemplateChoice ) - - # The alternative choices the user has for the data name item settings for - # the current input data context. The list may be empty, in which case the - # user cannot change the settings of the data name item. This value can be - # read only. - data_name_item_choices = List( ITemplateChoice ) - - diff --git a/apptools/template/itemplate_data_source.py b/apptools/template/itemplate_data_source.py deleted file mode 100644 index b4f7b59be..000000000 --- a/apptools/template/itemplate_data_source.py +++ /dev/null @@ -1,43 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the ITemplateDataSource interface for creating 'live' application -# data sources from a templatized data source object. -# -# Written by: David C. Morrill -# -# Date: 07/26/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the ITemplateDataSource interface for creating 'live' application - data sources from a templatized data source object. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Interface - -#------------------------------------------------------------------------------- -# 'ITemplateDataSource' interface: -#------------------------------------------------------------------------------- - -class ITemplateDataSource ( Interface ): - """ Defines the ITemplateDataSource interface for creating 'live' - application data sources from a templatized data source object. - """ - - def name_from_data_source ( self ): - """ Allows the object to provide a description of the possibly optional - data binding it requires. - - Returns - ------- - A **TemplateDataName** object describing the binding the data - source object requires. - """ - diff --git a/apptools/template/mutable_template.py b/apptools/template/mutable_template.py deleted file mode 100644 index b1e31370b..000000000 --- a/apptools/template/mutable_template.py +++ /dev/null @@ -1,42 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A concrete base class that implements the IMutableTemplate interface. -# -# Written by: David C. Morrill -# -# Date: 08/01/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A concrete base class that implements the IMutableTemplate interface. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Event, provides - -from .template_impl \ - import Template - -from .imutable_template \ - import IMutableTemplate - -#------------------------------------------------------------------------------- -# 'MutableTemplate' class: -#------------------------------------------------------------------------------- - -@provides(IMutableTemplate) -class MutableTemplate ( Template ): - """ A concrete base class that implements the IMutableTemplate interface. - """ - - #-- IMutableTemplate Interface Implementation ------------------------------ - - # An event fired when the template mutates (i.e. changes in some way that - # may affect the number of data sources it exposes, and so on): - template_mutated = Event diff --git a/apptools/template/template_choice.py b/apptools/template/template_choice.py deleted file mode 100644 index bc4bd2557..000000000 --- a/apptools/template/template_choice.py +++ /dev/null @@ -1,32 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A concrete implementation of the ITemplateChoice interface that can be used -# as is for simple cases, or extended for more complex ones. -# -# Written by: David C. Morrill -# -# Date: 07/29/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import HasPrivateTraits, Str, provides - -from .itemplate_choice \ - import ITemplateChoice - -#------------------------------------------------------------------------------- -# 'TemplateChoice' class: -#------------------------------------------------------------------------------- - -@provides(ITemplateChoice) -class TemplateChoice ( HasPrivateTraits ): - - # The user interface string for this choice: - choice_value = Str diff --git a/apptools/template/template_data_name.py b/apptools/template/template_data_name.py deleted file mode 100644 index 8e2b7d076..000000000 --- a/apptools/template/template_data_name.py +++ /dev/null @@ -1,137 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the TemplateDataName class used for binding a data source to a data -# context. -# -# Written by: David C. Morrill -# -# Date: 07/27/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the TemplateDataName class used for binding a data source to a data - context. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import HasPrivateTraits, Instance, Bool, Property, Undefined, \ - on_trait_change, cached_property - -from .itemplate_data_name_item \ - import ITemplateDataNameItem - -from .itemplate_data_context \ - import ITemplateDataContext - -from .template_impl \ - import Template - -from .template_traits \ - import TList, TStr, TBool - -#------------------------------------------------------------------------------- -# 'TemplateDataName' interface: -#------------------------------------------------------------------------------- - -class TemplateDataName ( Template ): - """ Defines the TemplateDataName class used for binding a data source to a - data context. - """ - - #-- Public Template Traits ------------------------------------------------- - - # The list of ITemplateDataNameItem's used to match the data context: - items = TList( ITemplateDataNameItem ) - - # A description of the template data name (for use in a user interface): - description = TStr - - # Is this binding optional? - optional = TBool( False ) - - #-- Public Non-Template Traits --------------------------------------------- - - # The data context the template name is matching against currently: - context = Instance( ITemplateDataContext ) - - # Is the binding resolved? - resolved = Property( Bool, depends_on = 'items.output_data_context' ) - - # The actual name of the context data that the data source is bound to: - context_data_name = Property - - # The context data the data source is bound to: - context_data = Property - - #-- Property Implementations ----------------------------------------------- - - @cached_property - def _get_resolved ( self ): - if len( self.items ) == 0: - return False - - context = self.items[-1].output_data_context - return ((context is not None) and - (len( context.data_context_values ) == 1) and - (len( context.data_contexts ) == 0)) - - def _get_context_data_name ( self ): - if self.resolved: - context = self.items[-1].output_data_context - path = context.data_context_path - if path != '': - path += '.' - name = context.data_context_name - if name != '': - name += '.' - - return '%s%s%s' % ( path, name, context.data_context_values[0] ) - - return '' - - def _get_context_data ( self ): - if self.resolved: - context = self.items[-1].output_data_context - return context.get_data_context_value( - context.data_context_values[0] ) - - return Undefined - - #-- Trait Event Handlers --------------------------------------------------- - - def _context_changed ( self, context ): - """ Resets the name whenever the context is changed. - """ - self._reset() - - @on_trait_change( ' items' ) - def _on_items_changed ( self ): - """ Resets the name whenever any of the name items are changed. - """ - self._reset() - - @on_trait_change( 'items:output_data_context' ) - def _on_output_data_context_changed ( self, item, name, old, new ): - i = self.items.index( item ) - if i < (len( self.items ) - 1): - self.items[ i + 1 ].input_data_context = new - - #-- Private Methods -------------------------------------------------------- - - def _reset ( self ): - """ Resets the name whenever any significant change occurs. - """ - items = self.items - n = len( items ) - if n > 0: - items[0].input_data_context = self.context - for i in range( 0, n - 1 ): - items[ i + 1 ].input_data_context = \ - items[ i ].output_data_context - diff --git a/apptools/template/template_data_names.py b/apptools/template/template_data_names.py deleted file mode 100644 index 336a4565d..000000000 --- a/apptools/template/template_data_names.py +++ /dev/null @@ -1,255 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the TemplateDataNames class used to manage application data source -# bindings to named context data. -# -# Written by: David C. Morrill -# -# Date: 07/26/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the TemplateDataNames class used to manage application data source - bindings to named context data. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import HasPrivateTraits, Instance, Int, List, Property, Delegate, \ - cached_property - -from traitsui.api \ - import View, Item, TableEditor, EnumEditor, TextEditor - -from traitsui.table_column \ - import ObjectColumn - -from .template_data_name \ - import TemplateDataName - -from .itemplate_data_context \ - import ITemplateDataContext - -from .template_choice \ - import TemplateChoice - -#------------------------------------------------------------------------------- -# Table editor support for editing a list of TemplateDataName objects: -#------------------------------------------------------------------------------- - -class BindingsColumn ( ObjectColumn ): - - # The column index: - index = Int - - #-- ObjectColumn Method Overrides ------------------------------------------ - - def is_editable ( self, object ): - return (self.index < len( object.data_name.items )) - - def get_editor ( self, object ): - return EnumEditor( values = - [ dnic.choice_value for dnic in object.data_name.items[ - self.index ].data_name_item_choices ] + - [ self.get_raw_value( object ) ] ) - -class ResolvedColumn ( ObjectColumn ): - - def get_raw_value( self, object ): - if object.data_name.resolved: - return '' - - return 'X' - - def get_cell_color ( self, object ): - if object.data_name.resolved: - return self.read_only_cell_color_ - - return self.cell_color_ - -class OptionalColumn ( ObjectColumn ): - - def get_raw_value( self, object ): - return ' X'[ object.data_name.optional ] - -# Define the table editor: -table_editor = TableEditor( - columns_name = 'table_columns', - configurable = False, - auto_size = False, - sortable = False, - scroll_dy = 4, - selection_bg_color = None -) - -# The standard columns: -std_columns = [ - ResolvedColumn( name = 'resolved', - label = '?', - editable = False, - width = 20, - horizontal_alignment = 'center', - cell_color = 0xFF8080 ), - OptionalColumn( name = 'optional', - label = '*', - editable = False, - width = 20, - horizontal_alignment = 'center' ), - ObjectColumn( name = 'description', - editor = TextEditor(), - width = 0.47 ) ] - -#------------------------------------------------------------------------------- -# 'TemplateDataNames' class: -#------------------------------------------------------------------------------- - -class TemplateDataNames ( HasPrivateTraits ): - - #-- Public Traits ---------------------------------------------------------- - - # The data context to which bindings are made: - context = Instance( ITemplateDataContext ) - - # The current set of data names to be bound to the context: - data_names = List( TemplateDataName ) - - # The list of unresolved, required bindings: - unresolved_data_names = Property( depends_on = 'data_names.resolved' ) - - # The list of optional bindings: - optional_data_names = Property( depends_on = 'data_names.optional' ) - - # The list of unresolved optional bindings: - unresolved_optional_data_names = Property( - depends_on = 'data_names.[resolved,optional]' ) - - #-- Private Traits --------------------------------------------------------- - - # List of 'virtual' data names for use by table editor: - virtual_data_names = List - - # The list of table editor columns: - table_columns = Property( depends_on = 'data_names' ) # List( ObjectColumn ) - - #-- Traits View Definitions ------------------------------------------------ - - view = View( - Item( 'virtual_data_names', - show_label = False, - style = 'custom', - editor = table_editor ) - ) - - #-- Property Implementations ----------------------------------------------- - - @cached_property - def _get_unresolved_data_names ( self ): - return [ dn for dn in self.data_names - if (not dn.resolved) and (not dn.optional) ] - - @cached_property - def _get_optional_data_names ( self ): - return [ dn for dn in self.data_names if dn.optional ] - - @cached_property - def _get_unresolved_optional_data_names ( self ): - return [ dn for dn in self.data_names - if (not dn.resolved) and dn.optional ] - - @cached_property - def _get_table_columns ( self ): - n = max( [ len( dn.items ) for dn in self.data_names ] ) - if n == 1: - return std_columns + [ BindingsColumn( name = 'value0', - label = 'Name', - width = 0.43 ) ] - width = 0.43 / n - return (std_columns + - [ BindingsColumn( name = 'value%d' % i, - index = i, - label = 'Name %d' % ( i + 1 ), - width = width ) for i in range( n ) ]) - - #-- Trait Event Handlers --------------------------------------------------- - - def _context_changed ( self, context ): - for data_name in self.data_names: - data_name.context = context - - def _data_names_changed ( self, old, new ): - """ Handles the list of 'data_names' being changed. - """ - # Make sure that all of the names are unique: - new = set( new ) - - # Update the old and new context links: - self._update_contexts( old, new ) - - # Update the list of virtual names based on the new set: - dns = [ VirtualDataName( data_name = dn ) for dn in new ] - dns.sort( lambda l, r: cmp( l.description, r.description ) ) - self.virtual_data_names = dns - - def _data_names_items_changed ( self, event ): - # Update the old and new context links: - old, new = event.old, event.new - self._update_contexts( old, new ) - - # Update the list of virtual names based on the old and new sets: - i = event.index - self.virtual_data_names[ i: i + len( old ) ] = [ - VirtualDataName( data_name = dn ) for dn in new ] - - #-- Private Methods -------------------------------------------------------- - - def _update_contexts ( self, old, new ): - """ Updates the data context for an old and new set of data names. - """ - for data_name in old: - data_name.context = None - - context = self.context - for data_name in new: - data_name.context = context - -#------------------------------------------------------------------------------- -# 'VirtualDataName' class: -#------------------------------------------------------------------------------- - -# Define the 'VirtualValue' property: -def _get_virtual_data ( self, name ): - return self.data_name.items[ - self.trait( name ).index ].data_name_item_choice.choice_value - -def _set_virtual_data ( self, name, new_value ): - old_value = _get_virtual_data( self, name ) - if old_value != new_value: - self.data_name.items[ - self.trait( name ).index ].data_name_item_choice = \ - TemplateChoice( choice_value = new_value ) - - self.trait_property_changed( name, old_value, new_value ) - -VirtualValue = Property( _get_virtual_data, _set_virtual_data ) - -class VirtualDataName ( HasPrivateTraits ): - - # The TemplateDataName this is a virtual copy of: - data_name = Instance( TemplateDataName ) - - # The data name description: - description = Delegate( 'data_name', modify = True ) - - # The 'virtual' traits of this object: - value0 = VirtualValue( index = 0 ) - value1 = VirtualValue( index = 1 ) - value2 = VirtualValue( index = 2 ) - value3 = VirtualValue( index = 3 ) - value4 = VirtualValue( index = 4 ) - value5 = VirtualValue( index = 5 ) - diff --git a/apptools/template/template_impl.py b/apptools/template/template_impl.py deleted file mode 100644 index 6ff65334f..000000000 --- a/apptools/template/template_impl.py +++ /dev/null @@ -1,160 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Defines the Template class, which provides a default implementation of the -# ITemplate interface that can be used when defining templatizable objects. -# -# Written by: David C. Morrill -# -# Date: 07/26/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Defines the Template class, which provides a default implementation of the - ITemplate interface that can be used when defining templatizable objects. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import HasPrivateTraits, Undefined, provides - -from .itemplate \ - import ITemplate - -#------------------------------------------------------------------------------- -# Metadata filter for extracting 'object' and 'datasource' items: -#------------------------------------------------------------------------------- - -def instance_or_datasource ( value ): - return value in ( 'instance', 'datasource' ) - -#------------------------------------------------------------------------------- -# Metadata filter for extracting traits with no 'template' metadata: -#------------------------------------------------------------------------------- - -def is_none ( value ): - return (value is None) - -#------------------------------------------------------------------------------- -# 'Template' class: -#------------------------------------------------------------------------------- - -@provides(ITemplate) -class Template ( HasPrivateTraits ): - """ Defines the Template class, which provides a default implementation of - the ITemplate interface that can be used when defining templatizable - objects. - """ - - #-- 'ITemplate' Interface Implementation ----------------------------------- - - def names_from_template ( self ): - """ 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. - """ - data_names = [] - - # Recursively propagate the request to all contained templatizable - # objects: - for value in self.get( template = 'instance' ).values(): - if isinstance( value, list ): - for v in value: - data_names.extend( v.names_from_template() ) - elif value is not None: - data_names.extend( value.names_from_template() ) - - # Add the bindings for all of our local data source objects: - for value in self.get( template = 'datasource' ).values(): - if isinstance( value, list ): - for v in value: - data_names.append( v.name_from_data_source() ) - elif value is not None: - data_names.append( value.name_from_data_source() ) - - # Return the list of TemplateDataName objects we collected: - return data_names - - def object_from_template ( self ): - """ Activates the object from its template data. - - Returns - ------- - The original object. - """ - # Convert all our data source objects to 'live' objects: - for value in self.get( template = 'datasource' ).values(): - if isinstance( value, list ): - for v in value: - v.object_from_template() - elif value is not None: - value.object_from_template() - - # Recursively propagate the request to all contained templatizable - # objects: - for value in self.get( template = 'instance' ).values(): - if isinstance( value, list ): - for v in value: - v.object_from_template() - elif value is not None: - value.object_from_template() - - # Reset all of our 'live' objects to undefined: - for name in self.trait_names( template = 'derived' ): - setattr( self, name, Undefined ) - - # Return ourselves as the new, live version of the object: - return self - - def template_from_object ( self ): - """ Returns a *templatized* version of the object that is safe for - serialization. - - Returns - ------- - A new object of the same class as the original. - """ - # Mark all traits without 'template' metadata as 'transient': - for trait in self.traits( template = is_none ).values(): - trait.transient = True - - # Collect all of the data source and templatizable objects we contain: - contents = self.get( template = instance_or_datasource ) - - # Convert them all to new objects in template form: - for name, value in contents.items(): - if isinstance( value, list ): - contents[ name ] = [ v.template_from_object() for v in value ] - else: - contents[ name ] = value.template_from_object() - - # Add all of the simple values which can just be copied: - contents.update( self.get( template = 'copy' ) ) - - # Finally return a new instance of our class containing just the - # templatizable values: - return self.__class__( **contents ) - - def activate_template ( self ): - """ Converts all contained 'TDerived' objects to real objects using the - template traits of the object. This method must be overridden in - subclasses. - - Returns - ------- - None - """ - raise NotImplementedError diff --git a/apptools/template/template_traits.py b/apptools/template/template_traits.py deleted file mode 100644 index b277a7a12..000000000 --- a/apptools/template/template_traits.py +++ /dev/null @@ -1,95 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Trait definitions useful when creating templatizable classes. -# -# Written by: David C. Morrill -# -# Date: 07/26/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Trait definitions useful when creating templatizable classes. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Instance, Int, Float, Str, List, Bool, Range, TraitType, Undefined - -from .itemplate_data_source \ - import ITemplateDataSource - -#------------------------------------------------------------------------------- -# Trait definitions: -#------------------------------------------------------------------------------- - -# Data source template trait: -TDataSource = Instance( ITemplateDataSource, template = 'datasource' ) - -# Templatizable object trait: -def TInstance ( *args, **kw ): - kw[ 'template' ] = 'instance' - return Instance( *args, **kw ) - -# A list of templatizable object traits: -def TList( *args, **kw ): - kw[ 'template' ] = 'instance' - return List( *args, **kw ) - -# Simple, copyable template traits: -TInt = Int( template = 'copy' ) -TFloat = Float( template = 'copy' ) -TStr = Str( template = 'copy' ) -TBool = Bool( template = 'copy' ) - -def TRange ( *args, **kw ): - kw[ 'template' ] = 'copy' - return Range( *args, **kw ) - -def TEnum ( *args, **kw ): - kw[ 'template' ] = 'copy' - return Enum( *args, **kw ) - -#------------------------------------------------------------------------------- -# 'TDerived' trait: -#------------------------------------------------------------------------------- - -class TDerived ( TraitType ): - """ Defines a trait property for handling attributes whose value depends - upon the templatizable traits of an object. - - Note that the implementation of the *activate_template* method is not - allowed to read the value of any **TDerived** traits, it may only set - them. - """ - - # Base trait metadata: - metadata = { 'template': 'derived', - 'transient': True } - - def get ( self, object, name ): - name += '_' - value = object.__dict__.get( name, Undefined ) - if value is not Undefined: - return value - - object.activate_template() - - value = object.__dict__.get( name, Undefined ) - if value is not Undefined: - return value - - object.__dict__[ name ] = None - return None - - def set ( self, object, name, value ): - xname = name + '_' - old = object.__dict__.get( xname, Undefined ) - if old is not value: - object.__dict__[ xname ] = value - object.trait_property_changed( name, old, value ) - diff --git a/apptools/template/test/__init__.py b/apptools/template/test/__init__.py deleted file mode 100644 index 8b1378917..000000000 --- a/apptools/template/test/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/apptools/template/test/enable_editor.py b/apptools/template/test/enable_editor.py deleted file mode 100644 index 06f872afa..000000000 --- a/apptools/template/test/enable_editor.py +++ /dev/null @@ -1,78 +0,0 @@ -#------------------------------------------------------------------------------- -# -# Traits UI editor for displaying Enable Components. -# -# Written by: David Morrill -# -# Date: 07/30/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" Traits UI editor for displaying Enable Components. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -import wx - -from traitsui.wx.editor \ - import Editor - -from traitsui.wx.basic_editor_factory \ - import BasicEditorFactory - -from enable.wx_backend.api \ - import Window - -#------------------------------------------------------------------------------- -# '_EnableEditor' class: -#------------------------------------------------------------------------------- - -class _EnableEditor ( Editor ): - """ Traits UI editor for displaying Enable Components. - """ - - # Override the default value to allow the control to be resizable: - scrollable = True - - #--------------------------------------------------------------------------- - # Finishes initializing the editor by creating the underlying toolkit - # widget: - #--------------------------------------------------------------------------- - - def init ( self, parent ): - """ Finishes initializing the editor by creating the underlying toolkit - widget. - """ - self._window = Window( parent, -1, - component = self.value, - bg_color = ( 0.698, 0.698, 0.698, 1.0 ) ) - self.control = self._window.control - self.control.SetSize( wx.Size( 300, 300 ) ) - - self.set_tooltip() - - #--------------------------------------------------------------------------- - # Updates the editor when the object trait changes external to the editor: - #--------------------------------------------------------------------------- - - def update_editor ( self ): - """ Updates the editor when the object trait changes externally to the - editor. - """ - self._window.component = self.value - -#------------------------------------------------------------------------------- -# Create the editor factory object: -#------------------------------------------------------------------------------- - -# wxPython editor factory for Enable component editors: -class EnableEditor ( BasicEditorFactory ): - - # The editor class to be created: - klass = _EnableEditor - diff --git a/apptools/template/test/scatter_plot.py b/apptools/template/test/scatter_plot.py deleted file mode 100644 index d0ea06b5b..000000000 --- a/apptools/template/test/scatter_plot.py +++ /dev/null @@ -1,212 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A simple scatter-plot template defined as a test for the template package. -# -# Written by: David C. Morrill -# (based on the original cp.plot geo_scatter_plot.py file) -# -# Date: 07/30/2007 -# -# (c) Copy 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A simple scatter-plot template defined as a test for the template package. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import HasPrivateTraits, Undefined - -from traitsui.api \ - import View, VGroup, Item, Label, Theme, TextEditor - -from traitsui.wx.themed_slider_editor \ - import ThemedSliderEditor - -from traitsui.wx.themed_text_editor \ - import ThemedTextEditor - -from chaco.api \ - import ScatterPlot, ArrayPlotData, Plot - -from chaco.tools.api \ - import PanTool, SimpleZoom - -from enable.api \ - import ColorTrait - -from chaco.scatter_markers \ - import marker_trait - -from apptools.template.api \ - import Template, TRange, TStr, TDerived, TDataSource - -from apptools.template.impl.api \ - import TemplateDataSource, ValueDataNameItem - -from .enable_editor \ - import EnableEditor - -#------------------------------------------------------------------------------- -# Trait definitions: -#------------------------------------------------------------------------------- - -# Template color trait: -TColor = ColorTrait( template = 'copy' ) - -#------------------------------------------------------------------------------- -# 'ScatterPlot' class: -#------------------------------------------------------------------------------- - -class ScatterPlot ( Template ): - - #-- Template Traits -------------------------------------------------------- - - # The plot index data source: - index = TDataSource - - # The plot value data source: - value = TDataSource - - # The title of the plot: - title = TStr( 'Scatter Plot' ) - - # The type of marker to use. This is a mapped trait using strings as the - # keys: - marker = marker_trait( template = 'copy', event = 'update' ) - - # The pixel size of the marker (doesn't include the thickness of the - # outline): - marker_size = TRange( 1, 5, 1, event = 'update' ) - - # The thickness, in pixels, of the outline to draw around the marker. If - # this is 0, no outline will be drawn. - line_width = TRange( 0.0, 5.0, 1.0 ) - - # The fill color of the marker: - color = TColor( 'red', event = 'update' ) - - # The color of the outline to draw around the marker - outline_color = TColor( 'black', event = 'update' ) - - #-- Derived Traits --------------------------------------------------------- - - plot = TDerived # Instance( ScatterPlot ) - - #-- Traits UI Views -------------------------------------------------------- - - # The scatter plot view: - template_view = View( - VGroup( - Item( 'title', - show_label = False, - style = 'readonly', - editor = ThemedTextEditor( - theme = Theme( '@GBB', alignment = 'center' ) ) - ), - Item( 'plot', - show_label = False, - resizable = True, - editor = EnableEditor(), - item_theme = Theme( '@GF5', margins = 0 ) - ) - ), - resizable = True - ) - - # The scatter plot options view: - options_view = View( - VGroup( - VGroup( - Label( 'Scatter Plot Options', - item_theme = Theme( '@GBB', alignment = 'center' ) ), - show_labels = False - ), - VGroup( - Item( 'title', editor = TextEditor() ), - Item( 'marker' ), - Item( 'marker_size', editor = ThemedSliderEditor() ), - Item( 'line_width', - label = 'Line Width', - editor = ThemedSliderEditor() ), - Item( 'color', label = 'Fill Color' ), - Item( 'outline_color', label = 'Outline Color' ), - group_theme = Theme( '@GF5', margins = ( -5, -1 ) ), - item_theme = Theme( '@G0B', margins = 0 ) - ) - ) - ) - - #-- Default Values --------------------------------------------------------- - - def _index_default ( self ): - """ Returns the default value for the 'index' trait. - """ - return TemplateDataSource( - items = [ ValueDataNameItem( name = 'index', - flatten = True ) ], - description = 'Scatter Plot Index' ) - - def _value_default ( self ): - """ Returns the default value for the 'value' trait. - """ - return TemplateDataSource( - items = [ ValueDataNameItem( name = 'value', - flatten = True ) ], - description = 'Scatter Plot Value' ) - - #-- ITemplate Interface Implementation ------------------------------------- - - def activate_template ( self ): - """ Converts all contained 'TDerived' objects to real objects using the - template traits of the object. This method must be overridden in - subclasses. - - Returns - ------- - None - """ - # If our data sources are still unbound, then just exit; someone must - # have marked them as optional: - if ((self.index.context_data is Undefined) or - (self.value.context_data is Undefined)): - return - - # Create a plot data object and give it this data: - pd = ArrayPlotData() - pd.set_data( 'index', self.index.context_data ) - pd.set_data( 'value', self.value.context_data ) - - # Create the plot: - self.plot = plot = Plot( pd ) - plot.plot( ( 'index', 'value' ), - type = 'scatter', - index_sort = 'ascending', - marker = self.marker, - color = self.color, - outline_color = self.outline_color, - marker_size = self.marker_size, - line_width = self.line_width, - bgcolor = 'white' ) - plot.trait_set( padding_left = 50, - padding_right = 0, - padding_top = 0, - padding_bottom = 20 ) - - # Attach some tools to the plot: - plot.tools.append( PanTool( plot, constrain_key = 'shift' ) ) - zoom = SimpleZoom( component = plot, tool_mode = 'box', - always_on = False ) - plot.overlays.append( zoom ) - - #-- Trait Event Handlers --------------------------------------------------- - - def _update_changed ( self ): - """ Handles a plot option being changed. - """ - self.plot = Undefined - diff --git a/apptools/template/test/scatter_plot_2.py b/apptools/template/test/scatter_plot_2.py deleted file mode 100644 index 91a769c81..000000000 --- a/apptools/template/test/scatter_plot_2.py +++ /dev/null @@ -1,197 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A simple dual scatter-plot template defined as a test for the template -# package. -# -# Written by: David C. Morrill -# (based on the original cp.plot geo_scatter_plot.py file) -# -# Date: 08/01/2007 -# -# (c) Copy 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A simple dual scatter-plot template defined as a test for the template - package. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Undefined - -from traitsui.api \ - import View, VGroup, Item, Label, Theme, TextEditor - -from traitsui.wx.themed_slider_editor \ - import ThemedSliderEditor - -from traitsui.wx.themed_text_editor \ - import ThemedTextEditor - -from enable.api \ - import ColorTrait - -from chaco.api \ - import HPlotContainer - -from chaco.scatter_markers \ - import marker_trait - -from apptools.template.api \ - import Template, TRange, TStr, TInstance, TDerived - -from .enable_editor \ - import EnableEditor - -from .scatter_plot \ - import ScatterPlot - -#------------------------------------------------------------------------------- -# Trait definitions: -#------------------------------------------------------------------------------- - -# Template color trait: -TColor = ColorTrait( template = 'copy' ) - -#------------------------------------------------------------------------------- -# 'ScatterPlot2' class: -#------------------------------------------------------------------------------- - -class ScatterPlot2 ( Template ): - - #-- Template Traits -------------------------------------------------------- - - # The title of the plot: - title = TStr( 'Dual Scatter Plots' ) - - # The type of marker to use. This is a mapped trait using strings as the - # keys: - marker = marker_trait( template = 'copy', event = 'update' ) - - # The pixel size of the marker (doesn't include the thickness of the - # outline): - marker_size = TRange( 1, 5, 1, event = 'update' ) - - # The thickness, in pixels, of the outline to draw around the marker. If - # this is 0, no outline will be drawn. - line_width = TRange( 0.0, 5.0, 1.0 ) - - # The fill color of the marker: - color = TColor( 'red', event = 'update' ) - - # The color of the outline to draw around the marker - outline_color = TColor( 'black', event = 'update' ) - - # The amount of space between plots: - spacing = TRange( 0.0, 20.0, 0.0 ) - - # The contained scatter plots: - scatter_plot_1 = TInstance( ScatterPlot, () ) - scatter_plot_2 = TInstance( ScatterPlot, () ) - - #-- Derived Traits --------------------------------------------------------- - - plot = TDerived - - #-- Traits UI Views -------------------------------------------------------- - - # The scatter plot view: - template_view = View( - VGroup( - Item( 'title', - show_label = False, - style = 'readonly', - editor = ThemedTextEditor( - theme = Theme( '@GBB', alignment = 'center' ) ) - ), - Item( 'plot', - show_label = False, - resizable = True, - editor = EnableEditor(), - item_theme = Theme( '@GF5', margins = 0 ) - ) - ), - resizable = True - ) - - # The scatter plot options view: - options_view = View( - VGroup( - VGroup( - Label( 'Scatter Plot Options', - item_theme = Theme( '@GBB', alignment = 'center' ) ), - show_labels = False - ), - VGroup( - Item( 'title', editor = TextEditor() ), - Item( 'marker' ), - Item( 'marker_size', editor = ThemedSliderEditor() ), - Item( 'line_width', - label = 'Line Width', - editor = ThemedSliderEditor() ), - Item( 'spacing', editor = ThemedSliderEditor() ), - Item( 'color', label = 'Fill Color' ), - Item( 'outline_color', label = 'Outline Color' ), - group_theme = Theme( '@GF5', margins = ( -5, -1 ) ), - item_theme = Theme( '@G0B', margins = 0 ) - ) - ) - ) - - #-- ITemplate Interface Implementation ------------------------------------- - - def activate_template ( self ): - """ Converts all contained 'TDerived' objects to real objects using the - template traits of the object. This method must be overridden in - subclasses. - - Returns - ------- - None - """ - plots = [ p for p in [ self.scatter_plot_1.plot, - self.scatter_plot_2.plot ] if p is not None ] - if len( plots ) == 2: - self.plot = HPlotContainer( spacing = self.spacing ) - self.plot.add( *plots ) - elif len( plots ) == 1: - self.plot = plots[0] - - #-- Default Values --------------------------------------------------------- - - def _scatter_plot_1_default ( self ): - """ Returns the default value for the first scatter plot. - """ - result = ScatterPlot() - result.index.description = 'Shared Plot Index' - result.value.description += ' 1' - - return result - - def _scatter_plot_2_default ( self ): - """ Returns the default value for the second scatter plot. - """ - result = ScatterPlot( index = self.scatter_plot_1.index ) - result.value.description += ' 2' - result.value.optional = True - - return result - - #-- Trait Event Handlers --------------------------------------------------- - - def _update_changed ( self, name, old, new ): - """ Handles a plot option being changed. - """ - setattr( self.scatter_plot_1, name, new ) - setattr( self.scatter_plot_2, name, new ) - self.plot = Undefined - - def _spacing_changed ( self, spacing ): - """ Handles the spacing between plots being changed. - """ - self.plot = Undefined - diff --git a/apptools/template/test/scatter_plot_nm.py b/apptools/template/test/scatter_plot_nm.py deleted file mode 100644 index 699608ce0..000000000 --- a/apptools/template/test/scatter_plot_nm.py +++ /dev/null @@ -1,236 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A n row x m column scatter-plot template defined as a test for the template -# package. -# -# Written by: David C. Morrill -# (based on the original cp.plot geo_scatter_plot.py file) -# -# Date: 08/01/2007 -# -# (c) Copy 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A n row x m column scatter-plot template defined as a test for the template - package. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -from traits.api \ - import Undefined - -from traitsui.api \ - import View, VGroup, Item, Label, Theme, TextEditor - -from traitsui.wx.themed_slider_editor \ - import ThemedSliderEditor - -from traitsui.wx.themed_text_editor \ - import ThemedTextEditor - -from enable.api \ - import ColorTrait - -from chaco.api \ - import GridPlotContainer, PlotComponent - -from chaco.scatter_markers \ - import marker_trait - -from apptools.template.api \ - import MutableTemplate, TRange, TStr, TList, TDerived - -from .enable_editor \ - import EnableEditor - -from .scatter_plot \ - import ScatterPlot - -#------------------------------------------------------------------------------- -# Trait definitions: -#------------------------------------------------------------------------------- - -# Template color trait: -TColor = ColorTrait( template = 'copy' ) - -#------------------------------------------------------------------------------- -# 'ScatterPlotNM' class: -#------------------------------------------------------------------------------- - -class ScatterPlotNM ( MutableTemplate ): - - #-- Template Traits -------------------------------------------------------- - - # The title of the plot: - title = TStr( 'NxM Scatter Plots' ) - - # The type of marker to use. This is a mapped trait using strings as the - # keys: - marker = marker_trait( template = 'copy', event = 'update' ) - - # The pixel size of the marker (doesn't include the thickness of the - # outline): - marker_size = TRange( 1, 5, 1, event = 'update' ) - - # The thickness, in pixels, of the outline to draw around the marker. If - # this is 0, no outline will be drawn. - line_width = TRange( 0.0, 5.0, 1.0 ) - - # The fill color of the marker: - color = TColor( 'red', event = 'update' ) - - # The color of the outline to draw around the marker - outline_color = TColor( 'black', event = 'update' ) - - # The number of rows of plots: - rows = TRange( 1, 3, 1, event = 'grid' ) - - # The number of columns of plots: - columns = TRange( 1, 5, 1, event = 'grid' ) - - # The contained scatter plots: - scatter_plots = TList( ScatterPlot ) - - #-- Derived Traits --------------------------------------------------------- - - plot = TDerived - - #-- Traits UI Views -------------------------------------------------------- - - # The scatter plot view: - template_view = View( - VGroup( - Item( 'title', - show_label = False, - style = 'readonly', - editor = ThemedTextEditor( - theme = Theme( '@GBB', alignment = 'center' ) ) - ), - Item( 'plot', - show_label = False, - resizable = True, - editor = EnableEditor(), - item_theme = Theme( '@GF5', margins = 0 ) - ) - ), - resizable = True - ) - - # The scatter plot options view: - options_view = View( - VGroup( - VGroup( - Label( 'Scatter Plot Options', - item_theme = Theme( '@GBB', alignment = 'center' ) ), - show_labels = False - ), - VGroup( - Item( 'title', editor = TextEditor() ), - Item( 'marker' ), - Item( 'marker_size', editor = ThemedSliderEditor() ), - Item( 'line_width', - label = 'Line Width', - editor = ThemedSliderEditor() ), - Item( 'color', label = 'Fill Color' ), - Item( 'outline_color', label = 'Outline Color' ), - Item( 'rows', editor = ThemedSliderEditor() ), - Item( 'columns', editor = ThemedSliderEditor() ), - group_theme = Theme( '@GF5', margins = ( -5, -1 ) ), - item_theme = Theme( '@G0B', margins = 0 ) - ) - ) - ) - - #-- ITemplate Interface Implementation ------------------------------------- - - def activate_template ( self ): - """ Converts all contained 'TDerived' objects to real objects using the - template traits of the object. This method must be overridden in - subclasses. - - Returns - ------- - None - """ - plots = [] - i = 0 - for r in range( self.rows ): - row = [] - for c in range( self.columns ): - plot = self.scatter_plots[i].plot - if plot is None: - plot = PlotComponent() - row.append( plot ) - i += 1 - plots.append( row ) - - self.plot = GridPlotContainer( shape = ( self.rows, self.columns ) ) - self.plot.component_grid = plots - - #-- Default Values --------------------------------------------------------- - - def _scatter_plots_default ( self ): - """ Returns the default value for the scatter plots list. - """ - plots = [] - for i in range( self.rows * self.columns ): - plots.append( ScatterPlot() ) - - self._update_plots( plots ) - - return plots - - #-- Trait Event Handlers --------------------------------------------------- - - def _update_changed ( self, name, old, new ): - """ Handles a plot option being changed. - """ - for sp in self.scatter_plots: - setattr( sp, name, new ) - - self.plot = Undefined - - def _grid_changed ( self ): - """ Handles the grid size being changed. - """ - n = self.rows * self.columns - plots = self.scatter_plots - if n < len( plots ): - self.scatter_plots = plots[:n] - else: - for j in range( len( plots ), n ): - plots.append( ScatterPlot() ) - - self._update_plots( plots ) - - self.template_mutated = True - - #-- Private Methods -------------------------------------------------------- - - def _update_plots ( self, plots ): - """ Update the data sources for all of the current plots. - """ - index = None - i = 0 - for r in range( self.rows ): - for c in range( self.columns ): - sp = plots[i] - i += 1 - desc = sp.value.description - col = desc.rfind( '[' ) - if col >= 0: - desc = desc[:col] - sp.value.description = '%s[%d,%d]' % ( desc, r, c ) - sp.value.optional = True - - if index is None: - index = sp.index - index.description = 'Shared Plot Index' - index.optional = True - else: - sp.index = index - diff --git a/apptools/template/test/template_view.py b/apptools/template/test/template_view.py deleted file mode 100644 index 3f7ddf77f..000000000 --- a/apptools/template/test/template_view.py +++ /dev/null @@ -1,370 +0,0 @@ -#------------------------------------------------------------------------------- -# -# A feature-based Traits UI plug-in for viewing templates. -# -# Written by: David C. Morrill -# -# Date: 07/30/2007 -# -# (c) Copyright 2007 by Enthought, Inc. -# -#------------------------------------------------------------------------------- - -""" A feature-based Traits UI plug-in for viewing templates. -""" - -#------------------------------------------------------------------------------- -# Imports: -#------------------------------------------------------------------------------- - -import sys - -from os.path \ - import split, splitext - -from pickle \ - import dump, load - -from traits.api \ - import HasPrivateTraits, Str, Instance, Any, File, Button, on_trait_change - -from traitsui.api \ - import View, VGroup, HGroup, Tabbed, Item, InstanceEditor, Theme, Label - -from traitsui.wx.themed_text_editor \ - import ThemedTextEditor - -from etsdevtools.developer.features.api \ - import DropFile - -from pyface.api \ - import FileDialog, OK - -from etsdevtools.developer.helper.themes \ - import TButton - -from blockcanvas.app.utils \ - import import_log_files - -from apptools.template.api \ - import ITemplate, ITemplateDataContext, TemplateDataNames, Template - -from .enable_editor \ - import EnableEditor - -#-- Adapters that might be used ------------------------------------------------ - -from apptools.template.impl.base_data_context_adapter \ - import BaseDataContextAdapter - -#------------------------------------------------------------------------------- -# Helper class: -#------------------------------------------------------------------------------- - -class Message ( HasPrivateTraits ): - - #-- Trait Definitions ------------------------------------------------------ - - # The message to be displayed: - message = Str - - #-- Traits View Definitions ------------------------------------------------ - - view = View( - Item( 'message', - style = 'readonly', - show_label = False, - editor = ThemedTextEditor( theme = '@GBB' ), - ) - ) - - #-- Object Overrides ------------------------------------------------------- - - def __init__ ( self, message = '', **traits ): - traits.setdefault( 'message', message ) - - super( Message, self ).__init__( **traits ) - -# Define some standard messages: -no_context = Message( 'Please provide a data context' ) -no_template = Message( 'Please provide a view template' ) -no_template_found = Message( 'No view templates found in Python source file' ) -no_bindings = Message( 'Please resolve all required template data bindings' ) -no_options = Message( 'No options are currently available' ) - -#------------------------------------------------------------------------------- -# 'TemplateView' class: -#------------------------------------------------------------------------------- - -class TemplateView ( HasPrivateTraits ): - """ A feature-based Traits UI plug-in for viewing templates. - """ - - #-- Public Traits ---------------------------------------------------------- - - # The name of the plugin: - name = Str( 'Template View' ) - - # The data context supplying the data to be viewed: - context = Instance( ITemplateDataContext, - connect = 'to: data context' ) - - # The name of the file containing a template view: - file_name = File( drop_file = DropFile( - extensions = [ '.py', '.tv', '.las' ], - tooltip = 'Drop a LAS data file, a saved view template ' - 'or a Python source file containing a view ' - 'template here.' ), - connect = 'to: template view' ) - - # The template to view: - template = Instance( ITemplate ) - - #-- Private Traits --------------------------------------------------------- - - # The name of the file to save the template in: - save_file_name = File - - # The current data names: - data_names = Instance( TemplateDataNames ) - - # The TemplateDataNames or Message being viewed: - names_view = Any( no_context ) - - # The name of the most recently loaded template file: - template_file_name = File - - # The template or message being viewed: - template_view = Any( no_template ) - - # The options view for the currently active template: - options_view = Any( no_options ) - - # The event fired when the user wants to save the template: - save_template = Button( 'Save Template' ) - - #-- Traits View Definitions ------------------------------------------------ - - view = View( - VGroup( - HGroup( - Item( 'file_name', show_label = False, width = 350 ), - TButton( 'save_template', - label = 'Save Template', - enabled_when = 'template is not None' ), - group_theme = Theme( '@GFB', margins = ( -7, -5 ) ) - ), - Tabbed( - VGroup( - '8', - Label( 'Data Bindings', - item_theme = Theme( '@GBB', alignment = 'center' ) - ), - Item( 'names_view', - style = 'custom', - resizable = True, - editor = InstanceEditor(), - export = 'DockWindowShell', - item_theme = Theme( '@GFB', margins = ( -5, -1 ) ) - ), - label = 'Data Bindings', - show_labels = False - ), - Item( 'template_view', - label = 'Template View', - style = 'custom', - resizable = True, - editor = InstanceEditor( view = 'template_view' ), - export = 'DockWindowShell' - ), - Item( 'options_view', - label = 'Template Options', - style = 'custom', - resizable = True, - editor = InstanceEditor( view = 'options_view' ), - export = 'DockWindowShell' - ), - id = 'tabbed', - dock = 'horizontal', - show_labels = False - ), - ), - id = 'template.test.template_view.TemplateView' - ) - - #-- Trait Event Handlers --------------------------------------------------- - - def _context_changed ( self, context ): - """ Handles the 'context' trait being changed. - """ - if context is None: - self.names_view = no_context - elif self.template is None: - self.names_view = no_template - else: - self._create_view() - - def _template_changed ( self, template ): - """ Handles the 'template' trait being changed. - """ - if self.context is None: - self.template_view = no_context - else: - self._create_view() - - def _file_name_changed ( self, file_name ): - """ Handles the 'file_name' trait being changed. - """ - ext = splitext( file_name )[1] - if ext == '.py': - self._load_python_template( file_name ) - elif ext == '.tv': - self._load_pickled_template( file_name ) - elif ext == '.las': - self._load_las_file( file_name ) - else: - # fixme: Display an informational message here... - pass - - def _save_template_changed ( self ): - """ Handles the user clicking the 'Save Template' button. - """ - self._save_pickled_template() - - @on_trait_change( 'data_names.unresolved_data_names' ) - def _on_unresolved_data_names ( self ): - if len( self.data_names.unresolved_data_names ) == 0: - self._create_object_view() - elif not isinstance( self.template_view, Message ): - self.template_view = no_bindings - self.options_view = no_options - - @on_trait_change( 'template.template_mutated?' ) - def _on_template_mutated ( self ): - """ Handles a mutable template changing. - """ - if self.context is not None: - self._create_view() - - #-- Private Methods -------------------------------------------------------- - - def _load_python_template ( self, file_name ): - """ Attempts to load a template from a Python source file. - """ - path, name = split( file_name ) - sys.path[0:0] = [ path ] - try: - ###values = {} - module_name, ext = splitext( name ) - module = __import__( module_name ) - values = module.__dict__ - ###execfile( file_name, values ) - template = values.get( 'template' ) - if template is None: - templates = [] - for value in values.values(): - try: - if (issubclass( value, Template ) and - ###(value.__module__ == '__builtin__')): - (value.__module__ == module_name)): - templates.append( value ) - except: - pass - - for i, template in enumerate( templates ): - for t in templates[ i + 1: ]: - if issubclass( template, t ): - break - else: - break - else: - self.template_view = no_template_found - return - - if not isinstance( template, Template ): - template = template() - - self.template = template - self.template_file_name = file_name - - except Exception as excp: - self.template_view = Message( str( excp ) ) - - # Clean up the Python path: - del sys.path[0] - - def _load_pickled_template ( self, file_name ): - """ Attempts to load a template from a pickle. - """ - # fixme: Implement this...load template from .tv pickle file. - fh = None - delete = False - try: - fh = open( file_name, 'rb' ) - file_name = load( fh ) - path, name = split( file_name ) - sys.path[0:0] = [ path ] - delete = True - module_name, ext = splitext( name ) - module = __import__( module_name ) - self.template = load( fh ) - self.template_file_name = file_name - except Exception as excp: - import traceback - traceback.print_exc() - self.template_view = Message( str( excp ) ) - - if fh is not None: - fh.close() - - if delete: - del sys.path[0] - - def _load_las_file ( self, file_name ): - """ Creates a data context from the specified LAS file. - """ - try: - self.context = import_log_files( file_name, 'las' ) - except Exception as excp: - self.names_view = Message( str( excp ) ) - - def _save_pickled_template ( self ): - file_name = self.save_file_name or self.file_name - fd = FileDialog( - action = 'save as', - default_path = file_name ) - #wildcard = 'Template files (*.tv)|*.tv|' ) - if fd.open() == OK: - self.save_file_name = file_name = fd.path - fh = None - try: - fh = open( file_name, 'wb' ) - dump( self.template_file_name, fh, -1 ) - dump( self.template.template_from_object(), fh, -1 ) - except: - # fixme: Display an informational message here... - import traceback - traceback.print_exc() - - if fh is not None: - fh.close() - - def _create_view ( self ): - """ Begins the process of creating a live view from a template and - data context object. - """ - self.data_names = self.names_view = nv = TemplateDataNames( - context = self.context, - data_names = self.template.names_from_template() ) - - if len( nv.unresolved_data_names ) == 0: - self._create_object_view() - else: - self.template_view = no_bindings - - def _create_object_view ( self ): - """ Create the object view from the current template. - """ - self.template.object_from_template() - self.template_view = self.options_view = self.template - From 30e96a7ac214f1dffb7cfc214810123091b018f8 Mon Sep 17 00:00:00 2001 From: Aaron Ayres Date: Tue, 3 Nov 2020 14:01:08 -0600 Subject: [PATCH 2/2] undo inadvertent change to example.ini from running the test suite --- apptools/preferences/tests/example.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apptools/preferences/tests/example.ini b/apptools/preferences/tests/example.ini index c535635ce..100dc6e16 100644 --- a/apptools/preferences/tests/example.ini +++ b/apptools/preferences/tests/example.ini @@ -3,7 +3,7 @@ bgcolor = blue width = 50 ratio = 1.0 visible = True -description = acme ui +description = 'acme ui' offsets = "[1, 2, 3, 4]" names = "['joe', 'fred', 'jane']"