Completed
Pull Request — master (#194)
by Jace
03:26
created

ImageStore   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%
Metric Value
dl 0
loc 34
rs 10
ccs 21
cts 24
cp 0.875
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 4 1
A __init__() 0 3 1
A latest() 0 3 1
B create() 0 17 5
1
# pylint: disable=no-member
0 ignored issues
show
introduced by
Locally disabling no-member (E1101)
Loading history...
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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__)
0 ignored issues
show
Coding Style Naming introduced by
The name log does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
11
12
13 1
class WordModel(db.Model):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
14
15 1
    __tablename__ = 'words'
16
17 1
    id = sa.Column(sa.String, primary_key=True, nullable=False)
0 ignored issues
show
Coding Style Naming introduced by
The name id does not conform to the class attribute naming conventions (([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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:
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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:
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
76 1
        return os.path.join(self.root, self.LATEST)
77
78 1
    def exists(self, image):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
79 1
        image.root = self.root
80
        # TODO: add a way to determine if the styled image was already generated
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
81 1
        return os.path.isfile(image.path) and not image.style
82
83 1
    def create(self, image):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
84 1
        if self.exists(image) and not self.regenerate_images:
85
            return
86
87 1
        try:
88 1
            ImageModel(image)
89
        except ImportError:
90
            log.warning("Unable to store models on this machine")
91
92 1
        image.root = self.root
93 1
        image.generate()
94
95 1
        try:
96 1
            os.remove(self.latest)
97 1
        except IOError:
98 1
            pass
99
        os.symlink(image.path, self.latest)
100