Move some repeatedly executed code to constructor - 2

This commit is contained in:
desbma
2015-01-15 21:52:16 +01:00
parent 14e211a0b4
commit dfb5216062
+6 -6
View File
@@ -189,6 +189,7 @@ class Fan:
pwm_num = int(__class__.LAST_DIGITS_REGEX.search(self.pwm_filepath).group(1)) pwm_num = int(__class__.LAST_DIGITS_REGEX.search(self.pwm_filepath).group(1))
self.fan_input_filepath = os.path.join(os.path.dirname(self.pwm_filepath), self.fan_input_filepath = os.path.join(os.path.dirname(self.pwm_filepath),
"fan%u_input" % (pwm_num)) "fan%u_input" % (pwm_num))
self.enable_filepath = "%s_enable" % (self.pwm_filepath)
self.start_value = start_value self.start_value = start_value
self.stop_value = stop_value self.stop_value = stop_value
self.startup = False self.startup = False
@@ -255,13 +256,12 @@ class Fan:
def setPwmValue(self, value): def setPwmValue(self, value):
""" Set fan PWM value. """ """ Set fan PWM value. """
assert(0 <= value <= 255) assert(0 <= value <= 255)
enabled_filepath = "%s_enable" % (self.pwm_filepath) with open(self.enable_filepath, "r+t") as enable_file:
with open(enabled_filepath, "r+t") as enabled_file: enabled_val = int(enable_file.read().strip())
enabled_val = int(enabled_file.read().strip())
if enabled_val != 1: if enabled_val != 1:
self.logger.warning("%s was %u, setting it to 1", enabled_filepath, enabled_val) self.logger.warning("%s was %u, setting it to 1", enable_filepath, enabled_val)
enabled_file.seek(0) enable_file.seek(0)
enabled_file.write("1") enable_file.write("1")
with open(self.pwm_filepath, "wt") as pwm_file: with open(self.pwm_filepath, "wt") as pwm_file:
self.logger.debug("Setting PWM value to %u" % (value)) self.logger.debug("Setting PWM value to %u" % (value))
pwm_file.write("%u" % (value)) pwm_file.write("%u" % (value))