Completed
Push — master ( 541538...7813dc )
by Jace
9s
created

WordModel.__repr__()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125
Metric Value
cc 1
dl 0
loc 3
ccs 1
cts 2
cp 0.5
crap 1.125
rs 10
1
# pylint: disable=no-member
2
3 1
import os
4 1
import logging
5
6 1
import sqlalchemy as sa
0 ignored issues
show
Configuration introduced by
The import sqlalchemy 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 ..extensions import db
9
10 1
log = logging.getLogger(__name__)
11
12
13 1
class WordModel(db.Model):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
14
15 1
    __tablename__ = 'words'
16
17 1
    id = sa.Column(sa.String, primary_key=True, nullable=False)
18 1
    meme_id = sa.Column(sa.String, nullable=False)
19 1
    occurances = sa.Column(sa.Integer)
20
21 1
    def __repr__(self):
22
        return "<Word(id='%s', meme_id='%s', occurances='%s')>" \
23
            % (self.id, self.meme_id, self.occurances)
24
25
26 1
class ImageModel:
27
28 1
    def __init__(self, image):
29 1
        self._word_models = {}
30 1
        self._words = []
31 1
        self.key = image.template.key
32
33
        # append all the individual words into an array
34 1
        for line in image.text.lines:
35 1
            self._words += line.lower().split(' ')
36
37
        # look-up the word from the database, count the
38
        # occurances in this particular set of text
39 1
        for word in self._words:
40
            # is there no entry? should we query for one
41
            # or create a new one?
42 1
            if not self._word_models.get(word):
43 1
                model = (
44
                    db.session.query(WordModel)
45
                    .filter(WordModel.id == word)
46
                    .first()
47
                )
48
49
                # doesn't exist, create a new model
50 1
                if not model:
51 1
                    model = WordModel(id=word, meme_id=self.key, occurances=0)
52
53 1
                model.occurances += 1
54 1
                self._word_models[word] = model
55
56
            else:
57 1
                self._word_models[word].occurances += 1
58
59
        # save all the updated occurance counts
60 1
        for key in self._word_models:
61 1
            if self._word_models[key]:
62 1
                db.session.add(self._word_models[key])
63 1
        db.session.commit()
64
65
66 1
class ImageStore:
67
68 1
    LATEST = "latest.jpg"
69
70 1
    def __init__(self, root, config):
71 1
        self.root = root
72 1
        self.regenerate_images = config.get('REGENERATE_IMAGES', False)
73
74 1
    @property
75
    def latest(self):
76 1
        return os.path.join(self.root, self.LATEST)
77
78 1
    def exists(self, image):
79 1
        image.root = self.root
80
        # TODO: add a way to determine if the styled image was already generated
81 1
        return os.path.isfile(image.path) and not image.style
82
83 1
    def create(self, image):
84 1
        if self.exists(image) and not self.regenerate_images:
85
            return
86
87 1
        ImageModel(image)
88
89 1
        image.root = self.root
90 1
        image.generate()
91
92 1
        try:
93 1
            os.remove(self.latest)
94 1
        except IOError:
95 1
            pass
96
        os.symlink(image.path, self.latest)
97