| Conditions | 13 |
| Total Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 167.0986 |
| Changes | 0 | ||
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 Lists.process_sort() 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.enums import SyncMedia |
|
| 84 | 1 | def process_sort(self, data, p_playlist, p_sections_map, t_list, t_list_items): |
|
| 85 | # Construct playlist mapper |
||
| 86 | mapper = PlaylistMapper(self.current, p_sections_map) |
||
| 87 | |||
| 88 | # Parse plex playlist items |
||
| 89 | mapper.plex.load(p_playlist) |
||
| 90 | |||
| 91 | # Parse trakt list items |
||
| 92 | mapper.trakt.load(t_list, t_list_items) |
||
| 93 | |||
| 94 | # Match playlist items and expand shows/seasons |
||
| 95 | m_trakt, m_plex = mapper.match() |
||
| 96 | |||
| 97 | log.debug( |
||
| 98 | 'Sort - Mapper Result (%d items)\nt_items:\n%s\n\np_items:\n%s', |
||
| 99 | len(m_trakt) + len(m_plex), |
||
| 100 | '\n'.join(self.format_mapper_result(m_trakt)), |
||
| 101 | '\n'.join(self.format_mapper_result(m_plex)) |
||
| 102 | ) |
||
| 103 | |||
| 104 | # Build a list of plex items (sorted by `p_index`) |
||
| 105 | p_playlist_items = [] |
||
| 106 | |||
| 107 | for item in mapper.plex.items.itervalues(): |
||
| 108 | p_playlist_items.append(item) |
||
| 109 | |||
| 110 | p_playlist_items = [ |
||
| 111 | i[1] |
||
| 112 | for i in sorted(p_playlist_items, key=lambda i: i[0]) |
||
| 113 | ] |
||
| 114 | |||
| 115 | # Iterate over trakt items, re-order plex items |
||
| 116 | t_index = 0 |
||
| 117 | |||
| 118 | for key, _, (_, p_items), (_, t_items) in m_trakt: |
||
| 119 | # Expand shows/seasons into episodes |
||
| 120 | for p_item, t_item in self.expand(p_items, t_items): |
||
| 121 | if not p_item: |
||
| 122 | continue |
||
| 123 | |||
| 124 | if p_item not in p_playlist_items: |
||
| 125 | log.info('Unable to find %r in "p_playlist_items"', p_item) |
||
| 126 | t_index += 1 |
||
| 127 | continue |
||
| 128 | |||
| 129 | p_index = p_playlist_items.index(p_item) |
||
| 130 | |||
| 131 | if p_index == t_index: |
||
| 132 | t_index += 1 |
||
| 133 | continue |
||
| 134 | |||
| 135 | p_after = p_playlist_items[t_index - 1] if t_index > 0 else None |
||
| 136 | |||
| 137 | log.info('[%2d:%2d] p_item: %r, t_item: %r (move after: %r)', |
||
| 138 | p_index, t_index, |
||
| 139 | p_item, t_item, |
||
| 140 | p_after |
||
| 141 | ) |
||
| 142 | |||
| 143 | # Move item in plex playlist |
||
| 144 | p_playlist.move( |
||
| 145 | p_item.playlist_item_id, |
||
| 146 | p_after.playlist_item_id if p_after else None |
||
| 147 | ) |
||
| 148 | |||
| 149 | # Remove item from current position |
||
| 150 | if t_index > p_index: |
||
| 151 | p_playlist_items[p_index] = None |
||
| 152 | else: |
||
| 153 | p_playlist_items.pop(p_index) |
||
| 154 | |||
| 155 | # Insert at new position |
||
| 156 | if p_playlist_items[t_index] is None: |
||
| 157 | p_playlist_items[t_index] = p_item |
||
| 158 | else: |
||
| 159 | p_playlist_items.insert(t_index, p_item) |
||
| 160 | |||
| 161 | t_index += 1 |
||
| 162 |
Methods which raise
NotImplementedErrorshould be overridden in concrete child classes.