Debian Codegraph @

blueprintcompiler/language/gtkbuilder_child.py

raw
1 # gtkbuilder_child.py
2 #
3 # Copyright 2022 James Westman <[email protected]>
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This file is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # SPDX-License-Identifier: LGPL-3.0-or-later
19
20
21 from functools import cached_property
22
23 from .common import *
24 from .gobject_object import Object
25 from .response_id import ExtResponse, decompile_response_type
26
27 ALLOWED_PARENTS: T.List[T.Tuple[str, str]] = [
28 ("Gtk", "Buildable"),
29 ("Gio", "ListStore"),
30 ]
31
32
33 class ChildInternal(AstNode):
34 grammar = ["internal-child", UseIdent("internal_child")]
35
36 @property
37 def internal_child(self) -> str:
38 return self.tokens["internal_child"]
39
40
41 class ChildType(AstNode):
42 grammar = UseIdent("child_type").expected("a child type")
43
44 @property
45 def child_type(self) -> str:
46 return self.tokens["child_type"]
47
48
49 class ChildExtension(AstNode):
50 grammar = ExtResponse
51
52 @property
53 def child(self) -> ExtResponse:
54 return self.children[0]
55
56 @docs()
57 def ref_docs(self):
58 return get_docs_section("Syntax ChildExtension")
59
60
61 class ChildAnnotation(AstNode):
62 grammar = ["[", AnyOf(ChildInternal, ChildExtension, ChildType), "]"]
63
64 @property
65 def child(self) -> T.Union[ChildInternal, ChildExtension, ChildType]:
66 return self.children[0]
67
68
69 class Child(AstNode):
70 grammar = [
71 Optional(ChildAnnotation),
72 Object,
73 ]
74
75 @property
76 def annotation(self) -> T.Optional[ChildAnnotation]:
77 annotations = self.children[ChildAnnotation]
78 return annotations[0] if len(annotations) else None
79
80 @property
81 def object(self) -> Object:
82 return self.children[Object][0]
83
84 @validate()
85 def parent_can_have_child(self):
86 if gir_class := self.parent.gir_class:
87 for namespace, name in ALLOWED_PARENTS:
88 parent_type = self.root.gir.get_type(name, namespace)
89 if gir_class.assignable_to(parent_type):
90 break
91 else:
92 hints = [
93 "only Gio.ListStore or Gtk.Buildable implementors can have children"
94 ]
95 if hasattr(gir_class, "properties") and "child" in gir_class.properties:
96 hints.append(
97 "did you mean to assign this object to the 'child' property?"
98 )
99 raise CompileError(
100 f"{gir_class.full_name} doesn't have children",
101 hints=hints,
102 )
103
104 @cached_property
105 def response_id(self) -> T.Optional[ExtResponse]:
106 """Get action widget's response ID.
107
108 If child is not action widget, returns `None`.
109 """
110 if (
111 self.annotation is not None
112 and isinstance(self.annotation.child, ChildExtension)
113 and isinstance(self.annotation.child.child, ExtResponse)
114 ):
115 return self.annotation.child.child
116 else:
117 return None
118
119 @validate()
120 def internal_child_unique(self):
121 if self.annotation is not None:
122 if isinstance(self.annotation.child, ChildInternal):
123 internal_child = self.annotation.child.internal_child
124 self.validate_unique_in_parent(
125 f"Duplicate internal child '{internal_child}'",
126 lambda x: (
127 x.annotation
128 and isinstance(x.annotation.child, ChildInternal)
129 and x.annotation.child.internal_child == internal_child
130 ),
131 )
132
133
134 @decompiler("child", element=True)
135 def decompile_child(ctx, gir, element):
136 if type := element["type"]:
137 if type == "action":
138 if decompiled := decompile_response_type(ctx.parent_node, element):
139 ctx.print(decompiled)
140 return
141
142 ctx.print(f"[{type}]")
143 elif internal_child := element["internal-child"]:
144 ctx.print(f"[internal-child {internal_child}]")
145 return gir