| Conditions | 14 |
| Total Lines | 108 |
| Lines | 0 |
| Ratio | 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.Config.read() 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 -*- |
||
| 91 | def read(self): |
||
| 92 | """Read the config file, if it exists. Using defaults otherwise.""" |
||
| 93 | for config_file in self.config_file_paths(): |
||
| 94 | if os.path.exists(config_file): |
||
| 95 | try: |
||
| 96 | with open(config_file, encoding='utf-8') as f: |
||
| 97 | self.parser.read_file(f) |
||
| 98 | self.parser.read(f) |
||
| 99 | logger.info("Read configuration file '{0}'".format(config_file)) |
||
| 100 | except UnicodeDecodeError as err: |
||
| 101 | logger.error("Cannot decode configuration file '{0}': {1}".format(config_file, err)) |
||
| 102 | sys.exit(1) |
||
| 103 | # Save the loaded configuration file path (issue #374) |
||
| 104 | self._loaded_config_file = config_file |
||
| 105 | break |
||
| 106 | |||
| 107 | # Quicklook |
||
| 108 | if not self.parser.has_section('quicklook'): |
||
| 109 | self.parser.add_section('quicklook') |
||
| 110 | self.parser.set('quicklook', 'cpu_careful', '50') |
||
| 111 | self.parser.set('quicklook', 'cpu_warning', '70') |
||
| 112 | self.parser.set('quicklook', 'cpu_critical', '90') |
||
| 113 | self.parser.set('quicklook', 'mem_careful', '50') |
||
| 114 | self.parser.set('quicklook', 'mem_warning', '70') |
||
| 115 | self.parser.set('quicklook', 'mem_critical', '90') |
||
| 116 | self.parser.set('quicklook', 'swap_careful', '50') |
||
| 117 | self.parser.set('quicklook', 'swap_warning', '70') |
||
| 118 | self.parser.set('quicklook', 'swap_critical', '90') |
||
| 119 | |||
| 120 | # CPU |
||
| 121 | if not self.parser.has_section('cpu'): |
||
| 122 | self.parser.add_section('cpu') |
||
| 123 | self.parser.set('cpu', 'user_careful', '50') |
||
| 124 | self.parser.set('cpu', 'user_warning', '70') |
||
| 125 | self.parser.set('cpu', 'user_critical', '90') |
||
| 126 | self.parser.set('cpu', 'iowait_careful', '50') |
||
| 127 | self.parser.set('cpu', 'iowait_warning', '70') |
||
| 128 | self.parser.set('cpu', 'iowait_critical', '90') |
||
| 129 | self.parser.set('cpu', 'system_careful', '50') |
||
| 130 | self.parser.set('cpu', 'system_warning', '70') |
||
| 131 | self.parser.set('cpu', 'system_critical', '90') |
||
| 132 | self.parser.set('cpu', 'steal_careful', '50') |
||
| 133 | self.parser.set('cpu', 'steal_warning', '70') |
||
| 134 | self.parser.set('cpu', 'steal_critical', '90') |
||
| 135 | |||
| 136 | # Per-CPU |
||
| 137 | if not self.parser.has_section('percpu'): |
||
| 138 | self.parser.add_section('percpu') |
||
| 139 | self.parser.set('percpu', 'user_careful', '50') |
||
| 140 | self.parser.set('percpu', 'user_warning', '70') |
||
| 141 | self.parser.set('percpu', 'user_critical', '90') |
||
| 142 | self.parser.set('percpu', 'iowait_careful', '50') |
||
| 143 | self.parser.set('percpu', 'iowait_warning', '70') |
||
| 144 | self.parser.set('percpu', 'iowait_critical', '90') |
||
| 145 | self.parser.set('percpu', 'system_careful', '50') |
||
| 146 | self.parser.set('percpu', 'system_warning', '70') |
||
| 147 | self.parser.set('percpu', 'system_critical', '90') |
||
| 148 | |||
| 149 | # Load |
||
| 150 | if not self.parser.has_section('load'): |
||
| 151 | self.parser.add_section('load') |
||
| 152 | self.parser.set('load', 'careful', '0.7') |
||
| 153 | self.parser.set('load', 'warning', '1.0') |
||
| 154 | self.parser.set('load', 'critical', '5.0') |
||
| 155 | |||
| 156 | # Mem |
||
| 157 | if not self.parser.has_section('mem'): |
||
| 158 | self.parser.add_section('mem') |
||
| 159 | self.parser.set('mem', 'careful', '50') |
||
| 160 | self.parser.set('mem', 'warning', '70') |
||
| 161 | self.parser.set('mem', 'critical', '90') |
||
| 162 | |||
| 163 | # Swap |
||
| 164 | if not self.parser.has_section('memswap'): |
||
| 165 | self.parser.add_section('memswap') |
||
| 166 | self.parser.set('memswap', 'careful', '50') |
||
| 167 | self.parser.set('memswap', 'warning', '70') |
||
| 168 | self.parser.set('memswap', 'critical', '90') |
||
| 169 | |||
| 170 | # FS |
||
| 171 | if not self.parser.has_section('fs'): |
||
| 172 | self.parser.add_section('fs') |
||
| 173 | self.parser.set('fs', 'careful', '50') |
||
| 174 | self.parser.set('fs', 'warning', '70') |
||
| 175 | self.parser.set('fs', 'critical', '90') |
||
| 176 | |||
| 177 | # Sensors |
||
| 178 | if not self.parser.has_section('sensors'): |
||
| 179 | self.parser.add_section('sensors') |
||
| 180 | self.parser.set('sensors', 'temperature_core_careful', '60') |
||
| 181 | self.parser.set('sensors', 'temperature_core_warning', '70') |
||
| 182 | self.parser.set('sensors', 'temperature_core_critical', '80') |
||
| 183 | self.parser.set('sensors', 'temperature_hdd_careful', '45') |
||
| 184 | self.parser.set('sensors', 'temperature_hdd_warning', '52') |
||
| 185 | self.parser.set('sensors', 'temperature_hdd_critical', '60') |
||
| 186 | self.parser.set('sensors', 'battery_careful', '80') |
||
| 187 | self.parser.set('sensors', 'battery_warning', '90') |
||
| 188 | self.parser.set('sensors', 'battery_critical', '95') |
||
| 189 | |||
| 190 | # Process list |
||
| 191 | if not self.parser.has_section('processlist'): |
||
| 192 | self.parser.add_section('processlist') |
||
| 193 | self.parser.set('processlist', 'cpu_careful', '50') |
||
| 194 | self.parser.set('processlist', 'cpu_warning', '70') |
||
| 195 | self.parser.set('processlist', 'cpu_critical', '90') |
||
| 196 | self.parser.set('processlist', 'mem_careful', '50') |
||
| 197 | self.parser.set('processlist', 'mem_warning', '70') |
||
| 198 | self.parser.set('processlist', 'mem_critical', '90') |
||
| 199 | |||
| 226 |