Ejemplo n.º 1
0
 def add_source(sourceDesc, adi):
     existing = sourceDesc.getDocument().getElementById(adi[3])
     if not existing:
         source = MeiElement('source')
         source.id = adi[3]
         source.addAttribute('type', adi[1])
         sourceDesc.addChild(source)
Ejemplo n.º 2
0
    def test_getattributeval(self):
        measure = MeiElement("measure")
        staff = MeiElement("staff")
        measure.addAttribute("n", "2")

        self.assertEqual(utilities.get_attribute_val(measure, "n"), "2")
        self.assertEqual(utilities.get_attribute_val(staff, "n", "1"), "1")
Ejemplo n.º 3
0
def make_invisible_space(MEI_tree, handle_mRest=False):
    """Turns all invisible notes, rests and mRests into
    <space> elements.
    """

    all_note_rest = MEI_tree.getDescendantsByName('note rest')
    all_mRest = MEI_tree.getDescendantsByName('mRest')

    # Replace notes and rests with spaces
    for item in all_note_rest:
        try:
            if item.getAttribute('visible').getValue() == 'false':
                space = MeiElement('space')
                attributes = item.getAttributes()
                for attr in attributes:
                    # Don't add octave or pitch attributes to space
                    if attr.getName() not in ['oct', 'pname']:
                        space.addAttribute(attr)
                # If mRest, calculate duration here?
                parent = item.getParent()
                parent.addChildBefore(item, space)
                parent.removeChild(item)
        except:  # doesn't have attribute `visible`
            pass
    # Replace mRests with nothing -- just remove them
    # Not currently supported by MEItoVexFlow
    if handle_mRest:
        for item in all_mRest:
            try:
                if item.getAttribute('visible').getValue() == 'false':
                    item.getParent().removeChild(item)
            except:  # doesn't have attribute `visible`
                pass
Ejemplo n.º 4
0
 def _create_graphic_element(self, imgfile):
     graphic = MeiElement("graphic")
     # xlink = MeiNamespace("xlink", "http://www.w3.org/1999/xlink")
     # ns_attr = MeiAttribute("xlink")
     graphic.addAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink")
     graphic.addAttribute("xlink:href", imgfile)
     return graphic
Ejemplo n.º 5
0
    def _create_staff_group(self, sg_list, staff_grp, n):
        '''
        Recursively create the staff group element from the parsed
        user input of the staff groupings
        '''
        if not sg_list:
            return staff_grp, n
        else:
            if type(sg_list[0]) is list:
                new_staff_grp, n = self._create_staff_group(sg_list[0], MeiElement('staffGrp'), n)
                staff_grp.addChild(new_staff_grp)
            else:
                # check for barthrough character
                if sg_list[0][-1] == '|':
                    # the barlines go through all the staves in the staff group
                    staff_grp.addAttribute('barthru', 'true')
                    # remove the barthrough character, should now only be an integer
                    sg_list[0] = sg_list[0][:-1]

                n_staff_defs = int(sg_list[0])
                # get current staffDef number
                for i in range(n_staff_defs):
                    staff_def = MeiElement('staffDef')
                    staff_def.addAttribute('n', str(n + i + 1))
                    staff_grp.addChild(staff_def)
                n += n_staff_defs

            return self._create_staff_group(sg_list[1:], staff_grp, n)
Ejemplo n.º 6
0
    def test_object_equality(self):
        el1 = MeiElement("note")
        el2 = MeiElement("accid")

        el1.addChild(el2)

        self.assertEqual(el1, el2.parent)
Ejemplo n.º 7
0
    def post(self, file):
        '''
        Add a dot ornament to a given element.
        '''

        neumeid = str(self.get_argument("id", ""))
        dot_form = str(self.get_argument("dotform", ""))

        # Bounding box
        ulx = str(self.get_argument("ulx", None))
        uly = str(self.get_argument("uly", None))
        lrx = str(self.get_argument("lrx", None))
        lry = str(self.get_argument("lry", None))

        mei_directory = os.path.abspath(conf.MEI_DIRECTORY)
        fname = os.path.join(mei_directory, file)
        self.mei = XmlImport.read(fname)

        punctum = self.mei.getElementById(neumeid)
        # check that a punctum element was provided
        if punctum.getName() == "neume" and punctum.getAttribute("name").getValue() == "punctum":
            note = punctum.getDescendantsByName("note")
            if len(note):
                # if a dot does not already exist on the note
                if len(note[0].getChildrenByName("dot")) == 0:
                    dot = MeiElement("dot")
                    dot.addAttribute("form", dot_form)
                    note[0].addChild(dot)

            self.update_or_add_zone(punctum, ulx, uly, lrx, lry)

        XmlExport.write(self.mei, fname)

        self.set_status(200)
