Conditions | 10 |
Total Lines | 56 |
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 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 |
||
18 | @classmethod |
||
19 | def build_request(cls, session, part=None, rating_key=None, view_offset=None): |
||
20 | # Retrieve metadata for session |
||
21 | if part is None: |
||
22 | part = session.part |
||
23 | |||
24 | if rating_key is None: |
||
25 | rating_key = session.rating_key |
||
26 | |||
27 | # Retrieve metadata |
||
28 | metadata = Metadata.get(rating_key) |
||
29 | |||
30 | # Queue a flush for the metadata cache |
||
31 | Metadata.cache.flush_queue() |
||
32 | |||
33 | # Validate metadata |
||
34 | if not metadata: |
||
35 | log.warn('Unable to retrieve metadata for rating_key %r', rating_key) |
||
36 | return None |
||
37 | |||
38 | if metadata.type not in ['movie', 'episode']: |
||
39 | log.info('Ignoring session with type %r for rating_key %r', metadata.type, rating_key) |
||
40 | return None |
||
41 | |||
42 | # Apply library/section filter |
||
43 | if not Filters.is_valid_metadata_section(metadata): |
||
44 | return None |
||
45 | |||
46 | # Parse guid |
||
47 | guid = Guid.parse(metadata.guid) |
||
48 | |||
49 | # Build request from guid/metadata |
||
50 | if type(metadata) is Movie: |
||
51 | result = cls.build_movie(metadata, guid, part) |
||
52 | elif type(metadata) is Episode: |
||
53 | result = cls.build_episode(metadata, guid, part) |
||
54 | else: |
||
55 | return None |
||
56 | |||
57 | if not result: |
||
58 | return None |
||
59 | |||
60 | # Retrieve media progress |
||
61 | if view_offset is not None: |
||
62 | # Calculate progress from `view_offset` parameter |
||
63 | progress = UpdateSession.get_progress( |
||
64 | metadata.duration, view_offset, |
||
65 | part, session.part_count, session.part_duration |
||
66 | ) |
||
67 | else: |
||
68 | # Use session progress |
||
69 | progress = session.progress |
||
70 | |||
71 | # Merge progress into request |
||
72 | return merge(result, { |
||
73 | 'progress': progress |
||
74 | }) |
||
152 |