/usr/share/pyshared/traitsui/extras/demo.py is in python-traitsui 4.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 | #-------------------------------------------------------------------------------
#
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: David C. Morrill
# Date: 09/15/2005
#
#-------------------------------------------------------------------------------
""" A Traits UI demo that borrows heavily from the design of the wxPython demo.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from __future__ import absolute_import
import sys
import glob
from configobj import ConfigObj
from traits.api import (HasTraits, HasPrivateTraits, Str, Instance, Property, Any,
Code, HTML, true, false, Dict)
from traitsui.api import (TreeEditor, ObjectTreeNode, TreeNodeObject, View, Item,
VSplit, Tabbed, VGroup, HGroup, Heading, Handler, UIInfo, InstanceEditor,
HTMLEditor, Include, spring)
from os import listdir
from os.path import (join, isdir, split, splitext, dirname, basename, abspath,
exists, isabs)
#-------------------------------------------------------------------------------
# Global data:
#-------------------------------------------------------------------------------
# Define the code used to populate the 'execfile' dictionary:
exec_str = """from traits.api import *
"""
#----------------------------------------------------------------------------
# Return a 'user-friendly' name for a specified string:
#----------------------------------------------------------------------------
def user_name_for ( name ):
name = name.replace( '_', ' ' )
return name[:1].upper() + name[1:]
#-------------------------------------------------------------------------------
# Parses the contents of a specified source file into module comment and
# source text:
#-------------------------------------------------------------------------------
def parse_source ( file_name ):
try:
fh = open( file_name, 'rb' )
source = fh.read().strip()
fh.close()
# Extract out the module comment as the description:
# FIXME: This isn't ideal: it will retrieve the first docstring found
# which might not be for the module as a whole. This needs to be
# improved later. For now, we want to make sure we catch docstrings
# even if there are comments etc. prior to them.
comment = ''
quotes_styles = ["'''", '"""']
for quotes in quotes_styles:
start_index = source.find(quotes)
if start_index >= 0:
col = source.find( quotes, start_index + 3 )
if col >= 0:
comment = source[ start_index + 3: col ]
source = source[: start_index].strip() + source[ col + 3: ].strip()
break
return ( comment, source )
except:
return ( '', '' )
#-------------------------------------------------------------------------------
# 'DemoFileHandler' class:
#-------------------------------------------------------------------------------
class DemoFileHandler ( Handler ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# The current 'info' object (for use by the 'write' method):
info = Instance( UIInfo )
#---------------------------------------------------------------------------
# Initializes the view:
#---------------------------------------------------------------------------
def init ( self, info ):
# Save the reference to the current 'info' object:
self.info = info
# Set up the 'print' logger:
df = info.object
df.log = ''
sys.stdout = sys.stderr = self
# Read in the demo source file:
df.description, df.source = parse_source( df.path )
# Try to run the demo source file:
# Append the path for the demo source file to sys.path, so as to
# resolve any local (relative) imports in the demo source file.
sys.path.append(dirname(df.path))
locals = df.parent.init_dic
locals[ '__name__' ] = '___main___'
locals['__file__'] = df.path
sys.modules[ '__main__' ].__file__ = df.path
try:
execfile( df.path, locals, locals )
demo = self._get_object( 'modal_popup', locals )
if demo is not None:
demo = ModalDemoButton( demo = demo )
else:
demo = self._get_object( 'popup', locals )
if demo is not None:
demo = DemoButton( demo = demo )
else:
demo = self._get_object( 'demo', locals )
# FIXME: If a 'demo' object could not be found, then try to execute
# the file setting __name__ to __main__. A lot of test scripts have
# the actual test running when __name__==__main__ and so we can at
# least run all test examples this way. Use a do_later loop so as to
# finish building the current UI before running the test.
if demo is None:
locals['__name__'] = '__main__'
#do_later(self.execute_test, df, locals)
except Exception, excp:
demo = DemoError( msg = str( excp ) )
# Clean up sys.path
sys.path.remove(dirname(df.path))
df.demo = demo
def execute_test(self, df, locals):
""" Executes the file in df.path in the namespace of locals."""
execfile(df.path, locals, locals)
#---------------------------------------------------------------------------
# Closes the view:
#---------------------------------------------------------------------------
def closed ( self, info, is_ok ):
""" Closes the view.
"""
info.object.demo = None
#---------------------------------------------------------------------------
# Get a specified object from the execution dictionary:
#---------------------------------------------------------------------------
def _get_object ( self, name, dic ):
object = dic.get( name ) or dic.get( name.capitalize() )
if object is not None:
if isinstance( type( object ), type ):
try:
object = object()
except:
pass
if isinstance( object, HasTraits ):
return object
return None
#---------------------------------------------------------------------------
# Handles 'print' output:
#---------------------------------------------------------------------------
def write ( self, text ):
self.info.object.log += text
def flush ( self ):
pass
# Create a singleton instance:
demo_file_handler = DemoFileHandler()
#-------------------------------------------------------------------------------
# 'DemoError' class:
#-------------------------------------------------------------------------------
class DemoError ( HasPrivateTraits ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# The error message text:
msg = Code
#---------------------------------------------------------------------------
# Traits view definitions:
#---------------------------------------------------------------------------
traits_view = View(
VGroup(
Heading( 'Error in source file' ),
Item( 'msg', style = 'custom', show_label = False ),
)
)
#-------------------------------------------------------------------------------
# 'DemoButton' class:
#-------------------------------------------------------------------------------
class DemoButton ( HasPrivateTraits ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# The demo to be launched via a button:
demo = Instance( HasTraits )
# The demo view item to use:
demo_item = Item( 'demo',
show_label = False,
editor = InstanceEditor( label = 'Run demo...', kind = 'live' )
)
#---------------------------------------------------------------------------
# Traits view definitions:
#---------------------------------------------------------------------------
traits_view = View(
VGroup(
VGroup(
Heading( 'Click the button to run the demo:' ),
'20'
),
HGroup(
spring,
Include( 'demo_item' ),
spring
)
),
resizable = True
)
#-------------------------------------------------------------------------------
# 'ModalDemoButton' class:
#-------------------------------------------------------------------------------
class ModalDemoButton ( DemoButton ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# The demo view item to use:
demo_item = Item( 'demo',
show_label = False,
editor = InstanceEditor( label = 'Run demo...', kind = 'modal' )
)
#-------------------------------------------------------------------------------
# 'DemoTreeNodeObject' class:
#-------------------------------------------------------------------------------
class DemoTreeNodeObject ( TreeNodeObject ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Cached result of 'tno_has_children':
_has_children = Any
# Cached result of 'tno_get_children':
_get_children = Any
#---------------------------------------------------------------------------
# Returns whether chidren of this object are allowed or not:
#---------------------------------------------------------------------------
def tno_allows_children ( self, node ):
""" Returns whether chidren of this object are allowed or not.
"""
return self.allows_children
#---------------------------------------------------------------------------
# Returns whether or not the object has children:
#---------------------------------------------------------------------------
def tno_has_children ( self, node = None ):
""" Returns whether or not the object has children.
"""
if self._has_children is None:
self._has_children = self.has_children()
return self._has_children
#---------------------------------------------------------------------------
# Gets the object's children:
#---------------------------------------------------------------------------
def tno_get_children ( self, node ):
""" Gets the object's children.
"""
if self._get_children is None:
self._get_children = self.get_children()
return self._get_children
#---------------------------------------------------------------------------
# Returns whether or not the object has children:
#---------------------------------------------------------------------------
def has_children ( self, node ):
""" Returns whether or not the object has children.
"""
raise NotImplementedError
#---------------------------------------------------------------------------
# Gets the object's children:
#---------------------------------------------------------------------------
def get_children ( self, node ):
""" Gets the object's children.
"""
raise NotImplementedError
#-------------------------------------------------------------------------------
# 'DemoFile' class:
#-------------------------------------------------------------------------------
class DemoFile ( DemoTreeNodeObject ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Parent of this file:
parent = Any
# Name of file system path to this file:
path = Property
# Name of the file:
name = Str
# UI form of the 'name':
nice_name = Property
# Files don't allow children:
allows_children = false
# Description of what the demo does:
description = HTML
# Source code for the demo:
source = Code
# Demo object whose traits UI is to be displayed:
demo = Instance( HasTraits )
# Log of all print messages displayed:
log = Code
_nice_name = Str
#---------------------------------------------------------------------------
# Implementation of the 'path' property:
#---------------------------------------------------------------------------
def _get_path ( self ):
return join( self.parent.path, self.name + '.py' )
#---------------------------------------------------------------------------
# Implementation of the 'nice_name' property:
#---------------------------------------------------------------------------
def _get_nice_name ( self ):
if not self._nice_name:
self._nice_name = user_name_for( self.name )
return self._nice_name
def _set_nice_name(self, value):
old = self.nice_name
self._nice_name = value
self.trait_property_changed('nice_name', old, value)
#---------------------------------------------------------------------------
# Returns whether or not the object has children:
#---------------------------------------------------------------------------
def has_children ( self ):
""" Returns whether or not the object has children.
"""
return False
#-------------------------------------------------------------------------------
# 'DemoPath' class:
#-------------------------------------------------------------------------------
class DemoPath ( DemoTreeNodeObject ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Parent of this package:
parent = Any
# Name of file system path to this package:
path = Property
# Name of the directory:
name = Str
# UI form of the 'name':
nice_name = Property
# Description of the contents of the directory:
description = Property( HTML )
# Source code contained in the '__init__.py' file:
source = Property( Code )
# Dictionary containing symbols defined by the path's '__init__.py' file:
init_dic = Property
# Should .py files be included?
use_files = true
# Paths do allow children:
allows_children = true
# Configuration dictionary for this node
# This trait is set when a config file exists for the parent of this path.
config_dict = Dict
# Configuration file for this node.
config_filename = Str
# Cached value of the nice_name property.
_nice_name = Str
#---------------------------------------------------------------------------
# Implementation of the 'path' property:
#---------------------------------------------------------------------------
def _get_path ( self ):
return join( self.parent.path, self.name )
#---------------------------------------------------------------------------
# Implementation of the 'nice_name' property:
#---------------------------------------------------------------------------
def _get_nice_name ( self ):
if not self._nice_name:
self._nice_name = user_name_for( self.name )
return self._nice_name
#---------------------------------------------------------------------------
# Setter for the 'nice_name' property:
#---------------------------------------------------------------------------
def _set_nice_name(self, value):
old = self.nice_name
self._nice_name = value
self.trait_property_changed('nice_name', old, value)
#---------------------------------------------------------------------------
# Implementation of the 'description' property:
#---------------------------------------------------------------------------
def _get_description ( self ):
if self._description is None:
self._get_init()
return self._description
#---------------------------------------------------------------------------
# Implementation of the 'source' property:
#---------------------------------------------------------------------------
def _get_source ( self ):
if self._source is None:
self._get_init()
return self._source
#---------------------------------------------------------------------------
# Implementation of the 'init_dic' property:
#---------------------------------------------------------------------------
def _get_init_dic ( self ):
init_dic = {}
description, source = parse_source( join( self.path, '__init__.py' ) )
exec (exec_str + source) in init_dic
return init_dic
# fixme: The following code should work, but doesn't, so we use the
# preceding code instead. Changing any trait in the object in
# this method causes the tree to behave as if the DemoPath object
# had been selected instead of a DemoFile object. May be due to
# an 'anytrait' listener in the TreeEditor?
#if self._init_dic is None:
# self._init_dic = {}
# #exec self.source in self._init_dic
#return self._init_dic.copy()
#---------------------------------------------------------------------------
# Initializes the description and source from the path's '__init__.py'
# file:
#---------------------------------------------------------------------------
def _get_init ( self ):
if self.use_files:
# Read in the '__init__.py' source file (if any):
self._description, source = parse_source(
join( self.path, '__init__.py' ) )
else:
self._description = ('<img src="traits_ui_demo.jpg">')
source = ''
self._source = exec_str + source
#---------------------------------------------------------------------------
# Returns whether or not the object has children:
#---------------------------------------------------------------------------
def has_children ( self ):
""" Returns whether or not the object has children.
"""
path = self.path
for name in listdir( path ):
cur_path = join( path, name )
if isdir( cur_path ):
return True
if self.use_files:
name, ext = splitext( name )
if (ext == '.py') and (name != '__init__'):
return True
return False
#---------------------------------------------------------------------------
# Gets the object's children:
#---------------------------------------------------------------------------
def get_children ( self ):
""" Gets the object's children.
"""
if self.config_dict or self.config_filename:
children = self.get_children_from_config()
else:
children = self.get_children_from_datastructure()
return children
#---------------------------------------------------------------------------
# Gets the object's children based on the filesystem structure.
#---------------------------------------------------------------------------
def get_children_from_datastructure( self ):
""" Gets the object's children based on the filesystem structure.
"""
dirs = []
files = []
path = self.path
for name in listdir( path ):
cur_path = join( path, name )
if isdir( cur_path ):
if self.has_py_files( cur_path ):
dirs.append( DemoPath( parent = self, name = name ) )
elif self.use_files:
name, ext = splitext( name )
if (ext == '.py') and (name != '__init__'):
files.append( DemoFile( parent = self, name = name ) )
dirs.sort( lambda l, r: cmp( l.name, r.name ) )
files.sort( lambda l, r: cmp( l.name, r.name ) )
return (dirs + files)
#---------------------------------------------------------------------------
# Gets the object's children as specified in its configuration file or
# dictionary.
#---------------------------------------------------------------------------
def get_children_from_config( self ):
"""
Gets the object's children as specified in its configuration file or
dictionary.
"""
if not self.config_dict:
if exists(self.config_filename):
try:
self.config_dict = ConfigObj(self.config_filename)
except:
pass
if not self.config_dict:
return self.get_children_from_datastructure()
dirs = []
files = []
for keyword, value in self.config_dict.items():
if not value.get('no_demo'):
sourcedir = value.pop('sourcedir', None)
if sourcedir is not None:
# This is a demo directory.
demoobj = DemoPath( parent = self, name = sourcedir )
demoobj.nice_name = keyword
demoobj.config_dict = value
dirs.append(demoobj)
else:
names = []
filenames = value.pop('files', [])
if not isinstance(filenames, list):
filenames = [filenames]
for filename in filenames:
filename = join(self.path, filename)
for name in glob.iglob(filename):
pathname, ext = splitext(name)
if (ext == '.py') and \
(basename(pathname) != '__init__'):
names.append(pathname)
if len(names) > 1:
config_dict = {}
for name in names:
config_dict[basename(name)] = {'files':
name + '.py'}
demoobj = DemoPath( parent = self, name = '')
demoobj.nice_name = keyword
demoobj.config_dict = config_dict
dirs.append(demoobj)
elif len(names) == 1:
file = DemoFile(parent=self, name=names[0])
file.nice_name = keyword
files.append(file)
dirs.sort( lambda l, r: cmp( l.nice_name, r.nice_name ) )
files.sort( lambda l, r: cmp( l.nice_name, r.nice_name ) )
return (dirs + files)
#---------------------------------------------------------------------------
# Returns whether the specified path contains any .py files:
#---------------------------------------------------------------------------
def has_py_files ( self, path ):
for name in listdir( path ):
cur_path = join( path, name )
if isdir( cur_path ):
if self.has_py_files( cur_path ):
return True
else:
name, ext = splitext( name )
if ext == '.py':
return True
return False
#-------------------------------------------------------------------------------
# Defines the demo tree editor:
#-------------------------------------------------------------------------------
path_view = View(
Tabbed(
Item( 'description',
label = 'Description',
show_label = False,
style = 'readonly',
editor=HTMLEditor(format_text=True)
),
Item( 'source',
label = 'Source',
show_label = False,
style = 'custom'
),
export = 'DockWindowShell',
id = 'tabbed'
),
id = 'traitsui.demos.demo.path_view',
#dock = 'horizontal'
)
demo_view = View(
#VSplit(
Tabbed(
Item( 'description',
label = 'Description',
show_label = False,
style = 'readonly',
editor=HTMLEditor(format_text=True)
),
Item( 'source',
label = 'Source',
show_label = False,
style = 'custom'
),
Item( 'demo',
label = 'Demo',
show_label = False,
style = 'custom',
resizable = True,
# FIXME:
# visible_when doesn't work correctly yet (for wx atleast)
# for tabbed items. Needs more investigation.
visible_when = 'demo',
),
Item( 'log',
show_label = False,
style = 'readonly'
),
export = 'DockWindowShell',
id = 'tabbed',
),
# JDM moving log panel provisionally to its own tab, distracting here.
#VGroup(
#Item( 'log',
#show_label = False,
#style = 'readonly'
#),
#label = 'Log'
#),
#export = 'DockWindowShell',
#id = 'vsplit'
#),
id = 'traitsui.demos.demo.file_view',
#dock = 'horizontal',
handler = demo_file_handler
)
demo_tree_editor = TreeEditor(
nodes = [
ObjectTreeNode( node_for = [ DemoPath ],
label = 'nice_name',
view = path_view ),
ObjectTreeNode( node_for = [ DemoFile ],
label = 'nice_name',
view = demo_view )
]
)
#-------------------------------------------------------------------------------
# 'Demo' class:
#-------------------------------------------------------------------------------
class Demo ( HasPrivateTraits ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Path to the root demo directory:
path = Str
# Root path object for locating demo files:
root = Instance( DemoPath )
# Title for the demo
title = Str
#---------------------------------------------------------------------------
# Traits view definitions:
#---------------------------------------------------------------------------
def default_traits_view(self):
""" Constructs the default traits view."""
traits_view = View(
Item( name = 'root',
id = 'root',
show_label = False,
editor = demo_tree_editor
),
title = self.title,
id = 'traitsui.demos.demo.Demo',
#dock = 'horizontal',
resizable = True,
# JDM: Seems that QT interface does not deal well with these size
# limits.
# With them, we get repeated:
# Object::disconnect: Parentheses expected, signal AdvancedCodeWidget::lostFocus
# But without them, it throws an exception on exit:
# Internal C++ object (_StickyDialog) already deleted.
# No, actually sometimes we get the latter even with them.
width = 950,
height = 900
)
return traits_view
#---------------------------------------------------------------------------
# Handles the 'root' trait being changed:
#---------------------------------------------------------------------------
def _root_changed ( self, root ):
""" Handles the 'root' trait being changed.
"""
root.parent = self
#-------------------------------------------------------------------------------
# Function to run the demo:
#-------------------------------------------------------------------------------
def demo ( use_files=False, dir_name = None, config_filename = '',
title = 'Traits UI Demos' ):
if dir_name is None:
dir_name = dirname(abspath( sys.argv[0] ))
path, name = split( dir_name )
if len(config_filename) > 0 and not isabs(config_filename):
config_filename = join(path, name, config_filename)
Demo( path = path,
title = title,
root = DemoPath( name = name,
use_files = use_files,
config_filename = config_filename )
).configure_traits()
|