Conditions | 16 |
Total Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Tests | 2 |
CRAP Score | 222.5803 |
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 GuidParser.parse() 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 | 1 | from oem.media.movie import MovieMatch |
|
15 | 1 | @classmethod |
|
16 | 1 | def parse(cls, guid, episode=None): |
|
17 | media = ( |
||
18 | GuidMatch.Media.Episode |
||
19 | if episode else GuidMatch.Media.Movie |
||
20 | ) |
||
21 | |||
22 | # Ensure guid is valid |
||
23 | if not guid or not guid.valid: |
||
24 | return GuidMatch( |
||
25 | media, guid, |
||
26 | invalid=True |
||
27 | ) |
||
28 | |||
29 | # Process guid episode identifier overrides |
||
30 | if episode and len(episode) == 2: |
||
31 | season_num, episode_num = episode |
||
32 | |||
33 | if guid.season is not None: |
||
34 | episode = guid.season, episode_num |
||
35 | |||
36 | # Process natively supported guid services |
||
37 | if guid.service in GUID_SERVICES: |
||
38 | episodes = None |
||
39 | |||
40 | if episode and len(episode) == 2: |
||
41 | episodes = [episode] |
||
42 | |||
43 | return GuidMatch( |
||
44 | media, guid, |
||
45 | episodes=episodes, |
||
46 | supported=True, |
||
47 | found=True |
||
48 | ) |
||
49 | |||
50 | # Process episode |
||
51 | if episode: |
||
52 | return cls.parse_episode(guid, episode) |
||
53 | |||
54 | # Process shows + movies |
||
55 | supported, (service, key) = ModuleManager['mapper'].id( |
||
56 | guid.service, guid.id, |
||
57 | resolve_mappings=False |
||
58 | ) |
||
59 | |||
60 | # Validate match |
||
61 | if not supported: |
||
62 | return GuidMatch(media, guid) |
||
63 | |||
64 | if not service or not key: |
||
65 | return GuidMatch(media, guid, supported=True) |
||
66 | |||
67 | # Validate identifier |
||
68 | if type(key) is list: |
||
69 | log.info('[%s/%s] - List keys are not supported', guid.service, guid.id) |
||
70 | return GuidMatch(media, guid, supported=True) |
||
71 | |||
72 | if type(key) not in [int, str]: |
||
73 | log.info('[%s/%s] - Unsupported key: %r', guid.service, guid.id, key) |
||
74 | return GuidMatch(media, guid, supported=True) |
||
75 | |||
76 | log.debug('[%s/%s] - Mapped to: %s/%s', guid.service, guid.id, service, key) |
||
77 | |||
78 | # Return movie/show match |
||
79 | return GuidMatch( |
||
80 | media, Guid.construct(service, key, matched=True), |
||
81 | supported=True, |
||
82 | found=True |
||
83 | ) |
||
190 |