Completed
Pull Request — master (#394)
by Jace
08:10
created

ImageService.create()   B

Complexity

Conditions 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.246

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
dl 0
loc 21
ccs 11
cts 14
cp 0.7856
crap 5.246
rs 8.439
c 1
b 0
f 0
1 1
import logging
2
3 1
from ..domain import Image, Font
4
5 1
from ._base import Service
6
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
class ImageService(Service):
12
13 1
    def __init__(self, template_store, font_store, image_store, **kwargs):
14 1
        super().__init__(**kwargs)
15 1
        self.template_store = template_store
16 1
        self.font_store = font_store
17 1
        self.image_store = image_store
18
19 1
    def create(self, template, text, font=None, **options):
20 1
        image = Image(
21
            template, text,
22
            font=font or self.font_store.find(Font.DEFAULT),
23
            watermark_font=self.font_store.find(Font.DEFAULT),
24
            **options,
0 ignored issues
show
introduced by
invalid syntax
Loading history...
25
        )
26
27 1
        try:
28 1
            self.image_store.create(image)
29 1
        except OSError as exception:
30 1
            if "name too long" in str(exception):
31 1
                exception = self.exceptions.FilenameTooLong
32 1
            elif "image file" in str(exception):
33 1
                exception = self.exceptions.InvalidImageLink
34 1
            raise exception from None  # pylint: disable=raising-bad-type
35
        except SystemError as exception:
36
            log.warning(exception)
37
            raise self.exceptions.InvalidImageLink from None
38
39
        return image
40