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