Total Complexity | 6 |
Total Lines | 36 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import log |
||
2 | |||
3 | from ._base import Service |
||
4 | from ..domain import Image, Font |
||
5 | |||
6 | |||
7 | class ImageService(Service): |
||
8 | |||
9 | def __init__(self, template_store, font_store, image_store, **kwargs): |
||
10 | super().__init__(**kwargs) |
||
11 | self.template_store = template_store |
||
12 | self.font_store = font_store |
||
13 | self.image_store = image_store |
||
14 | |||
15 | def create(self, template, text, font=None, **options): |
||
16 | image = Image( |
||
17 | template, text, |
||
18 | font=font or self.font_store.find(Font.DEFAULT), |
||
19 | watermark_font=self.font_store.find(Font.WATERMARK), |
||
20 | **options, |
||
21 | ) |
||
22 | |||
23 | try: |
||
24 | self.image_store.create(image) |
||
25 | except OSError as exception: |
||
26 | if "name too long" in str(exception): |
||
27 | exception = self.exceptions.FilenameTooLong |
||
28 | elif "image file" in str(exception): |
||
29 | exception = self.exceptions.InvalidImageLink |
||
30 | raise exception # pylint: disable=raising-bad-type |
||
31 | except (ValueError, SystemError) as exception: |
||
32 | log.warning(exception) |
||
33 | raise self.exceptions.InvalidImageLink |
||
34 | |||
35 | return image |
||
36 |