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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
#-------------------------------------------------------------------------------
#
# 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, 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, 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, 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
|