mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Merge pull request #139 from waveform80/exceptions-module
Move exceptions to their own sub-module
This commit is contained in:
@@ -5,13 +5,16 @@ from __future__ import (
|
|||||||
division,
|
division,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .devices import (
|
from .exc import (
|
||||||
GPIODeviceClosed,
|
GPIODeviceClosed,
|
||||||
GPIODeviceError,
|
GPIODeviceError,
|
||||||
|
InputDeviceError,
|
||||||
|
OutputDeviceError,
|
||||||
|
)
|
||||||
|
from .devices import (
|
||||||
GPIODevice,
|
GPIODevice,
|
||||||
)
|
)
|
||||||
from .input_devices import (
|
from .input_devices import (
|
||||||
InputDeviceError,
|
|
||||||
InputDevice,
|
InputDevice,
|
||||||
Button,
|
Button,
|
||||||
LineSensor,
|
LineSensor,
|
||||||
@@ -22,7 +25,6 @@ from .input_devices import (
|
|||||||
MCP3004,
|
MCP3004,
|
||||||
)
|
)
|
||||||
from .output_devices import (
|
from .output_devices import (
|
||||||
OutputDeviceError,
|
|
||||||
OutputDevice,
|
OutputDevice,
|
||||||
PWMOutputDevice,
|
PWMOutputDevice,
|
||||||
PWMLED,
|
PWMLED,
|
||||||
|
|||||||
@@ -12,8 +12,9 @@ except ImportError:
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from .input_devices import InputDeviceError, Button
|
from .exc import InputDeviceError, OutputDeviceError
|
||||||
from .output_devices import OutputDeviceError, LED, PWMLED, Buzzer, Motor
|
from .input_devices import Button
|
||||||
|
from .output_devices import LED, PWMLED, Buzzer, Motor
|
||||||
from .devices import CompositeDevice, SourceMixin
|
from .devices import CompositeDevice, SourceMixin
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,15 +4,18 @@ from __future__ import (
|
|||||||
absolute_import,
|
absolute_import,
|
||||||
division,
|
division,
|
||||||
)
|
)
|
||||||
|
nstr = str
|
||||||
|
str = type('')
|
||||||
|
|
||||||
import atexit
|
import atexit
|
||||||
import weakref
|
import weakref
|
||||||
from threading import Thread, Event, RLock
|
from threading import Thread, Event, RLock
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
from types import FunctionType
|
||||||
|
|
||||||
from RPi import GPIO
|
from RPi import GPIO
|
||||||
|
|
||||||
from .input_devices import InputDeviceError
|
from .exc import GPIODeviceError, GPIODeviceClosed, InputDeviceError
|
||||||
|
|
||||||
_GPIO_THREADS = set()
|
_GPIO_THREADS = set()
|
||||||
_GPIO_PINS = set()
|
_GPIO_PINS = set()
|
||||||
@@ -35,21 +38,28 @@ GPIO.setmode(GPIO.BCM)
|
|||||||
GPIO.setwarnings(False)
|
GPIO.setwarnings(False)
|
||||||
|
|
||||||
|
|
||||||
class GPIODeviceError(Exception):
|
class GPIOMeta(type):
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class GPIODeviceClosed(GPIODeviceError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class GPIOFixedAttrs(type):
|
|
||||||
# NOTE Yes, this is a metaclass. Don't be scared - it's a simple one.
|
# NOTE Yes, this is a metaclass. Don't be scared - it's a simple one.
|
||||||
|
|
||||||
def __call__(cls, *args, **kwargs):
|
def __new__(mcls, name, bases, cls_dict):
|
||||||
# Construct the class as normal and ensure it's a subclass of GPIOBase
|
# Construct the class as normal
|
||||||
# (defined below with a custom __setattrs__)
|
cls = super(GPIOMeta, mcls).__new__(mcls, name, bases, cls_dict)
|
||||||
result = super(GPIOFixedAttrs, cls).__call__(*args, **kwargs)
|
for attr_name, attr in cls_dict.items():
|
||||||
|
# If there's a method in the class which has no docstring, search
|
||||||
|
# the base classes recursively for a docstring to copy
|
||||||
|
if isinstance(attr, FunctionType) and not attr.__doc__:
|
||||||
|
for base_cls in cls.__mro__:
|
||||||
|
if hasattr(base_cls, attr_name):
|
||||||
|
base_fn = getattr(base_cls, attr_name)
|
||||||
|
if base_fn.__doc__:
|
||||||
|
attr.__doc__ = base_fn.__doc__
|
||||||
|
break
|
||||||
|
return cls
|
||||||
|
|
||||||
|
def __call__(mcls, *args, **kwargs):
|
||||||
|
# Construct the instance as normal and ensure it's an instance of
|
||||||
|
# GPIOBase (defined below with a custom __setattrs__)
|
||||||
|
result = super(GPIOMeta, mcls).__call__(*args, **kwargs)
|
||||||
assert isinstance(result, GPIOBase)
|
assert isinstance(result, GPIOBase)
|
||||||
# At this point __new__ and __init__ have all been run. We now fix the
|
# At this point __new__ and __init__ have all been run. We now fix the
|
||||||
# set of attributes on the class by dir'ing the instance and creating a
|
# set of attributes on the class by dir'ing the instance and creating a
|
||||||
@@ -59,9 +69,8 @@ class GPIOFixedAttrs(type):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
class GPIOBase(object):
|
# Cross-version compatible method of using a metaclass
|
||||||
__metaclass__ = GPIOFixedAttrs
|
class GPIOBase(GPIOMeta(nstr('GPIOBase'), (), {})):
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
# This overridden __setattr__ simply ensures that additional attributes
|
# This overridden __setattr__ simply ensures that additional attributes
|
||||||
# cannot be set on the class after construction (it manages this in
|
# cannot be set on the class after construction (it manages this in
|
||||||
|
|||||||
19
gpiozero/exc.py
Normal file
19
gpiozero/exc.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from __future__ import (
|
||||||
|
unicode_literals,
|
||||||
|
print_function,
|
||||||
|
absolute_import,
|
||||||
|
division,
|
||||||
|
)
|
||||||
|
|
||||||
|
class GPIODeviceError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class GPIODeviceClosed(GPIODeviceError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class InputDeviceError(GPIODeviceError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class OutputDeviceError(GPIODeviceError):
|
||||||
|
pass
|
||||||
|
|
||||||
@@ -14,17 +14,8 @@ from threading import Event
|
|||||||
from RPi import GPIO
|
from RPi import GPIO
|
||||||
from spidev import SpiDev
|
from spidev import SpiDev
|
||||||
|
|
||||||
from .devices import (
|
from .exc import InputDeviceError, GPIODeviceError, GPIODeviceClosed
|
||||||
GPIODeviceError,
|
from .devices import GPIODevice, CompositeDevice, GPIOQueue
|
||||||
GPIODeviceClosed,
|
|
||||||
GPIODevice,
|
|
||||||
CompositeDevice,
|
|
||||||
GPIOQueue,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class InputDeviceError(GPIODeviceError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class InputDevice(GPIODevice):
|
class InputDevice(GPIODevice):
|
||||||
|
|||||||
@@ -12,18 +12,8 @@ from itertools import repeat
|
|||||||
|
|
||||||
from RPi import GPIO
|
from RPi import GPIO
|
||||||
|
|
||||||
from .devices import (
|
from .exc import OutputDeviceError, GPIODeviceError, GPIODeviceClosed
|
||||||
GPIODeviceError,
|
from .devices import GPIODevice, GPIOThread, CompositeDevice, SourceMixin
|
||||||
GPIODeviceClosed,
|
|
||||||
GPIODevice,
|
|
||||||
GPIOThread,
|
|
||||||
CompositeDevice,
|
|
||||||
SourceMixin,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class OutputDeviceError(GPIODeviceError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class OutputDevice(SourceMixin, GPIODevice):
|
class OutputDevice(SourceMixin, GPIODevice):
|
||||||
|
|||||||
Reference in New Issue
Block a user