Completed
Pull Request — master (#132)
by Jace
08:27 queued 07:15
created

Template.default_text()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
cc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1 1
import os
2 1
import logging
3
4 1
import time
5 1
import requests
6 1
from PIL import Image
0 ignored issues
show
Configuration introduced by
The import PIL could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
7
8 1
from .text import Text
9
10
11 1
log = logging.getLogger(__name__)
12
13
14 1
class Template:
15
    """Blank image to generate a meme."""
16
17 1
    DEFAULT = 'default'
18 1
    EXTENSIONS = ('.png', '.jpg')
19
20 1
    SAMPLE_LINES = ["YOUR TEXT", "GOES HERE"]
21
22 1
    VALID_LINK_FLAG = '.valid_link.tmp'
23
24 1
    MIN_HEIGHT = 240
25 1
    MIN_WIDTH = 240
26
27 1
    def __init__(self, key,
28
                 name=None, lines=None, aliases=None, link=None, root=None):
29 1
        self.key = key
30 1
        self.name = name or ""
31 1
        self.lines = lines or []
32 1
        self.aliases = aliases or []
33 1
        self.link = link or ""
34 1
        self.root = root or ""
35
36 1
    def __str__(self):
37 1
        return self.key
38
39 1
    def __eq__(self, other):
40 1
        return self.key == other.key
41
42 1
    def __ne__(self, other):
43 1
        return self.key != other.key
44
45 1
    def __lt__(self, other):
46 1
        return self.name < other.name
47
48 1
    @property
49
    def path(self):
50 1
        return self.get_path()
51
52 1
    @property
53
    def default_text(self):
54 1
        return Text(self.lines)
55
56 1
    @property
57
    def default_path(self):
58 1
        return self.default_text.path or Text.EMPTY
59
60 1
    @property
61
    def sample_text(self):
62 1
        return self.default_text or Text(self.SAMPLE_LINES)
63
64 1
    @property
65
    def sample_path(self):
66 1
        return self.sample_text.path
67
68 1
    @property
69
    def aliases_lowercase(self):
70 1
        return [self.strip(a, keep_special=True) for a in self.aliases]
71
72 1
    @property
73
    def aliases_stripped(self):
74 1
        return [self.strip(a, keep_special=False) for a in self.aliases]
75
76 1
    @staticmethod
77 1
    def strip(text, keep_special=False):
78 1
        text = text.lower().strip().replace(' ', '-')
79 1
        if not keep_special:
80 1
            for char in ('-', '_', '!', "'"):
81 1
                text = text.replace(char, '')
82 1
        return text
83
84 1
    def get_path(self, *styles):
85 1
        for name in (n.lower() for n in (*styles, self.DEFAULT) if n):
86 1
            for extension in self.EXTENSIONS:
87 1
                path = os.path.join(self.root, self.key, name + extension)
88 1
                if os.path.isfile(path):
89 1
                    return path
90 1
        return None
91
92 1
    def validate(self, validators=None):
93 1
        if validators is None:
94
            validators = [
95
                self.validate_meta,
96
                self.validate_link,
97
                self.validate_size,
98
            ]
99 1
        for validator in validators:
100 1
            if not validator():
101 1
                return False
102 1
        return True
103
104 1
    def validate_meta(self):
105 1
        if not self.lines:
106
            log.error("Template '%s' has no default lines of text", self)
107
            return False
108 1
        if not self.name:
109 1
            log.error("Template '%s' has no name", self)
110 1
            return False
111 1
        if not self.name[0].isalnum():
112 1
            msg = "Template '%s' name %r should start with an alphanumeric"
113 1
            log.error(msg, self, self.name)
114 1
            return False
115 1
        if not self.path:
116 1
            log.error("Template '%s' has no default image", self)
117 1
            return False
118
        return True
119
120 1
    def validate_link(self):
121 1
        if self.link:
122 1
            flag = os.path.join(self.root, self.key, self.VALID_LINK_FLAG)
123 1
            if os.path.isfile(flag):
124 1
                log.info("Link already checked: %s", self.link)
125
            else:
126 1
                log.info("Checking link %s ...", self.link)
127 1
                try:
128 1
                    response = requests.get(self.link, timeout=5)
129
                except requests.exceptions.ReadTimeout:
0 ignored issues
show
Bug introduced by
The Module requests.exceptions does not seem to have a member named ReadTimeout.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
130
                    log.warning("Connection timed out")
131
                    return True  # assume URL is OK; it will be checked again
132 1
                if response.status_code >= 400 and response.status_code != 429:
133 1
                    msg = "Template '%s' link is invalid (%s)"
134 1
                    log.error(msg, self, response.status_code)
135 1
                    return False
136
                else:
137
                    with open(flag, 'w') as stream:
138
                        stream.write(str(int(time.time())))
139 1
        return True
140
141 1
    def validate_size(self):
142 1
        im = Image.open(self.path)
143 1
        w, h = im.size
144 1
        if w < self.MIN_WIDTH or h < self.MIN_HEIGHT:
145 1
            log.error("Image must be at least %ix%i (is %ix%i)",
146
                      self.MIN_WIDTH, self.MIN_HEIGHT, w, h)
147 1
            return False
148
        return True
149