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
|
""" Context adapter for Python dictionaries. """
# Enthought library imports.
from apptools.naming.api import Binding, ContextAdapter, naming_manager
class DictContextAdapter(ContextAdapter):
""" Context adapter for Python dictionaries. """
#### 'ContextAdapter' interface ###########################################
# The object that we are adapting.
#
#
# fixme: We would like to specialize the 'adaptee' trait here, but if we
# make it of type 'Dict' then, on assignment, traits will create a *copy*
# of the actual dict which I think you'll agree is not very adapter-like!
## adaptee = Dict
###########################################################################
# Protected 'Context' interface.
###########################################################################
def _is_bound(self, name):
""" Is a name bound in this context? """
return name in self.adaptee
def _lookup(self, name):
""" Looks up a name in this context. """
obj = self.adaptee[name]
return naming_manager.get_object_instance(obj, name, self)
def _lookup_binding(self, name):
""" Looks up the binding for a name in this context. """
return Binding(name=name, obj=self._lookup(name), context=self)
def _bind(self, name, obj):
""" Binds a name to an object in this context. """
state = naming_manager.get_state_to_bind(obj, name, self)
self.adaptee[name] = state
return
def _rebind(self, name, obj):
""" Rebinds a name to an object in this context. """
self._bind(name, obj)
return
def _unbind(self, name):
""" Unbinds a name from this context. """
del self.adaptee[name]
return
def _rename(self, old_name, new_name):
""" Renames an object in this context. """
# Bind the new name.
self._bind(new_name, self._lookup(old_name))
# Unbind the old one.
self._unbind(old_name)
return
def _create_subcontext(self, name):
""" Creates a sub-context of this context. """
# Create a dictionary of the same type as the one we are adapting.
sub = type(self.adaptee)()
self.adaptee[name] = sub
return DictContextAdapter(adaptee=sub)
def _destroy_subcontext(self, name):
""" Destroys a sub-context of this context. """
del self.adaptee[name]
return
def _list_bindings(self):
""" Lists the bindings in this context. """
bindings = []
for key in self.adaptee:
bindings.append(
Binding(name=str(key), obj=self._lookup(key), context=self)
)
return bindings
def _list_names(self):
""" Lists the names bound in this context. """
return [str(key) for key in self.adaptee]
#### EOF ######################################################################
|