Completed
Pull Request — master (#167)
by
unknown
21:11 queued 11:01
created

MemeModel.__repr__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

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