Skip to content

Commit

Permalink
Drop Python 2 gpiozero#799
Browse files Browse the repository at this point in the history
  • Loading branch information
fangchenli committed Oct 12, 2020
1 parent ca3bbae commit 03cb083
Show file tree
Hide file tree
Showing 42 changed files with 51 additions and 409 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: python
python:
- "3.6"
- "3.5"
- "2.7"
- "pypy"
- "3.6"
- "3.7"
- "pypy3"

install: "pip install -e .[test]"
Expand Down
6 changes: 0 additions & 6 deletions gpiozero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)

from .pins import (
Factory,
Expand Down
11 changes: 0 additions & 11 deletions gpiozero/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)
try:
from itertools import izip as zip
except ImportError:
pass

from time import sleep
from itertools import repeat, cycle, chain
from threading import Lock
Expand Down
11 changes: 0 additions & 11 deletions gpiozero/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,11 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
absolute_import,
print_function,
division,
)

import operator
import functools

from collections.abc import Mapping


str = type('')


# Copied from the MIT-licensed https://github.com/slezica/python-frozendict
class frozendict(Mapping):
def __init__(self, *args, **kwargs):
Expand Down
59 changes: 28 additions & 31 deletions gpiozero/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)
nstr = str
str = type('')

import os
import atexit
Expand All @@ -49,25 +41,25 @@
from .mixins import (
ValuesMixin,
SharedMixin,
)
)
from .exc import (
BadPinFactory,
DeviceClosed,
CompositeDeviceBadName,
CompositeDeviceBadOrder,
CompositeDeviceBadDevice,
GPIOPinMissing,
GPIOPinInUse,
GPIODeviceClosed,
NativePinFactoryFallback,
PinFactoryFallback,
)
)

from .compat import frozendict

native_fallback_message = (
'Falling back to the experimental pin factory NativeFactory because no other '
'pin factory could be loaded. For best results, install RPi.GPIO or pigpio. '
'See https://gpiozero.readthedocs.io/en/stable/api_pins.html for more information.'
'Falling back to the experimental pin factory NativeFactory because no other '
'pin factory could be loaded. For best results, install RPi.GPIO or pigpio. '
'See https://gpiozero.readthedocs.io/en/stable/api_pins.html for more information.'
)


Expand Down Expand Up @@ -108,6 +100,7 @@ def __call__(cls, *args, **kwargs):
# the refs counter and calls the original close method when
# it reaches zero
old_close = self.close

def close():
self._refs = max(0, self._refs - 1)
if not self._refs:
Expand All @@ -121,6 +114,7 @@ def close():
# just ignore the resulting KeyError here -
# it's already gone
pass

self.close = close
cls._instances[key] = weakref.proxy(self)
else:
Expand All @@ -137,7 +131,7 @@ def close():


# Cross-version compatible method of using a metaclass
class GPIOBase(GPIOMeta(nstr('GPIOBase'), (), {})):
class GPIOBase(GPIOMeta(str('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 All @@ -148,7 +142,7 @@ def __setattr__(self, name, value):
if hasattr(self, '__attrs__') and name not in self.__attrs__:
raise AttributeError(
"'%s' object has no attribute '%s'" % (
self.__class__.__name__, name))
self.__class__.__name__, name))
return super(GPIOBase, self).__setattr__(name, value)

def __del__(self):
Expand Down Expand Up @@ -237,7 +231,7 @@ class Device(ValuesMixin, GPIOBase):
allocating pins, providing low level interfaces (e.g. SPI), and clock
facilities (querying and calculating elapsed times).
"""
pin_factory = None # instance of a Factory sub-class
pin_factory = None # instance of a Factory sub-class

def __init__(self, **kwargs):
# Force pin_factory to be keyword-only, even in Python 2
Expand All @@ -263,9 +257,9 @@ def _default_pin_factory():
# updated along with the entry-points in setup.py.
default_factories = OrderedDict((
('rpigpio', 'gpiozero.pins.rpigpio:RPiGPIOFactory'),
('rpio', 'gpiozero.pins.rpio:RPIOFactory'),
('pigpio', 'gpiozero.pins.pigpio:PiGPIOFactory'),
('native', 'gpiozero.pins.native:NativeFactory'),
('rpio', 'gpiozero.pins.rpio:RPIOFactory'),
('pigpio', 'gpiozero.pins.pigpio:PiGPIOFactory'),
('native', 'gpiozero.pins.native:NativeFactory'),
))
name = os.environ.get('GPIOZERO_PIN_FACTORY')
if name is None:
Expand Down Expand Up @@ -390,6 +384,7 @@ class CompositeDevice(Device):
their :attr:`value` attributes will be accessible as named elements of
the composite device's tuple :attr:`value`.
"""

def __init__(self, *args, **kwargs):
self._all = ()
self._named = frozendict({})
Expand Down Expand Up @@ -445,21 +440,21 @@ def __repr__(self):
unnamed = len(self) - len(self._named)
if named > 0 and unnamed > 0:
return "<gpiozero.%s object containing %d devices: %s and %d unnamed>" % (
self.__class__.__name__,
len(self), ', '.join(self._order),
len(self) - len(self._named)
)
self.__class__.__name__,
len(self), ', '.join(self._order),
len(self) - len(self._named)
)
elif named > 0:
return "<gpiozero.%s object containing %d devices: %s>" % (
self.__class__.__name__,
len(self),
', '.join(self._order)
)
self.__class__.__name__,
len(self),
', '.join(self._order)
)
else:
return "<gpiozero.%s object containing %d unnamed devices>" % (
self.__class__.__name__,
len(self)
)
self.__class__.__name__,
len(self)
)
except DeviceClosed:
return "<gpiozero.%s object closed>" % (self.__class__.__name__)

