예제 #1
0
 def __init__(self, maxspines = 0):
     self.events = common.defList()
     self.lastEvents = common.defList()
     self.maxspines = maxspines
     self.spinePathData = False  
예제 #2
0
    def parseProtoSpines(self, protoSpines):
        maxSpines = len(protoSpines)
        currentSpineList = common.defList(default = None)
        spineCollection = SpineCollection()
        
        for i in range(0, self.fileLength):
            thisEventCollection = self.eventCollections[i]                    
            for j in range(0, maxSpines):
                thisEvent = protoSpines[j].eventList[i]                 
               
                if thisEvent is not None:  # something there
                    currentSpine = currentSpineList[j]
                    if currentSpine is None: 
                        ## first event after a None = new spine because 
                        ## Humdrum does not require *+ at the beginning
                        currentSpine = spineCollection.addSpine()
                        currentSpine.beginningPosition = i
                        currentSpineList[j] = currentSpine
                    
                    currentSpine.append(thisEvent)
                    thisEvent.protoSpineNum = currentSpine.id

            '''
            Now we check for spinePathData (from HumdrumDoc):
            *+    add a new spine (to the right of the current spine)
            *-    terminate a current spine 
            *^    split a spine (into two)
            *v    join (two or more) spines into one
            *x    exchange the position of two spines
            *     do nothing
            '''
 
            if thisEventCollection.spinePathData is True:
                newSpineList = common.defList()
                mergerActive = False
                exchangeActive = False
                for j in range(0, maxSpines):
                    thisEvent = protoSpines[j].eventList[i]                 
                    currentSpine = currentSpineList[j]
                    if thisEvent is None and currentSpine is not None:
                        ## should this happen?
                        newSpineList.append(currentSpine)
                    elif thisEvent is None:
                        continue
                    elif thisEvent.contents == "*-":  ## terminate spine
                        currentSpine.endingPosition = i
                    elif thisEvent.contents == "*^":  ## split spine
                        newSpine1 = spineCollection.addSpine()
                        newSpine1.beginningPosition = i+1
                        newSpine1.upstream = [currentSpine.id]
                        newSpine2 = spineCollection.addSpine()
                        newSpine2.beginningPosition = i+1
                        newSpine2.upstream = [currentSpine.id]
                        currentSpine.endingPosition = i
                        currentSpine.downstream = [newSpine1.id, newSpine2.id]
                        newSpineList.append(newSpine1)
                        newSpineList.append(newSpine2)
                    elif thisEvent.contents == "*v":  #merge spine -- n.b. we allow non-adjacent lines to be merged this is incorrect
                        if mergerActive is False:     #               per humdrum syntax, but is easily done.
                            mergeSpine = spineCollection.addSpine()
                            mergeSpine.beginningPosition = i+1
                            mergeSpine.upstream = [currentSpine.id]
                            mergerActive = mergeSpine
                            currentSpine.endingPosition = i
                            currentSpine.downstream = [mergeSpine.id]
                            newSpineList.append(mergeSpine)
                        else:  ## if second merger code is not found then a 1-1 spine "merge" occurs
                            mergeSpine = mergerActive
                            currentSpine.endingPosition = i
                            currentSpine.downstream = [mergeSpine.id]
                            mergeSpine.upstream.append(currentSpine.id)
                    elif thisEvent.contents == "*x":  # exchange spine    
                        if exchangeActive is False:
                            exchangeActive = currentSpine
                        else:  ## if second exchange is not found, then both lines disappear and exception is raised
                               ## n.b. we allow more than one PAIR of exchanges in a line
                            newSpineList.append(currentSpine)
                            newSpineList.append(exchangeActive)
                            exchangeActive = False;
                    else:  ## null processing code "*" 
                        newSpineList.append(currentSpine)
                        
                if exchangeActive is not False:
                    raise HumdrumException("Protospine found with unpaired exchange instruction")        
                currentSpineList = newSpineList
        return spineCollection