1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#-------------------------------------------------------------------------------
#
# 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:
#-------------------------------------------------------------------------------
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.
"""
implements ( ITemplateDataNameItem )
#-- '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 = 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
|