Example #1
0
 def test10_encode(self):
     for tname in sorted(data.iterkeys()):
         tdata=data[tname]
         print tname + "   " + self.pstr(data[tname][0]) + "  " + data[tname][1]
         iiif = IIIFRequest(**data[tname][0])
         self.assertEqual(iiif.url(),data[tname][1])
     print
Example #2
0
 def test08_parse_quality_bad(self):
     r = IIIFRequest(api_version="1.1")
     r.quality = "does_not_exist"
     self.assertRaises(IIIFError, r.parse_quality)
     # bad ones
     r.quality = ""
     self.assertRaises(IIIFError, r.parse_quality)
Example #3
0
 def test08_parse_quality_bad(self):
     r = IIIFRequest(api_version='1.1')
     r.quality = 'does_not_exist'
     self.assertRaises(IIIFError, r.parse_quality)
     # bad ones
     r.quality = ''
     self.assertRaises(IIIFError, r.parse_quality)
Example #4
0
 def test02_parse_non_negative_int(self):
     """Test parsing of non-negative integer."""
     r = IIIFRequest()
     self.assertEqual(r._parse_non_negative_int('1', 'a'), (1))
     self.assertRaises(ValueError, r._parse_non_negative_int, 'a', 'region')
     self.assertRaises(ValueError, r._parse_non_negative_int, '-1',
                       'region')
Example #5
0
    def __init__(self, prefix, identifier, config, klass, auth):
        """Initialize IIIFHandler setting key configurations.

        Positional parameters:
        prefix -- URI prefix (without leading or trailing slashes)
        identifier -- identifier of image
        config -- instance of Config class
        klass -- IIIFManipulator sub-class to do manipulations
        auth -- IIIFAuth sub-class for auth
        """
        self.prefix = prefix
        self.identifier = identifier
        self.config = config
        self.klass = klass
        self.api_version = config.api_version
        self.auth = auth
        self.degraded = False
        self.logger = logging.getLogger('IIIFHandler')
        #
        # Create objects to process request
        self.iiif = IIIFRequest(api_version=self.api_version,
                                identifier=self.identifier)
        self.manipulator = klass(api_version=self.api_version)
        #
        # Set up auth object with locations if not already done
        if (self.auth and not self.auth.login_uri):
            self.auth.login_uri = self.server_and_prefix + '/login'
            if (self.auth.logout_handler is not None):
                self.auth.logout_uri = self.server_and_prefix + '/logout'
            self.auth.access_token_uri = self.server_and_prefix + '/token'
        #
        # Response headers
        # -- All responses should have CORS header
        self.headers = {'Access-control-allow-origin': '*'}
Example #6
0
 def test10_encode(self):
     for tname in sorted(data.iterkeys()):
         tdata=data[tname]
         print tname + "   " + self.pstr(data[tname][0]) + "  " + data[tname][1]
         iiif = IIIFRequest(**data[tname][0])
         self.assertEqual(iiif.url(),data[tname][1])
     print
Example #7
0
 def test08_parse_quality_bad(self):
     r = IIIFRequest()
     r.quality='does_not_exist'
     self.assertRaises( IIIFError, r.parse_quality )
     # bad ones
     r.quality=''
     self.assertRaises( IIIFError, r.parse_quality )
Example #8
0
 def test02_parse_region_bad(self):
     r = IIIFRequest()
     r.region='pct:0,0,50,1000'
     self.assertRaises( IIIFError, r.parse_region )
     r.region='pct:-10,0,50,100'
     self.assertRaises( IIIFError, r.parse_region )
     r.region='square'
     self.assertRaises( IIIFError, r.parse_region )
Example #9
0
 def test1_encode(self):
     print "Encoding tests..."
     for tname in sorted(data.iterkeys()):
         tdata=data[tname]
         print tname + "   " + self.pstr(data[tname][0]) + "  " + data[tname][1]
         iiif = IIIFRequest(api_version='1.0',**data[tname][0])
         self.assertEqual(iiif.url(),data[tname][1])
     print
Example #10
0
 def test02_parse_non_negative_int(self):
     """Test parsing of non-negative integer."""
     r = IIIFRequest()
     self.assertEqual(r._parse_non_negative_int('1', 'a'), (1))
     self.assertRaises(ValueError,
                       r._parse_non_negative_int, 'a', 'region')
     self.assertRaises(ValueError,
                       r._parse_non_negative_int, '-1', 'region')