Expand Down Expand Up @@ -529,6 +524,7 @@ class GPIODevice(Device):
will be raised. If the pin is already in use by another device,
:exc:`GPIOPinInUse` will be raised.
"""

def __init__(self, pin=None, **kwargs):
super(GPIODevice, self).__init__(**kwargs)
# self._pin must be set before any possible exceptions can be raised
Expand Down Expand Up @@ -615,4 +611,5 @@ def _shutdown():
_threads_shutdown()
_devices_shutdown()


atexit.register(_shutdown)
8 changes: 0 additions & 8 deletions gpiozero/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)
str = type('')


class GPIOZeroError(Exception):
"Base class for all exceptions in GPIO Zero"
Expand Down
10 changes: 2 additions & 8 deletions gpiozero/input_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,8 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)

import warnings
from time import sleep, time
from time import sleep
from threading import Event, Lock
from statistics import median

Expand All @@ -55,6 +48,7 @@
except ImportError:
PiGPIOFactory = None


class InputDevice(GPIODevice):
"""
Represents a generic GPIO input device.
Expand Down
9 changes: 0 additions & 9 deletions gpiozero/internal_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)
str = type('')


import os
import io
import subprocess
Expand Down
10 changes: 1 addition & 9 deletions gpiozero/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)
nstr = str
str = type('')

import inspect
import weakref
from functools import wraps, partial
Expand All @@ -60,6 +51,7 @@
'e.g. btn.when_pressed = pressed() instead of btn.when_pressed = pressed'
)


class ValuesMixin(object):
"""
Adds a :attr:`values` property to the class which returns an infinite
Expand Down
8 changes: 1 addition & 7 deletions gpiozero/output_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
print_function,
absolute_import,
division,
)
str = type('')

from threading import Lock
from itertools import repeat, cycle, chain
Expand All @@ -59,6 +52,7 @@
except ImportError:
PiGPIOFactory = None


class OutputDevice(SourceMixin, GPIODevice):
"""
Represents a generic GPIO output device.
Expand Down
8 changes: 0 additions & 8 deletions gpiozero/pins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
absolute_import,
print_function,
division,
)
str = type('')

from weakref import ref
from collections import defaultdict
from threading import Lock
Expand Down
8 changes: 0 additions & 8 deletions gpiozero/pins/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
absolute_import,
print_function,
division,
)
str = type('')

import os
import sys
from textwrap import dedent
Expand Down
16 changes: 2 additions & 14 deletions gpiozero/pins/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,12 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (
unicode_literals,
absolute_import,
print_function,
division,
)
nstr = str
str = type('')

import io
import errno
import struct
from collections import defaultdict
from threading import Lock
try:
from time import monotonic
except ImportError:
from time import time as monotonic
from time import monotonic

try:
from spidev import SpiDev
Expand Down Expand Up @@ -93,7 +81,7 @@ def _get_revision(self):
revision = None
try:
with io.open('/proc/device-tree/system/linux,revision', 'rb') as f:
revision = hex(struct.unpack(nstr('>L'), f.read(4))[0])[2:]
revision = hex(struct.unpack(str('>L'), f.read(4))[0])[2:]
except IOError as e:
if e.errno != errno.ENOENT:
raise e
Expand Down
Loading

0 comments on commit 03cb083

Please sign in to comment.