From 02b8c6841da1aeb3ebb6ea57fd27e98d894b745f Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 18 Oct 2023 22:43:29 +0200 Subject: [PATCH] fix flake warnings --- .../buttons_usb_encoder.py | 2 +- .../map_buttons_usb_encoder.py | 2 +- .../displays/HD44780-i2c/i2c_lcd_driver.py | 16 +++---- components/gpio_control/GPIODevices/led.py | 1 - .../GPIODevices/shutdown_button.py | 46 +++++++++---------- .../gpio_control/GPIODevices/simple_button.py | 25 ++++++---- .../GPIODevices/two_button_control.py | 16 +++---- .../gpio_control/config_compatibility.py | 13 +++--- components/gpio_control/gpio_control.py | 2 +- .../MQTT-protocol/daemon_mqtt_client.py | 4 +- scripts/Reader.py.pcsc | 2 +- scripts/helperscripts/organizeFiles.py | 2 +- 12 files changed, 69 insertions(+), 62 deletions(-) diff --git a/components/controls/buttons_usb_encoder/buttons_usb_encoder.py b/components/controls/buttons_usb_encoder/buttons_usb_encoder.py index e5b56acfb..33d13657c 100644 --- a/components/controls/buttons_usb_encoder/buttons_usb_encoder.py +++ b/components/controls/buttons_usb_encoder/buttons_usb_encoder.py @@ -2,7 +2,7 @@ import sys -sys.path.append(".") # This command should be before imports of components +sys.path.append(".") # This command should be before imports of components import logging from evdev import categorize, ecodes, KeyEvent diff --git a/components/controls/buttons_usb_encoder/map_buttons_usb_encoder.py b/components/controls/buttons_usb_encoder/map_buttons_usb_encoder.py index 40b094135..6cf0bcb28 100644 --- a/components/controls/buttons_usb_encoder/map_buttons_usb_encoder.py +++ b/components/controls/buttons_usb_encoder/map_buttons_usb_encoder.py @@ -2,7 +2,7 @@ import sys -sys.path.append(".") # This command should be before imports of components +sys.path.append(".") # This command should be before imports of components from evdev import categorize, ecodes, KeyEvent from io_buttons_usb_encoder import current_device, write_button_map diff --git a/components/displays/HD44780-i2c/i2c_lcd_driver.py b/components/displays/HD44780-i2c/i2c_lcd_driver.py index bbf5b5090..d9c244e79 100755 --- a/components/displays/HD44780-i2c/i2c_lcd_driver.py +++ b/components/displays/HD44780-i2c/i2c_lcd_driver.py @@ -25,6 +25,7 @@ import smbus from time import sleep + class i2c_device: def __init__(self, addr, port=I2CBUS): self.addr = addr @@ -100,12 +101,13 @@ def read_block_data(self, cmd): LCD_BACKLIGHT = 0x08 LCD_NOBACKLIGHT = 0x00 -En = 0b00000100 # Enable bit -Rw = 0b00000010 # Read/Write bit -Rs = 0b00000001 # Register select bit +En = 0b00000100 # Enable bit +Rw = 0b00000010 # Read/Write bit +Rs = 0b00000001 # Register select bit + class lcd: - #initializes objects and lcd + # initializes objects and lcd def __init__(self): self.lcd_device = i2c_device(ADDRESS) @@ -120,7 +122,6 @@ def __init__(self): self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) sleep(0.2) - # clocks EN to latch command def lcd_strobe(self, data): self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT) @@ -165,7 +166,7 @@ def lcd_clear(self): self.lcd_write(LCD_RETURNHOME) # define backlight on/off (lcd.backlight(1); off= lcd.backlight(0) - def backlight(self, state): # for state, 1 = on, 0 = off + def backlight(self, state): # for state, 1 = on, 0 = off if state == 1: self.lcd_device.write_cmd(LCD_BACKLIGHT) elif state == 0: @@ -173,8 +174,7 @@ def backlight(self, state): # for state, 1 = on, 0 = off # add custom characters (0 - 7) def lcd_load_custom_chars(self, fontdata): - self.lcd_write(0x40); + self.lcd_write(0x40) for char in fontdata: for line in char: self.lcd_write_char(line) - diff --git a/components/gpio_control/GPIODevices/led.py b/components/gpio_control/GPIODevices/led.py index aff6dbc45..01d2d34ef 100644 --- a/components/gpio_control/GPIODevices/led.py +++ b/components/gpio_control/GPIODevices/led.py @@ -41,4 +41,3 @@ def __init__(self, pin, name='StatusLED'): time.sleep(1) self.logger.info('phoniebox-startup-scripts service active') self.on() - diff --git a/components/gpio_control/GPIODevices/shutdown_button.py b/components/gpio_control/GPIODevices/shutdown_button.py index ee8cb1e98..3f9eec341 100644 --- a/components/gpio_control/GPIODevices/shutdown_button.py +++ b/components/gpio_control/GPIODevices/shutdown_button.py @@ -34,30 +34,30 @@ def set_led(self, status): logger.debug('cannot set LED to {}: no LED pin defined'.format(status)) def callbackFunctionHandler(self, *args): - if self.is_pressed: # Should not be necessary, but handler gets called on rising edge too - logger.debug('ShutdownButton pressed, ensuring long press for {} seconds, checking each {}s'.format( - self.hold_time, self.iteration_time - )) - t_passed = 0 - led_state = True - while t_passed < self.hold_time: - self.set_led(led_state) - time.sleep(self.iteration_time) - t_passed += self.iteration_time - led_state = not led_state - if not self.is_pressed: - break - if t_passed >= self.hold_time: - # trippel off period to indicate command accepted - self.set_led(GPIO.HIGH) - time.sleep(.6) - # leave it on for the moment, it will be off when the system is down - self.when_pressed(*args) - else: - # switch off LED if pressing was cancelled early (during flashing) - self.set_led(GPIO.LOW) + if self.is_pressed: # Should not be necessary, but handler gets called on rising edge too + logger.debug('ShutdownButton pressed, ensuring long press for {} seconds, checking each {}s'.format( + self.hold_time, self.iteration_time + )) + t_passed = 0 + led_state = True + while t_passed < self.hold_time: + self.set_led(led_state) + time.sleep(self.iteration_time) + t_passed += self.iteration_time + led_state = not led_state + if not self.is_pressed: + break + if t_passed >= self.hold_time: + # trippel off period to indicate command accepted + self.set_led(GPIO.HIGH) + time.sleep(.6) + # leave it on for the moment, it will be off when the system is down + self.when_pressed(*args) + else: + # switch off LED if pressing was cancelled early (during flashing) + self.set_led(GPIO.LOW) def __repr__(self): return ''.format( - self.name, self.pin, self.hold_time, self.iteration_time, self.led_pin, print_edge_key(self.edge), self.bouncetime,self.antibouncehack, print_pull_up_down(self.pull_up_down) + self.name, self.pin, self.hold_time, self.iteration_time, self.led_pin, print_edge_key(self.edge), self.bouncetime, self.antibouncehack, print_pull_up_down(self.pull_up_down) ) diff --git a/components/gpio_control/GPIODevices/simple_button.py b/components/gpio_control/GPIODevices/simple_button.py index 69b6e3407..4b7149e7e 100644 --- a/components/gpio_control/GPIODevices/simple_button.py +++ b/components/gpio_control/GPIODevices/simple_button.py @@ -6,10 +6,11 @@ logger = logging.getLogger(__name__) -map_edge_parse = {'falling':GPIO.FALLING, 'rising':GPIO.RISING, 'both':GPIO.BOTH} -map_pull_parse = {'pull_up':GPIO.PUD_UP, 'pull_down':GPIO.PUD_DOWN, 'pull_off':GPIO.PUD_OFF} +map_edge_parse = {'falling': GPIO.FALLING, 'rising': GPIO.RISING, 'both': GPIO.BOTH} +map_pull_parse = {'pull_up': GPIO.PUD_UP, 'pull_down': GPIO.PUD_DOWN, 'pull_off': GPIO.PUD_OFF} map_edge_print = {GPIO.FALLING: 'falling', GPIO.RISING: 'rising', GPIO.BOTH: 'both'} -map_pull_print = {GPIO.PUD_UP:'pull_up', GPIO.PUD_DOWN: 'pull_down', GPIO.PUD_OFF: 'pull_off'} +map_pull_print = {GPIO.PUD_UP: 'pull_up', GPIO.PUD_DOWN: 'pull_down', GPIO.PUD_OFF: 'pull_off'} + def parse_edge_key(edge): if edge in [GPIO.FALLING, GPIO.RISING, GPIO.BOTH]: @@ -21,6 +22,7 @@ def parse_edge_key(edge): raise KeyError('Unknown Edge type {edge}'.format(edge=edge)) return result + def parse_pull_up_down(pull_up_down): if pull_up_down in [GPIO.PUD_UP, GPIO.PUD_DOWN, GPIO.PUD_OFF]: return pull_up_down @@ -31,6 +33,7 @@ def parse_pull_up_down(pull_up_down): raise KeyError('Unknown Pull Up/Down type {pull_up_down}'.format(pull_up_down=pull_up_down)) return result + def print_edge_key(edge): try: result = map_edge_print[edge] @@ -38,6 +41,7 @@ def print_edge_key(edge): result = edge return result + def print_pull_up_down(pull_up_down): try: result = map_pull_print[pull_up_down] @@ -45,6 +49,7 @@ def print_pull_up_down(pull_up_down): result = pull_up_down return result + # This function takes a holding time (fractional seconds), a channel, a GPIO state and an action reference (function). # It checks if the GPIO is in the state since the function was called. If the state # changes it return False. If the time is over the function returns True. @@ -93,7 +98,7 @@ def callbackFunctionHandler(self, *args): args = args[1:] logger.debug('args after: {}'.format(args)) - if self.antibouncehack: + if self.antibouncehack: time.sleep(0.1) inval = GPIO.input(self.pin) if inval != GPIO.LOW: @@ -132,32 +137,32 @@ def longPressHandler(self, *args): # instant action (except Postpone mode) if self.hold_mode != "Postpone": self.when_pressed(*args) - + # action(s) after hold_time if self.hold_mode == "Repeat": # Repeated call of main action (multiple times if button is held long enough) while checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW): self.when_pressed(*args) - + elif self.hold_mode == "Postpone": # Postponed call of main action (once) if checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW): self.when_pressed(*args) while checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW): pass - + elif self.hold_mode == "SecondFunc": # Call of secondary action (once) if checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW): self.when_held(*args) while checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW): pass - + elif self.hold_mode == "SecondFuncRepeat": # Repeated call of secondary action (multiple times if button is held long enough) while checkGpioStaysInState(self.hold_time, self.pin, GPIO.LOW): self.when_held(*args) - + def __del__(self): logger.debug('remove event detection') GPIO.remove_event_detect(self.pin) @@ -170,7 +175,7 @@ def is_pressed(self): def __repr__(self): return ''.format( - self.name, self.pin, print_edge_key(self.edge), self.hold_mode, self.hold_time, self.bouncetime,self.antibouncehack,print_pull_up_down(self.pull_up_down) + self.name, self.pin, print_edge_key(self.edge), self.hold_mode, self.hold_time, self.bouncetime, self.antibouncehack, print_pull_up_down(self.pull_up_down) ) diff --git a/components/gpio_control/GPIODevices/two_button_control.py b/components/gpio_control/GPIODevices/two_button_control.py index d4ceb0a11..e93584cd1 100644 --- a/components/gpio_control/GPIODevices/two_button_control.py +++ b/components/gpio_control/GPIODevices/two_button_control.py @@ -63,20 +63,20 @@ def __init__(self, hold_mode=None, hold_time=0.3, bouncetime=500, - antibouncehack=False, - edge='falling', + antibouncehack=False, + edge='falling', name='TwoButtonControl'): self.bcmPin1 = bcmPin1 self.bcmPin2 = bcmPin2 self.functionCallBtn1 = functionCallBtn1 self.functionCallBtn2 = functionCallBtn2 self.functionCallTwoBtns = functionCallTwoBtns - self.pull_up_down=pull_up_down - self.hold_mode=hold_mode - self.hold_time=hold_time - self.bouncetime=bouncetime - self.antibouncehack=antibouncehack - self.edge=edge + self.pull_up_down = pull_up_down + self.hold_mode = hold_mode + self.hold_time = hold_time + self.bouncetime = bouncetime + self.antibouncehack = antibouncehack + self.edge = edge self.btn1 = SimpleButton( pin=bcmPin1, name=name + 'Btn1', diff --git a/components/gpio_control/config_compatibility.py b/components/gpio_control/config_compatibility.py index 62922c299..9c2e92f5d 100644 --- a/components/gpio_control/config_compatibility.py +++ b/components/gpio_control/config_compatibility.py @@ -3,6 +3,7 @@ import os from shutil import copyfile + def Ini_CheckAndUpgrade(config): has_changed = False for section in config.sections(): @@ -37,8 +38,8 @@ def Ini_CheckAndUpgrade(config): has_changed = True if not config.has_option(section, 'hold_time'): config.set(section, 'hold_time', str(v)) - #PinUp: --> Pin1 - #PinDown: --> Pin2 + # PinUp: --> Pin1 + # PinDown: --> Pin2 if config.has_option(section, 'PinUp'): v = config.getint(section, 'PinUp') config.remove_option(section, 'PinUp') @@ -67,17 +68,17 @@ def Ini_CheckAndUpgrade(config): config.set(section, 'functionCall2', v) return has_changed - - + + def ConfigCompatibilityChecks(config, config_path): # Check for deprecated settings in gpio_settings.ini if not Ini_CheckAndUpgrade(config): return - + # If we reach here, gpio_settings.ini needed some patching... # Try creating a backup of the previous ini file - backup_path = config_path+'.bak' + backup_path = config_path + '.bak' if os.path.isfile(backup_path): return copyfile(config_path, backup_path) diff --git a/components/gpio_control/gpio_control.py b/components/gpio_control/gpio_control.py index 44ad68df1..113850431 100755 --- a/components/gpio_control/gpio_control.py +++ b/components/gpio_control/gpio_control.py @@ -118,7 +118,7 @@ def gpio_loop(self): config = configparser.ConfigParser(inline_comment_prefixes=";", delimiters=(':', '=')) config_path = os.path.expanduser('/home/pi/RPi-Jukebox-RFID/settings/gpio_settings.ini') config.read(config_path) - + ConfigCompatibilityChecks(config, config_path) phoniebox_function_calls = function_calls.phoniebox_function_calls() diff --git a/components/smart-home-automation/MQTT-protocol/daemon_mqtt_client.py b/components/smart-home-automation/MQTT-protocol/daemon_mqtt_client.py index b99b89eab..6ffe2e1ad 100644 --- a/components/smart-home-automation/MQTT-protocol/daemon_mqtt_client.py +++ b/components/smart-home-automation/MQTT-protocol/daemon_mqtt_client.py @@ -452,6 +452,7 @@ def regex(needle, hay, exception="-"): else: return exception + def getDuration(status): """ Find the duration of the track in the output from mpd status""" @@ -463,9 +464,10 @@ def getDuration(status): # this attribute value is split into two parts by ":" # first is the elapsed time and the second part is the duration duration = regex("\ntime: .*:(.*)\n", status, "0") - + return int(float(duration)) + REPEAT_MODE_OFF = "off" REPEAT_MODE_SINGLE = "single" REPEAT_MODE_PLAYLIST = "playlist" diff --git a/scripts/Reader.py.pcsc b/scripts/Reader.py.pcsc index dac10de7c..e0d2cc374 100644 --- a/scripts/Reader.py.pcsc +++ b/scripts/Reader.py.pcsc @@ -63,7 +63,7 @@ class Reader: 'Failed to release context: ' + \ SCardGetErrorMessage(hresult)) - return(toHexString(response, PACK)) + return (toHexString(response, PACK)) except error as e: print(e) diff --git a/scripts/helperscripts/organizeFiles.py b/scripts/helperscripts/organizeFiles.py index 596ee4839..06c372633 100644 --- a/scripts/helperscripts/organizeFiles.py +++ b/scripts/helperscripts/organizeFiles.py @@ -52,7 +52,7 @@ def readFolders(audioDir, relpath=None, isFirst=True): elif os.path.isdir(absf): childResult = readFolders(audioDir=absf, relpath=os.path.join(relpath, f), isFirst=False) for k, v in childResult.items(): - assert(k not in result) + assert (k not in result) result[k] = v if hasAudioFiles: result[relpath] = os.path.exists(os.path.join(audioDir, "folder.conf"))