Completed
Pull Request — master (#391)
by Jace
08:05
created

Cache._skip_cache()   B

Complexity

Conditions 7

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.116

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
dl 0
loc 19
ccs 13
cts 15
cp 0.8667
crap 7.116
rs 7.3333
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/cache/{self.name}.yml")
13
class Cache:
14
15 1
    SIZE = 100
16
17 1
    def __init__(self, filtered=True):
18 1
        self.items = []
19 1
        self.disabled = False
20 1
        self.filtered = filtered
21
22 1
    @property
23
    def name(self):
24 1
        return 'filtered' if self.filtered else 'unfiltered'
25
26 1
    def add(self, **kwargs):
27 1
        if self._skip_cache(kwargs):
28 1
            return
29
30 1
        log.info("Caching: %s", kwargs)
31
32 1
        self.items.insert(0, kwargs)
33 1
        while len(self.items) > self.SIZE:
34
            self.items.pop()
35
36 1
    def get(self, index):
37 1
        log.info("Getting cache index: %s", index)
38
39 1
        try:
40 1
            data = self.items[index]
41 1
        except IndexError:
42 1
            data = {}
43
44 1
        log.info("Retrieved cache: %s", data)
45
46 1
        return data
47
48 1
    def _skip_cache(self, kwargs):
49 1
        if self.disabled:
50
            log.debug("Caching disabled")
51
            return True
52
53 1
        if kwargs in self.items:
54 1
            log.debug("Already cached: %s", kwargs)
55 1
            return True
56
57 1
        if self.filtered:
58
59 1
            if kwargs['key'] == 'custom' or kwargs.get('alt'):
60 1
                log.debug("Skipped caching of custom background: %s", kwargs)
61 1
                return True
62 1
            if profanityfilter.is_profane(kwargs['path']):
63 1
                log.debug("Skipped caching of profane content: %s", kwargs)
64 1
                return True
65
66
        return False
67