def getMEIContent(self, dumpVisualization=False): """Extract zones and neumes from the source file""" neumeElements = [] zones = [] for elem in etree.parse( self.xmlFile).xpath('/gamera-database/glyphs/glyph'): # Get the relevant attributes from the glyph element startX = int(elem.get('ulx')) endX = startX + int(elem.get('ncols')) startY = int(elem.get('uly')) endY = startY + int(elem.get('nrows')) curNeumeName = elem.xpath('string(./ids/id/@name)') # Create the MEI neume element newNeumeElement = MeiElement('neume') neumeElements.append(newNeumeElement) newNeumeElement.id = generate_MEI_ID() splitName = curNeumeName[curNeumeName.find(".") + 1:] if (splitName in self.neumeNames): newNeumeElement.addAttribute( MeiAttribute('name', self.neumeNames[splitName])) elif len(splitName) < 3: newNeumeElement.addAttribute( MeiAttribute('name', "Letter " + splitName.upper())) else: newNeumeElement.addAttribute(MeiAttribute('name', splitName)) zoneID = generate_MEI_ID() newNeumeElement.addAttribute(MeiAttribute('facs', zoneID)) zones.append(Zone(zoneID, startX, startY, endX, endY)) zoneElements = [] for zone in self._sortZones(zones, dumpVisualization=dumpVisualization): newZoneElement = MeiElement('zone') zoneElements.append(newZoneElement) newZoneElement.id = zone.id newZoneElement.addAttribute(MeiAttribute('ulx', str(zone.startX))) newZoneElement.addAttribute(MeiAttribute('uly', str(zone.startY))) newZoneElement.addAttribute(MeiAttribute('lrx', str(zone.endX))) newZoneElement.addAttribute(MeiAttribute('lry', str(zone.endY))) return zoneElements, neumeElements
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)
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 getMEIContent(self, dumpVisualization=False): """Extract zones and neumes from the source file""" neumeElements = [] zones = [] for elem in etree.parse(self.xmlFile).xpath('/gamera-database/glyphs/glyph'): # Get the relevant attributes from the glyph element startX = int(elem.get('ulx')) endX = startX + int(elem.get('ncols')) startY = int(elem.get('uly')) endY = startY + int(elem.get('nrows')) curNeumeName = elem.xpath('string(./ids/id/@name)') # Create the MEI neume element newNeumeElement = MeiElement('neume') neumeElements.append(newNeumeElement) newNeumeElement.id = generate_MEI_ID() splitName = curNeumeName[curNeumeName.find(".") + 1:] if(splitName in self.neumeNames): newNeumeElement.addAttribute(MeiAttribute('name', self.neumeNames[splitName])) elif len(splitName) < 3: newNeumeElement.addAttribute(MeiAttribute('name', "Letter " + splitName.upper())) else: newNeumeElement.addAttribute(MeiAttribute('name', splitName)) zoneID = generate_MEI_ID() newNeumeElement.addAttribute(MeiAttribute('facs', zoneID)) zones.append(Zone(zoneID, startX, startY, endX, endY)) zoneElements = [] for zone in self._sortZones(zones, dumpVisualization=dumpVisualization): newZoneElement = MeiElement('zone') zoneElements.append(newZoneElement) newZoneElement.id = zone.id newZoneElement.addAttribute(MeiAttribute('ulx', str(zone.startX))) newZoneElement.addAttribute(MeiAttribute('uly', str(zone.startY))) newZoneElement.addAttribute(MeiAttribute('lrx', str(zone.endX))) newZoneElement.addAttribute(MeiAttribute('lry', str(zone.endY))) return zoneElements, neumeElements
def test_segfaultdebug(self): variants_list = [ ('3', 'VARIANT', '2', 'RISM1560-06'), ('6', 'VARIANT', '5', 'RISM1560-06') ] res = documentFromFile(os.path.join("test", "testdocs", "DC1317E.mei")) MEI_doc = res.getMeiDocument() mei = MeiElement('mei') # print(mei) head_score = chain_elems(mei, ['meiHead', 'workDesc', 'work', 'incip', 'score']) stg = chain_elems(mei, ['music', 'body', 'mdiv', 'score', 'scoreDef', 'staffGrp']) sd1 = MeiElement('staffDef') sd2 = MeiElement('staffDef') sd3 = MeiElement('staffDef') sd4 = MeiElement('staffDef') sd5 = MeiElement('staffDef') sd6 = MeiElement('staffDef') sd1.addAttribute('n', '1') sd2.addAttribute('n', '2') sd3.addAttribute('n', '3') sd4.addAttribute('n', '4') sd5.addAttribute('n', '5') sd6.addAttribute('n', '6') stg.addChild(sd1) stg.addChild(sd2) stg.addChild(sd3) stg.addChild(sd4) stg.addChild(sd5) stg.addChild(sd6) MEI_tree = MEI_doc.getRootElement() # MEI_tree2 = mei # print(MEI_tree) # print MEI_tree2 scoreDefs = MEI_tree.getDescendantsByName('scoreDef') # print(scoreDefs) mainScoreDef = MeiElement(scoreDefs[0]) mainScoreDef.id = "blahblahblah" meiHead = MEI_tree.getDescendantsByName('meiHead')[0] head_score = chain_elems(meiHead, ['workDesc', 'work', 'incip', 'score']) head_score.addChild(mainScoreDef) for variants_list_item in variants_list: stDefs = mainScoreDef.getDescendantsByName('staffDef') for staffDef in stDefs: if staffDef.hasAttribute('n') and staffDef.getAttribute('n').value == variants_list_item[0]: print("Removing staffDef n=" + staffDef.getAttribute('n').getValue()) staffDef.parent.removeChild(staffDef) # head_score.addChild(mainScoreDef) self.assertEqual(8, len(head_score.getDescendantsByName('staffDef')))
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 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)
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_exportnamespace2(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") music = MeiElement("music") music.id = "musid" music.addAttribute(attr) music.value = "mus!" root.addChild(music) 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\" meiversion=\"2013\">\n <music \ xml:id=\"musid\" xlink:title=\"my awesome thing\">mus!</music>\n</mei>\n" ret = XmlExport.meiDocumentToText(doc) self.assertEqual(expected, ret)
def _create_alteration_element(self): # accid = mod.accid_() accid = MeiElement("accid") accid.id = self._idgen() if self.glyph['form'] is "sharp": accid.addAttribute("accid", "s") elif self.glyph['form'] is "flat": accid.addAttribute("accid", "f") # zone = self._create_zone_element() # note.facs = zone.id return accid
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_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_segfaultdebug(self): variants_list = [('3', 'VARIANT', '2', 'RISM1560-06'), ('6', 'VARIANT', '5', 'RISM1560-06')] res = documentFromFile(os.path.join("test", "testdocs", "DC1317E.mei")) MEI_doc = res.getMeiDocument() mei = MeiElement('mei') # print(mei) head_score = chain_elems( mei, ['meiHead', 'workDesc', 'work', 'incip', 'score']) stg = chain_elems( mei, ['music', 'body', 'mdiv', 'score', 'scoreDef', 'staffGrp']) sd1 = MeiElement('staffDef') sd2 = MeiElement('staffDef') sd3 = MeiElement('staffDef') sd4 = MeiElement('staffDef') sd5 = MeiElement('staffDef') sd6 = MeiElement('staffDef') sd1.addAttribute('n', '1') sd2.addAttribute('n', '2') sd3.addAttribute('n', '3') sd4.addAttribute('n', '4') sd5.addAttribute('n', '5') sd6.addAttribute('n', '6') stg.addChild(sd1) stg.addChild(sd2) stg.addChild(sd3) stg.addChild(sd4) stg.addChild(sd5) stg.addChild(sd6) MEI_tree = MEI_doc.getRootElement() # MEI_tree2 = mei # print(MEI_tree) # print MEI_tree2 scoreDefs = MEI_tree.getDescendantsByName('scoreDef') # print(scoreDefs) mainScoreDef = MeiElement(scoreDefs[0]) mainScoreDef.id = "blahblahblah" meiHead = MEI_tree.getDescendantsByName('meiHead')[0] head_score = chain_elems(meiHead, ['workDesc', 'work', 'incip', 'score']) head_score.addChild(mainScoreDef) for variants_list_item in variants_list: stDefs = mainScoreDef.getDescendantsByName('staffDef') for staffDef in stDefs: if staffDef.hasAttribute('n') and staffDef.getAttribute( 'n').value == variants_list_item[0]: print("Removing staffDef n=" + staffDef.getAttribute('n').getValue()) staffDef.parent.removeChild(staffDef) # head_score.addChild(mainScoreDef) self.assertEqual(8, len(head_score.getDescendantsByName('staffDef')))
def _initMEI(self): """Initialize a new MEI document Sets the attributes meiDoc, surface, and initLayer """ self.meiDoc = MeiDocument() root = MeiElement("mei") root.id = generate_MEI_ID() self.meiDoc.root = root #needs meiHead here meiHead = MeiElement('meiHead') fileDesc = MeiElement('fileDesc') titleStmt = MeiElement('titleStmt') title = MeiElement('title') pubStmt = MeiElement('pubStmt') date = MeiElement('date') encodingDesc = MeiElement('encodingDesc') projectDesc = MeiElement('projectDesc') p = MeiElement('p') music = MeiElement('music') facsimile = MeiElement('facsimile') self.surface = MeiElement('surface') # Label the surface with the name of the input file, which could help # identify the original image label = os.path.basename(os.path.splitext(self.xmlFile)[0]) self.surface.addAttribute(MeiAttribute('label', label)) #systems get added to page #neumes get added to systems body = MeiElement('body') mdiv = MeiElement('mdiv') pages = MeiElement('pages') page = MeiElement('page') page.id = generate_MEI_ID() initSystem = MeiElement('system') initSystem.id = generate_MEI_ID() initStaff = MeiElement('staff') initStaff.id = generate_MEI_ID() self.initLayer = MeiElement('layer') self.initLayer.id = generate_MEI_ID() root.addChild(meiHead) meiHead.addChild(fileDesc) fileDesc.addChild(titleStmt) titleStmt.addChild(title) fileDesc.addChild(pubStmt) pubStmt.addChild(date) meiHead.addChild(encodingDesc) encodingDesc.addChild(projectDesc) projectDesc.addChild(p) root.addChild(music) music.addChild(facsimile) facsimile.addChild(self.surface) music.addChild(body) body.addChild(mdiv) mdiv.addChild(pages) pages.addChild(page) page.addChild(initSystem) initSystem.addChild(initStaff) initStaff.addChild(self.initLayer)
def add_editor(titleStmt, ali): existing = titleStmt.getDocument().getElementById(adi[3]) if not existing: editor = MeiElement('editor') editor.id = ali[3] titleStmt.addChild(editor)