Conditions | 27 |
Total Lines | 147 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 722.4122 |
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:
If many parameters/temporary variables are present:
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.core.constants import GUID_SERVICES |
|
74 | 1 | @elapsed.clock |
|
75 | def run(self): |
||
76 | # TODO process seasons |
||
77 | |||
78 | with elapsed.clock(Shows, 'run:shows'): |
||
79 | # Process shows |
||
80 | for sh_id, guid, p_show in self.p_shows: |
||
81 | # Increment one step |
||
82 | self.current.progress.group(Shows, 'shows').step() |
||
83 | |||
84 | # Ensure `guid` is available |
||
85 | if not guid or guid.service not in GUID_SERVICES: |
||
86 | mark_unsupported(self.p_shows_unsupported, sh_id, guid) |
||
87 | continue |
||
88 | |||
89 | key = (guid.service, guid.id) |
||
90 | |||
91 | # Try retrieve `pk` for `key` |
||
92 | pk = self.trakt.table('shows').get(key) |
||
93 | |||
94 | # Store in item map |
||
95 | self.current.map.add(p_show.get('library_section'), sh_id, [key, pk]) |
||
96 | |||
97 | if pk is None: |
||
98 | # No `pk` found |
||
99 | continue |
||
100 | |||
101 | # Iterate over changed data |
||
102 | for key, result in self.trakt.changes: |
||
103 | media, data = key[0:2] |
||
104 | |||
105 | if media != SyncMedia.Shows: |
||
106 | # Ignore changes that aren't for episodes |
||
107 | continue |
||
108 | |||
109 | if data == SyncData.Watchlist: |
||
110 | # Ignore watchlist data |
||
111 | continue |
||
112 | |||
113 | if not self.is_data_enabled(data): |
||
114 | # Data type has been disabled |
||
115 | continue |
||
116 | |||
117 | data_name = Cache.Data.get(data) |
||
118 | |||
119 | if data_name not in result.changes: |
||
120 | # No changes for collection |
||
121 | continue |
||
122 | |||
123 | for action, shows in result.changes[data_name].items(): |
||
124 | t_show = shows.get(pk) |
||
125 | |||
126 | if t_show is None: |
||
127 | # Unable to find matching show in trakt data |
||
128 | continue |
||
129 | |||
130 | # Execute show handlers |
||
131 | self.execute_handlers( |
||
132 | SyncMedia.Shows, data, |
||
133 | action=action, |
||
134 | |||
135 | key=sh_id, |
||
136 | |||
137 | p_item=p_show, |
||
138 | t_item=t_show |
||
139 | ) |
||
140 | |||
141 | # Stop progress group |
||
142 | self.current.progress.group(Shows, 'shows').stop() |
||
143 | |||
144 | with elapsed.clock(Shows, 'run:episodes'): |
||
145 | # Process episodes |
||
146 | for ids, guid, (season_num, episode_num), p_show, p_season, p_episode in self.p_episodes: |
||
147 | # Increment one step |
||
148 | self.current.progress.group(Shows, 'episodes').step() |
||
149 | |||
150 | # Ensure `guid` is available |
||
151 | if not guid or guid.service not in GUID_SERVICES: |
||
152 | mark_unsupported(self.p_shows_unsupported, ids['show'], guid) |
||
153 | continue |
||
154 | |||
155 | key = (guid.service, guid.id) |
||
156 | |||
157 | # Try retrieve `pk` for `key` |
||
158 | pk = self.trakt.table('shows').get(key) |
||
159 | |||
160 | if pk is None: |
||
161 | # No `pk` found |
||
162 | continue |
||
163 | |||
164 | if not ids.get('episode'): |
||
165 | # Missing `episode` rating key |
||
166 | continue |
||
167 | |||
168 | for key, result in self.trakt.changes: |
||
169 | media, data = key[0:2] |
||
170 | |||
171 | if media != SyncMedia.Episodes: |
||
172 | # Ignore changes that aren't for episodes |
||
173 | continue |
||
174 | |||
175 | if not self.is_data_enabled(data): |
||
176 | # Data type has been disabled |
||
177 | continue |
||
178 | |||
179 | data_name = Cache.Data.get(data) |
||
180 | |||
181 | if data_name not in result.changes: |
||
182 | # No changes for collection |
||
183 | continue |
||
184 | |||
185 | for action, shows in result.changes[data_name].items(): |
||
186 | t_show = shows.get(pk) |
||
187 | |||
188 | if t_show is None: |
||
189 | # Unable to find matching show in trakt data |
||
190 | continue |
||
191 | |||
192 | t_season = t_show.get('seasons', {}).get(season_num) |
||
193 | |||
194 | if t_season is None: |
||
195 | # Unable to find matching season in `t_show` |
||
196 | continue |
||
197 | |||
198 | t_episode = t_season.get('episodes', {}).get(episode_num) |
||
199 | |||
200 | if t_episode is None: |
||
201 | # Unable to find matching episode in `t_season` |
||
202 | continue |
||
203 | |||
204 | self.execute_handlers( |
||
205 | SyncMedia.Episodes, data, |
||
206 | action=action, |
||
207 | key=ids['episode'], |
||
208 | |||
209 | p_item=p_episode, |
||
210 | t_item=t_episode |
||
211 | ) |
||
212 | |||
213 | # Task checkpoint |
||
214 | self.checkpoint() |
||
215 | |||
216 | # Stop progress group |
||
217 | self.current.progress.group(Shows, 'episodes').stop() |
||
218 | |||
219 | # Log details |
||
220 | log_unsupported(log, 'Found %d unsupported show(s)\n%s', self.p_shows_unsupported) |
||
221 |
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.