def test_getpositionindocument(self): m = MeiElement("mei") m1 = MeiElement("music") musicid = m1.id b1 = MeiElement("body") s1 = MeiElement("staff") n1 = MeiElement("note") noteid = n1.id n2 = MeiElement("note") n3 = MeiElement("note") n4 = MeiElement("note") note4id = n4.id m.addChild(m1) m1.addChild(b1) b1.addChild(s1) s1.addChild(n1) s1.addChild(n2) s1.addChild(n3) doc = MeiDocument() doc.root = m self.assertEqual(4, n1.getPositionInDocument()) # an unattached element will return -1 self.assertEqual(-1, n4.getPositionInDocument())
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)
def test_elementsbyname(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") n4 = MeiElement("note") mei.addChild(mus) mus.addChild(body) body.addChild(staff) body.addChild(staff2) staff.addChild(n1) staff.addChild(n2) staff.addChild(n3) staff2.addChild(n4) doc = MeiDocument() doc.root = mei notes = doc.getElementsByName("note") self.assertEqual(4, len(notes)) rests = doc.getElementsByName("rest") self.assertEqual(0, len(rests)) n5 = MeiElement("note") staff2.addChild(n5) notes_new = doc.getElementsByName('note') self.assertEqual(5, len(notes_new))
def test_elementsbyname(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") n4 = MeiElement("note") mei.addChild(mus) mus.addChild(body) body.addChild(staff) body.addChild(staff2) staff.addChild(n1) staff.addChild(n2) staff.addChild(n3) staff2.addChild(n4) doc = MeiDocument() doc.root = mei notes = doc.getElementsByName("note") self.assertEqual(4, len(notes)) rests = doc.getElementsByName("rest") self.assertEqual(0, len(rests)) n5 = MeiElement("note") staff2.addChild(n5) notes_new = doc.getElementsByName('note') self.assertEqual(5, len(notes_new))
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)
def test_getpositionindocument(self): m = MeiElement("mei") m1 = MeiElement("music") musicid = m1.id b1 = MeiElement("body") s1 = MeiElement("staff") n1 = MeiElement("note") noteid = n1.id n2 = MeiElement("note") n3 = MeiElement("note") n4 = MeiElement("note") note4id = n4.id m.addChild(m1) m1.addChild(b1) b1.addChild(s1) s1.addChild(n1) s1.addChild(n2) s1.addChild(n3) doc = MeiDocument() doc.root = m self.assertEqual(4, n1.getPositionInDocument()) # an unattached element will return -1 self.assertEqual(-1, n4.getPositionInDocument())
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))
def test_documentwritefailure(self): doc = MeiDocument() root = MeiElement("mei") root.id = "myid" doc.root = root with self.assertRaises(FileWriteFailureException) as cm: ret = documentToFile(doc, "C:/StupidPath") self.assertTrue(isinstance(cm.exception, FileWriteFailureException))
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)
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)
def test_flattenedtree(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") doc = MeiDocument() mei.addChild(mus) doc.root = mei mus.addChild(body) body.addChild(staff) body.addChild(staff2) staff.addChild(n1) staff.addChild(n2) staff2.addChild(n3) doc.lookBack(n2, "mei") self.assertEqual(8, len(doc.getFlattenedTree())) staff.removeChild(n2) self.assertEqual(7, len(doc.getFlattenedTree())) self.assertEqual(staff2, doc.getFlattenedTree()[5]) staff.removeChildrenWithName("note") self.assertEqual(6, len(doc.getFlattenedTree())) body.deleteAllChildren() self.assertEqual(3, len(doc.getFlattenedTree())) children = MeiElementList() staff3 = MeiElement("staff") staff4 = MeiElement("staff") children.append(staff3) children.append(staff4) body.children = children self.assertEqual(5, len(doc.getFlattenedTree())) elements = [mei, mus, body, staff3, staff4] for i,el in enumerate(doc.getFlattenedTree()): self.assertEqual(elements[i], doc.getFlattenedTree()[i])
def test_flattenedtree(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") doc = MeiDocument() mei.addChild(mus) doc.root = mei mus.addChild(body) body.addChild(staff) body.addChild(staff2) staff.addChild(n1) staff.addChild(n2) staff2.addChild(n3) doc.lookBack(n2, "mei") self.assertEqual(8, len(doc.getFlattenedTree())) staff.removeChild(n2) self.assertEqual(7, len(doc.getFlattenedTree())) self.assertEqual(staff2, doc.getFlattenedTree()[5]) staff.removeChildrenWithName("note") self.assertEqual(6, len(doc.getFlattenedTree())) body.deleteAllChildren() self.assertEqual(3, len(doc.getFlattenedTree())) children = MeiElementList() staff3 = MeiElement("staff") staff4 = MeiElement("staff") children.append(staff3) children.append(staff4) body.children = children self.assertEqual(5, len(doc.getFlattenedTree())) elements = [mei, mus, body, staff3, staff4] for i, el in enumerate(doc.getFlattenedTree()): self.assertEqual(elements[i], doc.getFlattenedTree()[i])
def test_exportcomment(self): doc = MeiDocument() root = MeiElement("mei") root.id = "myid" doc.root = root comment = MeiElement("_comment") comment.value = "comment" comment.tail = "t" root.addChild(comment) expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<mei xml:id=\"myid\" xmlns=\"http://www.music-encoding.org/ns/mei\" meiversion=\"2013\">\n\t<!--comment-->t</mei>\n" ret = documentToText(doc) self.assertEqual(expected, ret)
def test_exportcomment(self): doc = MeiDocument() root = MeiElement("mei") root.id = "myid" doc.root = root comment = MeiElement("_comment") comment.value = "comment" comment.tail = "t" root.addChild(comment) expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<mei xml:id=\"myid\" xmlns=\"http://www.music-encoding.org/ns/mei\" meiversion=\"2013\">\n\t<!--comment-->t</mei>\n" ret = documentToText(doc) self.assertEqual(expected, ret)
def test_exportnamespace(self): doc = MeiDocument() root = MeiElement("mei") root.id = "myid" doc.root = root xlink = MeiNamespace("xlink", "http://www.w3.org/1999/xlink") attr = MeiAttribute(xlink, "title", "my awesome thing") root.addAttribute(attr) expected = "<?xml version=\"1.0\"?>\n<mei xmlns=\"http://www.music-encoding.org/ns/mei\" \ xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid\" xlink:title=\"my awesome thing\" meiversion=\"2013\"/>\n"; ret = XmlExport.meiDocumentToText(doc) self.assertEqual(expected, ret)
def test_exportvalueandtail(self): doc = MeiDocument() root = MeiElement("mei") root.id = "myid" doc.root = root note = MeiElement("note") note.id = "noteid" note.value = "value" note.tail = "tail" root.addChild(note) expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<mei xml:id=\"myid\" xmlns=\"http://www.music-encoding.org/ns/mei\" meiversion=\"2013\">\n\t<note xml:id=\"noteid\">value</note>tail</mei>\n" ret = documentToText(doc) self.assertEqual(expected, ret)
def test_exportvalueandtail(self): doc = MeiDocument() root = MeiElement("mei") root.id = "myid" doc.root = root note = MeiElement("note") note.id = "noteid" note.value = "value" note.tail = "tail" root.addChild(note) expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<mei xml:id=\"myid\" xmlns=\"http://www.music-encoding.org/ns/mei\" meiversion=\"2013\">\n\t<note xml:id=\"noteid\">value</note>tail</mei>\n" ret = documentToText(doc) self.assertEqual(expected, ret)
def test_exportProcessingInstructions(self): procinst = XmlInstructions() xpi1 = XmlProcessingInstruction("xml-model", "href=\"mei-2012.rng\" type=\"application/xml\" schematypens=\"http://purl.oclc.org/dsdl/schematron\"") xpi2 = XmlProcessingInstruction("xml-stylesheet", "href=\"mei-2012.rng\" type=\"application/xml\" schematypens=\"http://purl.oclc.org/dsdl/schematron\"") procinst.extend([xpi1, xpi2]) doc = MeiDocument() root = MeiElement("mei") root.id = "myid" doc.root = root ret = XmlExport.meiDocumentToText(doc, procinst) expected = "<?xml version=\"1.0\"?>\n<?xml-model href=\"mei-2012.rng\" type=\"application/xml\" \ schematypens=\"http://purl.oclc.org/dsdl/schematron\"?>\n<?xml-stylesheet href=\"mei-2012.rng\" type=\"application/xml\" \ schematypens=\"http://purl.oclc.org/dsdl/schematron\"?>\n<mei xmlns=\"http://www.music-encoding.org/ns/mei\" \ xml:id=\"myid\" meiversion=\"2013\"/>\n" self.assertEqual(expected, ret)
def test_elementbyid(self): mei = MeiElement("mei") mus = MeiElement("music") body = MeiElement("body") staff = MeiElement("staff") staff2 = MeiElement("staff") n1 = MeiElement("note") wantedId = n1.id n2 = MeiElement("note") n3 = MeiElement("note") n4 = MeiElement("note") mei.addChild(mus) mus.addChild(body) body.addChild(staff) body.addChild(staff2) staff.addChild(n1) staff.addChild(n2) staff.addChild(n3) staff2.addChild(n4) doc = MeiDocument() self.assertEqual(None, doc.getElementById(wantedId)) doc.root = mei self.assertEqual(n1, doc.getElementById(wantedId)) self.assertEqual(None, doc.getElementById("unknownID")) n5 = MeiElement("note") newid = n5.id staff2.addChild(n5) self.assertEqual(n5, doc.getElementById(newid)) staff2.removeChild(n5) self.assertEqual(None, doc.getElementById(newid))
def test_elementbyid(self): mei = MeiElement("mei") mus = MeiElement("music") body = MeiElement("body") staff = MeiElement("staff") staff2 = MeiElement("staff") n1 = MeiElement("note") wantedId = n1.id n2 = MeiElement("note") n3 = MeiElement("note") n4 = MeiElement("note") mei.addChild(mus) mus.addChild(body) body.addChild(staff) body.addChild(staff2) staff.addChild(n1) staff.addChild(n2) staff.addChild(n3) staff2.addChild(n4) doc = MeiDocument() self.assertEqual(None, doc.getElementById(wantedId)) doc.root = mei self.assertEqual(n1, doc.getElementById(wantedId)) self.assertEqual(None, doc.getElementById("unknownID")) n5 = MeiElement("note") newid = n5.id staff2.addChild(n5) self.assertEqual(n5, doc.getElementById(newid)) staff2.removeChild(n5) self.assertEqual(None, doc.getElementById(newid))
def generate_base_document(): ''' Generates a generic template for an MEI document for neume notation. Currently a bit of this is hardcoded and should probably be made more customizable. ''' meiDoc = MeiDocument("4.0.0") mei = MeiElement("mei") mei.addAttribute("meiversion", "4.0.0") meiDoc.root = mei # placeholder meiHead meihead = MeiElement('meiHead') mei.addChild(meihead) fileDesc = MeiElement('fileDesc') meihead.addChild(fileDesc) titleSt = MeiElement('titleStmt') fileDesc.addChild(titleSt) title = MeiElement('title') titleSt.addChild(title) title.setValue('MEI Encoding Output (%s)' % __version__) pubStmt = MeiElement('pubStmt') fileDesc.addChild(pubStmt) music = MeiElement("music") mei.addChild(music) facs = MeiElement("facsimile") music.addChild(facs) surface = MeiElement("surface") facs.addChild(surface) body = MeiElement('body') music.addChild(body) mdiv = MeiElement('mdiv') body.addChild(mdiv) score = MeiElement('score') mdiv.addChild(score) scoreDef = MeiElement('scoreDef') score.addChild(scoreDef) staffGrp = MeiElement('staffGrp') scoreDef.addChild(staffGrp) staffDef = MeiElement('staffDef') staffGrp.addChild(staffDef) # these hardcoded attributes define a single staff with 4 lines, neume notation, with a default c clef staffDef.addAttribute('n', '1') staffDef.addAttribute('lines', '4') staffDef.addAttribute('notationtype', 'neume') staffDef.addAttribute('clef.line', '3') staffDef.addAttribute('clef.shape', 'C') section = MeiElement('section') score.addChild(section) staff = MeiElement('staff') section.addChild(staff) layer = MeiElement('layer') staff.addChild(layer) return meiDoc, surface, layer
def test_getroot(self): doc = MeiDocument() mei = MeiElement("mei") doc.root = mei self.assertEqual(mei, doc.root)
def test_getroot(self): doc = MeiDocument() mei = MeiElement("mei") doc.root = mei self.assertEqual(mei, doc.root)