/usr/lib/python2.7/dist-packages/traits/adaptation/cached_adapter_factory.py is in python-traits 4.6.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 | #------------------------------------------------------------------------------
# Copyright (c) 2013, 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: Enthought, Inc.
#------------------------------------------------------------------------------
""" An adapter factory that caches adapters per instance. """
import weakref
from traits.api import Any, Bool, HasTraits, Property
from traits.util.api import import_symbol
class CachedAdapterFactory(HasTraits):
""" An adapter factory that caches adapters per instance.
We provide this class to provide the caching functionality of the
old traits 'adapts' implementation. However, note that the cache will
not be cleared unless you take care of cleaning the 'adaptee' trait once
your adapter are deleted.
This class will be removed when the 'adapts' function is removed.
"""
#### 'object' protocol #####################################################
def __call__(self, adaptee):
""" The adapter manager uses callables for adapter factories. """
adapter = self._adapter_cache.get(adaptee, None)
if adapter is None:
adapter = self.factory(adaptee)
self._adapter_cache[adaptee] = adapter
return adapter
#### 'CachedAdapterFactory' protocol #######################################
#: A callable that actually creates the adapters!
#:
#: The factory must ba callable that takes exactly one argument which is
#: the object to be adapted (known as the adaptee), and returns an
#: adapter from the `from_protocol` to the `to_protocol`.
#:
#: The factory can be specified as either a callable, or a string in the
#: form 'foo.bar.baz' which is turned into an import statement
#: 'from foo.bar import baz' and imported when the trait is first accessed.
factory = Property(Any)
#: True if the cache is empty, otherwise False.
#:
#: This method is mostly here to help testing - the framework does not
#: rely on it for any other purpose.
is_empty = Property(Bool)
def _get_is_empty(self):
return len(self._adapter_cache) == 0
#### Private protocol ######################################################
_adapter_cache = Any
def __adapter_cache_default(self):
return weakref.WeakKeyDictionary()
#: Shadow trait for the corresponding property.
_factory = Any
_factory_loaded = Bool(False)
def _get_factory(self):
""" Trait property getter. """
if not self._factory_loaded:
if isinstance(self._factory, basestring):
self._factory = import_symbol(self._factory)
self._factory_loaded = True
return self._factory
def _set_factory(self, factory):
""" Trait property setter. """
self._factory = factory
return
#### EOF #######################################################################
|