Conditions | 10 |
Total Lines | 59 |
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 | log.info('Ignoring session in filtered section: %r', metadata.section.title) |
||
45 | return None |
||
46 | |||
47 | # Parse guid |
||
48 | guid = Guid.parse(metadata.guid) |
||
49 | |||
50 | # Build request from guid/metadata |
||
51 | if type(metadata) is Movie: |
||
52 | result = cls.build_movie(metadata, guid, part) |
||
53 | elif type(metadata) is Episode: |
||
54 | result = cls.build_episode(metadata, guid, part) |
||
55 | else: |
||
56 | log.warn('Unknown metadata type: %r', type(metadata)) |
||
57 | return None |
||
58 | |||
59 | if not result: |
||
60 | log.warn('Unable to build request for session: %r', session) |
||
61 | return None |
||
62 | |||
63 | # Retrieve media progress |
||
64 | if view_offset is not None: |
||
65 | # Calculate progress from `view_offset` parameter |
||
66 | progress = UpdateSession.get_progress( |
||
67 | metadata.duration, view_offset, |
||
68 | part, session.part_count, session.part_duration |
||
69 | ) |
||
70 | else: |
||
71 | # Use session progress |
||
72 | progress = session.progress |
||
73 | |||
74 | # Merge progress into request |
||
75 | return merge(result, { |
||
76 | 'progress': progress |
||
77 | }) |
||
155 |