Conditions | 13 |
Total Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 159.9651 |
Changes | 4 | ||
Bugs | 0 | Features | 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 Main.queue() 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 plugin.core.message import InterfaceMessages |
|
55 | 1 | def queue(self, account, mode, data=None, media=SyncMedia.All, priority=10, trigger=SyncResult.Trigger.Manual, **kwargs): |
|
56 | """Queue a sync for the provided account |
||
57 | |||
58 | Note: if a sync is already queued for the provided account a `SyncError` will be raised. |
||
59 | |||
60 | :param account: Account to synchronize with trakt |
||
61 | :type account: int or plugin.models.Account |
||
62 | |||
63 | :param mode: Syncing mode (pull, push, etc..) |
||
64 | :type mode: int (plugin.sync.SyncMode) |
||
65 | |||
66 | :param data: Data to synchronize (collection, ratings, etc..) |
||
67 | :type data: int (plugin.sync.SyncData) |
||
68 | |||
69 | :param media: Media to synchronize (movies, shows, etc..) |
||
70 | :type media: int (plugin.sync.SyncMedia) |
||
71 | |||
72 | :return: `SyncResult` object with details on the sync outcome. |
||
73 | :rtype: plugin.sync.core.result.SyncResult |
||
74 | """ |
||
75 | if InterfaceMessages.critical: |
||
76 | raise QueueError('Error', InterfaceMessages.message) |
||
77 | |||
78 | if TZ_LOCAL is None: |
||
79 | raise QueueError('Error', 'Unable to retrieve system timezone') |
||
80 | |||
81 | try: |
||
82 | # Create new task |
||
83 | task = SyncTask.create(account, mode, data, media, trigger, **kwargs) |
||
84 | except Exception as ex: |
||
85 | log.warn('Unable to construct task: %s', ex, exc_info=True) |
||
86 | |||
87 | # Raise as queue error |
||
88 | raise QueueError('Error', 'Unable to construct task: %s' % ex) |
||
89 | |||
90 | with self._queue_lock: |
||
91 | # Ensure we only have one task queued per account |
||
92 | account_tasks = [ |
||
93 | t for (p, a, t) in self._queue.queue |
||
94 | if ( |
||
95 | a == task.account.id and |
||
96 | t.result and |
||
97 | (trigger != SyncResult.Trigger.Manual or t.result.trigger == trigger) |
||
98 | ) |
||
99 | ] |
||
100 | |||
101 | if len(account_tasks): |
||
102 | raise QueueError("Unable to queue sync", "Sync has already been queued for this account") |
||
103 | |||
104 | # Queue task until the thread is available |
||
105 | self._queue.put((priority, task.account.id, task), block=False) |
||
106 | |||
107 | # Ensure thread is active |
||
108 | self.spawn() |
||
109 | |||
110 | # Wait for task start |
||
111 | for x in xrange(10): |
||
112 | if task.started: |
||
113 | log.debug('Task %r has started', task) |
||
114 | return |
||
115 | |||
116 | time.sleep(1) |
||
117 | |||
118 | raise QueueError("Sync queued", "Sync will start once the currently queued tasks have finished") |
||
119 | |||
281 |