示例#1
0
def switch_tie(meifile, tstamp = False, keep_id = False):
    ''' Changes all ties expressed with attributes into elements. 
    If tstamp is set, it will attempt to generate tstamps instead of startid/endid pairs.
    
    @TODO
    At some point search() will support
    passing in args will narrow down the search by only retrieving objects with that attribute.
    Make sure to update this function when that happens 
    '''
    
    meifile_flat = meifile.flat()
    
    for n in meifile_flat:
        if n.has_attribute('tie'):
            if n.tie=='i' or n.tie=='m': #one tie element for each tie!
                #get ancestor measure
                measure = n.ancestor_by_name('measure')
                #create a tie element
                tie = mod.tie_()
                
                #determine attributes according to args
                atts = {'xml:id':generate_mei_id()}
                if tstamp:
                    atts['tstamp'] =  time.id_to_tstamp(n)
                if keep_id or (not keep_id and not tstamp):
                    atts['startid'] = n.id
                    
                #add attributes to tie
                tie.attributes = atts
                
                #add tie to measure
                measure.add_child(tie)
        #remove tie attribute
        n.remove_attribute('tie')
示例#2
0
def create_mei(filename):
	# build new mei file
	meifile=MeiDocument.MeiDocument()
	mei=mod.mei_()
	
	# header
	meihead=mod.meihead_()
	filedesc=mod.filedesc_()
	titlestmt=mod.titlestmt_()
	title=mod.title_()
	pubstmt=mod.pubstmt_()
	
	meihead.add_child(filedesc)
	filedesc.add_children([titlestmt, pubstmt])
	titlestmt.add_child(title)
	
	# music - facsimile, layout, body
	music=mod.music_()
	
	facsimile=mod.facsimile_()
	facsimile.id=generate_mei_id()
	surface=mod.surface_()
	surface.id=generate_mei_id()
	graphic=mod.graphic_()
	graphic.id=generate_mei_id()
	graphic.attributes={'xlink:href':'%s_original_image.tiff' % (filename,)}
	
	facsimile.add_child(surface)
	surface.add_child(graphic)
	
	layout=mod.layout_()
	layout.id=generate_mei_id()
	page=mod.page_()
	page.id=generate_mei_id()
	page.attributes={'n':filename}
	
	layout.add_child(page)
	
	body=mod.body_()
	mdiv=mod.mdiv_()
	mdiv.attributes={'type':'solesmes'}
	score=mod.score_()
	section=mod.section_()
	pb=mod.pb_()
	pb.id=generate_mei_id()
	pb.attributes={'pageref':page.id}
	body.add_child(mdiv)
	mdiv.add_child(score)
	score.add_child(section)
	section.add_child(pb)
	
	music.add_children([facsimile, layout, body])
		
	mei.add_children([meihead, music])
	
	meifile.addelement(mei)
	
	return meifile
示例#3
0
def add_text_lines(hocrfile, surface, section):
	"""
	helper method that adds lines in hocr file to 'surface' and 'section' in mei file
	"""
	div=mod.div_()
	div.id=generate_mei_id()
	lg=mod.lg_()
	lg.id=generate_mei_id()
	section.add_child(div)
	div.add_child(lg)
	for line in getlines(hocrfile):
		# for each line: make new zone and l objects, add zone to surface
		zone=mod.zone_()
		zone.id=generate_mei_id()
		zone.ulx=line['bbox'][0]
		zone.uly=line['bbox'][1]
		zone.lrx=line['bbox'][2]
		zone.lry=line['bbox'][3]
		l=mod.l_()
		l.id=generate_mei_id()
		l.facs=zone.id
		l.value=correct_text(line)
		lg.add_child(l)
		surface.add_child(zone)
示例#4
0
 def _articulations(self):
     if len(self.__articulations) > 1:
         # create elements & clean up attributes
         # remove all articulation children for safety
         self.remove_children('artic')
         self.remove_attribute('artic')
         artc = []
         for art in self.__articulations:
             a = artic_()
             a.id = generate_mei_id() # give it an id.
             a.attributes = {'artic': art}
             artc.append(a)
         self.add_children(artc)
     elif len(self.__articulations) == 1:
         # create an attribute & clean up any children
         self.attributes = {'artic': self.__articulations[0]}
         self.remove_children('artic')
     else:
         # clean up everything to do with articulations.
         self.remove_children('artic')
         self.remove_attribute('artic')
         self.__articulations = []
示例#5
0
def _xml_to_mei(el):
    """ Helper function for converting etree-parsed XML objects to a nested 
        set of MeiElements.
    """
    
    # etree automatically appends the namespace to every element. We need to 
    # strip that off.
    if isinstance(el, etree._Comment):
        obj = MeiComment.MeiComment(el.text)
        return obj
    
    ns_tag = el.tag.split('}')
    tagname = ns_tag[-1]
    ns = ns_tag[0].strip('{')
    
    # create the object name.
    objname = "{0}_".format(tagname)
    obj = getattr(mod, objname)(namespace=ns)
    
    if el.text and el.text.strip() != '':
        tx = ""
        for n in el.itertext():
            tx += " ".join(n.split())
            tx += " "
        obj.svalue = tx
    
    if el.text is not None:
        obj.value = el.text
    
    if el.tail is not None:
        obj.tail = el.tail
    
    # set the attributes
    d = {}
    if el.items():
        for k,v in el.items():
            # if the attribute has a namespace, be sure to convert it to its
            # prefix
            d[ns_to_prefix(k)] = v        
    
    # importing should *always* implement something like this.
    # This preserves existing XML:IDs, but also supplies them if they do not exist.
    # We set them as an attribute to support their existence as an XML attribute, but
    # the MeiElement catches this and also sets it to that item's ID.
    if "xml:id" not in d.keys():
        d['xml:id'] = generate_mei_id()
        
    obj.attributes = d
    
    # add any children.
    c = list(el)
    if len(c) > 0:
        # lg.debug("Adding children: {0}".format(tagname))
        # loopdy-loopdy! This calls itself for any children components found.
        m = map(_xml_to_mei, c)
        #lg.debug('Object: {0}; children {1}'.format(obj, m))
        obj.add_children(m)
        
    return obj