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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#-----------------------------------------------------------------------------
#
# Copyright (c) 2007 by Enthought, Inc.
# All rights reserved.
#
#-----------------------------------------------------------------------------
"""
A base class for actions that use the data plugin's services.
"""
# Standard library imports.
import logging
# Enthought library imports.
from enthought.envisage.api import UOL
from enthought.envisage.single_project.api import ProjectAction
# Data library imports.
from data.data import Data
# Application imports.
from data.plugin.services import IDATA_MODEL, IDATA_UI
# Create a logger for this module.
logger = logging.getLogger(__name__)
class DataPluginAction(ProjectAction):
"""
A base class for actions that use the data plugin's services.
"""
##########################################################################
# Attributes
##########################################################################
#### public 'DataPluginAction' interface #################################
# A reference to the data plugin's model service.
data_model_service = UOL
# A reference to the data plugin's UI service.
data_ui_service = UOL
##########################################################################
# 'object' interface
##########################################################################
#### operator methods ####################################################
def __init__(self, **kws):
"""
Constructor.
Extended to ensure that our UOL's have default identification strings.
"""
super(DataPluginAction, self).__init__(**kws)
if self.data_model_service is None:
self.data_model_service = 'service://%s' % IDATA_MODEL
if self.data_ui_service is None:
self.data_ui_service = 'service://%s' % IDATA_UI
return
##########################################################################
# 'DataPluginAction' interface.
##########################################################################
#### protected methods ###################################################
def _get_target_data(self, event, show_error=True):
"""
Return the data this action should import into.
If the show_error parameter is True, then an error message will be
displayed to the user if a data could not be located.
"""
data = None
# If the event has a node, then try to find the data from that node.
if hasattr(event, 'node'):
data = self._get_target_data_from_node(event.node)
# If we don't have a data yet, then try and find it from the current
# project selection.
if data is None:
data = self._get_target_data_from_project_selection()
# If we have not identified a data, and we're supposed to show an error,
# then do so now.
if data is None and show_error:
self._display_error('Unable to identify a Data to perform this '
'action with. Please select a target Data within the project '
'tree and try again.')
logger.debug('Target data: %s', data)
return data
def _get_target_data_from_node(self, node):
"""
Locate the data this action should import into from the specified node.
"""
# If the object itself is a data, then we're done
obj = node.obj
if isinstance(obj, Data):
return obj
# Otherwise, scan upward through the tree containing the node, looking
# for the closest parent that is a data.
data = None
context = node.context
while context is not None:
if hasattr(context, 'adaptee'):
obj = context.adaptee
if isinstance(obj, Data):
data = obj
break
if hasattr(context, 'context'):
context = context.context
else:
context = None
return data
def _get_target_data_from_project_selection(self):
"""
Locate the data this action should import into from project selection.
"""
data = None
# Find the first data in the project selection.
for item in self.model_service.selection:
if isinstance(item, Data):
data = item
break
elif hasattr(item, 'obj') and isinstance(item.obj, Data):
data = item.obj
break
return data
def _get_node_name(self, event, show_error=True):
""" Return the name of the selection
"""
if hasattr(event, 'node'):
if hasattr(event.node, 'name'):
return event.node.name
if show_error:
logger.error('Could not retrieve the name of the selected data')
return None
#### EOF #####################################################################
|