+# 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
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
# 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:
):
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
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
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
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):
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()
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()
# 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)