| Conditions | 10 |
| Total Lines | 76 |
| Code Lines | 38 |
| 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 glances.client_browser.GlancesClientBrowser.__display_server() 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 | # |
||
| 33 | def __display_server(self, server): |
||
| 34 | """Connect and display the given server""" |
||
| 35 | # Display the Glances client for the selected server |
||
| 36 | logger.debug(f"Selected server {server}") |
||
| 37 | |||
| 38 | if server['protocol'].lower() == 'rest': |
||
| 39 | # Display a popup |
||
| 40 | self.screen.display_popup( |
||
| 41 | 'Open the WebUI {}:{} in a Web Browser'.format(server['name'], server['port']), duration=1 |
||
| 42 | ) |
||
| 43 | # Try to open a Webbrowser |
||
| 44 | webbrowser.open(self.servers_list.get_uri(server), new=2, autoraise=1) |
||
| 45 | self.screen.active_server = None |
||
| 46 | return |
||
| 47 | |||
| 48 | # Connection can take time |
||
| 49 | # Display a popup |
||
| 50 | self.screen.display_popup('Connect to {}:{}'.format(server['name'], server['port']), duration=1) |
||
| 51 | |||
| 52 | # A password is needed to access to the server's stats |
||
| 53 | if server['password'] is None: |
||
| 54 | # First of all, check if a password is available in the [passwords] section |
||
| 55 | clear_password = self.servers_list.password.get_password(server['name']) |
||
| 56 | if ( |
||
| 57 | clear_password is None |
||
| 58 | or self.servers_list.get_servers_list()[self.screen.active_server]['status'] == 'PROTECTED' |
||
| 59 | ): |
||
| 60 | # Else, the password should be enter by the user |
||
| 61 | # Display a popup to enter password |
||
| 62 | clear_password = self.screen.display_popup( |
||
| 63 | 'Password needed for {}: '.format(server['name']), popup_type='input', is_password=True |
||
| 64 | ) |
||
| 65 | # Store the password for the selected server |
||
| 66 | if clear_password is not None: |
||
| 67 | self.set_in_selected('password', self.servers_list.password.get_hash(clear_password)) |
||
| 68 | |||
| 69 | # Display the Glance client on the selected server |
||
| 70 | logger.info("Connect Glances client to the {} server".format(server['key'])) |
||
| 71 | |||
| 72 | # Init the client |
||
| 73 | args_server = self.args |
||
| 74 | |||
| 75 | # Overwrite connection setting |
||
| 76 | args_server.client = server['ip'] |
||
| 77 | args_server.port = server['port'] |
||
| 78 | args_server.username = server['username'] |
||
| 79 | args_server.password = server['password'] |
||
| 80 | client = GlancesClient(config=self.config, args=args_server, return_to_browser=True) |
||
| 81 | |||
| 82 | # Test if client and server are in the same major version |
||
| 83 | if not client.login(): |
||
| 84 | self.screen.display_popup( |
||
| 85 | "Sorry, cannot connect to '{}'\n" "See '{}' for more details".format(server['name'], LOG_FILENAME) |
||
| 86 | ) |
||
| 87 | |||
| 88 | # Set the ONLINE status for the selected server |
||
| 89 | self.set_in_selected('status', 'OFFLINE') |
||
| 90 | else: |
||
| 91 | # Start the client loop |
||
| 92 | # Return connection type: 'glances' or 'snmp' |
||
| 93 | connection_type = client.serve_forever() |
||
| 94 | |||
| 95 | try: |
||
| 96 | logger.debug("Disconnect Glances client from the {} server".format(server['key'])) |
||
| 97 | except IndexError: |
||
| 98 | # Server did not exist anymore |
||
| 99 | pass |
||
| 100 | else: |
||
| 101 | # Set the ONLINE status for the selected server |
||
| 102 | if connection_type == 'snmp': |
||
| 103 | self.set_in_selected('status', 'SNMP') |
||
| 104 | else: |
||
| 105 | self.set_in_selected('status', 'ONLINE') |
||
| 106 | |||
| 107 | # Return to the browser (no server selected) |
||
| 108 | self.screen.active_server = None |
||
| 109 | |||
| 142 |