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