Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def getMultiMeasureSpanners(self, start, end=-1):
"""Return a dictionary of spanning elements encompassing,
landing or starting within selected measures"""
mm = self.musicEl.getDescendantsByName("measure")
try:
start_m = mm[start]
except IndexError:
raise BadApiRequest("Requested measure does not exist.")
table = {}
# Template of table dictionary (for reference):
# {
# "_targetMeasureID_" : {
# "_eventID_" : {
# "origin" : "_originMeasureID_",
# "distance" : 0,
# "startid" : "_startID_",
# "endid" : "_endID_",
# "tstamp" : "_beat_",
# "tstamp2" : "_Xm+beat_"
# }
# }
# }
meter = self.beatsInfo[change]
# Get list of staff numbers in current measure
sds = [int(sd.getAttribute("n").getValue())
for sd in measure.getClosestStaffDefs()]
# Set aside selected staff numbers
s_nos = []
# Proceed to locate requested staves
for ema_s in ema_m.staves:
if ema_s.number not in sds:
# CAREFUL: there may be issues with "silent" staves
# that may be defined by missing from current measure.
# TODO: Write test, fix.
raise BadApiRequest("Requested staff is not defined")
s_nos.append(ema_s.number)
for s_i, staff in enumerate(measure.getChildrenByName("staff")):
s_no = s_i
if staff.hasAttribute("n"):
s_no = int(staff.getAttribute("n").getValue())
if s_no in s_nos:
ema_s = ema_m.staves[s_nos.index(s_no)]
# Get other elements affecting the staff, e.g. slurs
around = []
for el in list(events):
if self._isInStaff(el, s_no):
around.append(el)
def music(self):
"""Get music element."""
musicEl = self.meiDoc.getElementsByName("music")
# Exception
if len(musicEl) != 1:
raise BadApiRequest(
"MEI document must have one and only one music element")
else:
return musicEl[0]
peers = sd.getPeers()
# get its position in the list of peers based on its id
# (either from @xml:id or added by pymei)
# use it to retrieve next following-sibling[1] element
sd_pos = next(
(i for i, x in enumerate(peers) if x.id == sd.getId()),
None)
following_el = peers[sd_pos + 1]
m_pos = _seekMeasure(following_el, sd_pos+1)
# If at this point a measure hasn't been located, there is
# something unusual with the data
if m_pos is None:
raise BadApiRequest(
"Could not locate measure after new score definition")
# Process for beat data if the scoreDef defines meter
count_att = sd.getAttribute("meter.count")
unit_att = sd.getAttribute("meter.unit")
if count_att and unit_att:
beats[str(m_pos)] = {"count": int(count_att.getValue())}
beats[str(m_pos)]["unit"] = int(unit_att.getValue())
else:
count_elm = sd.getDescendantsByName("meterSig")
if count_elm:
if len(count_elm) > 1:
raise BadApiRequest("Mixed meter is not supported")
count = count_elm[0].getAttribute("count")
unit = count_elm[0].getAttribute("unit")
if count and unit:
""" Determine whether a removed element needs to
be converted to a space or removed altogether"""
if is_last_b and is_last_m:
marked_for_removal.add(el)
else:
marked_as_space.add(el)
# shorten them names
tstamp_first = ema_beat_range.tstamp_first
tstamp_final = ema_beat_range.tstamp_final
co = self.ema_exp.completenessOptions
# check that the requested beats actually fit in the meter
if tstamp_first > int(meter["count"])+1 or \
tstamp_final > int(meter["count"])+1:
raise BadApiRequest(
"Request beat is out of measure bounds")
# Find all descendants with att.duration.musical (@dur)
for layer in staff.getDescendantsByName("layer"):
cur_beat = 1.0
is_first_match = True
for el in layer.getDescendants():
if el.hasAttribute("dur") and not el.hasAttribute("grace"):
dur = self._calculateDur(el, meter)
# TODO still problems with non-consecutive beat ranges
# e.g. @1@3
# exclude descendants at and in between tstamps
if cur_beat >= tstamp_first:
# We round to 4 decimal places to avoid issues caused by
# something unusual with the data
if m_pos is None:
raise BadApiRequest(
"Could not locate measure after new score definition")
# Process for beat data if the scoreDef defines meter
count_att = sd.getAttribute("meter.count")
unit_att = sd.getAttribute("meter.unit")
if count_att and unit_att:
beats[str(m_pos)] = {"count": int(count_att.getValue())}
beats[str(m_pos)]["unit"] = int(unit_att.getValue())
else:
count_elm = sd.getDescendantsByName("meterSig")
if count_elm:
if len(count_elm) > 1:
raise BadApiRequest("Mixed meter is not supported")
count = count_elm[0].getAttribute("count")
unit = count_elm[0].getAttribute("unit")
if count and unit:
beats[str(m_pos)] = {"count" : int(count.getValue())}
beats[str(m_pos)]["unit"] = int(unit.getValue())
else:
raise BadApiRequest(
"Could not locate meter and compute beats")
# Process for staff data if this scoreDef defines staves
if len(sd.getChildrenByName("staffGrp")) > 0:
# Get labels of staffDef and add them to the dictionary
staffDefs = sd.getDescendantsByName("staffDef")
labels = []
for staffDef in staffDefs: