| Conditions | 18 |
| Total Lines | 67 |
| Code Lines | 45 |
| 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.glances_system.Plugin.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 -*- |
||
| 95 | @GlancesPlugin._check_decorator |
||
| 96 | @GlancesPlugin._log_result_decorator |
||
| 97 | def update(self): |
||
| 98 | """Update the host/system info using the input method. |
||
| 99 | |||
| 100 | :return: the stats dict |
||
| 101 | """ |
||
| 102 | # Init new stats |
||
| 103 | stats = self.get_init_value() |
||
| 104 | |||
| 105 | if self.input_method == 'local': |
||
| 106 | # Update stats using the standard system lib |
||
| 107 | stats['os_name'] = platform.system() |
||
| 108 | stats['hostname'] = platform.node() |
||
| 109 | stats['platform'] = platform.architecture()[0] |
||
| 110 | if stats['os_name'] == "Linux": |
||
| 111 | try: |
||
| 112 | linux_distro = platform.linux_distribution() |
||
| 113 | except AttributeError: |
||
| 114 | stats['linux_distro'] = _linux_os_release() |
||
| 115 | else: |
||
| 116 | if linux_distro[0] == '': |
||
| 117 | stats['linux_distro'] = _linux_os_release() |
||
| 118 | else: |
||
| 119 | stats['linux_distro'] = ' '.join(linux_distro[:2]) |
||
| 120 | stats['os_version'] = platform.release() |
||
| 121 | elif stats['os_name'].endswith('BSD') or stats['os_name'] == 'SunOS': |
||
| 122 | stats['os_version'] = platform.release() |
||
| 123 | elif stats['os_name'] == "Darwin": |
||
| 124 | stats['os_version'] = platform.mac_ver()[0] |
||
| 125 | elif stats['os_name'] == "Windows": |
||
| 126 | os_version = platform.win32_ver() |
||
| 127 | stats['os_version'] = ' '.join(os_version[::2]) |
||
| 128 | # if the python version is 32 bit perhaps the windows operating |
||
| 129 | # system is 64bit |
||
| 130 | if stats['platform'] == '32bit' and 'PROCESSOR_ARCHITEW6432' in os.environ: |
||
| 131 | stats['platform'] = '64bit' |
||
| 132 | else: |
||
| 133 | stats['os_version'] = "" |
||
| 134 | # Add human readable name |
||
| 135 | if stats['os_name'] == "Linux": |
||
| 136 | stats['hr_name'] = stats['linux_distro'] |
||
| 137 | else: |
||
| 138 | stats['hr_name'] = '{} {}'.format(stats['os_name'], stats['os_version']) |
||
| 139 | stats['hr_name'] += ' {}'.format(stats['platform']) |
||
| 140 | |||
| 141 | elif self.input_method == 'snmp': |
||
| 142 | # Update stats using SNMP |
||
| 143 | try: |
||
| 144 | stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name]) |
||
| 145 | except KeyError: |
||
| 146 | stats = self.get_stats_snmp(snmp_oid=snmp_oid['default']) |
||
| 147 | # Default behavior: display all the information |
||
| 148 | stats['os_name'] = stats['system_name'] |
||
| 149 | # Windows OS tips |
||
| 150 | if self.short_system_name == 'windows': |
||
| 151 | for r, v in iteritems(snmp_to_human['windows']): |
||
| 152 | if re.search(r, stats['system_name']): |
||
| 153 | stats['os_name'] = v |
||
| 154 | break |
||
| 155 | # Add human readable name |
||
| 156 | stats['hr_name'] = stats['os_name'] |
||
| 157 | |||
| 158 | # Update the stats |
||
| 159 | self.stats = stats |
||
| 160 | |||
| 161 | return self.stats |
||
| 162 | |||
| 202 |