@@ -68,8 +68,8 @@ def link_subcomponents(self):
68
68
69
69
def add_input (self , name : str , _input : inputs .Input ) -> Self :
70
70
"""
71
- Add an input to the block.
72
- """ # not sure what else to say
71
+ Attach an input object to the block.
72
+ """
73
73
self .inputs [name ] = _input
74
74
for val in (_input .value , _input .obscurer ):
75
75
if isinstance (val , Block ):
@@ -78,15 +78,15 @@ def add_input(self, name: str, _input: inputs.Input) -> Self:
78
78
79
79
def add_field (self , name : str , _field : field .Field ) -> Self :
80
80
"""
81
- Add a field to the block.
82
- """ # not sure what else to sa
81
+ Attach a field object to the block.
82
+ """
83
83
self .fields [name ] = _field
84
84
return self
85
85
86
86
def set_mutation (self , _mutation : mutation .Mutation ) -> Self :
87
87
"""
88
- Attach a mutation object and call mutation.link_arguments()
89
- """ # this comment explains *what* this does, not *why*
88
+ Attach a mutation object and link it, also asking the mutation to link its own subcomponents.
89
+ """
90
90
self .mutation = _mutation
91
91
_mutation .block = self
92
92
_mutation .link_arguments ()
@@ -113,14 +113,15 @@ def check_toplevel(self):
113
113
@property
114
114
def target (self ):
115
115
"""
116
- Alias for sprite
117
- """ # remove this?
116
+ Alias for .sprite
117
+ """
118
+ # should this be deprecated?
118
119
return self .sprite
119
120
120
121
@property
121
122
def block_shape (self ) -> blockshape .BlockShape :
122
123
"""
123
- Search for the blockshape stored in blockshape.py
124
+ Search for & return the associated blockshape stored in blockshape.py
124
125
:return: The block's block shape (by opcode)
125
126
"""
126
127
_shape = blockshape .BlockShapes .find (self .opcode , "opcode" )
@@ -133,15 +134,16 @@ def block_shape(self) -> blockshape.BlockShape:
133
134
@property
134
135
def can_next (self ):
135
136
"""
136
- :return: Whether the block *can* have a next block (basically checks if it's not a cap block, also considering the behaviour of control_stop)
137
+ :return: Whether the block *can* have a next block (checks if it's not a cap block, but also considering the special behaviour of control_stop)
137
138
"""
138
139
_shape = self .block_shape
139
140
if _shape .is_cap is not blockshape .MUTATION_DEPENDENT :
140
141
return _shape .is_attachable
141
142
else :
142
143
if self .mutation is None :
143
144
# If there's no mutation, let's just assume yes
144
- warnings .warn (f"{ self } has no mutation! Assuming we can add block ;-;" )
145
+ # add filterwarnings?
146
+ warnings .warn (f"{ self } has no mutation! Assuming we can add blocks!" )
145
147
return True
146
148
147
149
return self .mutation .has_next
@@ -150,6 +152,7 @@ def can_next(self):
150
152
def id (self ) -> str :
151
153
"""
152
154
Work out the id of this block by searching through the sprite dictionary
155
+ If one cannot be found, generate a new one and return that.
153
156
"""
154
157
if self ._id :
155
158
return self ._id
@@ -191,8 +194,9 @@ def next_id(self):
191
194
@property
192
195
def relatives (self ) -> list [Block ]:
193
196
"""
194
- :return: A list of blocks which are related to this block (e.g. parent, next, inputs )
197
+ :return: A list of blocks which are related to this block (i.e. parent & next )
195
198
"""
199
+ # TODO: consider adding input blocks?
196
200
_ret = []
197
201
198
202
def yield_block (_block : Block | None ):
@@ -209,6 +213,7 @@ def children(self) -> list[Block | prim.Prim]:
209
213
"""
210
214
:return: A list of blocks that are inside of this block, **NOT INCLUDING THE ATTACHED BLOCK**
211
215
"""
216
+ # does this include procedure definitions' inner block?
212
217
_children = []
213
218
for _input in self .inputs .values ():
214
219
if isinstance (_input .value , Block ) or isinstance (_input .value , prim .Prim ):
@@ -223,7 +228,8 @@ def previous_chain(self):
223
228
"""
224
229
Recursive getter method to get all previous blocks in the blockchain (until hitting a top-level block)
225
230
"""
226
- if self .parent is None : # todo: use is_top_level?
231
+ # TODO: check if this hits the recursion limit
232
+ if self .parent is None : # TODO: use is_top_level?
227
233
return [self ]
228
234
229
235
return [self ] + self .parent .previous_chain
@@ -233,6 +239,7 @@ def attached_chain(self):
233
239
"""
234
240
Recursive getter method to get all next blocks in the blockchain (until hitting a bottom-levell block)
235
241
"""
242
+ # TODO: check if this hits the recursion limit
236
243
if self .next is None :
237
244
return [self ]
238
245
@@ -248,8 +255,7 @@ def complete_chain(self):
248
255
@property
249
256
def top_level_block (self ):
250
257
"""
251
- Get the first block in the block stack that this block is part of
252
- same as the old stack_parent property from sbedtior v1
258
+ Get the first (top level) block in the block stack that this block is part of
253
259
"""
254
260
return self .previous_chain [- 1 ]
255
261
@@ -266,7 +272,7 @@ def stack_tree(self):
266
272
Useful for showing a block stack in the console, using pprint
267
273
:return: A tree-like nested list structure representing the stack of blocks, including inputs, starting at this block
268
274
"""
269
- _tree = [self ]
275
+ _tree : list [ Block | prim . Prim | list [ Block ]] = [self ]
270
276
for child in self .children :
271
277
if isinstance (child , prim .Prim ):
272
278
_tree .append (child )
@@ -281,7 +287,7 @@ def stack_tree(self):
281
287
@property
282
288
def category (self ):
283
289
"""
284
- Works out what category of block this is using the opcode. Does not perform validation
290
+ Works out what category of block this is as a string, using the opcode. Does not perform validation
285
291
"""
286
292
return self .opcode .split ('_' )[0 ]
287
293
@@ -526,7 +532,7 @@ def attach_block(self, new: Block) -> Block:
526
532
def duplicate_single_block (self ) -> Block :
527
533
return self .attach_block (self .dcopy ())
528
534
529
- def attach_chain (self , * chain : Iterable [ Block ] ) -> Block :
535
+ def attach_chain (self , * chain : Block ) -> Block :
530
536
attaching_block = self
531
537
for _block in chain :
532
538
attaching_block = attaching_block .attach_block (_block )
@@ -576,4 +582,3 @@ def delete_chain(self):
576
582
"""
577
583
for _block in self .attached_chain :
578
584
_block .delete_single_block ()
579
-
0 commit comments