summaryrefslogtreecommitdiff
path: root/gi/_gtktemplate.py
blob: f253887ea7fefc39990d581740a4402ac1ba9e12 (plain)
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
# Copyright 2015 Dustin Spicuzza <dustin@virtualroadside.com>
#           2018 Nikita Churaev <lamefun.x0r@gmail.com>
#           2018 Christoph Reiter <reiter.christoph@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

import os
from collections import abc
from functools import partial

from gi.repository import GLib, GObject, Gio


def _extract_handler_and_args(obj_or_map, handler_name):
    handler = None
    if isinstance(obj_or_map, abc.Mapping):
        handler = obj_or_map.get(handler_name, None)
    else:
        handler = getattr(obj_or_map, handler_name, None)

    if handler is None:
        raise AttributeError('Handler %s not found' % handler_name)

    args = ()
    if isinstance(handler, abc.Sequence):
        if len(handler) == 0:
            raise TypeError("Handler %s tuple can not be empty" % handler)
        args = handler[1:]
        handler = handler[0]

    elif not callable(handler):
        raise TypeError('Handler %s is not a method, function or tuple' % handler)

    return handler, args


def define_builder_scope():
    from gi.repository import Gtk

    class BuilderScope(GObject.GObject, Gtk.BuilderScope):

        def __init__(self, scope_object=None):
            super().__init__()
            self._scope_object = scope_object

        def do_create_closure(self, builder, func_name, flags, obj):
            current_object = builder.get_current_object() or self._scope_object

            if not self._scope_object:
                current_object = builder.get_current_object()
                if func_name not in current_object.__gtktemplate_methods__:
                    return None

                current_object.__gtktemplate_handlers__.add(func_name)
                handler_name = current_object.__gtktemplate_methods__[func_name]
            else:
                current_object = self._scope_object
                handler_name = func_name

            swapped = int(flags & Gtk.BuilderClosureFlags.SWAPPED)
            if swapped:
                raise RuntimeError(
                    "%r not supported" % GObject.ConnectFlags.SWAPPED)
                return None

            handler, args = _extract_handler_and_args(current_object, handler_name)

            if obj:
                p = partial(handler, *args, swap_data=obj)
            else:
                p = partial(handler, *args)

            p.__gtk_template__ = True
            return p

    return BuilderScope


def connect_func(builder, obj, signal_name, handler_name,
                 connect_object, flags, cls):

    if handler_name not in cls.__gtktemplate_methods__:
        return

    method_name = cls.__gtktemplate_methods__[handler_name]
    template_inst = builder.get_object(cls.__gtype_name__)
    template_inst.__gtktemplate_handlers__.add(handler_name)
    handler = getattr(template_inst, method_name)

    after = int(flags & GObject.ConnectFlags.AFTER)
    swapped = int(flags & GObject.ConnectFlags.SWAPPED)
    if swapped:
        raise RuntimeError(
            "%r not supported" % GObject.ConnectFlags.SWAPPED)

    if connect_object is not None:
        if after:
            func = obj.connect_object_after
        else:
            func = obj.connect_object
        func(signal_name, handler, connect_object)
    else:
        if after:
            func = obj.connect_after
        else:
            func = obj.connect
        func(signal_name, handler)


def register_template(cls):
    from gi.repository import Gtk

    bound_methods = {}
    bound_widgets = {}

    for attr_name, obj in list(cls.__dict__.items()):
        if isinstance(obj, CallThing):
            setattr(cls, attr_name, obj._func)
            handler_name = obj._name
            if handler_name is None:
                handler_name = attr_name

            if handler_name in bound_methods:
                old_attr_name = bound_methods[handler_name]
                raise RuntimeError(
                    "Error while exposing handler %r as %r, "
                    "already available as %r" % (
                        handler_name, attr_name, old_attr_name))
            else:
                bound_methods[handler_name] = attr_name
        elif isinstance(obj, Child):
            widget_name = obj._name
            if widget_name is None:
                widget_name = attr_name

            if widget_name in bound_widgets:
                old_attr_name = bound_widgets[widget_name]
                raise RuntimeError(
                    "Error while exposing child %r as %r, "
                    "already available as %r" % (
                        widget_name, attr_name, old_attr_name))
            else:
                bound_widgets[widget_name] = attr_name
                cls.bind_template_child_full(widget_name, obj._internal, 0)

    cls.__gtktemplate_methods__ = bound_methods
    cls.__gtktemplate_widgets__ = bound_widgets

    if Gtk._version == "4.0":
        BuilderScope = define_builder_scope()
        cls.set_template_scope(BuilderScope())
    else:
        cls.set_connect_func(connect_func, cls)

    base_init_template = cls.init_template
    cls.__dontuse_ginstance_init__ = \
        lambda s: init_template(s, cls, base_init_template)
    # To make this file work with older PyGObject we expose our init code
    # as init_template() but make it a noop when we call it ourselves first
    cls.init_template = cls.__dontuse_ginstance_init__


