Passed
Push — develop ( 29d60b...a27323 )
by Dean
02:35
created

Lists   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 6.9%
Metric Value
dl 0
loc 149
ccs 4
cts 58
cp 0.069
rs 10
wmc 20

3 Methods

Rating   Name   Duplication   Size   Complexity  
B process_update() 0 42 4
F process_sort() 0 78 13
B process() 0 25 3
1 1
from plugin.sync.core.enums import SyncMedia
2 1
from plugin.sync.core.playlist.mapper import PlaylistMapper
3 1
from plugin.sync.modes.core.base import PullListsMode
4
5 1
import logging
6
7 1
log = logging.getLogger(__name__)
8
9
10 1
class Lists(PullListsMode):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
11 1
    def process(self, data, p_playlists, p_sections_map, t_list):
12
        log.debug('Processing list: %r', t_list)
13
14
        # Create/retrieve plex list
15
        p_playlist = self.get_playlist(
16
            p_playlists,
17
            uri='trakt://list/%s/%s' % (self.current.account.id, t_list.id),
0 ignored issues
show
Bug introduced by
The Instance of Lists 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...
18
            title=t_list.name
19
        )
20
21
        if not p_playlist:
22
            return
23
24
        # Retrieve trakt list items from cache
25
        t_list_items = self.trakt[(SyncMedia.Lists, data, t_list.id)]
0 ignored issues
show
Bug introduced by
The Instance of Lists 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...
26
27
        if t_list_items is None:
28
            log.warn('Unable to retrieve list items for: %r', t_list)
29
            return
30
31
        # Update (add/remove) list items
32
        self.process_update(data, p_playlist, p_sections_map, t_list, t_list_items.itervalues())
33
34
        # Sort list items
35
        self.process_sort(data, p_playlist, p_sections_map, t_list, t_list_items.itervalues())
36
37 1
    def process_update(self, data, p_playlist, p_sections_map, t_list=None, t_list_items=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
        # Construct playlist mapper
39
        mapper = PlaylistMapper(self.current, p_sections_map)
0 ignored issues
show
Bug introduced by
The Instance of Lists 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...
40
41
        # Parse plex playlist items
42
        mapper.plex.load(p_playlist)
43
44
        # Parse trakt list items
45
        mapper.trakt.load(t_list, t_list_items)
46
47
        # Match playlist items and expand shows/seasons
48
        m_trakt, m_plex = mapper.match()
49
50
        log.debug(
51
            'Update - Mapper Result (%d items)\nt_items:\n%s\n\np_items:\n%s',
52
            len(m_trakt) + len(m_plex),
53
            '\n'.join(self.format_mapper_result(m_trakt)),
54
            '\n'.join(self.format_mapper_result(m_plex))
55
        )
56
57
        # Iterate over matched trakt items
58
        for key, index, (p_index, p_items), (t_index, t_items) in m_trakt:
0 ignored issues
show
Unused Code introduced by
The variable t_index seems to be unused.
Loading history...
Unused Code introduced by
The variable index seems to be unused.
Loading history...
Unused Code introduced by
The variable p_index seems to be unused.
Loading history...
59
            # Expand shows/seasons into episodes
60
            for p_item, t_item in self.expand(p_items, t_items):
61
                # Get `SyncMedia` for `t_item`
62
                media = self.get_media(t_item)
63
64
                if media is None:
65
                    log.warn('Unable to identify media of "t_item" (p_item: %r, t_item: %r)', p_item, t_item)
66
                    continue
67
68
                # Execute handler
69
                self.execute_handlers(
0 ignored issues
show
Bug introduced by
The Instance of Lists 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...
70
                    media, data,
71
72
                    p_sections_map=p_sections_map,
73
                    p_playlist=p_playlist,
74
75
                    key=key,
76
77
                    p_item=p_item,
78
                    t_item=t_item
79
                )
80
81 1
    def process_sort(self, data, p_playlist, p_sections_map, t_list, t_list_items):
0 ignored issues
show
Unused Code introduced by
The argument data seems to be unused.
Loading history...
82
        # Construct playlist mapper
83
        mapper = PlaylistMapper(self.current, p_sections_map)
0 ignored issues
show
Bug introduced by
The Instance of Lists 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...
84
85
        # Parse plex playlist items
86
        mapper.plex.load(p_playlist)
87
88
        # Parse trakt list items
89
        mapper.trakt.load(t_list, t_list_items)
90
91
        # Match playlist items and expand shows/seasons
92
        m_trakt, m_plex = mapper.match()
93
94
        log.debug(
95
            'Sort - Mapper Result (%d items)\nt_items:\n%s\n\np_items:\n%s',
96
            len(m_trakt) + len(m_plex),
97
            '\n'.join(self.format_mapper_result(m_trakt)),
98
            '\n'.join(self.format_mapper_result(m_plex))
99
        )
100
101
        # Build a list of plex items (sorted by `p_index`)
102
        p_playlist_items = []
103
104
        for item in mapper.plex.items.itervalues():
0 ignored issues
show
Bug introduced by
The Instance of dict does not seem to have a member named itervalues.

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...
105
            p_playlist_items.append(item)
106
107
        p_playlist_items = [
108
            i[1]
109
            for i in sorted(p_playlist_items, key=lambda i: i[0])
110
        ]
111
112
        # Iterate over trakt items, re-order plex items
113
        t_index = 0
114
115
        for key, _, (_, p_items), (_, t_items) in m_trakt:
0 ignored issues
show
Unused Code introduced by
The variable key seems to be unused.
Loading history...
116
            # Expand shows/seasons into episodes
117
            for p_item, t_item in self.expand(p_items, t_items):
118
                if not p_item:
119
                    continue
120
121
                if p_item not in p_playlist_items:
122
                    log.info('Unable to find %r in "p_playlist_items"', p_item)
123
                    t_index += 1
124
                    continue
125
126
                p_index = p_playlist_items.index(p_item)
127
128
                if p_index == t_index:
129
                    t_index += 1
130
                    continue
131
132
                p_after = p_playlist_items[t_index - 1] if t_index > 0 else None
133
134
                log.info('[%2d:%2d] p_item: %r, t_item: %r (move after: %r)',
135
                    p_index, t_index,
136
                    p_item, t_item,
137
                    p_after
138
                )
139
140
                # Move item in plex playlist
141
                p_playlist.move(
142
                    p_item.playlist_item_id,
143
                    p_after.playlist_item_id if p_after else None
144
                )
145
146
                # Remove item from current position
147
                if t_index > p_index:
148
                    p_playlist_items[p_index] = None
149
                else:
150
                    p_playlist_items.pop(p_index)
151
152
                # Insert at new position
153
                if p_playlist_items[t_index] is None:
154
                    p_playlist_items[t_index] = p_item
155
                else:
156
                    p_playlist_items.insert(t_index, p_item)
157
158
                t_index += 1
159