Example #11
0
 def test02_parse_region_bad(self):
     r = IIIFRequest(api_version="1.1")
     r.region = "pct:0,0,50,1000"
     self.assertRaises(IIIFError, r.parse_region)
     r.region = "pct:-10,0,50,100"
     self.assertRaises(IIIFError, r.parse_region)
     r.region = "square"
     self.assertRaises(IIIFError, r.parse_region)
Example #12
0
 def test08_parse_quality_bad(self):
     """Parse quality - bad requests."""
     r = IIIFRequest(api_version='2.1')
     r.quality = 'does_not_exist'
     self.assertRaises(IIIFError, r.parse_quality)
     # bad ones
     r.quality = ''
     self.assertRaises(IIIFError, r.parse_quality)
Example #13
0
 def test1_encode(self):
     print "Encoding tests..."
     for tname in sorted(data.iterkeys()):
         tdata=data[tname]
         print tname + "   " + self.pstr(data[tname][0]) + "  " + data[tname][1]
         iiif = IIIFRequest(api_version='1.1',**data[tname][0])
         self.assertEqual(iiif.url(),data[tname][1])
     print
Example #14
0
 def test08_parse_quality_bad(self):
     """Parse quality - bad requests."""
     r = IIIFRequest(api_version='3.0')
     r.quality = 'does_not_exist'
     self.assertRaises(IIIFError, r.parse_quality)
     # bad ones
     r.quality = ''
     self.assertRaises(IIIFError, r.parse_quality)
Example #15
0
 def test02_parse_region_bad(self):
     """Bad regions."""
     r = IIIFRequest(api_version='2.0')
     r.region = 'pct:0,0,50,1000'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = 'pct:-10,0,50,100'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = 'square'
     self.assertRaises(IIIFError, r.parse_region)
Example #16
0
 def test02_parse_region_bad(self):
     """Bad regions."""
     r = IIIFRequest(api_version='2.0')
     r.region = 'pct:0,0,50,1000'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = 'pct:-10,0,50,100'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = 'square'
     self.assertRaises(IIIFError, r.parse_region)
Example #17
0
 def test12_decode_except(self):
     self.assertRaises(IIIFRequestBaseURI, IIIFRequest().split_url, ("id"))
     self.assertRaises(IIIFRequestBaseURI,
                       IIIFRequest().split_url, ("id%2Ffsdjkh"))
     self.assertRaises(IIIFError, IIIFRequest().split_url, ("id/"))
     self.assertRaises(IIIFError, IIIFRequest().split_url, ("id/bogus"))
     self.assertRaises(IIIFError,
                       IIIFRequest().split_url,
                       ("id1/all/270/!pct%3A75.23.jpg"))
Example #18
0
    def check_encoding(self, data, api_version):
        """Encoding.

        Checks that every for each test in the dict data,
        the data values (first element) encode to the URL
        given (second element).
        """
        for tname in sorted(data.keys()):
            tdata = data[tname]
            iiif = IIIFRequest(api_version=api_version, **data[tname][0])
            self.assertEqual(iiif.url(), data[tname][1])
Example #19
0
    def check_encoding(self, data, api_version):
        """Encoding.

        Checks that every for each test in the dict data,
        the data values (first element) encode to the URL
        given (second element).
        """
        for tname in sorted(data.keys()):
            tdata = data[tname]
            iiif = IIIFRequest(api_version=api_version, **data[tname][0])
            self.assertEqual(iiif.url(), data[tname][1])
Example #20
0
 def test18_url(self):
     r = IIIFRequest()
     r.size = None 
     r.size_wh = [11,22]
     self.assertEqual( r.url(identifier='abc1'), 'abc1/full/11,22/0/default' )
     r.size_wh = [100,None]
     self.assertEqual( r.url(identifier='abc2'), 'abc2/full/100,/0/default' )
     r.size_wh = [None,999]
     self.assertEqual( r.url(identifier='abc3'), 'abc3/full/,999/0/default' )
     r.size_wh = None
     self.assertEqual( r.url(identifier='abc4'), 'abc4/full/full/0/default' )
Example #21
0
 def test09_parse_format(self):
     """Test parse_format."""
     r = IIIFRequest(api_version='2.1')
     r.format = 'jpg'
     r.parse_format()
     r.format = 'something_else_Z134'
     r.parse_format()
     # Bad things
     r.format = 'no spaces allowed'
     self.assertRaises(IIIFRequestError, r.parse_format)
     r.format = '~'
     self.assertRaises(IIIFRequestError, r.parse_format)
     r.format = ''
     self.assertRaises(IIIFRequestError, r.parse_format)
