File: decompiler.py

package info (click to toggle)
blueprint-compiler 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,140 kB
  • sloc: python: 8,504; sh: 31; makefile: 6
file content (477 lines) | stat: -rw-r--r-- 14,354 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# decompiler.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# This file 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 3 of the
# License, or (at your option) any later version.
#
# This file 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 program.  If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

import typing as T
from collections import defaultdict
from dataclasses import dataclass
from enum import Enum

from . import formatter
from .gir import *
from .utils import Colors, escape_quote
from .xml_reader import Element, parse, parse_string

__all__ = ["decompile"]


_DECOMPILERS: dict[str, list] = defaultdict(list)
_CLOSING = {
    "{": "}",
    "[": "]",
}
_NAMESPACES = [
    ("GLib", "2.0"),
    ("GObject", "2.0"),
    ("Gio", "2.0"),
    ("Adw", "1"),
]


class LineType(Enum):
    NONE = 1
    STMT = 2
    BLOCK_START = 3
    BLOCK_END = 4


class DecompileCtx:
    def __init__(self, parent_gir: T.Optional[GirContext] = None) -> None:
        self.sub_decompiler = parent_gir is not None
        self._result: str = ""
        self.gir = parent_gir or GirContext()
        self._blocks_need_end: T.List[str] = []
        self._last_line_type: LineType = LineType.NONE
        self._obj_type_stack: list[T.Optional[GirType]] = []
        self._node_stack: list[Element] = []

        self.gir.add_namespace(get_namespace("Gtk", "4.0"))

    @property
    def result(self) -> str:
        imports = ""

        if not self.sub_decompiler:
            import_lines = sorted(
                [
                    f"using {ns} {namespace.version};"
                    for ns, namespace in self.gir.namespaces.items()
                    if ns != "Gtk"
                ]
            )
            imports += "\n".join(["using Gtk 4.0;", *import_lines])

        return formatter.format(imports + self._result)

    def type_by_cname(self, cname: str) -> T.Optional[GirType]:
        if type := self.gir.get_type_by_cname(cname):
            return type

        for ns, version in _NAMESPACES:
            try:
                namespace = get_namespace(ns, version)
                if type := namespace.get_type_by_cname(cname):
                    self.gir.add_namespace(namespace)
                    return type
            except:
                pass

        return None

    def start_block(self) -> None:
        self._blocks_need_end.append("")
        self._obj_type_stack.append(None)

    def end_block(self) -> None:
        if close := self._blocks_need_end.pop():
            self.print(close)
        self._obj_type_stack.pop()

    @property
    def current_obj_type(self) -> T.Optional[GirType]:
        return next((x for x in reversed(self._obj_type_stack) if x is not None), None)

    def push_obj_type(self, type: T.Optional[GirType]) -> None:
        self._obj_type_stack[-1] = type

    @property
    def current_node(self) -> T.Optional[Element]:
        if len(self._node_stack) == 0:
            return None
        else:
            return self._node_stack[-1]

    @property
    def parent_node(self) -> T.Optional[Element]:
        if len(self._node_stack) < 2:
            return None
        else:
            return self._node_stack[-2]

    @property
    def root_node(self) -> T.Optional[Element]:
        if len(self._node_stack) == 0:
            return None
        else:
            return self._node_stack[0]

    @property
    def template_class(self) -> T.Optional[str]:
        assert self.root_node is not None
        for child in self.root_node.children:
            if child.tag == "template":
                return child["class"]

        return None

    def find_object(self, id: str) -> T.Optional[Element]:
        assert self.root_node is not None
        for child in self.root_node.children:
            if child.tag == "template" and child["class"] == id:
                return child

        def find_in_children(node: Element) -> T.Optional[Element]:
            if node.tag in ["object", "menu"] and node["id"] == id:
                return node
            else:
                for child in node.children:
                    if result := find_in_children(child):
                        return result
            return None

        return find_in_children(self.root_node)

    def end_block_with(self, text: str) -> None:
        self._blocks_need_end[-1] = text

    def print(self, line: str, newline: bool = True) -> None:
        self._result += line

        if line.endswith("{") or line.endswith("["):
            if len(self._blocks_need_end):
                self._blocks_need_end[-1] = _CLOSING[line[-1]]

    # Converts a value from an XML element to a blueprint string
    # based on the given type. Returns a tuple of translator comments
    # (if any) and the decompiled syntax.
    def decompile_value(
        self,
        value: str,
        type: T.Optional[GirType],
        translatable: T.Optional[T.Tuple[str, str, str]] = None,
    ) -> T.Tuple[str, str]:
        def get_enum_name(value):
            for member in type.members.values():
                if (
                    member.nick == value
                    or member.c_ident == value
                    or str(member.value) == value
                ):
                    return member.name
            return value.replace("-", "_")

        if translatable is not None and truthy(translatable[0]):
            return decompile_translatable(value, *translatable)
        elif type is None:
            return "", f"{escape_quote(value)}"
        elif type.assignable_to(FloatType()):
            return "", str(value)
        elif type.assignable_to(BoolType()):
            val = truthy(value)
            return "", ("true" if val else "false")
        elif type.assignable_to(ArrayType(StringType())):
            items = ", ".join([escape_quote(x) for x in value.split("\n")])
            return "", f"[{items}]"
        elif (
            type.assignable_to(self.gir.namespaces["Gtk"].lookup_type("Gdk.Pixbuf"))
            or type.assignable_to(self.gir.namespaces["Gtk"].lookup_type("Gdk.Texture"))
            or type.assignable_to(
                self.gir.namespaces["Gtk"].lookup_type("Gdk.Paintable")
            )
            or type.assignable_to(
                self.gir.namespaces["Gtk"].lookup_type("Gtk.ShortcutAction")
            )
            or type.assignable_to(
                self.gir.namespaces["Gtk"].lookup_type("Gtk.ShortcutTrigger")
            )
        ):
            return "", escape_quote(value)
        elif value == self.template_class:
            return "", "template"
        elif type.assignable_to(
            self.gir.namespaces["Gtk"].lookup_type("GObject.Object")
        ) or isinstance(type, Interface):
            return "", ("null" if value == "" else value)
        elif isinstance(type, Bitfield):
            flags = [get_enum_name(flag) for flag in value.split("|")]
            return "", " | ".join(flags)
        elif isinstance(type, Enumeration):
            return "", get_enum_name(value)
        elif isinstance(type, TypeType):
            if t := self.type_by_cname(value):
                return "", f"typeof<{full_name(t)}>"
            else:
                return "", f"typeof<${value}>"
        else:
            return "", escape_quote(value)


