Passed
Push — develop ( de3ad7...3c2cc7 )
by Dean
02:41
created

Shows   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 6%
Metric Value
dl 0
loc 130
ccs 3
cts 50
cp 0.06
rs 10
wmc 21

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 122 21
1 1
from plugin.core.constants import GUID_SERVICES
2 1
from plugin.sync.core.enums import SyncData, 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
    data = [
15
        SyncData.Collection,
16
        SyncData.Playback,
17
        SyncData.Ratings,
18
        SyncData.Watched
19
    ]
20
21 1
    @elapsed.clock
22
    def run(self):
23
        # Retrieve show sections
24
        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...
25
26
        # Fetch episodes with account settings
27
        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...
28
            p_sections, ([
29
                MetadataItem.library_section
30
            ], [], []),
31
            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...
32
            parse_guid=True
33
        )
34
35
        # TODO process seasons
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
36
37
        # Calculate total number of episodes
38
        pending = {}
39
40
        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...
41
            t_episodes = [
42
                (key, se, ep)
43
                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...
44
                for se, t_season in t_show.seasons.items()
45
                for ep in t_season.episodes.iterkeys()
46
            ]
47
48
            if data not in pending:
49
                pending[data] = {}
50
51
            for key in t_episodes:
52
                pending[data][key] = False
53
54
        # Task started
55
        unsupported_shows = {}
56
57
        # Process shows
58
        for sh_id, guid, p_show in p_shows:
59
            if not guid or guid.service not in GUID_SERVICES:
60
                mark_unsupported(unsupported_shows, sh_id, guid)
61
                continue
62
63
            key = (guid.service, guid.id)
64
65
            # Try retrieve `pk` for `key`
66
            pk = self.trakt.table('shows').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...
67
68
            # Store in item map
69
            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...
70
71
            if pk is None:
72
                # No `pk` found
73
                continue
74
75
            # Execute data handlers
76
            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...
77
                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...
78
79
                # Execute show handlers
80
                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...
81
                    SyncMedia.Shows, data,
82
                    key=sh_id,
83
84
                    p_item=p_show,
85
                    t_item=t_show
86
                )
87
88
        # Process episodes
89
        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...
90
            if not guid or guid.service not in GUID_SERVICES:
91
                mark_unsupported(unsupported_shows, ids['show'], guid)
92
                continue
93
94
            key = (guid.service, guid.id)
95
96
            # Try retrieve `pk` for `key`
97
            pk = self.trakt.table('shows').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...
98
99
            if pk is None:
100
                # No `pk` found
101
                continue
102
103
            if not ids.get('episode'):
104
                # Missing `episode` rating key
105
                continue
106
107
            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...
108
                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...
109
110
                if t_show is None:
111
                    # Unable to find matching show in trakt data
112
                    continue
113
114
                t_season = t_show.seasons.get(season_num)
115
116
                if t_season is None:
117
                    # Unable to find matching season in `t_show`
118
                    continue
119
120
                t_episode = t_season.episodes.get(episode_num)
121
122
                if t_episode is None:
123
                    # Unable to find matching episode in `t_season`
124
                    continue
125
126
                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...
127
                    SyncMedia.Episodes, data,
128
                    key=ids['episode'],
129
130
                    p_item=p_episode,
131
                    t_item=t_episode
132
                )
133
134
                # Increment one step
135
                self.step(pending, data, (pk, season_num, episode_num))
136
137
            # Task checkpoint
138
            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...
139
140
        # Log details
141
        log_unsupported(log, 'Found %d unsupported show(s)\n%s', unsupported_shows)
142
        log.debug('Pending: %r', pending)
143