From 09b475856c815bb41fb501b92bbb4c56bce5bb91 Mon Sep 17 00:00:00 2001 From: Nils <> Date: Fri, 18 Dec 2020 13:48:34 +0100 Subject: [PATCH] link block names --- engine/block.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/engine/block.py b/engine/block.py index 225c22b..e4a7238 100644 --- a/engine/block.py +++ b/engine/block.py @@ -33,8 +33,8 @@ class Block(object): def __init__(self, track): self.data = list() - self.name = str(id(self)) - self._minimumInTicks = [0] # if this is bigger than the actual duration-sum of the content this will be used instead. Advice: best used in the form of x*210 (multiple of real base-durations) #is content linked, thats why it is a mutable list of len==1. + self._name = [ str(id(self)) ] #list of len==1 so it is mutable for linked blocks. See @property below + self._minimumInTicks = [0] # if this is bigger than the actual duration-sum of the content this will be used instead. Advice: best used in the form of x*210 (multiple of real base-durations) #is content linked, thats why it is a mutable list of len==1. See @property below self.linkedContentBlocks = WeakSet() #only new standalone blocks use this empty WeakSet. once you contentLink a block it will be overwritten. self._secondInit(parentTrack = track) @@ -45,14 +45,28 @@ class Block(object): self.linkedContentBlocks.add(self) self.rememberBlock() + @property + def name(self): + """name must be mutable so it is shared between content link blocks""" + return self._name[0] + + @name.setter + def name(self, name:str): + listId = id(self._name) #just a precaution + self._name.pop() + self._name.append(name) + assert len(self._name) == 1 + assert listId == id(self._name) + @property def minimumInTicks(self): + """minimumInTicks must be mutable so it is shared between content link blocks""" return self._minimumInTicks[0] @minimumInTicks.setter - def minimumInTicks(self, newValue): + def minimumInTicks(self, newValue:int): """Keep the mutable list at all cost""" - listId = id(self._minimumInTicks) + listId = id(self._minimumInTicks) #just a precaution self._minimumInTicks.pop() self._minimumInTicks.append(newValue) assert len(self._minimumInTicks) == 1 @@ -66,16 +80,17 @@ class Block(object): if serializedObject["data"] is None: #Found a content linked block which already has one member of its group in the score firstBlock = Block.firstBlockWithNewContentDuringDeserializeToObject[serializedObject["contentLinkGroup"]] #block with the same contentGroup. This is the one with the real data. self.data = firstBlock.data - self._minimumInTicks = firstBlock._minimumInTicks + self._minimumInTicks = firstBlock._minimumInTicks #mutable list + self._name = firstBlock._name #mutable list self.linkedContentBlocks = firstBlock.linkedContentBlocks #add self to this is in _secondInit else: #found a stand-alone block or the first one of a content link group self.linkedContentBlocks = WeakSet() self.data = [eval(item["class"]).instanceFromSerializedData(item, parentObject = self) for item in serializedObject["data"]] + self._name = [ str(serializedObject["name"]) ] #saved as string, used as list + self._minimumInTicks = [ int(serializedObject["minimumInTicks"]) ] #saved as int, used as list Block.firstBlockWithNewContentDuringDeserializeToObject[serializedObject["contentLinkGroup"]] = self for item in self.data: item.parentBlocks.add(self) - self.name = serializedObject["name"] - self._minimumInTicks = [int(serializedObject["minimumInTicks"])] #saved as int, used as list self._secondInit(parentTrack = parentObject) return self @@ -159,11 +174,11 @@ class Block(object): if self in copyItem.parentBlocks: #TODO: investigate copyItem.parentBlocks.remove(self) - if self.name.endswith("-copy"): + if self.name.endswith("-copy"): #name is mutable, but getter and setter deal with that. new.name = self.name else: new.name = self.name + "-copy" - new._minimumInTicks = self._minimumInTicks[:] #mutable + new._minimumInTicks = self._minimumInTicks[:] #it is a mutable value, we make a copy of the list (which has an int inside, which is immutable and copied automatically) return new def getUnlinkedData(self): @@ -210,7 +225,7 @@ class Block(object): new.linkedContentBlocks = self.linkedContentBlocks new.linkedContentBlocks.add(new) new.data = self.data #mutable. Will change in all blocks together. - new.name = self.name #immutable. Will change independently but we start with the same name. + new._name = self._name #mutable as well. new._minimumInTicks = self._minimumInTicks #mutable #Add the new block to the parentBlocks set but don't delete self from it, as in copy(). The items are now in more than one block simultaniously for item in new.data: