Completed
Pull Request — master (#302)
by
unknown
03:35 queued 01:22
created

ImageService.create()   B

Complexity

Conditions 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
dl 0
loc 18
rs 8.5454
ccs 12
cts 15
cp 0.8
crap 5.2
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, style=None, font=None):
20 1
        font = font or self.font_store.find(Font.DEFAULT)
21
22 1
        image = Image(template, text, style=style, font=font)
23
24 1
        try:
25 1
            self.image_store.create(image)
26 1
        except OSError as exception:
27 1
            if "name too long" in str(exception):
28 1
                exception = self.exceptions.FilenameTooLong
29 1
            elif "image file" in str(exception):
30 1
                exception = self.exceptions.InvalidImageLink
31 1
            raise exception from None
32
        except SystemError as exception:
33
            log.warning(exception)
34
            raise self.exceptions.InvalidImageLink from None
35
36
        return image
37