Ejemplo n.º 8
0
        def parse_token(token):
            def parse_attrs(token):
                def parse_attrs_str(attrs_str):
                    res = []
                    attr_pairs = attrs_str.split(",")
                    for attr_pair in attr_pairs:
                        if attr_pair == '':
                            continue
                        name_val = attr_pair.split("=")
                        if len(name_val) > 1:
                            attr = MeiAttribute(name_val[0], name_val[1])
                            res.append(attr)
                        else:
                            logging.warning("get_descendants(): invalid attribute specifier in expression: " + expr)
                    return res
                m = re.search("\[(.*)\]", token)
                attrs_str = ""
                if m is not None:
                    attrs_str = m.group(1)
                return parse_attrs_str(attrs_str)

            m = re.search("^([^\[]+)", token)
            elem = MeiElement(m.group(1))
            attrs = parse_attrs(token)
            for attr in attrs:
                elem.addAttribute(attr)
            return elem
Ejemplo n.º 9
0
 def test_getattributeval(self):
     measure = MeiElement('measure')
     staff = MeiElement('staff')
     measure.addAttribute('n', '2')
     
     self.assertEqual(utilities.get_attribute_val(measure, 'n'), '2')
     self.assertEqual(utilities.get_attribute_val(staff, 'n', '1'), '1')
Ejemplo n.º 10
0
 def add_editor(titleStmt, ali):
     existing = titleStmt.getDocument().getElementById(adi[3])
     if not existing:
         editor = MeiElement('editor')
         editor.id = ali[3]
         # Using 'replace' simply to have more natural name for a person
         editor.addAttribute('type', adi[1].replace('ction', 'ctor'))
         titleStmt.addChild(editor)
Ejemplo n.º 11
0
    def get_new_zone(self, ulx, uly, lrx, lry):
        zone = MeiElement("zone")
        zone.addAttribute("ulx", str(ulx))
        zone.addAttribute("uly", str(uly))
        zone.addAttribute("lrx", str(lrx))
        zone.addAttribute("lry", str(lry))

        return zone
Ejemplo n.º 12
0
    def create_zone(self, ulx, uly, lrx, lry):
        zone = MeiElement("zone")
        zone.addAttribute("ulx", ulx)
        zone.addAttribute("uly", uly)
        zone.addAttribute("lrx", lrx)
        zone.addAttribute("lry", lry)

        return zone
Ejemplo n.º 13
0
 def test_documentwritefailure(self):
     doc = MeiDocument()
     root = MeiElement("mei")
     root.id = "myid"
     doc.root = root
     with self.assertRaises(FileWriteFailureException) as cm:
         ret = XmlExport.meiDocumentToFile(doc, "C:/StupidPath")
     self.assertTrue(isinstance(cm.exception, FileWriteFailureException))
Ejemplo n.º 14
0
    def test_exporttostring(self):
        doc = MeiDocument()
        root = MeiElement("mei")
        root.id = "myid"
        doc.root = root

        expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<mei xml:id=\"myid\" xmlns=\"http://www.music-encoding.org/ns/mei\" meiversion=\"2013\" />\n"
        ret = documentToText(doc)
        self.assertEqual(expected, ret)
Ejemplo n.º 15
0
    def _create_staff(self, n, zone):
        '''
        Create a staff element, and attach a zone reference to it
        '''

        staff = MeiElement('staff')
        staff.addAttribute('n', str(n))
        staff.addAttribute('facs', '#'+zone.getId())

        return staff
Ejemplo n.º 16
0
    def test_setdocument(self):
        m = MeiElement("mei")
        doc = MeiDocument()

        with self.assertRaises(DocumentRootNotSetException) as cm:
            m.setDocument(doc)
        self.assertTrue(isinstance(cm.exception, DocumentRootNotSetException))
        doc.setRootElement(m)

        self.assertEqual(doc.root, m)
Ejemplo n.º 17
0
    def create_new_division(self, type):
        '''
        Make a new division and return the MEI element.
        Attach the facs data to the division element
        '''

        division = MeiElement("division")
        division.addAttribute("form", type)

        return division
Ejemplo n.º 18
0
    def test_parent(self):
        el1 = MeiElement("mei")
        el2 = MeiElement("meiHead")
        el3 = MeiElement("music")

        el2.parent = el1
        el3.parent = el1

        self.assertEqual("mei", el2.parent.name)
        self.assertEqual("mei", el3.parent.name)
