コード例 #1
0
ファイル: __init__.py プロジェクト: axiompiper/mods_generator
 def _process_mods_element(self, base_element, location_sections,
                           data_vals):
     #handle various MODS elements
     if base_element['element'] == u'mods:mods':
         if 'ID' in base_element['attributes']:
             self._xml_obj.id = data_vals[0][0]
     elif base_element['element'] == u'mods:name':
         if not self._cleared_fields.get(u'names', None):
             self._xml_obj.names = []
             self._cleared_fields[u'names'] = True
         self._add_name_data(base_element, location_sections, data_vals)
     elif base_element['element'] == u'mods:namePart':
         #grab the last name that was added
         name = self._xml_obj.names[-1]
         np = mods.NamePart(text=data_vals[0][0])
         if u'type' in base_element[u'attributes']:
             np.type = base_element[u'attributes'][u'type']
         name.name_parts.append(np)
     elif base_element[u'element'] == u'mods:titleInfo':
         if not self._cleared_fields.get(u'title_info_list', None):
             self._xml_obj.title_info_list = []
             self._cleared_fields[u'title_info_list'] = True
         self._add_title_data(base_element, location_sections, data_vals)
     elif base_element[u'element'] == u'mods:language':
         if not self._cleared_fields.get(u'languages', None):
             self._xml_obj.languages = []
             self._cleared_fields[u'languages'] = True
         for data in data_vals:
             language = mods.Language()
             language_term = mods.LanguageTerm(text=data[0])
             if u'authority' in location_sections[0][0]['attributes']:
                 language_term.authority = location_sections[0][0][
                     'attributes']['authority']
             if u'type' in location_sections[0][0]['attributes']:
                 language_term.type = location_sections[0][0][
                     u'attributes'][u'type']
             language.terms.append(language_term)
             self._xml_obj.languages.append(language)
     elif base_element[u'element'] == u'mods:genre':
         if not self._cleared_fields.get(u'genres', None):
             self._xml_obj.genres = []
             self._cleared_fields[u'genres'] = True
         for data in data_vals:
             genre = mods.Genre(text=data[0])
             if 'authority' in base_element['attributes']:
                 genre.authority = base_element['attributes']['authority']
             self._xml_obj.genres.append(genre)
     elif base_element['element'] == 'mods:originInfo':
         if not self._cleared_fields.get(u'origin_info', None):
             self._xml_obj.origin_info = None
             self._cleared_fields[u'origin_info'] = True
             self._xml_obj.create_origin_info()
         self._add_origin_info_data(base_element, location_sections,
                                    data_vals)
     elif base_element['element'] == 'mods:physicalDescription':
         if not self._cleared_fields.get(u'physical_description', None):
             self._xml_obj.physical_description = None
             self._cleared_fields[u'physical_description'] = True
             #can only have one physical description currently
             self._xml_obj.create_physical_description()
         data_divs = data_vals[0]
         for index, section in enumerate(location_sections):
             if section[0][u'element'] == 'mods:extent':
                 self._xml_obj.physical_description.extent = data_divs[
                     index]
             elif section[0][u'element'] == 'mods:digitalOrigin':
                 try:
                     self._xml_obj.physical_description.digital_origin = data_divs[
                         index]
                 except:
                     self._xml_obj.physical_description.digital_origin = section[
                         0][u'data']
             elif section[0][u'element'] == 'mods:note':
                 self._xml_obj.physical_description.note = data_divs[index]
     elif base_element['element'] == 'mods:typeOfResource':
         if not self._cleared_fields.get(u'typeOfResource', None):
             self._xml_obj.resource_type = None
             self._cleared_fields[u'typeOfResource'] = True
         self._xml_obj.resource_type = data_vals[0][0]
     elif base_element['element'] == 'mods:targetAudience':
         if not self._cleared_fields.get(u'targetAudience', None):
             self._xml_obj.resource_type = None
             self._cleared_fields[u'targetAudience'] = True
         ta = mods.TargetAudience(text=data_vals[0][0])
         self._xml_obj.target_audiences.append(ta)
     elif base_element['element'] == 'mods:abstract':
         if not self._cleared_fields.get(u'abstract', None):
             self._xml_obj.abstract = None
             self._cleared_fields[u'abstract'] = True
             #can only have one abstract currently
             self._xml_obj.create_abstract()
         self._xml_obj.abstract.text = data_vals[0][0]
     elif base_element['element'] == 'mods:note':
         if not self._cleared_fields.get(u'notes', None):
             self._xml_obj.notes = []
             self._cleared_fields[u'notes'] = True
         for data in data_vals:
             note = mods.Note(text=data[0])
             if 'type' in base_element['attributes']:
                 note.type = base_element['attributes']['type']
             if 'displayLabel' in base_element['attributes']:
                 note.label = base_element['attributes']['displayLabel']
             self._xml_obj.notes.append(note)
     elif base_element['element'] == 'mods:subject':
         if not self._cleared_fields.get(u'subjects', None):
             self._xml_obj.subjects = []
             self._cleared_fields[u'subjects'] = True
         for data in data_vals:
             subject = mods.Subject()
             if 'authority' in base_element['attributes']:
                 subject.authority = base_element['attributes']['authority']
             data_divs = data
             for section, div in zip(location_sections, data_divs):
                 if section[0]['element'] == 'mods:topic':
                     topic = mods.Topic(text=div)
                     subject.topic_list.append(topic)
                 elif section[0]['element'] == 'mods:temporal':
                     temporal = mods.Temporal(text=div)
                     subject.temporal_list.append(temporal)
                 elif section[0]['element'] == 'mods:geographic':
                     subject.geographic = div
                 elif section[0][
                         'element'] == 'mods:hierarchicalGeographic':
                     hg = mods.HierarchicalGeographic()
                     if section[1]['element'] == 'mods:country':
                         if 'data' in section[1]:
                             hg.country = section[1]['data']
                             if section[2]['element'] == 'mods:state':
                                 hg.state = div
                         else:
                             hg.country = div
                     subject.hierarchical_geographic = hg
             self._xml_obj.subjects.append(subject)
     elif base_element['element'] == 'mods:identifier':
         if not self._cleared_fields.get(u'identifiers', None):
             self._xml_obj.identifiers = []
             self._cleared_fields[u'identifiers'] = True
         for data in data_vals:
             identifier = mods.Identifier(text=data[0])
             if 'type' in base_element['attributes']:
                 identifier.type = base_element['attributes']['type']
             if 'displayLabel' in base_element['attributes']:
                 identifier.label = base_element['attributes'][
                     'displayLabel']
             self._xml_obj.identifiers.append(identifier)
     elif base_element['element'] == u'mods:location':
         if not self._cleared_fields.get(u'locations', None):
             self._xml_obj.locations = []
             self._cleared_fields[u'locations'] = True
         for data in data_vals:
             loc = mods.Location()
             data_divs = data
             for section, div in zip(location_sections, data_divs):
                 if section[0]['element'] == u'mods:url':
                     if section[0]['data']:
                         loc.url = section[0]['data']
                     else:
                         loc.url = div
                 elif section[0]['element'] == u'mods:physicalLocation':
                     if section[0]['data']:
                         loc.physical = mods.PhysicalLocation(
                             text=section[0]['data'])
                     else:
                         loc.physical = mods.PhysicalLocation(text=div)
                 elif section[0]['element'] == u'mods:holdingSimple':
                     hs = mods.HoldingSimple()
                     if section[1]['element'] == u'mods:copyInformation':
                         if section[2]['element'] == u'mods:note':
                             note = mods.Note(text=div)
                             ci = mods.CopyInformation()
                             ci.notes.append(note)
                             hs.copy_information.append(ci)
                             loc.holding_simple = hs
             self._xml_obj.locations.append(loc)
     elif base_element['element'] == u'mods:relatedItem':
         if not self._cleared_fields.get(u'related', None):
             self._xml_obj.related_items = []
             self._cleared_fields[u'related'] = True
         for data in data_vals:
             related_item = mods.RelatedItem()
             if u'type' in base_element[u'attributes']:
                 related_item.type = base_element[u'attributes'][u'type']
             if u'displayLabel' in base_element[u'attributes']:
                 related_item.label = base_element[u'attributes'][
                     u'displayLabel']
             if location_sections[0][0][u'element'] == u'mods:titleInfo':
                 if location_sections[0][1][u'element'] == u'mods:title':
                     related_item.title = data[0]
             self._xml_obj.related_items.append(related_item)
     else:
         raise RuntimeError('unhandled MODS element: %s' % base_element)
