Passed
Push — master ( 66020a...b25270 )
by Dean
03:03
created

Movies.run()   F

Complexity

Conditions 12

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 142.0881
Metric Value
cc 12
dl 0
loc 70
ccs 1
cts 30
cp 0.0333
crap 142.0881
rs 2.4217

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.core.constants import GUID_SERVICES
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
    data = [
15
        SyncData.Collection,
16
        SyncData.Playback,
17
        SyncData.Ratings,
18
        SyncData.Watched
19
    ]
20 1
    mode = SyncMode.FastPull
21
22 1
    def __init__(self, task):
23
        super(Movies, self).__init__(task)
24
25
        # Sections
26
        self.p_sections = None
27
        self.p_sections_map = None
28
29
        # Items
30
        self.p_count = None
31
        self.p_movies = None
32
33
        self.p_unsupported = None
34
35 1
    @elapsed.clock
36
    def construct(self):
37
        # Retrieve movie sections
38
        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...
39
40
        # Determine number of movies that will be processed
41
        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...
42
            self.p_sections,
43
            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...
44
        )
45
46
        # Increment progress steps total
47
        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...
48
49 1
    @elapsed.clock
50
    def start(self):
51
        # Fetch movies with account settings
52
        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...
53
            self.p_sections, [
54
                MetadataItem.library_section
55
            ],
56
            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...
57
            parse_guid=True
58
        )
59
60
        # Reset state
61
        self.p_unsupported = {}
62
63 1
    @elapsed.clock
64
    def run(self):
65
        # Process movies
66
        for mo_id, guid, p_item in self.p_movies:
67
            # Increment one step
68
            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...
69
70
            # Ensure `guid` is available
71
            if not guid or guid.service not in GUID_SERVICES:
72
                mark_unsupported(self.p_unsupported, mo_id, guid, p_item)
73
                continue
74
75
            key = (guid.service, guid.id)
76
77
            # Try retrieve `pk` for `key`
78
            pk = self.trakt.table('movies').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...
79
80
            # Store in item map
81
            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...
82
83
            if pk is None:
84
                # No `pk` found
85
                continue
86
87
            # Iterate over changed data
88
            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...
89
                media, data = key[0:2]
90
91
                if media != SyncMedia.Movies:
92
                    # Ignore changes that aren't for movies
93
                    continue
94
95
                if data == SyncData.Watchlist:
96
                    # Ignore watchlist data
97
                    continue
98
99
                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...
100
                    # Data type has been disabled
101
                    continue
102
103
                data_name = Cache.Data.get(data)
104
105
                if data_name not in result.changes:
106
                    # No changes for collection
107
                    continue
108
109
                for action, items in result.changes[data_name].items():
110
                    t_item = items.get(pk)
111
112
                    if t_item is None:
113
                        # No item found in changes
114
                        continue
115
116
                    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...
117
                        SyncMedia.Movies, data,
118
                        action=action,
119
                        key=mo_id,
120
121
                        p_item=p_item,
122
                        t_item=t_item
123
                    )
124
125
            # Task checkpoint
126
            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...
127
128
        # Stop progress group
129
        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...
130
131
        # Log details
132
        log_unsupported(log, 'Found %d unsupported movie(s)\n%s', self.p_unsupported)
133