File: trait_list_context_adapter.py

package info (click to toggle)
python-apptools 4.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,652 kB
  • sloc: python: 16,657; makefile: 77
file content (66 lines) | stat: -rw-r--r-- 2,159 bytes parent folder | download | duplicates (4)
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
""" Context adapter for trait lists. """


# Enthought library imports.
from traits.api import Any, List, Property, Str

# Local imports.
from list_context_adapter import ListContextAdapter


class TraitListContextAdapter(ListContextAdapter):
    """ Context adapter for trait lists. """

    #### 'Context' interface ##################################################

    # The name of the context within its own namespace.
    namespace_name = Property(Str)

    #### 'ContextAdapter' interface ###########################################

    # The object that we are adapting.
    adaptee = Any

    #### 'ListContextAdapter' interface #######################################

    # The list that we are adapting.
    collection = Property(List)

    #### 'TraitListContextAdapter' interface ##################################

    # The name of the object's trait that provides the list.
    trait_name = Str

    ###########################################################################
    # 'Context' interface.
    ###########################################################################

    def _get_namespace_name(self):
        """ Returns the name of the context within its own namespace. """

        return self.context.namespace_name + '/' + self.trait_name

    ###########################################################################
    # Protected 'ListContext' interface.
    ###########################################################################

    #### 'Properties' #########################################################

    def _get_collection(self):
        """ Returns the collection that we are adapting. """

        components = self.trait_name.split('.')
        if len(components) == 1:
            collection = getattr(self.adaptee, self.trait_name)

        else:
            # Find the object that contains the trait.
            obj = self.adaptee
            for component in components[:-1]:
                obj = getattr(obj, component)

            collection = getattr(obj, components[-1])

        return collection

#### EOF ######################################################################