Skip to content

Commit

Permalink
Merge pull request #139 from waveform80/exceptions-module
Browse files Browse the repository at this point in the history
Move exceptions to their own sub-module
  • Loading branch information
waveform80 committed Jan 7, 2016
2 parents ef5761d + 59ba715 commit 15ce68d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 45 deletions.
8 changes: 5 additions & 3 deletions gpiozero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
division,
)

from .devices import (
from .exc import (
GPIODeviceClosed,
GPIODeviceError,
InputDeviceError,
OutputDeviceError,
)
from .devices import (
GPIODevice,
)
from .input_devices import (
InputDeviceError,
InputDevice,
Button,
LineSensor,
Expand All @@ -22,7 +25,6 @@
MCP3004,
)
from .output_devices import (
OutputDeviceError,
OutputDevice,
PWMOutputDevice,
PWMLED,
Expand Down
5 changes: 3 additions & 2 deletions gpiozero/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
from time import sleep
from collections import namedtuple

from .input_devices import InputDeviceError, Button
from .output_devices import OutputDeviceError, LED, PWMLED, Buzzer, Motor
from .exc import InputDeviceError, OutputDeviceError
from .input_devices import Button
from .output_devices import LED, PWMLED, Buzzer, Motor
from .devices import CompositeDevice, SourceMixin


Expand Down
43 changes: 26 additions & 17 deletions gpiozero/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
absolute_import,
division,
)
nstr = str
str = type('')

import atexit
import weakref
from threading import Thread, Event, RLock
from collections import deque
from types import FunctionType

from RPi import GPIO

from .input_devices import InputDeviceError
from .exc import GPIODeviceError, GPIODeviceClosed, InputDeviceError

_GPIO_THREADS = set()
_GPIO_PINS = set()
Expand All @@ -35,21 +38,28 @@ def _gpio_threads_shutdown():
GPIO.setwarnings(False)


class GPIODeviceError(Exception):
pass


class GPIODeviceClosed(GPIODeviceError):
pass


class GPIOFixedAttrs(type):
class GPIOMeta(type):
# NOTE Yes, this is a metaclass. Don't be scared - it's a simple one.

def __call__(cls, *args, **kwargs):
# Construct the class as normal and ensure it's a subclass of GPIOBase
# (defined below with a custom __setattrs__)
result = super(GPIOFixedAttrs, cls).__call__(*args, **kwargs)
def __new__(mcls, name, bases, cls_dict):
# Construct the class as normal
cls = super(GPIOMeta, mcls).__new__(mcls, name, bases, cls_dict)
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)
# 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
Expand All @@ -59,9 +69,8 @@ def __call__(cls, *args, **kwargs):
return result


class GPIOBase(object):
__metaclass__ = GPIOFixedAttrs

# Cross-version compatible method of using a metaclass
class GPIOBase(GPIOMeta(nstr('GPIOBase'), (), {})):
def __setattr__(self, name, value):
# This overridden __setattr__ simply ensures that additional attributes
# cannot be set on the class after construction (it manages this in
Expand Down
19 changes: 19 additions & 0 deletions gpiozero/exc.py
Original file line number Diff line number Diff line change
@@ -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

13 changes: 2 additions & 11 deletions gpiozero/input_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,8 @@
from RPi import GPIO
from spidev import SpiDev

from .devices import (
GPIODeviceError,
GPIODeviceClosed,
GPIODevice,
CompositeDevice,
GPIOQueue,
)


class InputDeviceError(GPIODeviceError):
pass
from .exc import InputDeviceError, GPIODeviceError, GPIODeviceClosed
from .devices import GPIODevice, CompositeDevice, GPIOQueue


class InputDevice(GPIODevice):
Expand Down
14 changes: 2 additions & 12 deletions gpiozero/output_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,8 @@

from RPi import GPIO

from .devices import (
GPIODeviceError,
GPIODeviceClosed,
GPIODevice,
GPIOThread,
CompositeDevice,
SourceMixin,
)


class OutputDeviceError(GPIODeviceError):
pass
from .exc import OutputDeviceError, GPIODeviceError, GPIODeviceClosed
from .devices import GPIODevice, GPIOThread, CompositeDevice, SourceMixin


class OutputDevice(SourceMixin, GPIODevice):
Expand Down

0 comments on commit 15ce68d

Please sign in to comment.