Changeset 843

Show
Ignore:
Timestamp:
06/15/07 16:26:34 (2 years ago)
Author:
thomase
Message:

Magically expand shorthand references to RE, NPauly, KlPauly? and PECS

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • BADataMunger/trunk/placesaver.py

    r842 r843  
     1import re 
    12from os.path import join 
    23from StringIO import StringIO 
     
    1112TEI = "http://www.tei-c.org/ns/1.0" 
    1213XHTML = "http://www.w3.org/1999/xhtml" 
     14 
    1315 
    1416periods = { 
     
    8890        tag_refs.append(tag_bibl) 
    8991        # now, any other references  
     92        magicrefs = {} 
     93        if len(place.placenames) == 1: 
     94            for ref in place.references: 
     95                magicrefs[ref] = refmagic(ref, place.placenames[0]) 
     96        elif len(place.placenames) > 1: 
     97            for pn in place.placenames: 
     98                for ref in pn.references: 
     99                    magicrefs[ref] = refmagic(ref, pn) 
     100                    #print "bing: %s == %s" % (ref.encode('ascii', 'backslashreplace'), refmagic(ref, pn).encode('ascii', 'backslashreplace')) 
     101 
     102        if len(magicrefs) == 0 and len(place.references) > 0: 
     103            for ref in place.references: 
     104                magicrefs[ref] = ref 
     105                             
    90106        for ref in place.references: 
    91107            tag_bibl = etree.Element("{%s}bibl" % TEI) 
    92             tag_bibl.text = ref 
     108            try: 
     109                tag_bibl.text = magicrefs[ref] 
     110            except KeyError: 
     111                print 'KeyError %s' % ref.encode('ascii', 'backslashreplace') 
     112                print magicrefs 
    93113            tag_refs.append(tag_bibl) 
    94114        ge.append(tag_refs) 
     
    174194                for ref in pn.references: 
    175195                    tag_bibl = etree.Element("{%s}bibl" % TEI) 
    176                     tag_bibl.text = ref 
     196                    tag_bibl.text = refmagic(ref, pn) 
    177197                    tag_refs.append(tag_bibl) 
    178198                     
     
    360380    return result 
    361381     
    362          
     382def refmagic(ref, pn): 
     383    if ref == "RE": 
     384        result = "RE %s" % pn.name 
     385    elif ref == "NPauly": 
     386        result = "NPauly %s" % pn.name 
     387    elif ref == "KlPauly": 
     388        result = "KlPauly %s" % pn.name 
     389    elif ref == "PECS": 
     390        result = "PECS %s" % pn.name 
     391    else: 
     392        result = "" 
     393        ptrns = ["(RE) (\d+)", "(NPauly) (\d+)"] 
     394        for ptrn in ptrns: 
     395            m = re.match(ptrn, ref) 
     396            if m: 
     397                result = "%s %s %s" % (m.group(1), pn.name, m.group(2)) 
     398                break 
     399        if len(result) == 0: 
     400            result = ref 
     401    return result