Test Setup Failed
Push — develop ( e459d6...12ef70 )
by Dean
02:19
created

Movies.run()   F

Complexity

Conditions 12

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 142.4922
Metric Value
cc 12
dl 0
loc 72
ccs 1
cts 31
cp 0.0323
crap 142.4922
rs 2.3298

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 Movies.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 SyncMode, SyncData, SyncMedia
3 1
from plugin.sync.modes.core.base import Mode, log_unsupported, mark_unsupported
4
5 1
from plex_database.models import MetadataItem
6 1
from trakt_sync.cache.main import Cache
7 1
import elapsed
8 1
import logging
9
10 1
log = logging.getLogger(__name__)
11
12
13 1
class Movies(Mode):
14 1
    mode = SyncMode.FastPull
15
16 1
    def __init__(self, task):
17
        super(Movies, self).__init__(task)
18
19
        # Sections
20
        self.p_sections = None
21
        self.p_sections_map = None
22
23
        # Items
24
        self.p_count = None
25
        self.p_movies = None
26
27
        self.p_unsupported = None
28
29 1
    @elapsed.clock
30
    def construct(self):
31
        # Retrieve movie sections
32
        self.p_sections, self.p_sections_map = self.sections('movie')
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
33
34
        # Determine number of movies that will be processed
35
        self.p_count = self.plex.library.movies.count(
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
36
            self.p_sections,
37
            account=self.current.account.plex.key
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
38
        )
39
40
        # Increment progress steps total
41
        self.current.progress.group(Movies).add(self.p_count)
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
42
43 1
    @elapsed.clock
44
    def start(self):
45
        # Fetch movies with account settings
46
        self.p_movies = self.plex.library.movies.mapped(
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
47
            self.p_sections, [
48
                MetadataItem.library_section
49
            ],
50
            account=self.current.account.plex.key,
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
51
            parse_guid=True
52
        )
53
54
        # Reset state
55
        self.p_unsupported = {}
56
57 1
    @elapsed.clock
58
    def run(self):
59
        # Process movies
60
        for mo_id, guid, p_item in self.p_movies:
61
            # Increment one step
62
            self.current.progress.group(Movies).step()
0 ignored issues
show
Bug introduced by
The Instance of Movies 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
            # Ensure `guid` is available
65
            if not guid or guid.agent not in GUID_AGENTS:
66
                mark_unsupported(self.p_unsupported, mo_id, guid, p_item)
67
                continue
68
69
            key = (guid.agent, guid.sid)
70
71
            log.debug('Processing movie: %s', key)
72
73
            # Try retrieve `pk` for `key`
74
            pk = self.trakt.table.get(key)
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
75
76
            # Store in item map
77
            self.current.map.add(p_item.get('library_section'), mo_id, [key, pk])
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
78
79
            if pk is None:
80
                # No `pk` found
81
                continue
82
83
            # Iterate over changed data
84
            for key, result in self.trakt.changes:
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
85
                media, data = key[0:2]
86
87
                if media != SyncMedia.Movies:
88
                    # Ignore changes that aren't for movies
89
                    continue
90
91
                if data == SyncData.Watchlist:
92
                    # Ignore watchlist data
93
                    continue
94
95
                if not self.is_data_enabled(data):
0 ignored issues
show
Bug introduced by
The Instance of Movies does not seem to have a member named is_data_enabled.

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...
96
                    # Data type has been disabled
97
                    continue
98
99
                data_name = Cache.Data.get(data)
100
101
                if data_name not in result.changes:
102
                    # No changes for collection
103
                    continue
104
105
                for action, items in result.changes[data_name].items():
106
                    t_item = items.get(pk)
107
108
                    if t_item is None:
109
                        # No item found in changes
110
                        continue
111
112
                    self.execute_handlers(
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
113
                        SyncMedia.Movies, data,
114
                        action=action,
115
                        key=mo_id,
116
117
                        p_item=p_item,
118
                        t_item=t_item
119
                    )
120
121
            # Task checkpoint
122
            self.checkpoint()
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
123
124
        # Stop progress group
125
        self.current.progress.group(Movies).stop()
0 ignored issues
show
Bug introduced by
The Instance of Movies 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...
126
127
        # Log details
128
        log_unsupported(log, 'Found %d unsupported movie(s)\n%s', self.p_unsupported)
129