| Conditions | 11 |
| Total Lines | 66 |
| 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:
Complex classes like glances.GlancesClient.login() 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 | # -*- coding: utf-8 -*- |
||
| 95 | def login(self): |
||
| 96 | """Logon to the server.""" |
||
| 97 | ret = True |
||
| 98 | |||
| 99 | if not self.args.snmp_force: |
||
| 100 | # First of all, trying to connect to a Glances server |
||
| 101 | client_version = None |
||
| 102 | try: |
||
| 103 | client_version = self.client.init() |
||
| 104 | except socket.error as err: |
||
| 105 | # Fallback to SNMP |
||
| 106 | self.client_mode = 'snmp' |
||
| 107 | logger.error("Connection to Glances server failed ({0} {1})".format(err.errno, err.strerror)) |
||
| 108 | fallbackmsg = 'No Glances server found. Trying fallback to SNMP...' |
||
| 109 | if not self.return_to_browser: |
||
| 110 | print(fallbackmsg) |
||
| 111 | else: |
||
| 112 | logger.info(fallbackmsg) |
||
| 113 | except ProtocolError as err: |
||
| 114 | # Other errors |
||
| 115 | msg = "Connection to server failed" |
||
| 116 | if err.errcode == 401: |
||
| 117 | msg += " (Bad username/password)" |
||
| 118 | else: |
||
| 119 | msg += " ({0} {1})".format(err.errcode, err.errmsg) |
||
| 120 | self.log_and_exit(msg) |
||
| 121 | return False |
||
| 122 | |||
| 123 | if self.client_mode == 'glances': |
||
| 124 | # Check that both client and server are in the same major version |
||
| 125 | if version.split('.')[0] == client_version.split('.')[0]: |
||
| 126 | # Init stats |
||
| 127 | self.stats = GlancesStatsClient(config=self.config, args=self.args) |
||
| 128 | self.stats.set_plugins(json.loads(self.client.getAllPlugins())) |
||
| 129 | logger.debug("Client version: {0} / Server version: {1}".format(version, client_version)) |
||
| 130 | else: |
||
| 131 | self.log_and_exit("Client and server not compatible: \ |
||
| 132 | Client version: {0} / Server version: {1}".format(version, client_version)) |
||
| 133 | return False |
||
| 134 | |||
| 135 | else: |
||
| 136 | self.client_mode = 'snmp' |
||
| 137 | |||
| 138 | # SNMP mode |
||
| 139 | if self.client_mode == 'snmp': |
||
| 140 | logger.info("Trying to grab stats by SNMP...") |
||
| 141 | |||
| 142 | from glances.stats import GlancesStatsClientSNMP |
||
| 143 | |||
| 144 | # Init stats |
||
| 145 | self.stats = GlancesStatsClientSNMP(config=self.config, args=self.args) |
||
| 146 | |||
| 147 | if not self.stats.check_snmp(): |
||
| 148 | self.log_and_exit("Connection to SNMP server failed") |
||
| 149 | return False |
||
| 150 | |||
| 151 | if ret: |
||
| 152 | # Load limits from the configuration file |
||
| 153 | # Each client can choose its owns limits |
||
| 154 | self.stats.load_limits(self.config) |
||
| 155 | |||
| 156 | # Init screen |
||
| 157 | self.screen = GlancesCursesClient(args=self.args) |
||
| 158 | |||
| 159 | # Return result |
||
| 160 | return ret |
||
| 161 | |||
| 236 |