Completed
Pull Request — master (#173)
by Josh
10:01 queued 08:49
created

ImageStore.create()   A

Complexity

Conditions 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

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