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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
""" A UI for browsing a plugin. """
# Enthought library imports.
from enthought.envisage.api import ExtensionPoint, IPlugin
from enthought.traits.api import Delegate, HasTraits, Instance, List, Property
from enthought.traits.api import Code, Str
from enthought.traits.ui.api import Item, TableEditor, View, VGroup
from enthought.traits.ui.table_column import ObjectColumn # fixme: non-api!
class ExtensionPointModel(Hastraits):
""" A model for browsing an extension point. """
# The plugin that offered the extension point.
plugin = Instance(IPlugin)
# The extension point.
extension_point = Instance(IExtensionPoint)
#### 'ExtensionPointModel' interface ######################################
class ExtensionModel(Hastraits):
""" A model for browsing a contribution to an extension point. """
# The plugin that made the contribution.
plugin = Instance(IPlugin)
#### 'ContributionModel' interface ########################################
# The Id of the extension point that the contribution is for.
extension_point_id = Str
# The contributions to the extension point.
contributions = List
###########################################################################
# 'ApplicationModel' interface.
###########################################################################
#### Trait initializers ###################################################
def _contributions_default(self):
""" Trait initializer. """
return plugin.application.get_extensions(self.extension_point_id)
class PluginModel(HasTraits):
""" A model for browsing a plugin. """
# The plugin that we are a model for.
plugin = Instance(IPlugin)
#### 'PluginModel' interface ##############################################
# Models for each of the plugin's extension points.
extension_point_models = List(ExtensionPointModel)
# Models for each of the plugin's contributions to extension points.
extension_models = List(ExtensionModel)
###########################################################################
# 'ApplicationModel' interface.
###########################################################################
#### Trait initializers ###################################################
def _extension_models_default(self):
""" Trait initializer. """
extension_modelss = [
ExtensionModel(
plugin = plugin,
extension_point_id = extension_point.id
)
for extension_point in plugin.get_extension_points()
]
return extension_models
def _extension_point_models_default(self):
""" Trait initializer. """
extension_point_models = [
ExtensionPointModel(
plugin = plugin,
extension_point = extension_point
)
for extension_point in plugin.get_extension_points()
]
return extension_point_models
class ApplicationModel(HasTraits):
""" A model for browsing an application. """
# The application that we are a model for.
application = Instance(IApplication)
#### 'ApplicationModel' interface #########################################
# Models for each of the application's plugins.
plugin_models = List(PluginModel)
###########################################################################
# 'ApplicationModel' interface.
###########################################################################
#### Trait initializers ###################################################
def _plugin_models_default(self):
""" Trait initializer. """
return [PluginModel(plugin=plugin) for plugin in self.application]
extension_point_table_editor = TableEditor(
columns = [
ObjectColumn(name='id'),
#ObjectColumn(name='desc')
],
# selected = 'extension_point_selected',
editable = True,
edit_view = View(Item(name='desc', show_label=False), style='custom')
)
plugin_browser_view = View(
VGroup(
Item(name='id'),
Item(name='name'),
label='General'
),
VGroup(
Item(
name = 'extension_points',
editor = extension_point_table_editor,
show_label = False
),
label='Extension Points',
),
width = .8,
height = .6
)
class ExtensionPointBrowser(HasTraits):
""" The model used to view extension points.
This browser is required because 'ExatenionPoint' instances are trait
*types* and therefore do not have traits themselves and so this class is
really just a 'traitified' wrapper.
"""
#### 'ExtensionPointBrowser' interface ####################################
# The extension point that we are browsing.
extension_point = Instance(ExtensionPoint)
# The extension point's globally unique Id.
id = Str
# The extension point's description.
desc = Code#Str
###########################################################################
# 'ExtensionPoint' browser interface.
###########################################################################
def _id_default(self):
""" Trait initializer. """
return self.extension_point.id
def _desc_default(self):
""" Trait initializer. """
desc = self.extension_point.desc.strip()
lines = [line.replace(' ', '', 2) for line in desc.splitlines()]
return '\n'.join(lines)
# Convenience trait to delegate an attribute to a plugin.
DelegatedToPlugin = Delegate('plugin', modify=True)
class PluginBrowser(HasTraits):
""" A plugin browser.
Actually, this class exists just because to use a trait editor we have
to have a trait to edit!
"""
#### 'IPlugin' interface ##################################################
# The plugins Id.
id = DelegatedToPlugin
# The plugin name.
name = DelegatedToPlugin
#### 'PluginBrowser' interface ############################################
# The extension points offered by the plugin.
extension_points = Property(List)
# The plugin that we are browsing.
plugin = Instance(IPlugin)
# The default traits UI view.
traits_view = plugin_browser_view
###########################################################################
# 'PluginBrowser' interface.
###########################################################################
def _get_extension_points(self):
""" Property getter. """
extension_points = [
ExtensionPointBrowser(extension_point=extension_point)
for extension_point in self.plugin.extension_points
]
return extension_points
def browse_plugin(plugin):
""" Browse a plugin. """
import inspect
if inspect.isclass(plugin):
plugin = plugin()
plugin_browser = PluginBrowser(plugin=plugin)
plugin_browser.configure_traits(view=plugin_browser_view)
return
#### EOF ######################################################################
|