• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stevearc / godot_parser / #61

pending completion
#61

push

github

stevearc
chore: drop support for python 3.6

296 of 313 branches covered (94.57%)

Branch coverage included in aggregate %.

836 of 858 relevant lines covered (97.44%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/godot_parser/objects.py
1
""" Wrappers for Godot's non-primitive object types """
2

3
from functools import partial
1✔
4
from typing import Type, TypeVar
1✔
5

6
from .util import stringify_object
1✔
7

8
__all__ = [
1✔
9
    "GDObject",
10
    "Vector2",
11
    "Vector3",
12
    "Color",
13
    "NodePath",
14
    "ExtResource",
15
    "SubResource",
16
]
17

18
GD_OBJECT_REGISTRY = {}
1✔
19

20

21
class GDObjectMeta(type):
1✔
22
    """
23
    This is me trying to be too clever for my own good
24

25
    Odds are high that it'll cause some weird hard-to-debug issues at some point, but
26
    isn't it neeeeeat? -_-
27
    """
28

29
    def __new__(cls, name, bases, dct):
1✔
30
        x = super().__new__(cls, name, bases, dct)
1✔
31
        GD_OBJECT_REGISTRY[name] = x
1✔
32
        return x
1✔
33

34

35
GDObjectType = TypeVar("GDObjectType", bound="GDObject")
1✔
36

37

38
class GDObject(metaclass=GDObjectMeta):
1✔
39
    """
40
    Base class for all GD Object types
41

42
    Can be used to represent any GD type. For example::
43

44
        GDObject('Vector2', 1, 2) == Vector2(1, 2)
45
    """
46

47
    def __init__(self, name, *args) -> None:
1✔
48
        self.name = name
1✔
49
        self.args = list(args)
1✔
50

51
    @classmethod
1✔
52
    def from_parser(cls: Type[GDObjectType], parse_result) -> GDObjectType:
1✔
53
        name = parse_result[0]
1✔
54
        factory = GD_OBJECT_REGISTRY.get(name, partial(GDObject, name))
1✔
55
        return factory(*parse_result[1:])
1✔
56

57
    def __str__(self) -> str:
1✔
58
        return "%s( %s )" % (
1✔
59
            self.name,
60
            ", ".join([stringify_object(v) for v in self.args]),
61
        )
62

63
    def __repr__(self) -> str:
1✔
64
        return self.__str__()
1✔
65

66
    def __eq__(self, other) -> bool:
1✔
67
        if not isinstance(other, GDObject):
1✔
68
            return False
1✔
69
        return self.name == other.name and self.args == other.args
1✔
70

71
    def __ne__(self, other) -> bool:
1✔
72
        return not self.__eq__(other)
1✔
73

74

75
class Vector2(GDObject):
1✔
76
    def __init__(self, x: float, y: float) -> None:
1✔
77
        super().__init__("Vector2", x, y)
1✔
78

79
    def __getitem__(self, idx) -> float:
1✔
80
        return self.args[idx]
1✔
81

82
    def __setitem__(self, idx: int, value: float):
1✔
83
        self.args[idx] = value
1✔
84

85
    @property
1✔
86
    def x(self) -> float:
1✔
87
        """Getter for x"""
88
        return self.args[0]
1✔
89

90
    @x.setter
1✔
91
    def x(self, x: float) -> None:
1✔
92
        """Setter for x"""
93
        self.args[0] = x
1✔
94

95
    @property
1✔
96
    def y(self) -> float:
1✔
97
        """Getter for y"""
98
        return self.args[1]
1✔
99

100
    @y.setter
1✔
101
    def y(self, y: float) -> None:
1✔
102
        """Setter for y"""
103
        self.args[1] = y
1✔
104

105

106
class Vector3(GDObject):
1✔
107
    def __init__(self, x: float, y: float, z: float) -> None:
1✔
108
        super().__init__("Vector3", x, y, z)
1✔
109

110
    def __getitem__(self, idx: int) -> float:
1✔
111
        return self.args[idx]
1✔
112

113
    def __setitem__(self, idx: int, value: float) -> None:
1✔
114
        self.args[idx] = value
1✔
115

116
    @property
1✔
117
    def x(self) -> float:
1✔
118
        """Getter for x"""
119
        return self.args[0]
1✔
120

121
    @x.setter
1✔
122
    def x(self, x: float) -> None:
1✔
123
        """Setter for x"""
124
        self.args[0] = x
1✔
125

126
    @property
1✔
127
    def y(self) -> float:
1✔
128
        """Getter for y"""
129
        return self.args[1]
1✔
130

131
    @y.setter
1✔
132
    def y(self, y: float) -> None:
1✔
133
        """Setter for y"""
134
        self.args[1] = y
1✔
135

136
    @property
1✔
137
    def z(self) -> float:
1✔
138
        """Getter for z"""
139
        return self.args[2]
1✔
140

141
    @z.setter
1✔
142
    def z(self, z: float) -> None:
1✔
143
        """Setter for z"""
144
        self.args[2] = z
1✔
145

146

147
class Color(GDObject):
1✔
148
    def __init__(self, r: float, g: float, b: float, a: float) -> None:
1✔
149
        assert 0 <= r <= 1
1✔
150
        assert 0 <= g <= 1
1✔
151
        assert 0 <= b <= 1
1✔
152
        assert 0 <= a <= 1
1✔
153
        super().__init__("Color", r, g, b, a)
1✔
154

155
    def __getitem__(self, idx: int) -> float:
1✔
156
        return self.args[idx]
1✔
157

158
    def __setitem__(self, idx: int, value: float) -> None:
1✔
159
        self.args[idx] = value
1✔
160

161
    @property
1✔
162
    def r(self) -> float:
1✔
163
        """Getter for r"""
164
        return self.args[0]
1✔
165

166
    @r.setter
1✔
167
    def r(self, r: float) -> None:
1✔
168
        """Setter for r"""
169
        self.args[0] = r
1✔
170

171
    @property
1✔
172
    def g(self) -> float:
1✔
173
        """Getter for g"""
174
        return self.args[1]
1✔
175

176
    @g.setter
1✔
177
    def g(self, g: float) -> None:
1✔
178
        """Setter for g"""
179
        self.args[1] = g
1✔
180

181
    @property
1✔
182
    def b(self) -> float:
1✔
183
        """Getter for b"""
184
        return self.args[2]
1✔
185

186
    @b.setter
1✔
187
    def b(self, b: float) -> None:
1✔
188
        """Setter for b"""
189
        self.args[2] = b
1✔
190

191
    @property
1✔
192
    def a(self) -> float:
1✔
193
        """Getter for a"""
194
        return self.args[3]
1✔
195

196
    @a.setter
1✔
197
    def a(self, a: float) -> None:
1✔
198
        """Setter for a"""
199
        self.args[3] = a
1✔
200

201

202
class NodePath(GDObject):
1✔
203
    def __init__(self, path: str) -> None:
1✔
204
        super().__init__("NodePath", path)
1✔
205

206
    @property
1✔
207
    def path(self) -> str:
1✔
208
        """Getter for path"""
209
        return self.args[0]
1✔
210

211
    @path.setter
1✔
212
    def path(self, path: str) -> None:
1✔
213
        """Setter for path"""
214
        self.args[0] = path
1✔
215

216
    def __str__(self) -> str:
1✔
217
        return '%s("%s")' % (self.name, self.path)
1✔
218

219

220
class ExtResource(GDObject):
1✔
221
    def __init__(self, id: int) -> None:
1✔
222
        super().__init__("ExtResource", id)
1✔
223

224
    @property
1✔
225
    def id(self) -> int:
1✔
226
        """Getter for id"""
227
        return self.args[0]
1✔
228

229
    @id.setter
1✔
230
    def id(self, id: int) -> None:
1✔
231
        """Setter for id"""
232
        self.args[0] = id
1✔
233

234

235
class SubResource(GDObject):
1✔
236
    def __init__(self, id: int) -> None:
1✔
237
        super().__init__("SubResource", id)
1✔
238

239
    @property
1✔
240
    def id(self) -> int:
1✔
241
        """Getter for id"""
242
        return self.args[0]
1✔
243

244
    @id.setter
1✔
245
    def id(self, id: int) -> None:
1✔
246
        """Setter for id"""
247
        self.args[0] = id
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc