X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/80a2f37a06c217e101373df892bf4a083c03012a..21e2f930ada6afd0a031ffe93393f3a4820346d0:/src/adafruit_blinka/microcontroller/generic_linux/sysfs_pin.py diff --git a/src/adafruit_blinka/microcontroller/generic_linux/sysfs_pin.py b/src/adafruit_blinka/microcontroller/generic_linux/sysfs_pin.py index c5b56e6..86c9a5f 100644 --- a/src/adafruit_blinka/microcontroller/generic_linux/sysfs_pin.py +++ b/src/adafruit_blinka/microcontroller/generic_linux/sysfs_pin.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT """ Much code from https://github.com/vsergeev/python-periphery/blob/master/periphery/gpio.py Copyright (c) 2015-2019 vsergeev / Ivan (Vanya) A. Sergeev @@ -8,6 +11,7 @@ import os import errno import time + # pylint: disable=unnecessary-pass class GPIOError(IOError): """Base class for GPIO errors.""" @@ -132,10 +136,10 @@ class Pin: if not os.path.isdir(gpio_path): # Export the line try: - with open("/sys/class/gpio/export", "w") as f_export: + with open("/sys/class/gpio/export", "w", encoding="utf-8") as f_export: f_export.write("{:d}\n".format(self.id)) except IOError as e: - raise GPIOError(e.errno, "Exporting GPIO: " + e.strerror) + raise GPIOError(e.errno, "Exporting GPIO: " + e.strerror) from IOError # Loop until GPIO is exported exported = False @@ -155,7 +159,9 @@ class Pin: # permission rule application after export for i in range(self.GPIO_OPEN_RETRIES): try: - with open(os.path.join(gpio_path, "direction"), "w") as f_direction: + with open( + os.path.join(gpio_path, "direction"), "w", encoding="utf-8" + ) as f_direction: f_direction.write(direction.lower() + "\n") break except IOError as e: @@ -164,22 +170,26 @@ class Pin: ): raise GPIOError( e.errno, "Setting GPIO direction: " + e.strerror - ) + ) from IOError time.sleep(self.GPIO_OPEN_DELAY) else: # Write direction try: - with open(os.path.join(gpio_path, "direction"), "w") as f_direction: + with open( + os.path.join(gpio_path, "direction"), "w", encoding="utf-8" + ) as f_direction: f_direction.write(direction.lower() + "\n") except IOError as e: - raise GPIOError(e.errno, "Setting GPIO direction: " + e.strerror) + raise GPIOError( + e.errno, "Setting GPIO direction: " + e.strerror + ) from IOError # Open value try: self._fd = os.open(os.path.join(gpio_path, "value"), os.O_RDWR) except OSError as e: - raise GPIOError(e.errno, "Opening GPIO: " + e.strerror) + raise GPIOError(e.errno, "Opening GPIO: " + e.strerror) from OSError self._path = gpio_path @@ -192,7 +202,7 @@ class Pin: try: os.close(self._fd) except OSError as e: - raise GPIOError(e.errno, "Closing GPIO: " + e.strerror) + raise GPIOError(e.errno, "Closing GPIO: " + e.strerror) from OSError self._fd = None @@ -202,20 +212,20 @@ class Pin: os.write(unexport_fd, "{:d}\n".format(self.id).encode()) os.close(unexport_fd) except OSError as e: - raise GPIOError(e.errno, "Unexporting GPIO: " + e.strerror) + raise GPIOError(e.errno, "Unexporting GPIO: " + e.strerror) from OSError def _read(self): # Read value try: buf = os.read(self._fd, 2) except OSError as e: - raise GPIOError(e.errno, "Reading GPIO: " + e.strerror) + raise GPIOError(e.errno, "Reading GPIO: " + e.strerror) from OSError # Rewind try: os.lseek(self._fd, 0, os.SEEK_SET) except OSError as e: - raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror) + raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror) from OSError if buf[0] == b"0"[0]: return False @@ -235,13 +245,13 @@ class Pin: else: os.write(self._fd, b"0\n") except OSError as e: - raise GPIOError(e.errno, "Writing GPIO: " + e.strerror) + raise GPIOError(e.errno, "Writing GPIO: " + e.strerror) from OSError # Rewind try: os.lseek(self._fd, 0, os.SEEK_SET) except OSError as e: - raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror) + raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror) from OSError @property def chip_name(self): @@ -266,13 +276,17 @@ class Pin: gpio_path = "/sys/class/gpio/{:s}/label".format(self.chip_name) try: - with open(gpio_path, "r") as f_label: + with open(gpio_path, "r", encoding="utf-8") as f_label: label = f_label.read() except (GPIOError, IOError) as e: if isinstance(e, IOError): - raise GPIOError(e.errno, "Reading gpiochip label: " + e.strerror) + raise GPIOError( + e.errno, "Reading gpiochip label: " + e.strerror + ) from IOError - raise GPIOError(None, "Reading gpiochip label: " + e.strerror) + raise GPIOError( + None, "Reading gpiochip label: " + e.strerror + ) from GPIOError return label.strip() @@ -281,10 +295,14 @@ class Pin: def _get_direction(self): # Read direction try: - with open(os.path.join(self._path, "direction"), "r") as f_direction: + with open( + os.path.join(self._path, "direction"), "r", encoding="utf-8" + ) as f_direction: direction = f_direction.read() except IOError as e: - raise GPIOError(e.errno, "Getting GPIO direction: " + e.strerror) + raise GPIOError( + e.errno, "Getting GPIO direction: " + e.strerror + ) from IOError return direction.strip() @@ -296,10 +314,14 @@ class Pin: # Write direction try: - with open(os.path.join(self._path, "direction"), "w") as f_direction: + with open( + os.path.join(self._path, "direction"), "w", encoding="utf-8" + ) as f_direction: f_direction.write(direction.lower() + "\n") except IOError as e: - raise GPIOError(e.errno, "Setting GPIO direction: " + e.strerror) + raise GPIOError( + e.errno, "Setting GPIO direction: " + e.strerror + ) from IOError direction = property(_get_direction, _set_direction)