This commit is contained in:
Dave Jones
2016-08-14 11:27:58 +01:00
parent 9e99a445eb
commit 1ec2b763f5
3 changed files with 42 additions and 19 deletions

View File

@@ -7,6 +7,7 @@ from __future__ import (
nstr = str nstr = str
str = type('') str = type('')
import os
import atexit import atexit
import weakref import weakref
from collections import namedtuple from collections import namedtuple
@@ -14,12 +15,16 @@ from itertools import chain
from types import FunctionType from types import FunctionType
from threading import RLock from threading import RLock
import pkg_resources
from .threads import _threads_shutdown from .threads import _threads_shutdown
from .pins import _pins_shutdown
from .mixins import ( from .mixins import (
ValuesMixin, ValuesMixin,
SharedMixin, SharedMixin,
) )
from .exc import ( from .exc import (
BadPinFactory,
DeviceClosed, DeviceClosed,
CompositeDeviceBadName, CompositeDeviceBadName,
CompositeDeviceBadOrder, CompositeDeviceBadOrder,
@@ -30,25 +35,32 @@ from .exc import (
) )
from .compat import frozendict from .compat import frozendict
# Get a pin implementation to use as the default; we prefer RPi.GPIO's here
# as it supports PWM, and all Pi revisions. If no third-party libraries are def _default_pin_factory(name=os.getenv('GPIOZERO_PIN_FACTORY', None)):
# available, however, we fall back to a pure Python implementation which group = 'gpiozero_pin_factories'
# supports platforms like PyPy if name is None:
from .pins import _pins_shutdown # If no factory is explicitly specified, try various names in
try: # "preferred" order. Note that in this case we only select from
from .pins.rpigpio import RPiGPIOPin # gpiozero distribution so without explicitly specifying a name (via
pin_factory = RPiGPIOPin # the environment) it's impossible to auto-select a factory from
except ImportError: # outside the base distribution
try: #
from .pins.rpio import RPIOPin # We prefer RPi.GPIO here as it supports PWM, and all Pi revisions. If
pin_factory = RPIOPin # no third-party libraries are available, however, we fall back to a
except ImportError: # pure Python implementation which supports platforms like PyPy
try: dist = pkg_resources.get_distribution('gpiozero')
from .pins.pigpiod import PiGPIOPin for name in ('RPiGPIOPin', 'RPIOPin', 'PiGPIOPin', 'NativePin'):
pin_factory = PiGPIOPin try:
except ImportError: return pkg_resources.load_entry_point(dist, group, name)
from .pins.native import NativePin except ImportError:
pin_factory = NativePin pass
raise BadPinFactory('Unable to locate any default pin factory!')
else:
for factory in pkg_resources.iter_entry_points(group, name):
return factory.load()
raise BadPinFactory('Unable to locate pin factory "%s"' % name)
pin_factory = _default_pin_factory()
_PINS = set() _PINS = set()

View File

@@ -22,6 +22,9 @@ class BadWaitTime(GPIOZeroError, ValueError):
class BadQueueLen(GPIOZeroError, ValueError): class BadQueueLen(GPIOZeroError, ValueError):
"Error raised when non-positive queue length is specified" "Error raised when non-positive queue length is specified"
class BadPinFactory(GPIOZeroError, ImportError):
"Error raised when an unknown pin factory name is specified"
class CompositeDeviceError(GPIOZeroError): class CompositeDeviceError(GPIOZeroError):
"Base class for errors specific to the CompositeDevice hierarchy" "Base class for errors specific to the CompositeDevice hierarchy"

View File

@@ -61,6 +61,14 @@ if sys.version_info[:2] == (3, 2):
__extra_requires__['test'][1] = 'coverage<4.0dev' __extra_requires__['test'][1] = 'coverage<4.0dev'
__entry_points__ = { __entry_points__ = {
'gpiozero_pin_factories': [
'PiGPIOPin = gpiozero.pins.pigpiod:PiGPIOPin',
'RPiGPIOPin = gpiozero.pins.rpigpio:RPiGPIOPin',
'RPIOPin = gpiozero.pins.rpio:RPIOPin',
'NativePin = gpiozero.pins.native:NativePin',
'MockPin = gpiozero.pins.mock:MockPin',
'MockPWMPin = gpiozero.pins.mock:MockPWMPin',
],
} }