/usr/lib/python2.7/dist-packages/apptools/naming/dynamic_context.py is in python-apptools 4.3.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 | #-----------------------------------------------------------------------------
#
# Copyright (c) 2006 by Enthought, Inc.
# All rights reserved.
#
#-----------------------------------------------------------------------------
""" Provider of a framework that dynamically determines the contents of a
context at the time of interaction with the contents rather than at the
time a class is written.
This capability is particularly useful when the object acting as a context
is part of a plug-in application -- such as Envisage. In general, this
capability allows the context to be:
- Extendable by contributions from somewhere other than the original
code writer
- Dynamic in that the elements it is composed of can change each time
someone interacts with the contents of the context.
It should be noted that this capability is explicitly different from
contexts that look at another container to determine their contents, such
as a file system context!
Users of this framework contribute items to a dynamic context by adding
traits to the dynamic context instance. (This addition can happen
statically through the use of a Traits Category.) The trait value is the
context item's value and the trait definition's metadata determines how the
item is treated within the context. The support metadata is:
context_name: A non-empty string
Represents the name of the item within this context. This must be
present for the trait to show up as a context item though the value
may change over time as the item gets bound to different names.
context_order: A float value
Indicates the position for the item within this context. All
dynamically contributed context items are sorted by ascending order
of this value using the standard list sort function.
is_context: A boolean value
True if the item is itself a context.
"""
# Standardlibrary imports
import logging
# Local imports
from binding import Binding
from context import Context
from exception import OperationNotSupportedError
# Setup a logger for this module.
logger = logging.getLogger(__name__)
class DynamicContext(Context):
""" A framework that dynamically determines the contents of a context at
the time of interaction with the contents rather than at the time a
context class is written.
It should be noted that this capability is explicitly different from
contexts that look at another container to determine their contents,
such as a file system context!
"""
##########################################################################
# 'Context' interface.
##########################################################################
### protected interface ##################################################
def _is_bound(self, name):
""" Is a name bound in this context?
"""
item = self._get_contributed_context_item(name)
result = item != (None, None)
return result
def _is_context(self, name):
""" Returns True if a name is bound to a context.
"""
item = self._get_contributed_context_item(name)
if item != (None, None):
obj, trait = item
result = True == trait.is_context
else:
result = False
return result
def _list_bindings(self):
""" Lists the bindings in this context.
"""
result = [ Binding(name=n, obj=o, context=self) for n, o, t in \
self._get_contributed_context_items() ]
return result
def _list_names(self):
""" Lists the names bound in this context.
"""
result = [ n for n, o, t in self._get_contributed_context_items() ]
return result
def _lookup(self, name):
""" Looks up a name in this context.
"""
item = self._get_contributed_context_item(name)
if item != (None, None):
obj, trait = item
result = obj
else:
result = None
return result
def _rename(self, old_name, new_name):
""" Renames an object in this context.
"""
item = self._get_contributed_context_item(old_name)
if item != (None, None):
obj, trait = item
trait.context_name = new_name
else:
raise ValueError('Name "%s" not in context', old_name)
def _unbind(self, name):
""" Unbinds a name from this context.
"""
# It is an error to try to unbind any contributed context items
item = self._get_contributed_context_item(name)
if item != (None, None):
raise OperationNotSupportedError('Unable to unbind ' + \
'built-in with name [%s]' % name)
##########################################################################
# 'DynamicContext' interface.
##########################################################################
### protected interface ##################################################
def _get_contributed_context_item(self, name):
""" If the specified name matches a contributed context item then
returns a tuple of the item's current value and trait definition
(in that order.) Otherwise, returns a tuple of (None, None).
"""
result = (None, None)
for n, o, t in self._get_contributed_context_items():
if n == name:
result = (o, t)
return result
def _get_contributed_context_items(self):
""" Returns an ordered list of items to be treated as part of our
context.
Each item in the list is a tuple of its name, object, and trait
definition (in that order.)
"""
# Our traits that get treated as context items are those that declare
# themselves via metadata on the trait definition.
filter = {
'context_name': lambda v: v is not None and len(v) > 0
}
traits = self.traits(**filter)
# Sort the list of context items according to the name of the item.
traits = [ (t.context_order, n, t) for n, t in traits.items() ]
traits.sort()
# Convert these trait definitions into a list of name and object tuples.
result = [(t.context_name, getattr(self, n), t) for order, n, t \
in traits]
return result
### EOF ######################################################################
|