Example #22
0
def get_image_url(message, config):
    '''Returns an image URL that conforms to the IIIF Image API.'''

    # The last path component is a URL-encoded identifier.
    iiif_image_server_base_url, url_encoded_identifier = message[
        'iiif_image_info_url'].rsplit('/', maxsplit=1)

    # The `baseurl` keyword argument must have a trailing slash.
    image_api_request = IIIFRequest(baseurl=iiif_image_server_base_url + '/')
    image_api_params = {
        **config['iiif']['image_api_default_params'], 'size': '640,',
        'identifier': unquote(url_encoded_identifier)
    }
    return image_api_request.url(**image_api_params)
Example #23
0
 def test12_decode_except(self):
     """Decoding exceptions."""
     self.assertRaises(IIIFRequestBaseURI,
                       IIIFRequest(api_version='2.0').split_url, ("id"))
     self.assertRaises(IIIFRequestBaseURI,
                       IIIFRequest(api_version='2.0').split_url,
                       ("id%2Ffsdjkh"))
     self.assertRaises(IIIFError,
                       IIIFRequest(api_version='2.0').split_url, ("id/"))
     self.assertRaises(IIIFError,
                       IIIFRequest(api_version='2.0').split_url,
                       ("id/bogus"))
     self.assertRaises(IIIFError,
                       IIIFRequest(api_version='2.0').split_url,
                       ("id1/all/270/!pct%3A75.23.jpg"))
Example #24
0
 def test04_parse_size_bad(self):
     """Bad size."""
     r = IIIFRequest(api_version='1.1')
     self.assertRaises(IIIFError, r.parse_size, ',0.0')
     self.assertRaises(IIIFError, r.parse_size, '0.0,')
     self.assertRaises(IIIFError, r.parse_size, '1.0,1.0')
     self.assertRaises(IIIFError, r.parse_size, '1,1,1')
Example #25
0
 def test13_rotation_to_apply(self):
     """Test rotation_to_apply."""
     m = IIIFManipulator()
     m.request = IIIFRequest()
     # 0
     m.request.rotation_deg = 0
     self.assertEqual(m.rotation_to_apply(), (False, False))
     # 90 and only90s
     m.request.rotation_deg = 90
     self.assertEqual(m.rotation_to_apply(True), (False, 90))
     # 100
     m.request.rotation_deg = 100
     self.assertEqual(m.rotation_to_apply(), (False, 100))
     # 100 and only90s
     m.request.rotation_deg = 100
     self.assertRaises(IIIFError, m.rotation_to_apply, True)
     # 45 and mirror
     m.request.rotation_mirror = True
     m.request.rotation_deg = 45
     self.assertEqual(m.rotation_to_apply(), (True, 45))
     # 45 and no-mirror
     self.assertRaises(IIIFError, m.rotation_to_apply, True, True)
     # mirror request and no-mirror
     m.request.rotation_mirror = True
     self.assertRaises(IIIFError, m.rotation_to_apply, False, True)
Example #26
0
    def __init__(self, prefix, identifier, config, klass, auth):
        """Initialize IIIFHandler setting key configurations.

        Positional parameters:
        prefix -- URI prefix (without leading or trailing slashes)
        identifier -- identifier of image
        config -- instance of Config class
        klass -- IIIFManipulator sub-class to do manipulations
        auth -- IIIFAuth sub-class instance for auth or None
        """
        self.prefix = prefix
        self.identifier = identifier
        self.config = config
        self.klass = klass
        self.api_version = config.api_version
        self.auth = auth
        self.degraded = False
        self.logger = logging.getLogger('IIIFHandler')
        #
        # Create objects to process request
        self.iiif = IIIFRequest(api_version=self.api_version,
                                identifier=self.identifier)
        self.manipulator = klass(api_version=self.api_version)
        #
        # Set up auth object with locations if not already done
        if (self.auth and not self.auth.login_uri):
            self.auth.login_uri = self.server_and_prefix + '/login'
            if (self.auth.logout_handler is not None):
                self.auth.logout_uri = self.server_and_prefix + '/logout'
            self.auth.access_token_uri = self.server_and_prefix + '/token'
        #
        # Response headers
        # -- All responses should have CORS header
        self.headers = {'Access-control-allow-origin': '*'}
Example #27
0
 def test06_do_rotation(self):
     m = IIIFManipulator()
     m.request = IIIFRequest()
     m.request.rotation_deg = 0.0
     m.do_rotation()
     # non 0.0 raises
     m.request.rotation_deg = 1.0
     self.assertRaises(IIIFError, m.do_rotation)
