Test Setup Failed
Push — develop ( 06bf6f...d0ed0b )
by Dean
03:31
created

Shows.run()   F

Complexity

Conditions 21

Size

Total Lines 122

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 435.05
Metric Value
cc 21
dl 0
loc 122
ccs 1
cts 48
cp 0.0208
crap 435.05
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like Shows.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1 1
from plugin.sync.core.constants import GUID_AGENTS
2 1
from plugin.sync.core.enums import SyncMedia
3 1
from plugin.sync.modes.core.base import log_unsupported, mark_unsupported
4 1
from plugin.sync.modes.pull.base import Base
5
6 1
from plex_database.models import MetadataItem
7 1
import elapsed
8 1
import logging
9
10 1
log = logging.getLogger(__name__)
11
12
13 1
class Shows(Base):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
14 1
    @elapsed.clock
15
    def run(self):
16
        # Retrieve show sections
17
        p_sections, p_sections_map = self.sections('show')
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named sections.

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...
Unused Code introduced by
The variable p_sections_map seems to be unused.
Loading history...
18
19
        # Fetch episodes with account settings
20
        p_shows, p_seasons, p_episodes = self.plex.library.episodes.mapped(
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named plex.

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...
Unused Code introduced by
The variable p_seasons seems to be unused.
Loading history...
21
            p_sections, ([
22
                MetadataItem.library_section
23
            ], [], []),
24
            account=self.current.account.plex.key,
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named current.

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...
25
            parse_guid=True
26
        )
27
28
        # TODO process seasons
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
29
30
        # Calculate total number of episodes
31
        pending = {}
32
33
        for data in self.get_data(SyncMedia.Episodes):
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named get_data.

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...
34
            t_episodes = [
35
                (key, se, ep)
36
                for key, t_show in self.trakt[(SyncMedia.Episodes, data)].items()
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named trakt.

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...
37
                for se, t_season in t_show.seasons.items()
38
                for ep in t_season.episodes.iterkeys()
39
            ]
40
41
            if data not in pending:
42
                pending[data] = {}
43
44
            for key in t_episodes:
45
                pending[data][key] = False
46
47
        # Task started
48
        unsupported_shows = {}
49
50
        # Process shows
51
        for sh_id, guid, p_show in p_shows:
52
            if not guid or guid.agent not in GUID_AGENTS:
53
                mark_unsupported(unsupported_shows, sh_id, guid, p_show)
54
                continue
55
56
            key = (guid.agent, guid.sid)
57
58
            # Try retrieve `pk` for `key`
59
            pk = self.trakt.table.get(key)
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named trakt.

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...
60
61
            # Store in item map
62
            self.current.map.add(p_show.get('library_section'), sh_id, [key, pk])
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named current.

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...
63
64
            if pk is None:
65
                # No `pk` found
66
                continue
67
68
            # Execute data handlers
69
            for data in self.get_data(SyncMedia.Shows):
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named get_data.

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...
70
                t_show = self.trakt[(SyncMedia.Shows, data)].get(pk)
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named trakt.

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...
71
72
                # Execute show handlers
73
                self.execute_handlers(
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named execute_handlers.

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...
74
                    SyncMedia.Shows, data,
75
                    key=sh_id,
76
77
                    p_item=p_show,
78
                    t_item=t_show
79
                )
80
81
        # Process episodes
82
        for ids, guid, (season_num, episode_num), p_show, p_season, p_episode in p_episodes:
0 ignored issues
show
Unused Code introduced by
The variable p_season seems to be unused.
Loading history...
83
            if not guid or guid.agent not in GUID_AGENTS:
84
                mark_unsupported(unsupported_shows, ids['show'], guid, p_show)
85
                continue
86
87
            key = (guid.agent, guid.sid)
88
89
            # Try retrieve `pk` for `key`
90
            pk = self.trakt.table.get(key)
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named trakt.

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...
91
92
            if pk is None:
93
                # No `pk` found
94
                continue
95
96
            if not ids.get('episode'):
97
                # Missing `episode` rating key
98
                continue
99
100
            for data in self.get_data(SyncMedia.Episodes):
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named get_data.

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...
101
                t_show = self.trakt[(SyncMedia.Episodes, data)].get(pk)
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named trakt.

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
103
                if t_show is None:
104
                    # Unable to find matching show in trakt data
105
                    continue
106
107
                t_season = t_show.seasons.get(season_num)
108
109
                if t_season is None:
110
                    # Unable to find matching season in `t_show`
111
                    continue
112
113
                t_episode = t_season.episodes.get(episode_num)
114
115
                if t_episode is None:
116
                    # Unable to find matching episode in `t_season`
117
                    continue
118
119
                self.execute_handlers(
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named execute_handlers.

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...
120
                    SyncMedia.Episodes, data,
121
                    key=ids['episode'],
122
123
                    p_item=p_episode,
124
                    t_item=t_episode
125
                )
126
127
                # Increment one step
128
                self.step(pending, data, (pk, season_num, episode_num))
129
130
            # Task checkpoint
131
            self.checkpoint()
0 ignored issues
show
Bug introduced by
The Instance of Shows does not seem to have a member named checkpoint.

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...
132
133
        # Log details
134
        log_unsupported(log, 'Found %d unsupported show(s)\n%s', unsupported_shows)
135
        log.debug('Pending: %r', pending)
136