File: gobject_object.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 (139 lines) | stat: -rw-r--r-- 3,881 bytes parent folder | download | duplicates (2)
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
# gobject_object.py
#
# Copyright 2022 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 functools import cached_property

from blueprintcompiler.errors import T
from blueprintcompiler.lsp_utils import DocumentSymbol

from .common import *
from .response_id import ExtResponse
from .types import ClassName, ConcreteClassName

RESERVED_IDS = {
    "this",
    "self",
    "template",
    "true",
    "false",
    "null",
    "none",
    "item",
    "expr",
    "typeof",
}


class ObjectContent(AstNode):
    grammar = ["{", Until(OBJECT_CONTENT_HOOKS, "}")]

    @property
    def gir_class(self):
        return self.parent.gir_class


class Object(AstNode):
    grammar: T.Any = [
        ConcreteClassName,
        Optional(UseIdent("id")),
        ObjectContent,
    ]

    @property
    def id(self) -> str:
        return self.tokens["id"]

    @property
    def class_name(self) -> ClassName:
        return self.children[ClassName][0]

    @property
    def content(self) -> ObjectContent:
        return self.children[ObjectContent][0]

    @property
    def signature(self) -> str:
        if self.id:
            return f"{self.class_name.gir_type.full_name} {self.id}"
        elif t := self.class_name.gir_type:
            return f"{t.full_name}"
        else:
            return f"{self.class_name.as_string}"

    @property
    def document_symbol(self) -> T.Optional[DocumentSymbol]:
        return DocumentSymbol(
            self.class_name.as_string,
            SymbolKind.Object,
            self.range,
            self.children[ClassName][0].range,
            self.id,
        )

    @property
    def gir_class(self) -> GirType:
        if self.class_name is None:
            raise CompilerBugError()
        return self.class_name.gir_type

    @cached_property
    def action_widgets(self) -> T.List[ExtResponse]:
        """Get list of widget's action widgets.

        Empty if object doesn't have action widgets.
        """
        from .gtkbuilder_child import Child

        return [
            child.response_id
            for child in self.content.children[Child]
            if child.response_id
        ]

    @validate("id")
    def object_id_not_reserved(self):
        from .gtkbuilder_template import Template

        if not isinstance(self, Template) and self.id in RESERVED_IDS:
            raise CompileWarning(f"{self.id} may be a confusing object ID")


def validate_parent_type(node, ns: str, name: str, err_msg: str):
    parent = node.root.gir.get_type(name, ns)
    container_type = node.parent_by_type(Object).gir_class
    if container_type and not container_type.assignable_to(parent):
        raise CompileError(
            f"{container_type.full_name} is not a {ns}.{name}, so it doesn't have {err_msg}"
        )


@decompiler("object")
def decompile_object(ctx: DecompileCtx, gir, klass, id=None):
    gir_class = ctx.type_by_cname(klass)
    klass_name = (
        decompile.full_name(gir_class) if gir_class is not None else "$" + klass
    )
    if id is None:
        ctx.print(f"{klass_name} {{")
    else:
        ctx.print(f"{klass_name} {id} {{")
    ctx.push_obj_type(gir_class)
    return gir_class