/usr/share/pyshared/traitsui/view_elements.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 | #------------------------------------------------------------------------------
#
# 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: 10/18/2004
#
#------------------------------------------------------------------------------
""" Define the ViewElements class, which is used to define a (typically
class-based) hierarchical name space of related ViewElement objects.
Normally there is a ViewElements object associated with each Traits-based
class, which contains all of the ViewElement objects associated with the
class. The ViewElements object is also linked to the ViewElements objects
of its associated class's parent classes.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from __future__ import absolute_import
from traits.api import HasStrictTraits, List, Dict, Str, Int, Any, TraitError
from traits.trait_base import enumerate
from .view_element import ViewElement
#-------------------------------------------------------------------------------
# Trait definitions:
#-------------------------------------------------------------------------------
# Trait for contents of a ViewElements object
content_trait = Dict( str, ViewElement )
#-------------------------------------------------------------------------------
# 'ViewElements' class:
#-------------------------------------------------------------------------------
class ViewElements ( HasStrictTraits ):
""" Defines a hierarchical name space of related ViewElement objects.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Dictionary containing the named ViewElement items
content = content_trait
#---------------------------------------------------------------------------
# Finds a specified ViewElement within the specified (optional) search
# context:
#---------------------------------------------------------------------------
def find ( self, name, stack = None ):
""" Finds a specified ViewElement within the specified (optional) search
context.
"""
# Assume search starts from the beginning the of the search order:
i = 0
# If a stack was specified, see if there is a matching entry in the
# stack already:
if stack is not None:
for ssi in stack:
if name == ssi.id:
# Match found, resume search at next ViewElements object
# in the search order:
i = ssi.context + 1
break
# Search for a matching name starting at the specified ViewElements
# object in the search order:
for j, ves in enumerate( self._get_search_order()[i:] ):
result = ves.content.get( name )
if result is not None:
# Match found. If there is a stack, push matching name and
# ViewElements context onto it:
if stack is not None:
stack[0:0] = [ SearchStackItem( id = name,
context = i + j ) ]
# Return the ViewElement object that matched the name:
return result
# Indicate no match was found:
return None
#---------------------------------------------------------------------------
# Returns a sorted list of all names accessible from the ViewElements
# object that are of a specified (ViewElement) type:
#---------------------------------------------------------------------------
def filter_by ( self, klass = None ):
""" Returns a sorted list of all names accessible from the ViewElements
object that are of a specified (ViewElement) type.
"""
if klass is None:
from . import view
klass = view.View
result = []
# Add each item in the search order which is of the right class and
# which is not already in the result list:
for ves in self._get_search_order():
for name, ve in ves.content.items():
if isinstance( ve, klass ) and (name not in result):
result.append( name )
# Sort the resulting list of names:
result.sort()
# Return the result:
return result
#---------------------------------------------------------------------------
# Handles the 'parents' list being updated:
#---------------------------------------------------------------------------
def _parents__changed ( self ):
self._search_order = None
def _parents_items_changed ( self ):
self._search_order = None
#---------------------------------------------------------------------------
# Returns the current search order (computing it if necessary):
#---------------------------------------------------------------------------
def _get_search_order ( self ):
if self._search_order is None:
self._search_order = self._mro()
return self._search_order
#---------------------------------------------------------------------------
# Compute the Python 'C3' algorithm used to determine a class's 'mro'
# and apply it to the 'parents' of the ViewElements to determine the
# correct search order:
#---------------------------------------------------------------------------
def _mro ( self ):
return self._merge(
[ [ self ] ] +
[ parent._get_search_order()[:] for parent in self.parents ] +
[ self.parents[:] ] )
def _merge ( self, seqs ):
result = []
while True:
# Remove any empty sequences from the list:
seqs = [ seq for seq in seqs if len( seq ) > 0 ]
if len( seqs ) == 0:
return result
# Find merge candidates among the sequence heads:
for seq in seqs:
candidate = seq[0]
if len( [ s for s in seqs if candidate in s[1:] ] ) == 0:
break
else:
raise TraitError, "Inconsistent ViewElements hierarchy"
# Add the candidate to the result:
result.append( candidate )
# Then remove the candidate:
for seq in seqs:
if seq[0] == candidate:
del seq[0]
#---------------------------------------------------------------------------
# Returns a 'pretty print' version of the ViewElements object:
#---------------------------------------------------------------------------
def __repr__ ( self ):
""" Returns a "pretty print" version of the ViewElements object.
"""
return self.content.__repr__()
#-------------------------------------------------------------------------------
# Define forward reference traits:
#-------------------------------------------------------------------------------
ViewElements.add_class_trait( 'parents', List( ViewElements ) )
ViewElements.add_class_trait( '_search_order', Any )
#-------------------------------------------------------------------------------
# 'SearchStackItem' class:
#-------------------------------------------------------------------------------
class SearchStackItem ( HasStrictTraits ):
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Name that was looked up
id = Str
# Index into the 'mro' list of ViewElements that the ID was found in
context = Int
|