Test Failed
Push — develop ( a80574...5ed66c )
by Dean
02:36
created

Matcher.configure()   C

Complexity

Conditions 7

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.1782

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
ccs 11
cts 13
cp 0.8462
rs 5.5
cc 7
crap 7.1782
1 1
from plugin.core.cache import CacheManager
0 ignored issues
show
Bug introduced by
The name cache does not seem to exist in module plugin.core.
Loading history...
Configuration introduced by
Unable to import 'plugin.core.cache' (invalid syntax (<string>, line 150))

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...
2 1
from plugin.core.enums import MatcherMode
3 1
from plugin.modules.core.base import Module
4 1
from plugin.preferences import Preferences
5
6 1
from plex import Plex
7 1
from plex.objects.library.metadata.episode import Episode
8 1
import plex_database.matcher
9 1
import plex_metadata.matcher
10 1
import logging
11
12 1
log = logging.getLogger(__name__)
13
14
15 1
class Matcher(Module):
16 1
    __key__ = 'matcher'
17
18 1
    def __init__(self):
19 1
        self._cache = None
20
21 1
        self._database_matcher = None
22 1
        self._metadata_matcher = None
23
24 1
    @property
25
    def database(self):
26 1
        return self._database_matcher
27
28 1
    @property
29
    def metadata(self):
30
        return self._metadata_matcher
31
32 1
    def configure(self):
33 1
        extended = Preferences.get('matcher.mode') == MatcherMode.PlexExtended
34
35
        # Configure matchers
36 1
        matchers = [
37
            self._database_matcher,
38
            self._metadata_matcher,
39
40
            # Default matchers
41
            plex_database.matcher.Default,
42
            plex_metadata.matcher.Default
43
        ]
44
45 1
        for matcher in matchers:
46
            if not matcher:
47 1
                continue
48
49
            # Update cache
50 1
            if self._cache:
51 1
                matcher.cache = self._cache
52 1
53
            # Configure features
54 1
            if matcher.caper_enabled != extended or matcher.extend_enabled != extended:
55
                matcher.caper_enabled = extended
56 1
                matcher.extend_enabled = extended
57
58 1
                log.info('Extended matcher has been %s on %r', 'enabled' if extended else 'disabled', matcher)
59
60
        return True
61
62
    def flush(self, force=False):
63
        if not self._cache:
64
            return False
65 1
66
        self._cache.flush(force=force)
67
        return True
68
69
    def prime(self, keys=None, force=False):
70
        if not self._cache:
71
            return DummyContext()
72
73
        return self._cache.prime(
74 1
            keys=keys,
75
            force=force
76
        )
77
78
    def process(self, episode):
79
        if not episode or not isinstance(episode, Episode):
80
            raise ValueError('Invalid value specified for the "episode" parameter: %r' % (episode,))
81
82
        if not self._metadata_matcher:
83
            return episode.season.index, [episode.index]
84
85
        # Process episode with matcher
86
        season, episodes = self._metadata_matcher.process(episode)
87 1
88 1
        log.debug('Matcher returned season: %r, episodes: %r', season, episodes)
89
        return season, episodes
90
91 1
    def start(self):
92
        if self._database_matcher and self._metadata_matcher:
93
            return
94 1
95 1
        if self._database_matcher is False or self._metadata_matcher is False:
96
            return
97 1
98 1
        try:
99
            self._cache = CacheManager.get('plex.matcher')
100
101
            self._database_matcher = plex_database.matcher.Matcher(self._cache, Plex.client)
0 ignored issues
show
Bug introduced by
The Class Plex does not seem to have a member named client.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
102
            self._metadata_matcher = plex_metadata.matcher.Matcher(self._cache, Plex.client)
0 ignored issues
show
Bug introduced by
The Class Plex does not seem to have a member named client.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
103
        except Exception as ex:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
104
            log.warn('Unable to initialize matchers: %s', ex, exc_info=True)
105
106
            # Mark attributes as disabled
107
            self._cache = None
108
            self._database_matcher = None
109 1
            self._metadata_matcher = None
110
            return
111
112 1
        # Configure matchers
113 1
        self.configure()
114
115
116 1
class DummyContext(object):
117
    def __enter__(self):
118
        pass
119
120
    def __exit__(self, exc_type, exc_val, exc_tb):
121
        pass
122