| Conditions | 12 |
| Total Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 156 |
| 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 Base.build_request() 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.filters import Filters |
||
| 19 | @classmethod |
||
| 20 | def build_request(cls, session, part=None, rating_key=None, view_offset=None): |
||
| 21 | # Retrieve metadata for session |
||
| 22 | if part is None: |
||
| 23 | part = session.part |
||
| 24 | |||
| 25 | if rating_key is None: |
||
| 26 | rating_key = session.rating_key |
||
| 27 | |||
| 28 | # Retrieve metadata |
||
| 29 | metadata = Metadata.get(rating_key) |
||
| 30 | |||
| 31 | # Validate metadata |
||
| 32 | if not metadata: |
||
| 33 | log.warn('Unable to retrieve metadata for rating_key %r', rating_key) |
||
| 34 | return None |
||
| 35 | |||
| 36 | if metadata.type not in ['movie', 'episode']: |
||
| 37 | log.info('Ignoring session with type %r for rating_key %r', metadata.type, rating_key) |
||
| 38 | return None |
||
| 39 | |||
| 40 | # Apply library/section filter |
||
| 41 | if not Filters.is_valid_metadata_section(metadata): |
||
| 42 | log.info('Ignoring session in filtered section: %r', metadata.section.title) |
||
| 43 | return None |
||
| 44 | |||
| 45 | # Parse guid |
||
| 46 | guid = Guid.parse(metadata.guid, strict=True) |
||
| 47 | |||
| 48 | if not guid or not guid.valid: |
||
| 49 | log_unsupported_guid(log, guid) |
||
| 50 | return None |
||
| 51 | |||
| 52 | # Build request from guid/metadata |
||
| 53 | if type(metadata) is Movie: |
||
| 54 | result = cls.build_movie(metadata, guid, part) |
||
| 55 | elif type(metadata) is Episode: |
||
| 56 | result = cls.build_episode(metadata, guid, part) |
||
| 57 | else: |
||
| 58 | log.warn('Unknown metadata type: %r', type(metadata)) |
||
| 59 | return None |
||
| 60 | |||
| 61 | if not result: |
||
| 62 | log.info('Unable to build request for session: %r', session) |
||
| 63 | return None |
||
| 64 | |||
| 65 | # Retrieve media progress |
||
| 66 | if view_offset is not None: |
||
| 67 | # Calculate progress from `view_offset` parameter |
||
| 68 | progress = UpdateSession.get_progress( |
||
| 69 | metadata.duration, view_offset, |
||
| 70 | part, session.part_count, session.part_duration |
||
| 71 | ) |
||
| 72 | else: |
||
| 73 | # Use session progress |
||
| 74 | progress = session.progress |
||
| 75 | |||
| 76 | # Merge progress into request |
||
| 77 | return merge(result, { |
||
| 78 | 'progress': progress |
||
| 79 | }) |
||
| 170 |
This check looks for lines that are too long. You can specify the maximum line length.