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