Conditions | 9 |
Total Lines | 118 |
Lines | 0 |
Ratio | 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:
1 | from core.helpers import timestamp, pad_title, function_path, redirect |
||
50 | @route(PLUGIN_PREFIX + '/sync') |
||
51 | def ControlsMenu(account_id=1, title=None, message=None, refresh=None, message_only=False): |
||
52 | account = AccountManager.get(Account.id == account_id) |
||
53 | |||
54 | # Build sync controls menu |
||
55 | oc = ObjectContainer( |
||
56 | title2=LF('controls:title', account.name), |
||
57 | no_cache=True, |
||
58 | |||
59 | art=function_path('Cover.png', account_id=account.id, refresh=account.refreshed_ts) |
||
60 | ) |
||
61 | |||
62 | # Start result message |
||
63 | if title and message: |
||
64 | oc.add(DirectoryObject( |
||
65 | key=Callback(ControlsMenu, account_id=account.id, refresh=timestamp()), |
||
66 | title=pad_title(title), |
||
67 | summary=message |
||
68 | )) |
||
69 | |||
70 | if message_only: |
||
71 | return oc |
||
72 | |||
73 | # Active sync status |
||
74 | Active.create( |
||
75 | oc, |
||
76 | callback=Callback(ControlsMenu, account_id=account.id, refresh=timestamp()), |
||
77 | account=account |
||
78 | ) |
||
79 | |||
80 | # |
||
81 | # Full |
||
82 | # |
||
83 | |||
84 | oc.add(DirectoryObject( |
||
85 | key=Callback(Synchronize, account_id=account.id, refresh=timestamp()), |
||
86 | title=pad_title(SyncMode.title(SyncMode.Full)), |
||
87 | summary=Status.build(account, SyncMode.Full), |
||
88 | |||
89 | thumb=R("icon-sync.png"), |
||
90 | art=function_path('Cover.png', account_id=account.id, refresh=account.refreshed_ts) |
||
91 | )) |
||
92 | |||
93 | # |
||
94 | # Pull |
||
95 | # |
||
96 | |||
97 | oc.add(DirectoryObject( |
||
98 | key=Callback(Pull, account_id=account.id, refresh=timestamp()), |
||
99 | title=pad_title('%s from trakt' % SyncMode.title(SyncMode.Pull)), |
||
100 | summary=Status.build(account, SyncMode.Pull), |
||
101 | |||
102 | thumb=R("icon-sync_down.png"), |
||
103 | art=function_path('Cover.png', account_id=account.id, refresh=account.refreshed_ts) |
||
104 | )) |
||
105 | |||
106 | oc.add(DirectoryObject( |
||
107 | key=Callback(FastPull, account_id=account.id, refresh=timestamp()), |
||
108 | title=pad_title('%s from trakt' % SyncMode.title(SyncMode.FastPull)), |
||
109 | summary=Status.build(account, SyncMode.FastPull), |
||
110 | |||
111 | thumb=R("icon-sync_down.png"), |
||
112 | art=function_path('Cover.png', account_id=account.id, refresh=account.refreshed_ts) |
||
113 | )) |
||
114 | |||
115 | # |
||
116 | # Push |
||
117 | # |
||
118 | |||
119 | p_account = account.plex |
||
120 | |||
121 | try: |
||
122 | # Retrieve account libraries/sections |
||
123 | with p_account.authorization(): |
||
124 | sections = Plex['library'].sections() |
||
125 | except Exception, ex: |
||
126 | # Build message |
||
127 | if p_account is None: |
||
128 | message = "Plex account hasn't been authenticated" |
||
129 | else: |
||
130 | message = str(ex.message or ex) |
||
131 | |||
132 | # Redirect to error message |
||
133 | log.warn('Unable to retrieve account libraries/sections: %s', message, exc_info=True) |
||
134 | |||
135 | return redirect('/sync', |
||
136 | account_id=account_id, |
||
137 | title='Error', |
||
138 | message=message, |
||
139 | message_only=True |
||
140 | ) |
||
141 | |||
142 | section_keys = [] |
||
143 | |||
144 | f_allow, f_deny = Filters.get('filter_sections') |
||
145 | |||
146 | for section in sections.filter(['show', 'movie'], titles=f_allow): |
||
147 | oc.add(DirectoryObject( |
||
148 | key=Callback(Push, account_id=account.id, section=section.key, refresh=timestamp()), |
||
149 | title=pad_title('%s "%s" to trakt' % (SyncMode.title(SyncMode.Push), section.title)), |
||
150 | summary=Status.build(account, SyncMode.Push, section.key), |
||
151 | |||
152 | thumb=R("icon-sync_up.png"), |
||
153 | art=function_path('Cover.png', account_id=account.id, refresh=account.refreshed_ts) |
||
154 | )) |
||
155 | section_keys.append(section.key) |
||
156 | |||
157 | if len(section_keys) > 1: |
||
158 | oc.add(DirectoryObject( |
||
159 | key=Callback(Push, account_id=account.id, refresh=timestamp()), |
||
160 | title=pad_title('%s all to trakt' % SyncMode.title(SyncMode.Push)), |
||
161 | summary=Status.build(account, SyncMode.Push), |
||
162 | |||
163 | thumb=R("icon-sync_up.png"), |
||
164 | art=function_path('Cover.png', account_id=account.id, refresh=account.refreshed_ts) |
||
165 | )) |
||
166 | |||
167 | return oc |
||
168 | |||
365 |