def __call__(self, parser, args, values, options_string=None):
     if exists(values):
         xml_doc = ElementTree.parse(values)
         xml_root = xml_doc.getroot()
         datafields = xml_root.findall("datafield")
         metadata = []
         identifier = ""
         title = "unknown"
         description = "A Cultural Heritage Object from the University of Chicago Library"
         for datafield in datafields:
             new_field = MarcField(field=datafield.attrib["tag"].strip())
             valid_subfields = [x.code for x in new_field.subfields]
             real_subfields = datafield.findall("subfield")
             for r_sf in real_subfields:
                 if new_field.field == '245' and r_sf.attrib['code'] == 'a':
                     title = r_sf.text
                 if new_field.label and "electronic location" in new_field.label.lower():
                     fragment = urlparse(r_sf.text).path
                     if fragment[0] == '/':
                         fragment = fragment[1:]
                     identifier += fragment
                 if r_sf.attrib["code"] in valid_subfields:
                     field_label = new_field.label
                     subfield_label = [x.label for x in new_field.subfields if x.code == r_sf.attrib["code"]]
                     if subfield_label[0].lower() in field_label.lower():
                         label = field_label
                     else:
                         label = field_label + " " + subfield_label[0]
                     new_mfield = IIIFMetadataField(label, r_sf.text)
                     metadata.append(new_mfield)
         new_iiif_mdata_box = IIIFMetadataBoxFromMarc(title, description, identifier, metadata)
         new_iiif = IIIFDataExtractionFromMarc(new_iiif_mdata_box)
         setattr(args, self.dest, new_iiif.to_dict())
     else:
         raise ArgumentError(self, "value passed for record does not exist on disk")
Example #2
0
 def testBuildIIIFDataExtractionFromMarc(self):
     """a test to successfully build a new instance of IIIFDataExtractionFromMarc
     """
     metadata_box = IIIFMetadataBoxFromMarc(
         'a label', 'a description', '/foo/bar',
         [IIIFMetadataField("a field", "a value")])
     new_object = IIIFDataExtractionFromMarc(metadata_box)
     self.assertEqual(new_object.show_title(), 'a label')
     self.assertEqual(new_object.show_description(), 'a description')
Example #3
0
 def testChangeDescription(self):
     """a test to successfully change the description for the record from a pre-defined description
     """
     test_object = IIIFDataExtractionFromMarc.from_dict(self.data)
     test_object.change_description(
         'A totally new description that is way better than the old description'
     )
     self.assertEqual(
         test_object.show_description(),
         'A totally new description that is way better than the old description'
     )
Example #4
0
 def testRemoveMetadata(self):
     """a test to remove a particular metadata field
     """
     test_object = IIIFDataExtractionFromMarc.from_dict(self.data)
     test_object.remove_metadata({"Local Subject": "Test Subject"})
     check = False
     for n in test_object.show_metadata():
         if n.get('value') == 'Local Subject':
             check = True
             break
     self.assertEqual(check, False)
Example #5
0
 def testModifyingMetadata(self):
     """a test to modify a partricular metadata field with a new value
     """
     test_object = IIIFDataExtractionFromMarc.from_dict(self.data)
     test_object.modify_metadata({'Local Subject': "Test Subject"},
                                 'A new subject')
     check = False
     for n in test_object.show_metadata():
         if n.get('value') == 'A new subject':
             check = True
             break
     self.assertEqual(check, True)
Example #6
0
 def testLoadFromDict(self):
     """a test to successfully load an instance of IIIDataExtractionFromMarc from a dictionary containing a MARC record
     """
     test_object = IIIFDataExtractionFromMarc.from_dict(self.data)
     self.assertEqual(test_object.show_title(), 'A Title of a CHO')
     self.assertEqual(test_object.show_description(),
                      '1 online resource (1 map) : col.')
     self.assertEqual(test_object.show_metadata(),
                      [{
                          'label': 'Local Subject',
                          'value': 'Test Subject'
                      }, {
                          'label': 'Electronic Location and Access',
                          'value': 'http://example.org/foo'
                      }])
Example #7
0
 def testChangeTitle(self):
     """a test to change the title of the IIIF record from original title to new title
     """
     test_object = IIIFDataExtractionFromMarc.from_dict(self.data)
     test_object.change_title('A brand new title')
     self.assertEqual(test_object.show_title(), 'A brand new title')