コード例 #2
0
 def get_mods_obj(self, update=False):
     if self._mods_obj:
         #if we have mods already, and we're not updating, just return it
         if not update:
             return self._mods_obj
     else:  #no self._mods_obj
         if update:
             raise Exception('no mods obj - can\'t update')
         self._mods_obj = mods.make_mods()
     #at this point, we want to put the form data into the mods obj (could be update or new)
     self._mods_obj.title_info_list = []  #clear out any old titles
     title = mods.TitleInfo()
     title.title = self._form_data['title']
     if self._form_data['title_language']:
         title.node.set('lang', self._form_data['title_language'])
     self._mods_obj.title_info_list.append(title)
     if self._form_data['english_title']:
         english_title = mods.TitleInfo()
         english_title.title = self._form_data['english_title']
         english_title.node.set('lang', 'en')
         self._mods_obj.title_info_list.append(english_title)
     if self._form_data['genre']:
         self._mods_obj.genres = []  #clear out any old genres
         genre = mods.Genre(text=self._form_data['genre'].text)
         genre.authority = 'aat'
         self._mods_obj.genres.append(genre)
     if self._form_data['abstract']:
         if not self._mods_obj.abstract:
             self._mods_obj.create_abstract()
         self._mods_obj.abstract.text = self._form_data[
             'abstract']  #overwrites old abstract if present
     if self._form_data['impression_date']:
         #clear out old dateOther data, or create originInfo if needed
         if self._mods_obj.origin_info:
             self._mods_obj.origin_info.other = []
         else:
             self._mods_obj.create_origin_info()
         date_other = mods.DateOther(
             date=self._form_data['impression_date'])
         date_other.type = 'impression'
         self._mods_obj.origin_info.other.append(date_other)
     #clear out any old names:
     #   if this is a new object, there aren't any names anyway.
     #   if this is an update, either the new names will be put in, or they don't want any names.
     self._mods_obj.names = []
     for p in self._person_formset_data:
         name = mods.Name()
         np = mods.NamePart(text=p['person'].name)
         name.name_parts.append(np)
         role = mods.Role(text=p['role'].text)
         name.roles.append(role)
         href = '{%s}href' % app_settings.XLINK_NAMESPACE
         name.node.set(href, p['person'].trp_id)
         self._mods_obj.names.append(name)
     #clear out old notes data, preserving any annotor info
     self._mods_obj.notes = [
         note for note in self._mods_obj.notes if note.type == 'resp'
     ]
     for i in self._inscription_formset_data:
         note = mods.Note(text=i['text'])
         note.type = 'inscription'
         note.label = i['location']
         self._mods_obj.notes.append(note)
     annotator_note = mods.Note(text=self._annotator)
     annotator_note.type = 'resp'
     self._mods_obj.notes.append(annotator_note)
     return self._mods_obj
コード例 #3
0
ファイル: mods_mapper.py プロジェクト: bcail/etd_app
 def _get_notes(self):
     candidate = self.thesis.candidate
     note_text = 'Thesis (%s)--Brown University, %s' % (candidate.degree.abbreviation, candidate.year)
     return [mods.Note(type='thesis', text=note_text)]