Пример #1
0
 def testBuildManifestWithASequence(self):
     m = Manifest("http://lib.uchicago.edu/")
     s = Sequence("http://lib.uchicago.edu/")
     s.canvases = [Canvas("http://lib.uchicago.edu/")]
     m.sequences = [s]
     return self.assertEqual(str(m.sequences), "[Sequence for http://lib.uchicago.edu/]") and \
            self.assertEqual(m.id, 'http://lib.uchicago.edu/')
def main():
    arguments = ArgumentParser(description="A CLI application to make it easy to add your first sequence of images to a IIIF record",
                               epilog="Version " + __version__)
    record_arg = arguments.add_argument("record", action='store', type=str, help="The path to the IIIF record that you want to extend")
    img_file_arg = arguments.add_argument("image_file", type=str, action="store", help="The location of the list of IIIF image links to ")
    arguments.add_argument("-o", "--output", action="store", type=str, help="Optional path to save the new extended record. Default is stdout")
    try:
        parsed_args = arguments.parse_args()
        are_there_images = False
        if not exists(parsed_args.image_file):
            raise ArgumentError(img_file_arg, "the file must exist on disk to be read")
        
        if not exists(parsed_args.record):
            raise ArgumentError(record_arg, "the file must exist on disk to be read")
        img_links = [x.strip() for x in open(parsed_args.image_file, 'r', encoding="utf-8").readlines()]
        data = json.load(open(parsed_args.record, 'r', encoding="utf-8"))

        m = Manifest.load(json.dumps(data))
        old_sequence = m.sequences
        for n_sequence in old_sequence:
            for n_canvas in n_sequence.canvases:
                try:
                    n_canvas.images
                    are_there_images = True
                except ValueError:
                    pass
        new_sequence = Sequence("http://example.org/foo")
        canvases = []
        count = 1
        for n_link in img_links:
           img_data = requests.get(n_link).json()
           height, width = img_data['height'], img_data['width']
           new_canvas = Canvas("http://example.org/biz" + str(count))
           new_canvas.label = "Image {}".format(count)
           new_canvas.height = height
           new_canvas.width = width
           new_annotation = Annotation("http://example.org/bar{}".format(count), new_canvas.id)
           url_object = urlparse(n_link)
           new_img = ImageResource(url_object.scheme, url_object.netloc, '', url_object.path, "image/jpeg")
           new_annotation.resource = new_img
           new_canvas.images = [new_annotation]
           canvases.append(new_canvas)
           count += 1
        new_sequence.canvases = canvases
        if are_there_images:
            m.sequences = [new_sequence] + old_sequence
        else:
            m.sequences = [new_sequence]
        string = json.dumps(m.to_dict(), indent=4)
        if parsed_args.output and exists(dirname(abspath(parsed_args.output))) and not exists(abspath(parsed_args.output)):
            with open(parsed_args.output, 'w+', encoding="utf-8") as wf:
                wf.write(string)
        else:
            stdout.write(string)
        return 0
    except KeyboardInterrupt:
        return 131
 def __call__(self, parser, args, values, option_strings=None):
     if exists(values):
         try:
             setattr(self, self.dest, Manifest.load(values))
         except:
             msg = "the IIIF record is not a valid IIIF record; could not be loaded"
             raise ArgumentError(self, msg)
     else:
         msg = "{} does not exist on-disk; cannot load file".format(values)
         raise ArgumentError(self, msg)
Пример #4
0
 def testLoadFromString(self):
     input_string = '{"@type": "sc:Manifest", "label": "Test", "description": "A test", "@id": "https://www.lib.uchicago.edu/", "viewingHint": "individuals", "viewingDirection": "left-to-right", "@context": "https://iiif.io/api/presentation/2/context.json"}'
     x = Manifest.load(input_string)
     return self.assertEqual(x.type, "sc:Manifest") and \
            self.assertEqual(x.id, "http://www2.lib.uchicago.edu/") and \
            self.assertEqual(x.context, "https://iiif.io/api/presentation/2/context.json") and \
            self.assertEqual(x.viewingDirection, "left-to-right") and \
            self.assertEqual(x.viewingHint, "individuals") and \
            self.assertEqual(x.description, "A test") and \
            self.assertEqual(x.label, "Test")
