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

@@ -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):