<s> import re <EOL> import uuid <EOL> from lxml . builder import ElementMaker <EOL> XSI_NS = '<STR_LIT>' <EOL> class MODS ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , prefix , config , db ) : <EOL> self . prefix = prefix <EOL> self . config = config <EOL> self . db = db <EOL> self . ns = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> self . schemas = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> def get_namespace ( self ) : <EOL> return self . ns [ self . prefix ] <EOL> def get_schema_location ( self ) : <EOL> return self . schemas [ self . prefix ] <EOL> def __call__ ( self , element , metadata ) : <EOL> data = metadata . record <EOL> MODS = ElementMaker ( namespace = self . ns [ '<STR_LIT>' ] , nsmap = self . ns ) <EOL> DAI = ElementMaker ( namespace = self . ns [ '<STR_LIT>' ] , nsmap = self . ns ) <EOL> GAL = ElementMaker ( namespace = self . ns [ '<STR_LIT>' ] , nsmap = self . ns ) <EOL> mods = MODS . mods ( version = "<STR_LIT>" ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) : <EOL> mods . append ( MODS . identifier ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> type = "<STR_LIT>" ) ) <EOL> for key , value in data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , { } ) . items ( ) : <EOL> mods . append ( MODS . identifier ( value , type = key ) ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT:title>' ) : <EOL> titleInfo = MODS . titleInfo ( <EOL> MODS . title ( data [ '<STR_LIT>' ] [ '<STR_LIT:title>' ] [ <NUM_LIT:0> ] ) <EOL> ) <EOL> titleInfo . attrib [ '<STR_LIT>' % self . ns [ '<STR_LIT>' ] ] = data [ '<STR_LIT>' ] . get ( <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> mods . append ( titleInfo ) <EOL> mods . append ( MODS . typeOfResource ( '<STR_LIT:text>' ) ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) : <EOL> mods . append ( MODS . genre ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT:url>' ) : <EOL> location_el = MODS . location ( MODS . url ( data [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] [ <NUM_LIT:0> ] , <EOL> usage = "<STR_LIT>" , <EOL> access = "<STR_LIT>" ) ) <EOL> for asset in data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ ] ) : <EOL> if asset . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> location_el . append ( MODS . url ( asset [ '<STR_LIT>' ] , <EOL> access = '<STR_LIT>' ) ) <EOL> mods . append ( location_el ) <EOL> public_assets = [ a for a in data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ ] ) <EOL> if a . get ( '<STR_LIT>' ) == '<STR_LIT>' ] <EOL> if len ( public_assets ) == <NUM_LIT:1> : <EOL> phys_descr_el = MODS . physicalDescription ( ) <EOL> if asset . get ( '<STR_LIT>' ) : <EOL> phys_descr_el . append ( MODS . internetMediaType ( asset [ '<STR_LIT>' ] ) ) <EOL> if asset . get ( '<STR_LIT>' ) : <EOL> try : <EOL> kbytes = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , <EOL> str ( int ( asset [ '<STR_LIT>' ] ) / <NUM_LIT> ) [ : : - <NUM_LIT:1> ] ) [ : : - <NUM_LIT:1> ] <EOL> phys_descr_el . append ( MODS . extent ( '<STR_LIT>' % kbytes ) ) <EOL> except : <EOL> pass <EOL> phys_descr_el . append ( MODS . digitalOrigin ( '<STR_LIT>' ) ) <EOL> mods . append ( phys_descr_el ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT:description>' ) : <EOL> mods . append ( MODS . abstract ( data [ '<STR_LIT>' ] [ '<STR_LIT:description>' ] [ <NUM_LIT:0> ] ) ) <EOL> for ctype in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> contributor_data = [ ] <EOL> for id in data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % ctype , [ ] ) : <EOL> contributor = self . db . get_metadata ( id ) <EOL> contributor [ '<STR_LIT:id>' ] = id <EOL> contributor_data . append ( contributor ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % ctype ) : <EOL> contributor_data = [ s for s in data [ '<STR_LIT>' ] [ <EOL> '<STR_LIT>' % ctype ] ] <EOL> if not contributor_data : <EOL> contributor_data = [ { '<STR_LIT:name>' : [ a ] } for a in data [ <EOL> '<STR_LIT>' ] . get ( ctype , [ ] ) ] <EOL> dai_list = [ ] <EOL> for contributor in contributor_data : <EOL> contributor_name = contributor . get ( '<STR_LIT:name>' , [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> unique_id = uuid . uuid4 ( ) . hex <EOL> if unique_id [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> unique_id = '<STR_LIT:_>' + unique_id <EOL> name = MODS . name ( <EOL> MODS . displayForm ( contributor_name ) , <EOL> type = '<STR_LIT>' , <EOL> ID = unique_id <EOL> ) <EOL> surname = contributor . get ( '<STR_LIT>' ) <EOL> if surname : <EOL> surname = surname [ <NUM_LIT:0> ] <EOL> prefix = contributor . get ( '<STR_LIT>' ) <EOL> if prefix : <EOL> surname = u'<STR_LIT>' % ( surname , prefix [ <NUM_LIT:0> ] ) <EOL> name . append ( MODS . namePart ( surname , type = "<STR_LIT>" ) ) <EOL> initials = contributor . get ( '<STR_LIT>' ) <EOL> firstname = contributor . get ( '<STR_LIT>' ) <EOL> if firstname : <EOL> name . append ( MODS . namePart ( firstname [ <NUM_LIT:0> ] , type = "<STR_LIT>" ) ) <EOL> elif initials : <EOL> name . append ( MODS . namePart ( initials [ <NUM_LIT:0> ] , type = "<STR_LIT>" ) ) <EOL> role = contributor . get ( '<STR_LIT>' ) <EOL> if role : <EOL> role = role [ <NUM_LIT:0> ] <EOL> else : <EOL> roles = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> role = roles [ ctype ] <EOL> name . append ( <EOL> MODS . role ( <EOL> MODS . roleTerm ( role , <EOL> type = '<STR_LIT:code>' , <EOL> authority = '<STR_LIT>' ) <EOL> ) ) <EOL> mods . append ( name ) <EOL> dai = contributor . get ( '<STR_LIT>' ) <EOL> if dai : <EOL> dai_list . append ( ( unique_id , dai ) ) <EOL> if dai_list : <EOL> daiList = DAI . daiList ( ) <EOL> daiList . attrib [ '<STR_LIT>' % XSI_NS ] = '<STR_LIT>' % ( <EOL> self . ns [ '<STR_LIT>' ] , <EOL> self . schemas [ '<STR_LIT>' ] ) <EOL> for id , dai in dai_list : <EOL> daiList . append ( DAI . identifier ( <EOL> dai [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] , <EOL> IDref = id , <EOL> authority = '<STR_LIT>' ) ) <EOL> mods . append ( MODS . extension ( daiList ) ) <EOL> for corp in data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ ] ) : <EOL> roles = MODS . role ( ) <EOL> if corp . get ( '<STR_LIT>' ) : <EOL> roles . append ( MODS . roleTerm ( corp [ '<STR_LIT>' ] , <EOL> authority = "<STR_LIT>" , <EOL> type = "<STR_LIT:text>" ) ) <EOL> if corp . get ( '<STR_LIT>' ) : <EOL> roles . append ( MODS . roleTerm ( corp [ '<STR_LIT>' ] , <EOL> authority = "<STR_LIT>" , <EOL> type = "<STR_LIT:code>" ) ) <EOL> mods . append ( MODS . name ( <EOL> MODS . namePart ( corp [ '<STR_LIT:name>' ] ) , <EOL> roles , <EOL> type = "<STR_LIT>" ) ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) : <EOL> lang_el = MODS . language ( <EOL> MODS . languageTerm ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> type = "<STR_LIT:code>" , <EOL> authority = "<STR_LIT>" ) ) <EOL> if data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> lang_el . append ( MODS . languageTerm ( '<STR_LIT>' , type = "<STR_LIT:text>" ) ) <EOL> if data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> lang_el . append ( MODS . languageTerm ( '<STR_LIT>' , type = "<STR_LIT:text>" ) ) <EOL> mods . append ( lang_el ) <EOL> for host in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> title = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % host ) <EOL> part_type = { '<STR_LIT>' : '<STR_LIT:host>' } . get ( host , host ) <EOL> relitem = MODS . relatedItem ( type = part_type ) <EOL> if title : <EOL> relitem . append ( MODS . titleInfo ( MODS . title ( title [ <NUM_LIT:0> ] ) ) ) <EOL> else : <EOL> continue <EOL> issn = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % host ) <EOL> if issn : <EOL> relitem . append ( <EOL> MODS . identifier ( issn [ <NUM_LIT:0> ] , <EOL> type = "<STR_LIT>" ) ) <EOL> host_uri = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % host ) <EOL> if host_uri : <EOL> relitem . append ( <EOL> MODS . identifier ( host_uri [ <NUM_LIT:0> ] , <EOL> type = "<STR_LIT>" ) ) <EOL> volume = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % host ) <EOL> issue = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % host ) <EOL> start_page = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % host ) <EOL> end_page = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % host ) <EOL> if volume or issue or end_page or start_page : <EOL> part = MODS . part ( ) <EOL> if volume : <EOL> part . append ( MODS . detail ( MODS . number ( volume [ <NUM_LIT:0> ] ) , <EOL> type = "<STR_LIT>" ) ) <EOL> if issue : <EOL> part . append ( MODS . detail ( MODS . number ( issue [ <NUM_LIT:0> ] ) , <EOL> type = "<STR_LIT>" ) ) <EOL> if start_page or end_page : <EOL> extent = MODS . extent ( unit = "<STR_LIT>" ) <EOL> if start_page : <EOL> extent . append ( MODS . start ( start_page [ <NUM_LIT:0> ] ) ) <EOL> if end_page : <EOL> extent . append ( MODS . end ( end_page [ <NUM_LIT:0> ] ) ) <EOL> part . append ( extent ) <EOL> relitem . append ( part ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT>' % host ) : <EOL> relitem . append ( <EOL> MODS . originInfo ( <EOL> MODS . publisher ( <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' % host ] [ <NUM_LIT:0> ] ) ) ) <EOL> mods . append ( relitem ) <EOL> origin = MODS . originInfo ( ) <EOL> mods . append ( origin ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) : <EOL> origin . append ( MODS . publisher ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT:date>' ) : <EOL> origin . append ( MODS . dateIssued ( data [ '<STR_LIT>' ] [ '<STR_LIT:date>' ] [ <NUM_LIT:0> ] , <EOL> encoding = '<STR_LIT>' ) ) <EOL> classifications = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ ] ) <EOL> for classification in classifications : <EOL> if classification . count ( '<STR_LIT:#>' ) == <NUM_LIT:1> : <EOL> authority , value = classification . split ( '<STR_LIT:#>' ) <EOL> mods . append ( MODS . classification ( value , authority = authority ) ) <EOL> else : <EOL> mods . append ( MODS . classification ( classification ) ) <EOL> subjects = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ ] ) <EOL> if subjects : <EOL> s_el = MODS . subject ( ) <EOL> for subject in subjects : <EOL> s_el . append ( MODS . topic ( subject ) ) <EOL> mods . append ( s_el ) <EOL> if data [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) : <EOL> mods . append ( MODS . accessCondition ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) ) <EOL> projects = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ ] ) <EOL> funders = set ( [ prj [ '<STR_LIT>' ] for prj in projects if prj . get ( '<STR_LIT>' ) ] ) <EOL> funder_ids = { } <EOL> for funder in funders : <EOL> unique_id = uuid . uuid4 ( ) . hex <EOL> if unique_id [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> unique_id = '<STR_LIT:_>' + unique_id <EOL> funder_ids [ funder ] = unique_id <EOL> mods . append ( <EOL> MODS . name ( MODS . namePart ( funder ) , <EOL> MODS . role ( MODS . roleTerm ( '<STR_LIT>' , <EOL> authority = '<STR_LIT>' , <EOL> type = '<STR_LIT:code>' ) ) , <EOL> ID = unique_id , <EOL> type = '<STR_LIT>' ) ) <EOL> if projects : <EOL> galList = GAL . grantAgreementList ( ) <EOL> galList . attrib [ '<STR_LIT>' % XSI_NS ] = '<STR_LIT>' % ( <EOL> self . ns [ '<STR_LIT>' ] , <EOL> self . schemas [ '<STR_LIT>' ] ) <EOL> mods . append ( MODS . extension ( galList ) ) <EOL> for prj in projects : <EOL> el = GAL . grantAgreement ( code = prj [ '<STR_LIT:id>' ] ) <EOL> if prj . get ( '<STR_LIT>' ) : <EOL> el . append ( GAL . funder ( IDref = funder_ids [ prj [ '<STR_LIT>' ] ] ) ) <EOL> if prj . get ( '<STR_LIT:title>' ) : <EOL> el . append ( GAL . title ( prj [ '<STR_LIT:title>' ] ) ) <EOL> galList . append ( el ) <EOL> info = data [ '<STR_LIT>' ] . get ( '<STR_LIT>' , { } ) <EOL> if info : <EOL> record_info_el = MODS . recordInfo ( ) <EOL> if info . get ( '<STR_LIT:source>' ) : <EOL> record_info_el . append ( MODS . recordContentSource ( info [ '<STR_LIT:source>' ] ) ) <EOL> if info . get ( '<STR_LIT>' ) : <EOL> record_info_el . append ( MODS . recordIdentifier ( info [ '<STR_LIT>' ] ) ) <EOL> for key , value in info . get ( '<STR_LIT>' , { } ) . items ( ) : <EOL> record_info_el . append ( MODS . recordIdentifier ( value , source = key ) ) <EOL> if info . get ( '<STR_LIT>' ) : <EOL> record_info_el . append ( MODS . recordOrigin ( info [ '<STR_LIT>' ] ) ) <EOL> if info . get ( '<STR_LIT>' ) : <EOL> record_info_el . append ( <EOL> MODS . recordCreationDate ( info [ '<STR_LIT>' ] , <EOL> encoding = "<STR_LIT>" ) ) <EOL> if info . get ( '<STR_LIT>' ) : <EOL> record_info_el . append ( <EOL> MODS . recordChangeDate ( info [ '<STR_LIT>' ] , <EOL> encoding = "<STR_LIT>" ) ) <EOL> mods . append ( record_info_el ) <EOL> mods . attrib [ '<STR_LIT>' % XSI_NS ] = '<STR_LIT>' % ( <EOL> self . ns [ '<STR_LIT>' ] , <EOL> self . schemas [ '<STR_LIT>' ] ) <EOL> element . append ( mods ) <EOL> class NL_MODS ( MODS ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , prefix , config , db ) : <EOL> super ( NL_MODS , self ) . __init__ ( prefix , config , db ) <EOL> self . ns [ '<STR_LIT>' ] = self . ns [ '<STR_LIT>' ] <EOL> self . schemas [ '<STR_LIT>' ] = self . schemas [ '<STR_LIT>' ] <EOL> def __call__ ( self , element , metadata ) : <EOL> super ( NL_MODS , self ) . __call__ ( element , metadata ) <EOL> for el in element . xpath ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , namespaces = self . ns ) : <EOL> el . attrib [ '<STR_LIT>' ] = '<STR_LIT>' </s>
