| Conditions | 20 |
| Total Lines | 73 |
| Code Lines | 50 |
| 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.plugins.system.PluginModel.update() 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 -*- |
||
| 137 | @GlancesPluginModel._check_decorator |
||
| 138 | @GlancesPluginModel._log_result_decorator |
||
| 139 | def update(self): |
||
| 140 | """Update the host/system info using the input method. |
||
| 141 | |||
| 142 | :return: the stats dict |
||
| 143 | """ |
||
| 144 | # Init new stats |
||
| 145 | stats = self.get_init_value() |
||
| 146 | |||
| 147 | if self.input_method == 'local': |
||
| 148 | # Update stats using the standard system lib |
||
| 149 | stats['os_name'] = platform.system() |
||
| 150 | stats['hostname'] = platform.node() |
||
| 151 | stats['platform'] = platform.architecture()[0] |
||
| 152 | if stats['os_name'] == "Linux": |
||
| 153 | try: |
||
| 154 | linux_distro = platform.linux_distribution() |
||
| 155 | except AttributeError: |
||
| 156 | stats['linux_distro'] = _linux_os_release() |
||
| 157 | else: |
||
| 158 | if linux_distro[0] == '': |
||
| 159 | stats['linux_distro'] = _linux_os_release() |
||
| 160 | else: |
||
| 161 | stats['linux_distro'] = ' '.join(linux_distro[:2]) |
||
| 162 | stats['os_version'] = platform.release() |
||
| 163 | elif stats['os_name'].endswith('BSD') or stats['os_name'] == 'SunOS': |
||
| 164 | stats['os_version'] = platform.release() |
||
| 165 | elif stats['os_name'] == "Darwin": |
||
| 166 | stats['os_version'] = platform.mac_ver()[0] |
||
| 167 | elif stats['os_name'] == "Windows": |
||
| 168 | os_version = platform.win32_ver() |
||
| 169 | stats['os_version'] = ' '.join(os_version[::2]) |
||
| 170 | # if the python version is 32 bit perhaps the windows operating |
||
| 171 | # system is 64bit |
||
| 172 | if stats['platform'] == '32bit' and 'PROCESSOR_ARCHITEW6432' in os.environ: |
||
| 173 | stats['platform'] = '64bit' |
||
| 174 | else: |
||
| 175 | stats['os_version'] = "" |
||
| 176 | |||
| 177 | # Add human readable name |
||
| 178 | if self.system_info_msg: |
||
| 179 | try: |
||
| 180 | stats['hr_name'] = self.system_info_msg.format(**stats) |
||
| 181 | except KeyError as e: |
||
| 182 | logger.debug(f'Error in system_info_msg ({e})') |
||
| 183 | stats['hr_name'] = '{os_name} {os_version} {platform}'.format(**stats) |
||
| 184 | elif stats['os_name'] == "Linux": |
||
| 185 | stats['hr_name'] = '{linux_distro} {platform} / {os_name} {os_version}'.format(**stats) |
||
| 186 | else: |
||
| 187 | stats['hr_name'] = '{os_name} {os_version} {platform}'.format(**stats) |
||
| 188 | |||
| 189 | elif self.input_method == 'snmp': |
||
| 190 | # Update stats using SNMP |
||
| 191 | try: |
||
| 192 | stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name]) |
||
| 193 | except KeyError: |
||
| 194 | stats = self.get_stats_snmp(snmp_oid=snmp_oid['default']) |
||
| 195 | # Default behavior: display all the information |
||
| 196 | stats['os_name'] = stats['system_name'] |
||
| 197 | # Windows OS tips |
||
| 198 | if self.short_system_name == 'windows': |
||
| 199 | for r, v in iteritems(snmp_to_human['windows']): |
||
| 200 | if re.search(r, stats['system_name']): |
||
| 201 | stats['os_name'] = v |
||
| 202 | break |
||
| 203 | # Add human readable name |
||
| 204 | stats['hr_name'] = stats['os_name'] |
||
| 205 | |||
| 206 | # Update the stats |
||
| 207 | self.stats = stats |
||
| 208 | |||
| 209 | return self.stats |
||
| 210 | |||
| 243 |