def init_template(self, cls, base_init_template):
    self.init_template = lambda: None

    if self.__class__ is not cls:
        raise TypeError(
            "Inheritance from classes with @Gtk.Template decorators "
            "is not allowed at this time")

    self.__gtktemplate_handlers__ = set()

    base_init_template(self)

    for widget_name, attr_name in self.__gtktemplate_widgets__.items():
        self.__dict__[attr_name] = self.get_template_child(cls, widget_name)

    for handler_name, attr_name in self.__gtktemplate_methods__.items():
        if handler_name not in self.__gtktemplate_handlers__:
            raise RuntimeError(
                "Handler '%s' was declared with @Gtk.Template.Callback "
                "but was not present in template" % handler_name)


class Child(object):

    def __init__(self, name=None, **kwargs):
        self._name = name
        self._internal = kwargs.pop("internal", False)
        if kwargs:
            raise TypeError("Unhandled arguments: %r" % kwargs)


class CallThing(object):

    def __init__(self, name, func):
        self._name = name
        self._func = func


class Callback(object):

    def __init__(self, name=None):
        self._name = name

    def __call__(self, func):
        return CallThing(self._name, func)


def validate_resource_path(path):
    """Raises GLib.Error in case the resource doesn't exist"""

    try:
        Gio.resources_get_info(path, Gio.ResourceLookupFlags.NONE)
    except GLib.Error:
        # resources_get_info() doesn't handle overlays but we keep using it
        # as a fast path.
        # https://gitlab.gnome.org/GNOME/pygobject/issues/230
        Gio.resources_lookup_data(path, Gio.ResourceLookupFlags.NONE)


class Template(object):

    def __init__(self, **kwargs):
        self.string = None
        self.filename = None
        self.resource_path = None
        if "string" in kwargs:
            self.string = kwargs.pop("string")
        elif "filename" in kwargs:
            self.filename = kwargs.pop("filename")
        elif "resource_path" in kwargs:
            self.resource_path = kwargs.pop("resource_path")
        else:
            raise TypeError(
                "Requires one of the following arguments: "
                "string, filename, resource_path")

        if kwargs:
            raise TypeError("Unhandled keyword arguments %r" % kwargs)

    @classmethod
    def from_file(cls, filename):
        return cls(filename=filename)

    @classmethod
    def from_string(cls, string):
        return cls(string=string)

    @classmethod
    def from_resource(cls, resource_path):
        return cls(resource_path=resource_path)

    Callback = Callback

    Child = Child

    def __call__(self, cls):
        from gi.repository import Gtk

        if not isinstance(cls, type) or not issubclass(cls, Gtk.Widget):
            raise TypeError("Can only use @Gtk.Template on Widgets")

        if "__gtype_name__" not in cls.__dict__:
            raise TypeError(
                "%r does not have a __gtype_name__. Set it to the name "
                "of the class in your template" % cls.__name__)

        if hasattr(cls, "__gtktemplate_methods__"):
            raise TypeError("Cannot nest template classes")

        if self.string is not None:
            data = self.string
            if not isinstance(data, bytes):
                data = data.encode("utf-8")
            bytes_ = GLib.Bytes.new(data)
            cls.set_template(bytes_)
            register_template(cls)
            return cls
        elif self.resource_path is not None:
            validate_resource_path(self.resource_path)
            cls.set_template_from_resource(self.resource_path)
            register_template(cls)
            return cls
        else:
            assert self.filename is not None
            file_ = Gio.File.new_for_path(os.fspath(self.filename))
            bytes_ = GLib.Bytes.new(file_.load_contents()[1])
            cls.set_template(bytes_)
            register_template(cls)
            return cls


__all__ = ["Template"]