Conditions | 10 |
Total Lines | 113 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 110 |
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 UpdateWSession.to_dict() 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 | from plugin.core.helpers.variable import to_integer, merge, resolve |
||
63 | def to_dict(self, obj, info, fetch=False): |
||
64 | fetch = resolve(fetch, obj, info) |
||
65 | |||
66 | view_offset = to_integer(info.get('viewOffset')) |
||
67 | |||
68 | result = { |
||
69 | 'rating_key': to_integer(info.get('ratingKey')), |
||
70 | 'view_offset': view_offset, |
||
71 | |||
72 | 'updated_at': datetime.utcnow() |
||
73 | } |
||
74 | |||
75 | if not fetch: |
||
76 | # Return simple update |
||
77 | return merge(result, { |
||
78 | 'progress': self.get_progress( |
||
79 | obj.duration, view_offset, |
||
80 | obj.part, obj.part_count, obj.part_duration |
||
81 | ) |
||
82 | }) |
||
83 | |||
84 | # Retrieve session key |
||
85 | session_key = to_integer(info.get('sessionKey')) |
||
86 | |||
87 | if not session_key: |
||
88 | log.info('Missing session key, unable to fetch session details') |
||
89 | return result |
||
90 | |||
91 | # Retrieve active sessions |
||
92 | log.debug('Fetching details for session #%s', session_key) |
||
93 | |||
94 | p_sessions = Plex['status'].sessions() |
||
95 | |||
96 | if not p_sessions: |
||
97 | log.info('Unable to retrieve active sessions') |
||
98 | return result |
||
99 | |||
100 | # Find session matching `session_key` |
||
101 | p_item = p_sessions.get(session_key) |
||
102 | |||
103 | if not p_item: |
||
104 | log.info('Unable to find session with key %r', session_key) |
||
105 | return result |
||
106 | |||
107 | # Retrieve metadata and guid |
||
108 | p_metadata, guid = self.get_metadata(p_item.rating_key) |
||
109 | |||
110 | if not p_metadata: |
||
111 | log.info('Unable to retrieve metadata for rating_key %r', p_item.rating_key) |
||
112 | return result |
||
113 | |||
114 | if not guid: |
||
115 | return merge(result, { |
||
116 | 'duration': p_metadata.duration, |
||
117 | 'progress': self.get_progress(p_metadata.duration, view_offset) |
||
118 | }) |
||
119 | |||
120 | # Parse episode with matcher |
||
121 | part = 1 |
||
122 | part_count = 1 |
||
123 | part_duration = p_metadata.duration |
||
124 | |||
125 | if p_metadata.type == 'episode': |
||
126 | p_season, p_episodes = ModuleManager['matcher'].process(p_metadata) |
||
127 | |||
128 | # Determine the current part number |
||
129 | part_duration, part = self.get_part(p_metadata.duration, view_offset, len(p_episodes)) |
||
130 | |||
131 | # Update `part_count` attribute |
||
132 | part_count = len(p_episodes) |
||
133 | |||
134 | if part_count > 1: |
||
135 | log.debug('Current part: %s (part_count: %s, part_duration: %s)', part, part_count, part_duration) |
||
136 | |||
137 | # Find matching client + user for session |
||
138 | try: |
||
139 | # Create/Retrieve `Client` for session |
||
140 | result['client'] = ClientManager.get.or_create( |
||
141 | p_item.session.player, |
||
142 | |||
143 | fetch=True, |
||
144 | match=True, |
||
145 | filtered_exception=True |
||
146 | ) |
||
147 | |||
148 | # Create/Retrieve `User` for session |
||
149 | result['user'] = UserManager.get.or_create( |
||
150 | p_item.session.user, |
||
151 | |||
152 | fetch=True, |
||
153 | match=True, |
||
154 | filtered_exception=True |
||
155 | ) |
||
156 | |||
157 | # Pick account from `client` or `user` objects |
||
158 | result['account'] = self.get_account(result) |
||
159 | except FilteredException: |
||
160 | log.debug('Activity has been filtered') |
||
161 | |||
162 | result['client'] = None |
||
163 | result['user'] = None |
||
164 | |||
165 | result['account'] = None |
||
166 | |||
167 | return merge(result, { |
||
168 | 'part': part, |
||
169 | 'part_count': part_count, |
||
170 | 'part_duration': part_duration, |
||
171 | |||
172 | 'duration': p_metadata.duration, |
||
173 | 'progress': self.get_progress( |
||
174 | p_metadata.duration, view_offset, |
||
175 | part, part_count, part_duration |
||
176 | ) |
||
185 |