Example #28
0
 def test12_decode_good(self):
     """Decoding examples that should work."""
     r = IIIFRequest(api_version='2.1', baseurl='1.1_netpbm/a/')
     r.split_url('1.1_netpbm/a/b/full/full/0/default')
     self.assertEqual(r.identifier, 'b')
     # id with slashes in it
     r = IIIFRequest(api_version='2.1', allow_slashes_in_identifier=True)
     r.split_url('a/b/c/full/full/0/default')
     self.assertFalse(r.info)
     self.assertEqual(r.identifier, 'a/b/c')
     r = IIIFRequest(api_version='2.1', allow_slashes_in_identifier=True)
     r.split_url('a/b/info.json')
     self.assertTrue(r.info)
     self.assertEqual(r.identifier, 'a/b')
Example #29
0
 def test06_do_rotation(self):
     """Test do_rotation, error if any rotation."""
     m = IIIFManipulator()
     m.request = IIIFRequest()
     m.do_rotation(False, 0.0)
     # mirror raises
     self.assertRaises(IIIFError, m.do_rotation, True, 0.0)
     # non 0.0 raises
     self.assertRaises(IIIFError, m.do_rotation, False, 10.0)
Example #30
0
 def test04_parse_size_bad(self):
     """Bad sizes."""
     r = IIIFRequest(api_version='2.0')
     self.assertRaises(IIIFError, r.parse_size, ',0.0')
     self.assertRaises(IIIFError, r.parse_size, '0.0,')
     self.assertRaises(IIIFError, r.parse_size, '1.0,1.0')
     self.assertRaises(IIIFError, r.parse_size, '1,1,1')
     # 'max' not allowed in 2.0, introduced in 2.1
     self.assertRaises(IIIFError, r.parse_size, 'max')
Example #31
0
 def test11_decode(self):
     for tname in sorted(data.iterkeys()):
         tdata = data[tname]
         pstr = self.pstr(data[tname][0])
         for turl in data[tname][1:]:
             iiif = IIIFRequest(api_version='1.1').split_url(turl)
             tstr = self.pstr(iiif.__dict__)
             print tname + "   " + turl + " -> " + tstr
             self.assertEqual(tstr, pstr)
     print
Example #32
0
 def test04_do_region(self):
     m = IIIFManipulator()
     m.request = IIIFRequest()
     m.request.region_full = True
     m.do_region()
     # and now without region_full
     m.width = -1
     m.height = -1
     m.request.region_full = False
     self.assertRaises(IIIFError, m.do_region)
Example #33
0
 def test2_decode(self):
     print "Decoding tests..."
     for tname in sorted(data.iterkeys()):
         tdata = data[tname]
         pstr = self.pstr(data[tname][0])
         for turl in data[tname][1:]:
             iiif = IIIFRequest().parse_url(turl)
             tstr = self.pstr(iiif.__dict__)
             print "    <tr>\n      <td>" + tstr + "</td>\n      <td>" + turl + "</td>\n    </tr>"
             self.assertEqual(tstr, pstr)
     print
Example #34
0
 def test20_bad_response_codes(self):
     """Response codes."""
     for (path, code) in [("id/b", 400), ("id/b/c", 400), ("id/b/c/d", 400),
                          ("id/full/full/0/default.jpg/extra", 400)]:
         got_code = None
         try:
             IIIFRequest(api_version='2.0').split_url(path)
         except IIIFError as e:
             got_code = e.code
         self.assertEqual(
             got_code, code, "Bad code %s, expected %d, for path %s" %
             (str(got_code), code, path))
Example #35
0
 def test06_parse_rotation_bad(self):
     r = IIIFRequest(api_version='1.1')
     r.rotation = '-1'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = '-0.0000001'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = '360.0000001'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = 'abc'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = '1!'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = '!!4'
     self.assertRaises(IIIFError, r.parse_rotation)
Example #36
0
    def test19_split_url(self):
        """Test split_url() method.

        Most parts are common to all versions, except handling
        of info.json extensions.
        """
        # api_version=1.0, format=xyz -> bad
        r = IIIFRequest(api_version='1.0')
        r.baseurl = 'http://ex.org/a/'
        self.assertRaises(IIIFError, r.split_url, 'http://ex.org/a/b/info.xyz')
        # api_version=2.1, format=xml -> bad
        r = IIIFRequest(api_version='2.1')
        r.baseurl = 'http://ex.org/a/'
        self.assertRaises(IIIFError, r.split_url, 'http://ex.org/a/b/info.xml')
        # api_version=2.1, format=xyz -> bad
        r = IIIFRequest(api_version='2.1')
        r.baseurl = 'http://ex.org/a/'
        self.assertRaises(IIIFError, r.split_url, 'http://ex.org/a/b/info.xyz')
