1
#  Copyright (c) 2005-2020, Enthought, Inc.
2
#  All rights reserved.
3
#
4
#  This software is provided without warranty under the terms of the BSD
5
#  license included in LICENSE.txt and may be redistributed only
6
#  under the conditions described in the aforementioned license.  The license
7
#  is also available online at http://www.enthought.com/licenses/BSD.txt
8
#
9
#  Thanks for using Enthought open source!
10
#
11

12 11
from traitsui.testing.tester import locator
13

14

15 11
def _get_editor_by_name(ui, name):
16
    """ Return a single Editor from an instance of traitsui.ui.UI with
17
    a given extended name. Raise if zero or many editors are found.
18

19
    Parameters
20
    ----------
21
    ui : traitsui.ui.UI
22
        The UI from which an editor will be retrieved.
23
    name : str
24
        A single name for retrieving an editor on a UI.
25

26
    Returns
27
    -------
28
    editor : Editor
29
        The single editor found.
30
    """
31 8
    editors = ui.get_editors(name)
32

33 11
    all_names = [editor.name for editor in ui._editors]
34 11
    if not editors:
35 8
        raise ValueError(
36
            "No editors can be found with name {!r}. "
37
            "Found these: {!r}".format(name, all_names)
38
        )
39 11
    if len(editors) > 1:
40 8
        raise ValueError(
41
            "Found multiple editors with name {!r}.".format(name))
42 8
    editor, = editors
43 8
    return editor
44

45

46 11
def _get_editor_by_id(ui, id):
47
    """ Return single Editor from an instance of traitsui.ui.UI with
48
    the given identifier.
49

50
    Parameters
51
    ----------
52
    ui : traitsui.ui.UI
53
        The UI from which an editor will be retrieved.
54
    id : str
55
        Id for finding an item in the UI.
56

57
    Returns
58
    -------
59
    editor : Editor
60
        The single editor found.
61
    """
62 8
    try:
63 8
        editor = getattr(ui.info, id)
64 0
    except AttributeError:
65 0
        raise ValueError(
66
            "No editors found with id {!r}. Got these: {!r}".format(
67
                id, ui._names)
68
            )
69 8
    return editor
70

71

72 11
def register_nested_ui_solvers(registry, target_class, nested_ui_getter):
73
    """ Function to register solvers for obtaining nested targets inside a
74
    nested traitsui.ui.UI inside a (parent) target.
75

76
    For example, an instance of TreeEditor may contain a nested
77
    ``traitsui.ui.UI`` instance. In that case, the ``target_class`` will the
78
    TreeEditor and the ``nested_ui_getter`` specifies how to obtain the nested
79
    ``traitsui.ui.UI`` UI from it.
80

81
    Parameters
82
    ----------
83
    registry : TargetRegistry
84
        The registry being registered to
85
    target_class : subclass of type
86
        The type of a UI target being used as the target_class for the
87
        solvers
88
    nested_ui_getter : callable(target: target_class) -> traitsui.ui.UI
89
        A callable specific to the particular target_class to obtain a nested
90
        UI.
91
    """
92

93 11
    registry.register_solver(
94
        target_class=target_class,
95
        locator_class=locator.TargetByName,
96
        solver=lambda wrapper, location: (
97
            _get_editor_by_name(
98
                ui=nested_ui_getter(wrapper.target),
99
                name=location.name,
100
            )
101
        ),
102
    )
103 11
    registry.register_solver(
104
        target_class=target_class,
105
        locator_class=locator.TargetById,
106
        solver=lambda wrapper, location: (
107
            _get_editor_by_id(
108
                ui=nested_ui_getter(wrapper.target),
109
                id=location.id,
110
            )
111
        ),
112
    )

Read our documentation on viewing source code .

Loading