def __init__(self, path, query, headers, post_data=None): if not _BASE_URL_RE.match(path): raise IllegalRequestError('Request path "%s" does not match expected format' % path) self.path = path.split('/') self.query = _flatten_qs(parse_qs(query)) self.headers = headers id, size, format = parse_filename('/'.join(self.path[-2:])) self.id = id self.size = size self.format = format
def read_best(self, image_id, image_size): best_size = None best_format = None best_area = None for realpath in self.search(image_id): filename = realpath[:] _, size, format = parse_filename(filename) if best_size is None or size.area > best_area: best_size = size best_format = format best_area = size.area return self.read(image_id, best_size, best_format)
def test_parse_filename(self): id, size, format = filename.parse_filename('name/100x200.jpg') self.assertEqual(id, 'name') self.assertEqual(size, (100, 200)) self.assertEqual(format, 'JPEG') id, size, format = filename.parse_filename('name_9999/100x200.jpg') self.assertEqual(id, 'name_9999') self.assertEqual(size, (100, 200)) self.assertEqual(format, 'JPEG') id, size, format = filename.parse_filename('name/x200.png') self.assertEqual(id, 'name') self.assertEqual(size, ('', 200)) self.assertEqual(format, 'PNG') id, size, format = filename.parse_filename('name/100x.webp') self.assertEqual(id, 'name') self.assertEqual(size, (100, '')) self.assertEqual(format, 'WEBP') self.assertRaises(UnsupportImageFormatError, filename.parse_filename, 'name/100x200.txt') self.assertRaises(IllegalFilenameError, filename.parse_filename, '100x200.jpg') self.assertRaises(IllegalFilenameError, filename.parse_filename, 'name.jpg')