Add configuration of mock factory via env-vars

Also tidied up some docs
This commit is contained in:
Dave Jones
2016-10-21 20:49:31 +01:00
parent b0c807da19
commit ba1a7e6497
4 changed files with 59 additions and 12 deletions

View File

@@ -32,6 +32,31 @@ The default pin factory can be replaced by specifying a value for the
>>> gpiozero.Device._pin_factory
<gpiozero.pins.native.NativeFactory object at 0x762c26b0>
To set the ``GPIOZERO_PIN_FACTORY`` for the rest of your session you can
export this value:
.. code-block:: console
pi@raspberrypi $ export GPIOZERO_PIN_FACTORY=native
pi@raspberrypi $ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiozero
>>> gpiozero.Device._pin_factory
<gpiozero.pins.native.NativeFactory object at 0x762c26b0>
>>> quit()
pi@raspberrypi $ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiozero
>>> gpiozero.Device._pin_factory
<gpiozero.pins.native.NativeFactory object at 0x76401330>
If you add the ``export`` command to your :file:`~/.bashrc` file, you'll set
the default pin factory for all future sessions too.
The following values, and the corresponding :class:`Factory` and :class:`Pin`
classes are listed in the table below. Factories are listed in the order that
they are tried by default.
@@ -71,8 +96,10 @@ script:
.. code-block:: console
$ export GPIOZERO_PIN_FACTORY=pigpio
$ PIGPIO_ADDR=remote-pi python3 my_script.py
$ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=remote-pi python3 my_script.py
Like the ``GPIOZERO_PIN_FACTORY`` value, these can be exported from your
:file:`~/.bashrc` script too.
.. warning::

View File

@@ -26,12 +26,14 @@ from ..exc import (
class Factory(object):
"""
Generates pins, SPI, and I2C interfaces for devices. This is an abstract
base class for pin factories. Descendents must override:
base class for pin factories. Descendents *must* override the following
methods:
* :meth:`_get_address`
* :meth:`pin_address`
Descendents may override:
Descendents *may* additionally override the following methods, if
applicable:
* :meth:`close`
* :meth:`pin`
@@ -123,15 +125,16 @@ class Pin(object):
be it GPIO, SPI, ADC, etc.
Descendents should override property getters and setters to accurately
represent the capabilities of pins. The following functions *must* be
overridden:
represent the capabilities of pins. Descendents *must* override the
following methods:
* :meth:`_get_address`
* :meth:`_get_function`
* :meth:`_set_function`
* :meth:`_get_state`
The following functions *may* be overridden if applicable:
Descendents *may* additionally override the following methods, if
applicable:
* :meth:`close`
* :meth:`output_with_state`
@@ -389,13 +392,13 @@ class Pin(object):
class SPI(object):
"""
Abstract interface for `Serial Peripheral Interface`_ (SPI) implementations.
Descendents *must* override the following:
Abstract interface for `Serial Peripheral Interface`_ (SPI)
implementations. Descendents *must* override the following methods:
* :meth:`transfer`
* :meth:`_get_clock_mode`
Descendents *may* override the following methods:
Descendents *may* override the following methods, if applicable:
* :meth:`read`
* :meth:`write`

View File

@@ -7,6 +7,7 @@ from __future__ import (
str = type('')
import os
from collections import namedtuple
from time import time, sleep
from threading import Thread, Event
@@ -15,6 +16,8 @@ try:
except ImportError:
from ..compat import isclose
import pkg_resources
from ..exc import PinPWMUnsupported, PinSetInput, PinFixedPull
from ..devices import Device
from .pi import PiPin
@@ -387,9 +390,17 @@ class MockSPIDevice(object):
class MockFactory(LocalPiFactory):
def __init__(self, revision='a21041', pin_class=MockPin):
def __init__(
self, revision=os.getenv('GPIOZERO_MOCK_REVISION', 'a21041'),
pin_class=os.getenv('GPIOZERO_MOCK_PIN_CLASS', MockPin)):
super(MockFactory, self).__init__()
self._revision = revision
if not issubclass(pin_class, MockPin):
if isinstance(pin_class, bytes):
pin_class = pin_class.decode('ascii')
dist = pkg_resources.get_distribution('gpiozero')
group = 'gpiozero_mock_pin_classes'
pin_class = pkg_resources.load_entry_point(dist, group, pin_class.lower())
self.pin_class = pin_class
def _get_address(self):

View File

@@ -73,7 +73,13 @@ __entry_points__ = {
'rpio = gpiozero.pins.rpio:RPIOFactory',
'native = gpiozero.pins.native:NativeFactory',
'mock = gpiozero.pins.mock:MockFactory',
'mockpwm = gpiozero.pins.mock:MockPWMFactory',
],
'gpiozero_mock_pin_classes': [
'mockpin = gpiozero.pins.mock:MockPin',
'mockpwmpin = gpiozero.pins.mock:MockPWMPin',
'mockpulleduppin = gpiozero.pins.mock:MockPulledUpPin',
'mockchargingpin = gpiozero.pins.mock:MockChargingPin',
'mocktriggerpin = gpiozero.pins.mock:MockTriggerPin',
],
'console_scripts': [
'pinout = gpiozero.cli.pinout:main',