def decompile_element(
    ctx: DecompileCtx, gir: T.Optional[GirContext], xml: Element
) -> None:
    try:
        decompilers = [d for d in _DECOMPILERS[xml.tag] if d._filter(ctx)]
        if len(decompilers) == 0:
            raise UnsupportedError(f"unsupported XML tag: <{xml.tag}>")

        decompiler = decompilers[0]

        if decompiler._element:
            args = [ctx, gir, xml]
            kwargs: T.Dict[str, T.Optional[str]] = {}
        else:
            args = [ctx, gir]
            kwargs = {canon(name): value for name, value in xml.attrs.items()}
            if decompiler._cdata:
                if len(xml.children):
                    kwargs["cdata"] = None
                else:
                    kwargs["cdata"] = xml.cdata

        ctx._node_stack.append(xml)
        ctx.start_block()

        try:
            gir = decompiler(*args, **kwargs)
        except TypeError as e:
            raise UnsupportedError(tag=xml.tag)

        if not decompiler._skip_children:
            for child in xml.children:
                decompile_element(ctx, gir, child)

        ctx.end_block()
        ctx._node_stack.pop()

    except UnsupportedError as e:
        raise e


def decompile(data: str) -> str:
    ctx = DecompileCtx()

    xml = parse(data)
    decompile_element(ctx, None, xml)

    return ctx.result


def decompile_string(data: str) -> str:
    ctx = DecompileCtx()

    xml = parse_string(data)
    decompile_element(ctx, None, xml)

    return ctx.result


def canon(string: str) -> str:
    if string == "class":
        return "klass"
    else:
        return string.replace("-", "_").lower()


def truthy(string: str) -> bool:
    return string is not None and string.lower() in ["yes", "true", "t", "y", "1"]


def full_name(gir: GirType) -> str:
    return gir.name if gir.full_name.startswith("Gtk.") else gir.full_name


def lookup_by_cname(gir, cname: str) -> T.Optional[GirType]:
    if isinstance(gir, GirContext):
        return gir.get_type_by_cname(cname)
    else:
        return gir.get_containing(Repository).get_type_by_cname(cname)