Example #37
0
 def test02_derive(self):
     m = IIIFManipulator()
     r = IIIFRequest()
     r.parse_url('id1/full/full/0/default')
     tmp = tempfile.mkdtemp()
     outfile = os.path.join(tmp,'testout.png')
     try:
         m.derive(srcfile='testimages/test1.png',
                  request=r, outfile=outfile);
         self.assertTrue( os.path.isfile(outfile) )
         self.assertEqual( os.path.getsize(outfile), 65810 )
     finally:
         shutil.rmtree(tmp) 
     # and where path to outfile must be created
     outfile = os.path.join(tmp,'a','b','testout.png')
     try:
         m.derive(srcfile='testimages/test1.png',
                  request=r, outfile=outfile);
         self.assertTrue( os.path.isfile(outfile) )
         self.assertEqual( os.path.getsize(outfile), 65810 )
     finally:
         shutil.rmtree(tmp) 
Example #38
0
 def test05_do_size(self):
     m = IIIFManipulator()
     m.request = IIIFRequest()
     m.request.size_pct = 100
     m.request.size = '100'
     m.do_size()
     m.request.size_pct = None
     m.request.size = 'full'
     m.do_size()
     # and raise for not 100 or full
     m.request.size_pct = None
     m.request.size = '1,1'
     self.assertRaises(IIIFError, m.do_size)
Example #39
0
 def test08_do_format(self):
     m = IIIFManipulator()
     m.request = IIIFRequest()
     m.request.format = None
     m.srcfile = 'abc'
     m.do_format()
     self.assertEqual(m.outfile, m.srcfile)
     # failure to copy if srcfile and outfile specified same
     m.outfile = m.srcfile
     self.assertRaises(IIIFError, m.do_format)
     # any format specified -> raise
     m.request.format = 'something'
     self.assertRaises(IIIFError, m.do_format)
Example #40
0
 def test02_derive(self):
     """Test derive method by running complete conversions."""
     m = IIIFManipulator()
     r = IIIFRequest()
     r.parse_url('id1/full/full/0/default')
     tmp = tempfile.mkdtemp()
     outfile = os.path.join(tmp, 'testout.png')
     try:
         m.derive(srcfile='testimages/test1.png',
                  request=r, outfile=outfile)
         self.assertTrue(os.path.isfile(outfile))
         self.assertEqual(os.path.getsize(outfile), 65810)
     finally:
         shutil.rmtree(tmp)
     # and where path to outfile must be created
     outfile = os.path.join(tmp, 'a', 'b', 'testout.png')
     try:
         m.derive(srcfile='testimages/test1.png',
                  request=r, outfile=outfile)
         self.assertTrue(os.path.isfile(outfile))
         self.assertEqual(os.path.getsize(outfile), 65810)
     finally:
         shutil.rmtree(tmp)
Example #41
0
    def check_decoding(self, data, api_version):
        """Decoding.

        Reverse of check_encoding(). Checks that for each test
        the URL (second element) is decoded to the given data
        values (first element).
        """
        for tname in sorted(data.keys()):
            tdata = data[tname]
            pstr = self._pstr(data[tname][0])
            for turl in data[tname][1:]:
                iiif = IIIFRequest(api_version).split_url(turl)
                tstr = self._pstr(iiif.__dict__)
                self.assertEqual(tstr, pstr)
Example #42
0
 def test13_quality_to_apply(self):
     m = IIIFManipulator()
     m.request = IIIFRequest()
     # v1.0,v1.1 none
     m.request.quality = None
     m.api_version = '1.0'
     self.assertEqual(m.quality_to_apply(), 'native')
     m.api_version = '1.1'
     self.assertEqual(m.quality_to_apply(), 'native')
     m.api_version = '2.0'
     self.assertEqual(m.quality_to_apply(), 'default')
     # anything else
     m.request.quality = 'something'
     self.assertEqual(m.quality_to_apply(), 'something')
Example #43
0
 def test02_parse_region_bad(self):
     """Parse region."""
     r = IIIFRequest(api_version='2.1')
     r.region = 'pct:0,0,50,1000'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = 'pct:-10,0,50,100'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = 'pct:a,b,c,d'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = 'a,b,c,d'
     self.assertRaises(IIIFError, r.parse_region)
     # zero size
     r.region = '0,0,0,100'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = '0,0,100,0'
     self.assertRaises(IIIFError, r.parse_region)
     # bad  name
     r.region = '!square'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = 'square!'
     self.assertRaises(IIIFError, r.parse_region)
     r.region = ''
     self.assertRaises(IIIFError, r.parse_region)
