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