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
|
# (C) Copyright 2005-2025 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in 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!
""" The base class for all directory contexts. """
# Enthought library imports.
from traits.api import Dict
# Local imports.
from .context import Context
from .exception import NameNotFoundError
class DirContext(Context):
""" The base class for all directory contexts. """
# The attributes of every object in the context. The attributes for the
# context itself have the empty string as the key.
#
# {str name : dict attributes}
_attributes = Dict
###########################################################################
# 'DirContext' interface.
###########################################################################
def get_attributes(self, name):
""" Returns the attributes associated with a named object. """
# If the name is empty then we return the attributes of this context.
if len(name) == 0:
attributes = self._get_attributes(name)
else:
# Parse the name.
components = self._parse_name(name)
# If there is exactly one component in the name then the operation
# takes place in this context.
if len(components) == 1:
atom = components[0]
if not self._is_bound(atom):
raise NameNotFoundError(name)
# Do the actual get.
attributes = self._get_attributes(atom)
# Otherwise, attempt to continue resolution into the next context.
else:
if not self._is_bound(components[0]):
raise NameNotFoundError(components[0])
next_context = self._get_next_context(components[0])
attributes = next_context.get_attributes(
"/".join(components[1:])
)
return attributes
def set_attributes(self, name, attributes):
""" Sets the attributes associated with a named object. """
# If the name is empty then we set the attributes of this context.
if len(name) == 0:
attributes = self._set_attributes(name, attributes)
else:
# Parse the name.
components = self._parse_name(name)
# If there is exactly one component in the name then the operation
# takes place in this context.
if len(components) == 1:
atom = components[0]
if not self._is_bound(atom):
raise NameNotFoundError(name)
# Do the actual set.
self._set_attributes(atom, attributes)
# Otherwise, attempt to continue resolution into the next context.
else:
if not self._is_bound(components[0]):
raise NameNotFoundError(components[0])
next_context = self._get_next_context(components[0])
next_context.set_attributes(
"/".join(components[1:]), attributes
)
# fixme: Non-JNDI
def find_bindings(self, visitor):
"""Find bindings with attributes matching criteria in visitor.
Visitor is a function that is passed the bindings for each level of the
heirarchy and the attribute dictionary for those bindings. The visitor
examines the bindings and dictionary and returns the bindings it is
interested in.
"""
bindings = visitor(self.list_bindings(), self._attributes)
# recursively check other sub contexts.
for binding in self.list_bindings():
obj = binding.obj
if isinstance(obj, DirContext):
bindings.extend(obj.find_bindings(visitor))
return bindings
###########################################################################
# Protected 'DirContext' interface.
###########################################################################
def _get_attributes(self, name):
""" Returns the attributes of an object in this context. """
attributes = self._attributes.setdefault(name, {})
return attributes.copy()
def _set_attributes(self, name, attributes):
""" Sets the attributes of an object in this context. """
self._attributes[name] = attributes
###########################################################################
# Protected 'Context' interface.
###########################################################################
def _unbind(self, name):
""" Unbinds a name from this context. """
super(DirContext, self)._unbind(name)
if name in self._attributes:
del self._attributes[name]
def _rename(self, old_name, new_name):
""" Renames an object in this context. """
super(DirContext, self)._rename(old_name, new_name)
if old_name in self._attributes:
self._attributes[new_name] = self._attributes[old_name]
del self._attributes[old_name]
def _destroy_subcontext(self, name):
""" Destroys a sub-context of this context. """
super(DirContext, self)._destroy_subcontext(name)
if name in self._attributes:
del self._attributes[name]
|