Ejemplo n.º 19
0
def wrap_whole_measure(staff, ALT_TYPE):
    """Enclose the entire contents of a staff in a measure
    inside the <app> element. Will be done if variants overlap
    in illegal ways.
    """
    rich_wrapper_name = 'app'
    rich_default_name = 'lem'
    if ALT_TYPE == EMENDATION:
        rich_wrapper_name = 'choice'
        rich_default_name = 'sic'
    old_layers = staff.getChildrenByName('layer')
    notelist = []
    if len(old_layers) > 0:
        old_layer = staff.getChildrenByName('layer')[0]
        notelist.extend(get_descendants(old_layer, 'note rest space mRest'))
        staff.removeChild(old_layer)
    new_layer = MeiElement('layer')
    rich_wrapper = MeiElement(rich_wrapper_name)
    rich_default_elem = MeiElement(rich_default_name)
    for note in notelist:
        rich_default_elem.addChild(note)
    rich_wrapper.addChild(rich_default_elem)
    new_layer.addChild(rich_wrapper)
    staff.addChild(new_layer)
    return rich_wrapper
Ejemplo n.º 20
0
    def test_documentpointers(self):
        mei = MeiElement("mei")
        mus = MeiElement("music")
        body = MeiElement("body")
        staff = MeiElement("staff")
        staff2 = MeiElement("staff")
        n1 = MeiElement("note")
        n2 = MeiElement("note")
        n3 = MeiElement("note")

        self.assertEqual(None, mei.document)
        mei.addChild(mus)
        self.assertEqual(None, mus.document)

        doc = MeiDocument()
        mus.addChild(body)
        doc.root = mei

        self.assertEqual(doc, mei.document)
        self.assertEqual(doc, mus.document)
        self.assertEqual(doc, body.document)

        self.assertEqual(None, staff.document)
        body.addChild(staff)
        self.assertEqual(doc, staff.document)
Ejemplo n.º 21
0
    def insert_puncta(self, nid, bboxes):
        '''
        Insert a punctum for each note in the reference neume
        before the reference neume in the MEI document.
        '''
        ref_neume = self.mei.getElementById(nid)
        parent = ref_neume.getParent()

        # get underlying notes
        notes = ref_neume.getDescendantsByName("note")
        nids = []
        for n, bb in zip(notes, bboxes):
            punctum = MeiElement("neume")
            punctum.addAttribute("name", "punctum")
            nc = MeiElement("nc")
            nc.addChild(n)
            punctum.addChild(nc)

            # add generated punctum id to return to client
            nids.append(punctum.getId())

            # add facs data for the punctum
            zone = self.get_new_zone(bb["ulx"], bb["uly"], bb["lrx"], bb["lry"])
            self.add_zone(punctum, zone)

            # insert the punctum before the reference neume
            parent.addChildBefore(ref_neume, punctum)

        return nids
Ejemplo n.º 22
0
 def test_removechild(self):
     p = MeiElement("layer")
     el1 = MeiElement("note")
     el2 = MeiElement("note")
     
     p.addChild(el1)
     p.addChild(el2)
     
     self.assertEqual(2, len(p.children))
     p.removeChild(el1)
     self.assertEqual(1, len(p.children))
Ejemplo n.º 23
0
 def create_note(dur, pname, octave, color=None):
     n = MeiElement('note')
     n.addAttribute('dur', dur)
     n.addAttribute('pname', pname)
     n.addAttribute('oct', octave)
     if color:
         n.addAttribute('color', color)
     return n
Ejemplo n.º 24
0
 def test_getnextmeasure(self):
     section = MeiElement('section')
     m1 = MeiElement('measure')
     sb = MeiElement('sb')
     m2 = MeiElement('measure')
     
     section.addChild(m1)
     section.addChild(sb)
     section.addChild(m2)
     
     self.assertEqual(utilities.get_next_measure(m1), m2)
     self.assertEqual(utilities.get_next_measure(m2), None)
Ejemplo n.º 25
0
    def _create_zone(self, ulx, uly, lrx, lry):
        '''
        Create a zone element
        '''

        zone = MeiElement('zone')
        zone.addAttribute('ulx', str(ulx))
        zone.addAttribute('uly', str(uly))
        zone.addAttribute('lrx', str(lrx))
        zone.addAttribute('lry', str(lry))

        return zone
Ejemplo n.º 26
0
    def test_children(self):
        el1 = MeiElement("mei")
        el2 = MeiElement("meiHead")
        el3 = MeiElement("music")

        self.assertEqual(0, len(el1.children))

        el1.addChild(el2)
        el1.addChild(el3)

        self.assertEqual("mei", el2.parent.name)
        self.assertTrue(el1.hasChildren("music"))
Ejemplo n.º 27
0
def change_arranger_element(MEI_tree):
	"""Changes all occurrence of the <arranger> tag to <editor>"""
	all_arranger = MEI_tree.getDescendantsByName('arranger')
	for arranger in all_arranger:
		parent = arranger.getParent()
		editor = MeiElement('editor')
		arranger_children = arranger.getChildren()
		arranger_value = arranger.getValue()
		for child in arranger_children:
			editor.addChild(child)
		editor.setValue(arranger_value)
		parent.addChild(editor)
		parent.removeChild(arranger)
