Conditions | 11 |
Total Lines | 78 |
Code Lines | 42 |
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.downloader.downloader_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 |
||
89 | def downloader_main(osversion, radioversion=None, softwareversion=None, |
||
90 | localdir=None, debricks=True, radios=True, cores=False, altsw=None): |
||
91 | """ |
||
92 | Archivist's download function, abstracted out. |
||
93 | |||
94 | :param osversion: OS version, 10.x.y.zzzz. |
||
95 | :type osversion: str |
||
96 | |||
97 | :param radioversion: Radio version, 10.x.y.zzzz. Can be guessed. |
||
98 | :type radioversion: str |
||
99 | |||
100 | :param softwareversion: Software release, 10.x.y.zzzz. Can be guessed. |
||
101 | :type softwareversion: str |
||
102 | |||
103 | :param localdir: Working directory. Local by default. |
||
104 | :type localdir: str |
||
105 | |||
106 | :param debricks: Whether to download debrick OS files. True by default. |
||
107 | :type debricks: bool |
||
108 | |||
109 | :param radios: Whether to download radio files. True by default. |
||
110 | :type radios: bool |
||
111 | |||
112 | :param cores: Whether to download core OS files. False by default. |
||
113 | :type cores: bool |
||
114 | |||
115 | :param altsw: Radio software release, if not the same as OS. |
||
116 | :type altsw: str |
||
117 | """ |
||
118 | radioversion = scriptutils.return_radio_version(osversion, radioversion) |
||
119 | softwareversion, swchecked = scriptutils.return_sw_checked(softwareversion, osversion) |
||
120 | if altsw: |
||
121 | altsw, altchecked = scriptutils.return_radio_sw_checked(altsw, radioversion) |
||
122 | localdir = utilities.dirhandler(localdir, os.getcwd()) |
||
123 | argutils.standard_preamble("downloader", osversion, softwareversion, radioversion, altsw) |
||
124 | if not any((debricks, radios, cores)): |
||
125 | print("NO FILES SPECIFIED, DEFAULTING TO DEBRICKS + RADIOS") |
||
126 | debricks = True |
||
127 | radios = True |
||
128 | baseurl, alturl = scriptutils.get_baseurls(softwareversion, altsw) |
||
129 | osurls, corurls, radurls = utilities.bulk_urls(softwareversion, osversion, radioversion, cores, altsw) |
||
130 | |||
131 | # Check availability of software releases |
||
132 | scriptutils.check_sw(baseurl, softwareversion, swchecked) |
||
133 | if altsw: |
||
134 | scriptutils.check_radio_sw(alturl, altsw, altchecked) |
||
135 | |||
136 | # Check availability of OS, radio |
||
137 | if debricks: |
||
138 | scriptutils.check_os_bulk(osurls) |
||
139 | osurls = scriptutils.bulk_avail(osurls) |
||
140 | if cores: |
||
141 | scriptutils.check_os_bulk(corurls) |
||
142 | corurls = scriptutils.bulk_avail(corurls) |
||
143 | if radios: |
||
144 | radurls, radioversion = scriptutils.check_radio_bulk(radurls, radioversion) |
||
145 | radurls = scriptutils.bulk_avail(radurls) |
||
146 | |||
147 | # Download files |
||
148 | print("BEGIN DOWNLOADING...") |
||
149 | urllist = [] |
||
150 | if debricks: |
||
151 | urllist += osurls |
||
152 | if radios: |
||
153 | urllist += radurls |
||
154 | if cores: |
||
155 | urllist += corurls |
||
156 | urllist = list(set(urllist)) # pop duplicates |
||
157 | if urllist: |
||
158 | sess = requests.Session() |
||
159 | networkutils.download_bootstrap(urllist, localdir, workers=5, session=sess) |
||
160 | print("ALL FILES DOWNLOADED") |
||
161 | else: |
||
162 | print("NO FILES TO DOWNLOAD!") |
||
163 | raise SystemExit |
||
164 | |||
165 | # Test bar files |
||
166 | scriptutils.test_bar_files(localdir, urllist) |
||
167 | |||
171 |