yet another stupid bug
[librarian.git] / librarian / formats / cover / evens / __init__.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import urllib
7 from urllib2 import urlopen, URLError
8
9 from librarian import DCNS, BuildError
10 from .. import Cover
11
12
13 class EvensCover(Cover):
14     format_name = u"Evens cover image"
15     width = 1024
16     height = 1365
17     author_top = 900
18     title_top = 30
19     logo_bottom = 100
20
21     def set_images(self, ctx):
22         try:
23             cover_url = self.doc.meta.get(DCNS('relation.coverimage.url'))[0]
24         except IndexError:
25             raise BuildError('No cover specified (metadata field relation.coverimage.url missing)')
26         if not cover_url:
27             raise BuildError('No cover specified (metadata field relation.coverimage.url empty)')
28         if cover_url.startswith('file://'):
29             cover_url = ctx.files_path + urllib.quote(cover_url[7:])
30         IMG_EXT = ('png', 'jpg', 'jpeg', 'gif')
31         if '.' not in cover_url or cover_url.rsplit('.')[1].lower() not in IMG_EXT:
32             raise BuildError('Wrong cover format, should be PNG, JPG or GIF')
33         try:
34             self.background_img = urlopen(cover_url)
35         except URLError:
36             raise BuildError('Cannot open the cover image: %s' % cover_url)
37
38         if getattr(ctx, 'cover_logo', None):
39             self.logo_width = 150
40             self.logo_file = urlopen(ctx.cover_logo)