Completed
Push — master ( 625365...51d9af )
by Jace
18s
created

Cache.add()   B

Complexity

Conditions 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8.1174

Importance

Changes 0
Metric Value
cc 6
dl 0
loc 22
ccs 11
cts 18
cp 0.6111
crap 8.1174
rs 7.7857
c 0
b 0
f 0
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 = 100
16
17 1
    def __init__(self):
18 1
        self.items = []
19 1
        self.disable = False
20
21 1
    def add(self, **kwargs):
22 1
        if self.disable:
23 1
            log.debug("Caching disabled")
24 1
            return
25
26 1
        if kwargs in self.items:
27
            log.debug("Already cached: %s", kwargs)
28
            return
29 1
        if kwargs['key'] == 'custom':
30
            log.debug("Skipped caching of custom background: %s", kwargs)
31
            return
32 1
        if profanityfilter.is_profane(kwargs['path']):
33
            log.debug("Skipped caching of profane content: %s", kwargs)
34
            return
35
36 1
        log.info("Caching: %s", kwargs)
37
38 1
        self.items.insert(0, kwargs)
39 1
        while len(self.items) > self.SIZE:
40
            self.items.pop()
41
42 1
        yorm.save(self)
43
44 1
    def get(self, index):
45 1
        log.info("Getting cache index: %s", index)
46
47 1
        try:
48 1
            data = self.items[index]
49 1
        except IndexError:
50 1
            data = {}
51
52 1
        log.info("Retrieved cache: %s", data)
53
54
        return data
55