Conditions | 13 |
Total Lines | 124 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
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 bbarchivist.scripts.lazyloader.lazyloader_main() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | #!/usr/bin/env python3 |
||
219 | def lazyloader_main(device, osversion, radioversion=None, softwareversion=None, localdir=None, autoloader=False, download=True, altsw=None, core=False): |
||
220 | """ |
||
221 | Wrap the tools necessary to make one autoloader. |
||
222 | |||
223 | :param device: Device family to create loader for. |
||
224 | :type device: int |
||
225 | |||
226 | :param osversion: OS version, 10.x.y.zzzz. |
||
227 | :type osversion: str |
||
228 | |||
229 | :param radioversion: Radio version, 10.x.y.zzzz. |
||
230 | :type radioversion: str |
||
231 | |||
232 | :param softwareversion: Software version, 10.x.y.zzzz. |
||
233 | :type softwareversion: str |
||
234 | |||
235 | :param localdir: Working path. Default is local dir. |
||
236 | :type localdir: str |
||
237 | |||
238 | :param autoloader: Whether to run loader. Default is false. Windows-only. |
||
239 | :type autoloader: bool |
||
240 | |||
241 | :param download: Whether to download files. Default is true. |
||
242 | :type download: bool |
||
243 | |||
244 | :param altsw: Radio software release, if not the same as OS. |
||
245 | :type altsw: str |
||
246 | |||
247 | :param core: Whether to create a core/radio loader. Default is false. |
||
248 | :type core: bool |
||
249 | """ |
||
250 | radioversion = scriptutils.return_radio_version(osversion, radioversion) |
||
251 | softwareversion, swc = scriptutils.return_sw_checked(softwareversion, osversion) |
||
252 | if altsw == "checkme": |
||
253 | altsw, altchecked = scriptutils.return_radio_sw_checked(altsw, radioversion) |
||
254 | argutils.standard_preamble("lazyloader", osversion, softwareversion, radioversion, altsw) |
||
255 | print("DEVICE: {0}".format(bbconstants.DEVICES[device])) |
||
256 | |||
257 | # Make dirs |
||
258 | bd_o, bd_r, ld_o, ld_r, zd_o, zd_r = barutils.make_dirs(localdir, osversion, radioversion) |
||
259 | osurl = radiourl = None |
||
260 | |||
261 | # Create download URLs |
||
262 | baseurl = utilities.create_base_url(softwareversion) |
||
263 | if altsw: |
||
264 | alturl = utilities.create_base_url(altsw) |
||
265 | osurl, radiourl = utilities.generate_lazy_urls(softwareversion, osversion, radioversion, device) |
||
266 | if altsw: |
||
267 | radiourl = radiourl.replace(baseurl, alturl) |
||
268 | if core: |
||
269 | osurl = osurl.replace(".desktop", "") |
||
270 | |||
271 | #Terminate if device is STL100-1 and OS >= 10.3.3 |
||
272 | splitos = [int(i) for i in osversion.split(".")] |
||
273 | if device == 0 and utilities.newer_103(splitos, 3): |
||
274 | print("STL100-1 UNSUPPORTED IN 10.3.3+!") |
||
275 | print("\nEXITING...") |
||
276 | raise SystemExit |
||
277 | |||
278 | if download: |
||
279 | # Check availability of software releases |
||
280 | scriptutils.check_sw(baseurl, softwareversion, swc) |
||
281 | if altsw: |
||
282 | scriptutils.check_radio_sw(alturl, altsw, altchecked) |
||
283 | |||
284 | # Check availability of OS, radio |
||
285 | scriptutils.check_os_single(osurl, osversion, device) |
||
286 | radiourl, radioversion = scriptutils.check_radio_single(radiourl, radioversion) |
||
287 | dllist = [osurl, radiourl] |
||
288 | |||
289 | # Check cached |
||
290 | osfile = os.path.join(localdir, bd_o, os.path.basename(osurl)) |
||
291 | radfile = os.path.join(localdir, bd_r, os.path.basename(radiourl)) |
||
292 | |||
293 | if download: |
||
294 | # Download files |
||
295 | print("DOWNLOADING...") |
||
296 | sess = requests.Session() |
||
297 | networkutils.download_bootstrap(dllist, outdir=localdir, workers=2, session=sess) |
||
298 | elif all(os.path.exists(x) for x in [osfile, radfile]): |
||
299 | # Already downloaded in previous session |
||
300 | print("USING CACHED OS/RADIO...") |
||
301 | barutils.replace_bar_pair(localdir, osfile, radfile) |
||
302 | |||
303 | # Test bar files |
||
304 | scriptutils.test_bar_files(localdir, dllist) |
||
305 | |||
306 | # Extract bar files |
||
307 | print("EXTRACTING...") |
||
308 | barutils.extract_bars(localdir) |
||
309 | |||
310 | # Test signed files |
||
311 | scriptutils.test_signed_files(localdir) |
||
312 | |||
313 | # Move bar files |
||
314 | print("MOVING BAR FILES...") |
||
315 | barutils.move_bars(localdir, bd_o, bd_r) |
||
316 | |||
317 | # Generate loader |
||
318 | altradio = radioversion if altsw else None |
||
319 | loadergen.generate_lazy_loader(osversion, device, os.path.abspath(localdir), altradio, core) |
||
320 | |||
321 | # Test loader |
||
322 | suffix = loadergen.format_suffix(bool(altradio), altradio, core) |
||
323 | loadername = loadergen.generate_filename(device, osversion, suffix) |
||
324 | loaderpath = os.path.join(localdir, loadername) |
||
325 | scriptutils.test_single_loader(loaderpath) |
||
326 | |||
327 | # Remove signed files |
||
328 | print("REMOVING SIGNED FILES...") |
||
329 | barutils.remove_signed_files(localdir) |
||
330 | |||
331 | # Move loaders |
||
332 | print("MOVING LOADERS...") |
||
333 | barutils.move_loaders(localdir, ld_o, ld_r, zd_o, zd_r) |
||
334 | loadername = os.path.join(ld_o, loadername) |
||
335 | |||
336 | # Delete empty folders |
||
337 | print("REMOVING EMPTY FOLDERS...") |
||
338 | barutils.remove_empty_folders(localdir) |
||
339 | |||
340 | if autoloader: |
||
341 | subprocess.call(loadername) |
||
342 | print("\nFINISHED!!!") |
||
343 | |||
348 |