Completed
Pull Request — master (#247)
by Jace
02:55
created

Cache.add()   B

Complexity

Conditions 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.5412

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
c 2
b 0
f 0
dl 0
loc 18
ccs 8
cts 15
cp 0.5333
crap 7.5412
rs 8.5454
1 1
import logging
2
3 1
import yorm
0 ignored issues
show
Configuration introduced by
The import yorm 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 yorm.types import List, Object
0 ignored issues
show
Configuration introduced by
The import yorm.types 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 1
import profanityfilter
0 ignored issues
show
Configuration introduced by
The import profanityfilter 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
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
@yorm.attr(items=List.of_type(Object))
12 1
@yorm.sync("data/images/cache.yml")
13
class Cache:
14
15 1
    SIZE = 9
16
17 1
    def __init__(self):
18 1
        self.items = []
19
20 1
    def add(self, **kwargs):
21 1
        if kwargs in self.items:
22
            log.debug("Already cached: %s", kwargs)
23
            return
24 1
        if kwargs['key'] == 'custom':
25
            log.debug("Skipped caching of custom: %s", kwargs)
26
            return
27 1
        if profanityfilter.is_profane(kwargs['path']):
28
            log.debug("Skipped caching of profanity: %s", kwargs)
29
            return
30
31 1
        log.info("Caching: %s", kwargs)
32
33 1
        self.items.insert(0, kwargs)
34 1
        while len(self.items) > self.SIZE:
35
            self.items.pop()
36
37 1
        yorm.save(self)
38
39 1
    def get(self, index):
40 1
        log.info("Getting cache index: %s", index)
41
42 1
        try:
43 1
            data = self.items[index]
44 1
        except IndexError:
45 1
            data = {}
46
47 1
        log.info("Retrieved cache: %s", data)
48
49
        return data
50