Conditions | 15 |
Total Lines | 83 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Tests | 25 |
CRAP Score | 31.5036 |
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 mine.cli.run() 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 | #!/usr/bin/env python |
||
142 | def run( |
||
143 | 1 | path=None, |
|
144 | cleanup=True, |
||
145 | delay=None, |
||
146 | 1 | switch=None, |
|
147 | edit=False, |
||
148 | 1 | delete=False, |
|
149 | force=False, |
||
150 | 1 | ): |
|
151 | """Run the program. |
||
152 | |||
153 | 1 | :param path: custom settings file path |
|
154 | :param cleanup: remove unused items from the config |
||
155 | :param delay: number of seconds to delay before repeating |
||
156 | |||
157 | :param switch: computer name to queue for launch |
||
158 | 1 | ||
159 | 1 | :param edit: launch the configuration file for editing |
|
160 | 1 | ||
161 | :param delete: attempt to delete conflicted files |
||
162 | 1 | :param force: actually delete conflicted files |
|
163 | 1 | ||
164 | """ # pylint: disable=too-many-arguments,too-many-branches |
||
165 | manager = get_manager() |
||
166 | if not manager.is_running(services.APPLICATION): |
||
167 | manager.start(services.APPLICATION) |
||
168 | |||
169 | root = services.find_root() |
||
170 | path = path or services.find_config_path(root=root) |
||
171 | |||
172 | data = Data() |
||
173 | yorm.sync(data, path) |
||
174 | |||
175 | config = data.config |
||
176 | status = data.status |
||
177 | 1 | ||
178 | 1 | log.info("Identifying current computer...") |
|
179 | computer = config.computers.get_current() |
||
180 | 1 | log.info("Current computer: %s", computer) |
|
181 | 1 | ||
182 | if edit: |
||
183 | return manager.launch(path) |
||
184 | if delete: |
||
185 | return services.delete_conflicts(root, force=force) |
||
186 | 1 | ||
187 | 1 | if switch is True: |
|
188 | 1 | switch = computer |
|
189 | 1 | elif switch is False: |
|
190 | data.close_all_applications(config, manager) |
||
191 | 1 | elif switch: |
|
192 | 1 | switch = config.computers.match(switch) |
|
193 | 1 | ||
194 | 1 | if switch: |
|
195 | if switch != computer: |
||
196 | 1 | data.close_all_applications(config, manager) |
|
197 | 1 | data.queue_all_applications(config, status, switch) |
|
198 | |||
199 | 1 | while True: |
|
200 | data.launch_queued_applications(config, status, computer, manager) |
||
201 | data.update_status(config, status, computer, manager) |
||
202 | |||
203 | if delay is None: |
||
204 | break |
||
205 | |||
206 | log.info("Delaying %s seconds for files to sync...", delay) |
||
207 | time.sleep(delay) |
||
208 | |||
209 | step = 1 |
||
210 | elapsed = 0 |
||
211 | log.info("Waiting %s seconds for status changes...", delay) |
||
212 | while elapsed < delay and not data.modified: |
||
213 | time.sleep(step) |
||
214 | elapsed += step |
||
215 | |||
216 | services.delete_conflicts(root, config_only=True, force=True) |
||
217 | |||
218 | if cleanup: |
||
219 | data.prune_status(config, status) |
||
220 | |||
221 | if delay is None: |
||
222 | return _restart_daemon(manager) |
||
223 | |||
224 | return True |
||
225 | |||
245 |