| Conditions | 14 |
| Total Lines | 87 |
| 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 autolookup_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.
| 1 | #!/usr/bin/env python3 |
||
| 137 | @decorators.wrap_keyboard_except |
||
| 138 | def autolookup_main(osversion, loop=False, log=False, autogen=False, inc=3, sql=False, quiet=False, ceiling=9996, mailer=False, prod=False, no2=False): |
||
|
2 ignored issues
–
show
|
|||
| 139 | """ |
||
| 140 | Lookup a software release from an OS. Can iterate. |
||
| 141 | |||
| 142 | :param osversion: OS version, 10.x.y.zzzz. |
||
| 143 | :type osversion: str |
||
| 144 | |||
| 145 | :param loop: Whether or not to automatically lookup. Default is false. |
||
| 146 | :type loop: bool |
||
| 147 | |||
| 148 | :param log: Whether to log. Default is false. |
||
| 149 | :type log: bool |
||
| 150 | |||
| 151 | :param autogen: Whether to create text links. Default is false. |
||
| 152 | :type autogen: bool |
||
| 153 | |||
| 154 | :param inc: Lookup increment. Default is 3. |
||
| 155 | :type inc: int |
||
| 156 | |||
| 157 | :param sql: Whether to add valid lookups to a database. Default is false. |
||
| 158 | :type sql: bool |
||
| 159 | |||
| 160 | :param quiet: Whether to only output if release exists. Default is false. |
||
| 161 | :type quiet: bool |
||
| 162 | |||
| 163 | :param ceiling: When to stop loop. Default is 9996 (i.e. 10.x.y.9996). |
||
| 164 | :type ceiling: int |
||
| 165 | |||
| 166 | :param mailer: Whether to email new valid links. Default is false. |
||
| 167 | :type mailer: bool |
||
| 168 | |||
| 169 | :param prod: Whether to check only the production server. Default is false. |
||
| 170 | :type prod: bool |
||
| 171 | |||
| 172 | :param no2: Whether to skip Alpha2/Beta2 servers. Default is false. |
||
| 173 | :type no2: bool |
||
| 174 | """ |
||
| 175 | if mailer: |
||
| 176 | sql = True |
||
| 177 | smtpc = smtputils.smtp_config_loader() |
||
| 178 | smtpc = smtputils.smtp_config_generator(smtpc) |
||
| 179 | smtpc['homepath'] = None |
||
| 180 | pword = smtpc['password'] |
||
| 181 | smtputils.smtp_config_writer(**smtpc) |
||
| 182 | else: |
||
| 183 | pword = None |
||
| 184 | scriptutils.slim_preamble("AUTOLOOKUP") |
||
| 185 | record = utilities.prep_logfile() if log else None |
||
| 186 | sess = requests.Session() |
||
| 187 | while True: |
||
| 188 | if loop and int(osversion.split(".")[3]) > ceiling: |
||
| 189 | raise KeyboardInterrupt |
||
| 190 | print("NOW SCANNING: {0}".format(osversion), end="\r") |
||
| 191 | if not prod: |
||
| 192 | results = networkutils.sr_lookup_bootstrap(osversion, sess, no2) |
||
| 193 | else: |
||
| 194 | res = networkutils.sr_lookup(osversion, networkutils.SERVERS["p"], sess) |
||
| 195 | results = {"p": res, "a1": None, "a2": None, "b1": None, "b2": None} |
||
| 196 | if results is None: |
||
| 197 | raise KeyboardInterrupt |
||
| 198 | a1rel, a1av = networkutils.clean_availability(results, 'a1') |
||
| 199 | if not no2: |
||
| 200 | a2rel, a2av = networkutils.clean_availability(results, 'a2') |
||
| 201 | else: |
||
| 202 | a2rel = "SR not in system" |
||
| 203 | a2av = " " |
||
| 204 | b1rel, b1av = networkutils.clean_availability(results, 'b1') |
||
| 205 | b2rel, b2av = networkutils.clean_availability(results, 'b2') |
||
| 206 | prel, pav, avail = scriptutils.prod_avail(results, mailer, osversion, pword) |
||
| 207 | avpack = (a1av, a2av, b1av, b2av, pav) |
||
| 208 | swrelease = scriptutils.clean_swrel(set([a1rel, a2rel, b1rel, b2rel, prel])) |
||
| 209 | if swrelease != "": |
||
| 210 | out = scriptutils.autolookup_output(osversion, swrelease, avail, avpack, sql) |
||
| 211 | scriptutils.autolookup_printer(out, avail, log, quiet, record) |
||
| 212 | if autogen and avail == "Available": |
||
| 213 | rad = utilities.increment(osversion, 1) |
||
| 214 | scriptutils.linkgen(osversion, rad, prel) |
||
| 215 | if not loop: |
||
| 216 | raise KeyboardInterrupt # hack, but whatever |
||
| 217 | else: |
||
| 218 | if int(osversion.split(".")[3]) > ceiling: |
||
| 219 | raise KeyboardInterrupt |
||
| 220 | else: |
||
| 221 | osversion = utilities.increment(osversion, inc) |
||
| 222 | swrelease = "" |
||
| 223 | continue |
||
| 224 | |||
| 228 |