def decompiler(
    tag,
    cdata=False,
    parent_type: T.Optional[str] = None,
    parent_tag: T.Optional[str] = None,
    skip_children=False,
    element=False,
):
    def decorator(func):
        func._cdata = cdata
        func._skip_children = skip_children
        func._element = element

        def filter(ctx):
            if parent_type is not None:
                if (
                    ctx.current_obj_type is None
                    or ctx.current_obj_type.full_name != parent_type
                ):
                    return False

            if parent_tag is not None:
                if not any(x.tag == parent_tag for x in ctx._node_stack):
                    return False

            return True

        func._filter = filter

        _DECOMPILERS[tag].append(func)
        return func

    return decorator


@decompiler("interface")
def decompile_interface(ctx, gir, domain=None):
    if domain is not None:
        ctx.print(f"translation-domain {escape_quote(domain)};")
    return gir


@decompiler("requires")
def decompile_requires(ctx, gir, lib=None, version=None):
    return gir


@decompiler("placeholder")
def decompile_placeholder(ctx, gir):
    pass


def decompile_translatable(
    string: str,
    translatable: T.Optional[str],
    context: T.Optional[str],
    comments: T.Optional[str],
) -> T.Tuple[str, str]:
    if translatable is not None and truthy(translatable):
        if comments is None:
            comments = ""
        else:
            comments = comments.replace("/*", " ").replace("*/", " ")
            comments = f"/* Translators: {comments} */"

        if context is not None:
            return comments, f"C_({escape_quote(context)}, {escape_quote(string)})"
        else:
            return comments, f"_({escape_quote(string)})"
    else:
        return "", f"{escape_quote(string)}"


@decompiler("property", cdata=True)
def decompile_property(
    ctx: DecompileCtx,
    gir,
    name,
    cdata,
    bind_source=None,
    bind_property=None,
    bind_flags=None,
    translatable="false",
    comments=None,
    context=None,
):
    name = name.replace("_", "-")
    if cdata is None:
        ctx.print(f"{name}: ")
        ctx.end_block_with(";")
    elif bind_source:
        flags = ""
        bind_flags = bind_flags or []
        if "sync-create" not in bind_flags:
            flags += " no-sync-create"
        if "invert-boolean" in bind_flags:
            flags += " inverted"
        if "bidirectional" in bind_flags:
            flags += " bidirectional"

        if bind_source == ctx.template_class:
            bind_source = "template"

        ctx.print(f"{name}: bind {bind_source}.{bind_property}{flags};")
    elif truthy(translatable):
        comments, translatable = decompile_translatable(
            cdata, translatable, context, comments
        )
        if comments is not None:
            ctx.print(comments)
        ctx.print(f"{name}: {translatable};")
    elif gir is None or gir.properties.get(name) is None:
        ctx.print(f"{name}: {escape_quote(cdata)};")
    elif (
        gir.assignable_to(ctx.gir.get_class("BuilderListItemFactory", "Gtk"))
        and name == "bytes"
    ):
        sub_ctx = DecompileCtx(ctx.gir)

        xml = parse_string(cdata)
        decompile_element(sub_ctx, None, xml)

        ctx.print(sub_ctx.result)
    else:
        _, string = ctx.decompile_value(cdata, gir.properties.get(name).type)
        ctx.print(f"{name}: {string};")
    return gir


@decompiler("attribute", cdata=True)
def decompile_attribute(
    ctx, gir, name, cdata, translatable="false", comments=None, context=None
):
    decompile_property(
        ctx,
        gir,
        name,
        cdata,
        translatable=translatable,
        comments=comments,
        context=context,
    )


@decompiler("attributes")
def decompile_attributes(ctx, gir):
    ctx.print("attributes {")


@dataclass
class UnsupportedError(Exception):
    message: str = "unsupported feature"
    tag: T.Optional[str] = None

    def print(self, filename: str):
        print(f"\n{Colors.RED}{Colors.BOLD}error: {self.message}{Colors.CLEAR}")
        print(f"in {Colors.UNDERLINE}{filename}{Colors.NO_UNDERLINE}")
        if self.tag:
            print(f"in tag {Colors.BLUE}{self.tag}{Colors.CLEAR}")
        print(
            f"""{Colors.FAINT}The compiler might support this feature, but the porting tool does not. You
probably need to port this file manually.{Colors.CLEAR}\n"""
        )