Пример #5
0
 def testBuildMixOfRecords(self):
     collection = Collection(
         "http://www.lib.uchicago.edu/collex/?digital=on&view=collections")
     manifest = Manifest("http://www2.lib.uchicago.edu/")
     sequence = Sequence(
         "http://www.lib.uchicago.edu/about/directory/?view=staff&subject=All+Subject+Specialists"
     )
     return self.assertEqual(collection.type, "sc:Collection") and \
            self.assertEqual(collection.id, "http://www.lib.uchicago.edu/collex/?digital=on&view=collections") and \
            self.assertEqual(manifest.type, "sc:Manifest") and \
            self.assertEqual(manifest.id, "http://www2.lib.uchicago.edu/") and \
            self.assertEqual(sequence.type, "sc:Sequence") and \
            self.assertEqual(sequence.id, "http://www.lib.uchicago.edu/about/directory/?view=staff&subject=All+Subject+Specialists")
Пример #6
0
 def testLoadFromStringWithMetadata(self):
     input_dict = {
         "@type": "sc:Manifest",
         "label": "Test",
         "description": "A test",
         "@id": "https://www.lib.uchicago.edu/",
         "metadata": [{
             "label": "A Test Label",
             "value": "A test value"
         }],
         "viewingHint": "individuals",
         "viewingDirection": "left-to-right",
         "@context": "https://iiif.io/api/presentation/2/context.json"
     }
     x = Manifest.load(json.dumps(input_dict))
     metadata = x.metadata
     return self.assertEqual(metadata[0].label.lower(), 'a test label')
Пример #7
0
 def testLoadFromStringAndGetBackSameInToDict(self):
     input_dict = {
         "@type": "sc:Manifest",
         "label": "Test",
         "description": "A test",
         "@id": "https://www.lib.uchicago.edu/",
         "metadata": [{
             "label": "A Test Label",
             "value": "A test value"
         }],
         "viewingHint": "individuals",
         "viewingDirection": "left-to-right",
         "@context": "https://iiif.io/api/presentation/2/context.json"
     }
     x = Manifest.load(json.dumps(input_dict))
     converted = x.to_dict()
     metadata = x.metadata
     print(converted.get("metadata"))
     return self.assertNotEqual(converted.get("metadata"), None) and \
            self.assertEqual(converted.get("metadata")[0]["label"].lower(), "a test label") and \
            self.assertEqual(converted.get("viewingHint") , "individuals") and \
            self.assertEqual(converted.get("@context"), "https://iiif.io/api/presentation/2/context.json")
Пример #8
0
 def testGetManifestDict(self):
     manifest = Manifest("http://lib.uchicago.edu/manifest")
     manifest.viewingHint = "individuals"
     manifest.viewingDirection = "left-to-right"
     sequence = Sequence("http://lib.uchicago.edu/sequence")
     canvas = Canvas("http://lib.uchicago.edu/canvas")
     canvas.height = 1000
     canvas.width = 509
     canvas.label = "A Canvas"
     canvas.description = "This a IIIF Canvas created programatically"
     annotation = Annotation("http://lib.uchicago.edu/annotation",
                             "https://example.org/bar")
     an_image = ImageResource('https', 'iiif-server.lib.uchicago.edu', '',
                              'default-photo.original.jpg', 'image/jpeg')
     an_image.set_width(1)
     an_image.set_height(1)
     annotation.resource = an_image
     canvas.images = [annotation]
     sequence.canvases = [canvas]
     manifest.sequences = [sequence]
     return self.assertEqual(len(manifest.to_dict()["sequences"]), 1) and \
            self.assertEqual(manifest.to_dict()["viewingHint"] == "individuals")
Пример #9
0
}
img_resource["@type"] = "dctypes:Image"
an_annotation["resource"] = img_resource
a_canvas["images"] = [an_annotation]
a_seq["canvases"] = [a_canvas]
out["sequences"] = [a_seq]

# testing loading image resource
imgresource_string = json.dumps(img_resource)
i = ImageResource.load(imgresource_string)

# testing loading canvas
canvas_string = json.dumps(a_canvas)
c = Canvas.load(canvas_string)
print(c)

# testing loading sequence
sequence_string = json.dumps(a_seq)
s = Sequence.load(sequence_string)
print(s)

# testing loading manifest
manifest_string = json.dumps(out)
m = Manifest.load(manifest_string)
print(m.sequences)
for s in m.sequences:
    print(s.canvases)
    for canvas in s.canvases:
        print(canvas.images)
        for anno in canvas.images:
            print(anno.resource)
Пример #10
0
 def testBuildCollectionWithMembers(self):
     c = Collection("http://lib.uchicago.edu/")
     c.members = [Manifest("http://lib.uchicago.edu/")]
     return self.assertEqual(str(c.members), "[Manifest for http://lib.uchicago.edu/]") and \
            self.assertEqual(c.id, 'http://lib.uchicago.edu/')