Example #44
0
 def test06_parse_rotation_bad(self):
     r = IIIFRequest(api_version="1.1")
     r.rotation = "-1"
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = "-0.0000001"
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = "360.0000001"
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = "abc"
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = "1!"
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = "!!4"
     self.assertRaises(IIIFError, r.parse_rotation)
Example #45
0
 def test04_parse_size_bad(self):
     """Parse size - bad requests."""
     r = IIIFRequest(api_version='2.1')
     self.assertRaises(IIIFError, r.parse_size, ',0.0')
     self.assertRaises(IIIFError, r.parse_size, '0.0,')
     self.assertRaises(IIIFError, r.parse_size, '1.0,1.0')
     self.assertRaises(IIIFError, r.parse_size, '1,1,1')
     self.assertRaises(IIIFError, r.parse_size, 'bad-size')
     # bad pct size
     self.assertRaises(IIIFError, r.parse_size, 'pct:a')
     self.assertRaises(IIIFError, r.parse_size, 'pct:-1')
     # bad bang pixel size
     self.assertRaises(IIIFError, r.parse_size, '!1,')
     self.assertRaises(IIIFError, r.parse_size, '!,1')
     self.assertRaises(IIIFError, r.parse_size, '0,1')
     self.assertRaises(IIIFError, r.parse_size, '2,0')
Example #46
0
 def test06_parse_rotation_bad(self):
     r = IIIFRequest()
     r.rotation='-1'
     self.assertRaises( IIIFError, r.parse_rotation )
     r.rotation='-0.0000001'
     self.assertRaises( IIIFError, r.parse_rotation )
     r.rotation='360.0000001'
     self.assertRaises( IIIFError, r.parse_rotation )
     r.rotation='abc'
     self.assertRaises( IIIFError, r.parse_rotation )
     r.rotation='1!'
     self.assertRaises( IIIFError, r.parse_rotation )
     r.rotation='!!4'
     self.assertRaises( IIIFError, r.parse_rotation )
Example #47
0
 def test02_parse_region_bad(self):
     r = IIIFRequest()
     r.region='pct:0,0,50,1000'
     self.assertRaises( IIIFError, r.parse_region )
     r.region='pct:-10,0,50,100'
     self.assertRaises( IIIFError, r.parse_region )
     r.region='pct:a,b,c,d'
     self.assertRaises( IIIFError, r.parse_region )
     r.region='a,b,c,d'
     self.assertRaises( IIIFError, r.parse_region )
     # zero size
     r.region='0,0,0,100'
     self.assertRaises( IIIFError, r.parse_region )
     r.region='0,0,100,0'
     self.assertRaises( IIIFError, r.parse_region )
Example #48
0
 def test06_parse_rotation_bad(self):
     """Parse rotation - bad requests."""
     r = IIIFRequest(api_version='2.1')
     r.rotation = '-1'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = '-0.0000001'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = '360.0000001'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = 'abc'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = '1!'
     self.assertRaises(IIIFError, r.parse_rotation)
     r.rotation = '!!4'
     self.assertRaises(IIIFError, r.parse_rotation)
Example #49
0
 def test11_size_to_apply(self):
     """Test size_to_apply."""
     m = IIIFManipulator()
     m.request = IIIFRequest()
     # full
     m.request.size_full = True
     self.assertEqual(m.size_to_apply(), (None, None))
     # pct
     m.width = 1000
     m.height = 2000
     m.request.size_full = False
     m.request.size_pct = 10
     self.assertEqual(m.size_to_apply(), (100, 200))
     # pct > 100%
     m.width = 1000
     m.height = 2000
     m.request.size_pct = 101
     self.assertEqual(m.size_to_apply(), (1010, 2020))
     # !w,h
     m.request.size_pct = None
     m.request.size_bang = True
     m.request.size_wh = (100, 400)
     self.assertEqual(m.size_to_apply(), (100, 200))
     # w,h
     m.request.size_bang = False
     m.request.size_wh = (100, 400)
     self.assertEqual(m.size_to_apply(), (100, 400))
     # w,
     m.request.size_bang = False
     m.request.size_wh = (100, None)
     self.assertEqual(m.size_to_apply(), (100, 200))
     # ,h
     m.request.size_bang = False
     m.request.size_wh = (None, 100)
     self.assertEqual(m.size_to_apply(), (50, 100))
     # ,h zero size
     m.request.size_bang = False
     m.request.size_wh = (None, 0)
     self.assertRaises(IIIFError, m.size_to_apply)
     # w, -> full
     m.request.size_bang = False
     m.request.size_wh = (1000, None)
     self.assertEqual(m.size_to_apply(), (None, None))
