| Conditions | 8 |
| Total Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 65.1556 |
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:
| 1 | 1 | from plugin.sync.core.enums import SyncMedia |
|
| 76 | 1 | def update_changed(self, data, p_playlist, p_sections_map, t_list=None, t_list_items=None): |
|
| 77 | # Retrieve changed items |
||
| 78 | t_changes = self.get_changed_items(data) |
||
| 79 | |||
| 80 | if not t_changes: |
||
| 81 | log.debug('No changes detected in %r collection', data) |
||
| 82 | return |
||
| 83 | |||
| 84 | # Construct playlist mapper |
||
| 85 | mapper = PlaylistMapper(self.current, p_sections_map) |
||
| 86 | |||
| 87 | # Parse plex playlist items |
||
| 88 | mapper.plex.load(p_playlist) |
||
| 89 | |||
| 90 | # Parse trakt list items |
||
| 91 | mapper.trakt.load(t_list, t_list_items) |
||
| 92 | |||
| 93 | # Match playlist items and expand shows/seasons |
||
| 94 | m_trakt, m_plex = mapper.match() |
||
| 95 | |||
| 96 | log.debug( |
||
| 97 | 'Update - Mapper Result (%d items)\nt_items:\n%s\n\np_items:\n%s', |
||
| 98 | len(m_trakt) + len(m_plex), |
||
| 99 | '\n'.join(self.format_mapper_result(m_trakt)), |
||
| 100 | '\n'.join(self.format_mapper_result(m_plex)) |
||
| 101 | ) |
||
| 102 | |||
| 103 | log.debug( |
||
| 104 | 'Update - Changes (%d keys)\n%s', |
||
| 105 | len(t_changes), |
||
| 106 | '\n'.join(self.format_changes(t_changes)), |
||
| 107 | ) |
||
| 108 | |||
| 109 | # Iterate over matched trakt items |
||
| 110 | for key, index, (p_index, p_items), (t_index, t_items) in m_trakt: |
||
| 111 | # Expand shows/seasons into episodes |
||
| 112 | for p_item, t_item in self.expand(p_items, t_items): |
||
| 113 | if len(key) < 1: |
||
| 114 | log.warn('Invalid "key" format: %r', key) |
||
| 115 | continue |
||
| 116 | |||
| 117 | actions = list(t_changes.get(key[0], [])) |
||
| 118 | |||
| 119 | if not actions: |
||
| 120 | continue |
||
| 121 | |||
| 122 | if len(actions) > 1: |
||
| 123 | log.warn('Multiple actions returned for %r: %r', key[0], actions) |
||
| 124 | continue |
||
| 125 | |||
| 126 | # Get `SyncMedia` for `t_item` |
||
| 127 | media = self.get_media(t_item) |
||
| 128 | |||
| 129 | if media is None: |
||
| 130 | log.warn('Unable to identify media of "t_item" (p_item: %r, t_item: %r)', p_item, t_item) |
||
| 131 | continue |
||
| 132 | |||
| 133 | # Execute handler |
||
| 134 | self.execute_handlers( |
||
| 135 | media, data, |
||
| 136 | |||
| 137 | action=actions[0], |
||
| 138 | |||
| 139 | p_sections_map=p_sections_map, |
||
| 140 | p_playlist=p_playlist, |
||
| 141 | |||
| 142 | key=key, |
||
| 143 | |||
| 144 | p_item=p_item, |
||
| 145 | t_item=t_item |
||
| 146 | ) |
||
| 242 |