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