Example #50
0
 def test07_do_quality(self):
     m = IIIFManipulator()
     m.request = IIIFRequest()
     m.api_version = '1.1'
     m.request.quality = 'native'
     m.do_quality()
     m.api_version = '2.0'
     m.request.quality = 'default'
     m.do_quality()
     # raise it not appropriate no-op
     m.api_version = '1.1'
     m.request.quality = 'default'
     self.assertRaises(IIIFError, m.do_quality)
     m.api_version = '1.1'
     m.request.quality = 'other'
     self.assertRaises(IIIFError, m.do_quality)
     m.api_version = '2.0'
     m.request.quality = 'native'
     self.assertRaises(IIIFError, m.do_quality)
     m.api_version = '2.0'
     m.request.quality = 'other'
     self.assertRaises(IIIFError, m.do_quality)
Example #51
0
 def generate_tile(self,region,size):
     """Generate one tile for this given region,size of this region."""
     r = IIIFRequest(identifier=self.identifier,api_version=self.api_version)
     if (region == 'full'):
         r.region_full = True
     else:
         r.region_xywh = region # [rx,ry,rw,rh]
     r.size_wh = [size[0],None] # [sw,sh] -> [sw,] canonical form
     r.format = 'jpg'
     path = r.url()
     # Generate...
     if (self.dryrun):
         print "%s / %s" % (self.dst,path)
     else:
         m = IIIFManipulatorPIL(api_version=self.api_version)
         try:
             m.derive(srcfile=self.src, request=r, outfile=os.path.join(self.dst,path))
             print "%s / %s" % (self.dst,path)
         except IIIFZeroSizeError as e:
             print "%s / %s - zero size, skipped" % (self.dst,path)
Example #52
0
    def test19_split_url(self):
        """Test split_url() method.

        Most parts are common to all versions, except handling
        of info.json extensions.
        """
        # api_version=1.0, format=xyz -> bad
        r = IIIFRequest(api_version='1.0')
        r.baseurl = 'http://ex.org/a/'
        self.assertRaises(IIIFError, r.split_url,
                          'http://ex.org/a/b/info.xyz')
        # api_version=2.1, format=xml -> bad
        r = IIIFRequest(api_version='2.1')
        r.baseurl = 'http://ex.org/a/'
        self.assertRaises(IIIFError, r.split_url,
                          'http://ex.org/a/b/info.xml')
        # api_version=2.1, format=xyz -> bad
        r = IIIFRequest(api_version='2.1')
        r.baseurl = 'http://ex.org/a/'
        self.assertRaises(IIIFError, r.split_url,
                          'http://ex.org/a/b/info.xyz')
Example #53
0
 def test01_parse_region(self):
     r = IIIFRequest()
     r.region=None
     r.parse_region()
     self.assertTrue(r.region_full)
     r.region='full'
     r.parse_region()
     self.assertTrue(r.region_full)
     r.region='0,1,90,100'
     r.parse_region()
     self.assertFalse(r.region_full)
     self.assertFalse(r.region_pct)
     self.assertEqual(r.region_xywh, [0,1,90,100] )
     r.region='pct:2,3,91,99'
     r.parse_region()
     self.assertFalse(r.region_full)
     self.assertTrue(r.region_pct)
     self.assertEqual(r.region_xywh, [2,3,91,99] )
     r.region='pct:10,10,50,50'
     r.parse_region()
     self.assertFalse(r.region_full)
     self.assertTrue(r.region_pct)
     self.assertEqual(r.region_xywh, [10.0,10.0,50.0,50.0])
     r.region='pct:0,0,100,100'
     r.parse_region()
     self.assertFalse(r.region_full)
     self.assertTrue(r.region_pct)
     self.assertEqual(r.region_xywh, [0.0,0.0,100.0,100.0])
Example #54
0
 def test20_parse_w_comma_h(self):
     r = IIIFRequest()
     self.assertEquals( r._parse_w_comma_h('1,2','a'), (1,2) )
Example #55
0
 def test07_parse_quality(self):
     r = IIIFRequest()
     r.quality=None
     r.parse_quality()
     self.assertEqual(r.quality_val, 'default')
     r.quality='default'
     r.parse_quality()
     self.assertEqual(r.quality_val, 'default')
     r.quality='bitonal'
     r.parse_quality()
     self.assertEqual(r.quality_val, 'bitonal')
     r.quality='gray'
     r.parse_quality()
     self.assertEqual(r.quality_val, 'gray')