Ejemplo n.º 28
0
    def _create_zone_element(self):
        zone = MeiElement("zone")
        # zone = mod.zone_()
        # zone.id = self._idgen()
        # zone.attributes = {'ulx': self.glyph['coord'][0], 'uly': self.glyph['coord'][1], \
        #                     'lrx': self.glyph['coord'][2], 'lry': self.glyph['coord'][3]}

        zone.addAttribute("ulx", str(self.glyph['coord'][0]))
        zone.addAttribute("uly", str(self.glyph['coord'][1]))
        zone.addAttribute("lrx", str(self.glyph['coord'][2]))
        zone.addAttribute("lry", str(self.glyph['coord'][3]))
        self.surface.addChild(zone)
        return zone
Ejemplo n.º 29
0
    def update_or_add_zone(self, punctum, ulx, uly, lrx, lry):
        facsid = punctum.getAttribute("facs").getValue()
        if facsid:
            # the zone exists already
            zone = self.mei.getElementById(facsid)
        else:
            # create a new zone
            zone = MeiElement("zone")

        # update bounding box data
        zone.addAttribute("ulx", ulx)
        zone.addAttribute("uly", uly)
        zone.addAttribute("lrx", lrx)
        zone.addAttribute("lry", lry)
Ejemplo n.º 30
0
    def _create_division_element(self):
        division = MeiElement("division")
        # division = mod.division_()
        # division.id = self._idgen()
        zone = self._create_zone_element()
        division.addAttribute("facs", str(zone.id))

        if self.glyph['form']:
            div = str(self.glyph['form'][0])
        else:
            div = "minor"

        division.addAttribute("form", div)
        return division
Ejemplo n.º 31
0
 def __init__(self):
     MeiElement.__init__(self, "instrGrp")
Ejemplo n.º 32
0
 def __init__(self):
     MeiElement.__init__(self, "label")
Ejemplo n.º 33
0
 def __init__(self):
     MeiElement.__init__(self, "dot")
Ejemplo n.º 34
0
 def __init__(self):
     MeiElement.__init__(self, "group")
Ejemplo n.º 35
0
 def __init__(self):
     MeiElement.__init__(self, "layerDef")
Ejemplo n.º 36
0
 def __init__(self):
     MeiElement.__init__(self, "abbr")
Ejemplo n.º 37
0
 def __init__(self):
     MeiElement.__init__(self, "castList")
Ejemplo n.º 38
0
 def __init__(self):
     MeiElement.__init__(self, "lb")
Ejemplo n.º 39
0
 def __init__(self):
     MeiElement.__init__(self, "castItem")
Ejemplo n.º 40
0
 def __init__(self):
     MeiElement.__init__(self, "fw")
Ejemplo n.º 41
0
 def __init__(self):
     MeiElement.__init__(self, "expansion")
Ejemplo n.º 42
0
 def __init__(self):
     MeiElement.__init__(self, "num")
Ejemplo n.º 43
0
 def __init__(self):
     MeiElement.__init__(self, "note")
Ejemplo n.º 44
0
 def __init__(self):
     MeiElement.__init__(self, "music")
Ejemplo n.º 45
0
 def __init__(self):
     MeiElement.__init__(self, "custos")
Ejemplo n.º 46
0
 def __init__(self):
     MeiElement.__init__(self, "hairpin")
Ejemplo n.º 47
0
 def __init__(self):
     MeiElement.__init__(self, "clefGrp")
Ejemplo n.º 48
0
 def __init__(self):
     MeiElement.__init__(self, "keySig")
Ejemplo n.º 49
0
 def __init__(self):
     MeiElement.__init__(self, "chord")
Ejemplo n.º 50
0
 def __init__(self):
     MeiElement.__init__(self, "dynam")
Ejemplo n.º 51
0
 def __init__(self):
     MeiElement.__init__(self, "instrDef")
Ejemplo n.º 52
0
 def __init__(self):
     MeiElement.__init__(self, "grpSym")
Ejemplo n.º 53
0
 def __init__(self):
     MeiElement.__init__(self, "castGrp")
Ejemplo n.º 54
0
 def __init__(self):
     MeiElement.__init__(self, "incip")
Ejemplo n.º 55
0
 def __init__(self):
     MeiElement.__init__(self, "edition")
Ejemplo n.º 56
0
 def __init__(self):
     MeiElement.__init__(self, "keyAccid")
Ejemplo n.º 57
0
 def __init__(self):
     MeiElement.__init__(self, "ending")
Ejemplo n.º 58
0
 def __init__(self):
     MeiElement.__init__(self, "identifier")
Ejemplo n.º 59
0
 def __init__(self):
     MeiElement.__init__(self, "mdiv")
Ejemplo n.º 60
0
 def __init__(self):
     MeiElement.__init__(self, "mei")