File: action_manager_builder.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (310 lines) | stat: -rw-r--r-- 10,713 bytes parent folder | download
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# (C) Copyright 2005-2023 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!

""" Base action manager builder class

This module provides a base class that takes a schema for an action manager
and builds the concrete action manager and its groups and items, folding in
schema additions.
"""

import logging

from collections import defaultdict

from traits.api import HasTraits, Instance, List

from .schema import Schema, ToolBarSchema
from .schema_addition import SchemaAddition
from ._topological_sort import before_after_sort


# Logging.
logger = logging.getLogger(__name__)


class ActionManagerBuilder(HasTraits):
    """ Builds action managers from schemas, merging schema additions.
    """

    #: An ActionController to use with the action managers created by the
    #: builder.  May be None.
    controller = Instance("pyface.action.action_controller.ActionController")

    #: Schema additions to apply to all managers built by this builder.
    #: Any additions which do not match part of the supplied schema will be
    #: ignored.
    additions = List(Instance(SchemaAddition))

    # ------------------------------------------------------------------------
    # 'ActionManagerBuilder' interface.
    # ------------------------------------------------------------------------

    def create_action_manager(self, schema):
        """ Create a manager for the given schema using the additions.

        Any additions whose paths do not match the supplied

        Parameters
        ----------
        schema : Schema
            An Schema for an ActionManager subclass (ie. one of MenuBarSchema,
            MenuSchema, or ToolBarSchema).

        Returns
        -------
        manager : ActionManager
            The concrete ActionManager instance corresponding to the Schema
            with addtions.  This does not yet have concrete toolkit widgets
            associated with it: usually those will be created separately.
        """
        additions_map = defaultdict(list)
        for addition in self.additions:
            if addition.path:
                additions_map[addition.path].append(addition)

        manager = self._create_action_manager_recurse(schema, additions_map)
        manager.controller = self.controller
        return manager

    def get_additional_toolbar_schemas(self):
        """ Get any top-level toolbars from additions.

        Unlike menus, there is no base toolbar manager, so additions which
        contribute new toolbars appear with no path.  It is up to the class
        using the builder how it wants to handle these additional toolbars.

        Returns
        -------
        schemas : list of ToolBarSchema
            The additional toolbars specified in self.additions.
        """
        schemas = []
        for addition in self.additions:
            if not addition.path:
                schema = addition.factory()
                if isinstance(schema, ToolBarSchema):
                    schemas.append(schema)
                else:
                    logger.error(
                        "Invalid top-level schema addition: %r. Only "
                        "ToolBar schemas can be path-less.",
                        schema,
                    )
        return schemas

    def prepare_item(self, item, path):
        """ Called immediately after a concrete Pyface item has been created
        (or, in the case of items that are not produced from schemas,
        immediately before they are processed).

        This hook can be used to perform last-minute transformations or
        configuration. Returns a concrete Pyface item.

        Parameters
        ----------
        item : pyface.action item
            A concrete Pyface item.
        path : str
            The path to the item in the Schema.

        Returns
        -------
        item : pyface.action item
            A modified or transformed Pyface item.
        """
        return item

    # ------------------------------------------------------------------------
    # Private interface.
    # ------------------------------------------------------------------------

    def _get_ordered_schemas(self, schemas):
        begin = []
        middle = []
        end = []

        for schema in schemas:
            absolute_position = getattr(schema, "absolute_position", None)
            if absolute_position is None:
                middle.append(schema)
            elif absolute_position == "last":
                end.append(schema)
            else:
                begin.append(schema)

        schemas = (
            before_after_sort(begin)
            + before_after_sort(middle)
            + before_after_sort(end)
        )
        return schemas

    def _group_items_by_id(self, items):
        """ Group a list of action items by their ID.

        Action items are Schemas and Groups, MenuManagers, etc.

        Return a dictionary {item_id: list_of_items}, and a list containing
        all the ids ordered by their appearance in the `all_items` list. The
        ordered IDs are used as a replacement for an ordered dictionary, to
        keep compatibility with Python <2.7 .

        """

        ordered_items_ids = []
        id_to_items = defaultdict(list)

        for item in items:
            if item.id not in id_to_items:
                ordered_items_ids.append(item.id)
            id_to_items[item.id].append(item)

        return id_to_items, ordered_items_ids

    def _group_items_by_class(self, items):
        """ Group a list of action items by their class.

        Action items are Schemas and Groups, MenuManagers, etc.

        Return a dictionary {item_class: list_of_items}, and a list containing
        all the classes ordered by their appearance in the `all_items` list.
        The ordered classes are used as a replacement for an ordered
        dictionary, to keep compatibility with Python <2.7 .

        """

        ordered_items_class = []
        class_to_items = defaultdict(list)

        for item in items:
            if item.__class__ not in class_to_items:
                ordered_items_class.append(item.__class__)
            class_to_items[item.__class__].append(item)

        return class_to_items, ordered_items_class

    def _unpack_schema_additions(self, items):
        """ Unpack additions, since they may themselves be schemas. """

        unpacked_items = []

        for item in items:
            if isinstance(item, SchemaAddition):
                unpacked_items.append(item.factory())
            else:
                unpacked_items.append(item)

        return unpacked_items

    def _merge_items_with_same_path(self, id_to_items, ordered_items_ids):
        """ Merge items with the same path if possible.

        Items must be subclasses of `Schema` and they must be instances of
        the same class to be merged.

        """

        merged_items = []
        for item_id in ordered_items_ids:
            items_with_same_id = id_to_items[item_id]

            # Group items by class.
            class_to_items, ordered_items_class = self._group_items_by_class(
                items_with_same_id
            )

            for items_class in ordered_items_class:
                items_with_same_class = class_to_items[items_class]

                if len(items_with_same_class) == 1:
                    merged_items.extend(items_with_same_class)

                else:
                    # Only schemas can be merged.
                    if issubclass(items_class, Schema):
                        # Merge into a single schema.
                        items_content = sum(
                            (item.items for item in items_with_same_class), []
                        )

                        merged_item = items_with_same_class[0].clone_traits()
                        merged_item.items = items_content
                        merged_items.append(merged_item)

                    else:
                        merged_items.extend(items_with_same_class)

        return merged_items

    def _preprocess_schemas(self, schema, additions, path):
        """ Sort and merge a schema and a set of schema additions. """

        # Determine the order of the items at this path.
        if additions[path]:
            all_items = self._get_ordered_schemas(
                schema.items + additions[path]
            )
        else:
            all_items = schema.items

        unpacked_items = self._unpack_schema_additions(all_items)

        id_to_items, ordered_items_ids = self._group_items_by_id(
            unpacked_items
        )

        merged_items = self._merge_items_with_same_path(
            id_to_items, ordered_items_ids
        )

        return merged_items

    def _create_action_manager_recurse(self, schema, additions, path=""):
        """ Recursively create a manager for the given schema and additions map.

        Items with the same path are merged together in a single entry if
        possible (i.e., if they have the same class).

        When a list of items is merged, their children are added to a clone
        of the first item in the list. As a consequence, traits like menu
        names etc. are inherited from the first item.

        """
        from pyface.action.action_manager import ActionManager

        # Compute the new action path.
        if path:
            path = path + "/" + schema.id
        else:
            path = schema.id

        preprocessed_items = self._preprocess_schemas(schema, additions, path)

        # Create the actual children by calling factory items.
        children = []
        for item in preprocessed_items:
            if isinstance(item, Schema):
                item = self._create_action_manager_recurse(
                    item, additions, path
                )
            else:
                item = self.prepare_item(item, path + "/" + item.id)

            if isinstance(item, ActionManager):
                # Give even non-root action managers a reference to the
                # controller so that custom Groups, MenuManagers, etc. can get
                # access to the state it holds.
                item.controller = self.controller

            children.append(item)

        # Finally, create the pyface.action instance for this schema.
        return self.prepare_item(schema.create(children), path)