Example #56
0
 def test05_parse_rotation(self):
     r = IIIFRequest()
     r.parse_rotation('0')
     self.assertEqual(r.rotation_mirror, False)
     self.assertEqual(r.rotation_deg, 0.0)
     r.parse_rotation('0.0000')
     self.assertEqual(r.rotation_mirror, False)
     self.assertEqual(r.rotation_deg, 0.0)
     r.parse_rotation('0.000001')
     self.assertEqual(r.rotation_mirror, False)
     self.assertEqual(r.rotation_deg, 0.000001)
     r.parse_rotation('180')
     self.assertEqual(r.rotation_mirror, False)
     self.assertEqual(r.rotation_deg, 180.0)
     r.parse_rotation('360')
     self.assertEqual(r.rotation_mirror, False)
     self.assertEqual(r.rotation_deg, 0.0)
     r.parse_rotation('!0')
     self.assertEqual(r.rotation_mirror, True)
     self.assertEqual(r.rotation_deg, 0.0)
     r.parse_rotation('!0.000')
     self.assertEqual(r.rotation_mirror, True)
     self.assertEqual(r.rotation_deg, 0.0)
     r.parse_rotation('!123.45678')
     self.assertEqual(r.rotation_mirror, True)
     self.assertEqual(r.rotation_deg, 123.45678)
Example #57
0
 def test03_parse_size(self):
     r = IIIFRequest()
     r.parse_size('pct:100')
     self.assertEqual(r.size_pct, 100.0)
     self.assertFalse(r.size_bang)
     r.parse_size('1,2')
     self.assertFalse(r.size_pct)
     self.assertFalse(r.size_bang)
     self.assertEqual(r.size_wh, (1,2))
     r.parse_size('3,')
     self.assertFalse(r.size_pct)
     self.assertFalse(r.size_bang)
     self.assertEqual(r.size_wh, (3,None))
     r.parse_size(',4')
     self.assertFalse(r.size_pct)
     self.assertFalse(r.size_bang)
     self.assertEqual(r.size_wh, (None,4))
     r.parse_size('!5,6')
     self.assertFalse(r.size_pct)
     self.assertTrue(r.size_bang)
     self.assertEqual(r.size_wh, (5,6))
Example #58
0
 def test01_parse_w_comma_h(self):
     """Test w,h parsing."""
     r = IIIFRequest()
     self.assertEqual(r._parse_w_comma_h('1,2', 'a'), (1, 2))
     self.assertRaises(IIIFError, r._parse_w_comma_h, ',', 'region')
     self.assertRaises(IIIFError, r._parse_w_comma_h, '1.0,1.0', 'size')
Example #59
0
 def test04_allow_slashes_in_identifier_munger(self):
     """Test request munger, list in, list out."""
     r = IIIFRequest()
     # Image requests
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['a', 'b', 'c', 'region', 'size', 'rotation', 'quality.ext']),
         ['a/b/c', 'region', 'size', 'rotation', 'quality.ext'])
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['a', 'b', 'c', 'd', 'region', 'size', 'rotation', 'quality.ext']),
         ['a/b/c/d', 'region', 'size', 'rotation', 'quality.ext'])
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['a', 'region', 'size', 'rotation', 'quality.ext']),
         ['a', 'region', 'size', 'rotation', 'quality.ext'])
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['region', 'size', 'rotation', 'quality.ext']),
         ['region', 'size', 'rotation', 'quality.ext'])
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['size', 'rotation', 'quality.ext']),
         ['size', 'rotation', 'quality.ext'])
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['something']),
         ['something'])
     # Image Information requests
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['a', 'b', 'c', 'info.ext']),
         ['a/b/c', 'info.ext'])
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['a', 'b', 'info.ext']),
         ['a/b', 'info.ext'])
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['a', 'info.ext']),
         ['a', 'info.ext'])
     self.assertEqual(r._allow_slashes_in_identifier_munger(
         ['info.ext']),
         ['info.ext'])
Example #60
0
 def test03_str(self):
     """Simple tests of str() method."""
     r = IIIFRequest()
     r.baseurl = 'http://ex.org/'
     r.identifier = 'abc'
     # info
     r.info = True
     r.format = 'json'
     self.assertTrue(re.search(r'INFO request', str(r)))
     self.assertTrue(re.search(r'format=json', str(r)))
     # non-info
     r.info = False
     r.region = 'R'
     r.size = 'S'
     r.rotation = 'X'
     r.quality = 'Q'
     r.format = 'jpg'
     self.assertFalse(re.search(r'INFO request', str(r)))
     self.assertTrue(re.search(r'region=R', str(r)))
     self.assertTrue(re.search(r'format=jpg', str(r)))