mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Merge pull request #504 from RPi-Distro/pinout-cli-tool
Add pinout cli tool, close #444, close #497
This commit is contained in:
26
docs/cli_tools.rst
Normal file
26
docs/cli_tools.rst
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
==================
|
||||||
|
Command-line Tools
|
||||||
|
==================
|
||||||
|
|
||||||
|
Pinout
|
||||||
|
======
|
||||||
|
|
||||||
|
The gpiozero package contains a database of information about the various
|
||||||
|
revisions of Raspberry Pi. This is queried by the ``pinout`` command-line
|
||||||
|
tool to output details of the GPIO pins available.
|
||||||
|
|
||||||
|
Unless specified, the revision of the current device will be detected. A
|
||||||
|
particular revision may be selected with the --revision command-line
|
||||||
|
option. For example::
|
||||||
|
|
||||||
|
pinout --revision 000d
|
||||||
|
|
||||||
|
By default, the output will include ANSI color codes if run in a color-capable
|
||||||
|
terminal. This behaviour may be overridden by the --color or --monochrome
|
||||||
|
options to force colored or non-colored output, respectively. For example::
|
||||||
|
|
||||||
|
pinout --monochrome
|
||||||
|
|
||||||
|
Full usage details are available with::
|
||||||
|
|
||||||
|
pinout --help
|
||||||
@@ -18,6 +18,7 @@ Table of Contents
|
|||||||
api_tools
|
api_tools
|
||||||
api_pins
|
api_pins
|
||||||
api_exc
|
api_exc
|
||||||
|
cli_tools
|
||||||
source_values
|
source_values
|
||||||
changelog
|
changelog
|
||||||
license
|
license
|
||||||
|
|||||||
0
gpiozero/cli/__init__.py
Normal file
0
gpiozero/cli/__init__.py
Normal file
66
gpiozero/cli/pinout.py
Executable file
66
gpiozero/cli/pinout.py
Executable file
@@ -0,0 +1,66 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
|
pinout - gpiozero command-line pinout tool.
|
||||||
|
|
||||||
|
Output Raspberry Pi GPIO pinout information.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import unicode_literals, absolute_import, print_function, division
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from gpiozero import *
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args(args):
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description=__doc__
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-r', '--revision',
|
||||||
|
dest='revision',
|
||||||
|
default='',
|
||||||
|
help='RPi revision. Default is to autodetect revision of current device'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-c', '--color',
|
||||||
|
action="store_true",
|
||||||
|
default=None,
|
||||||
|
help='Force colored output (by default, the output will include ANSI'
|
||||||
|
'color codes if run in a color-capable terminal). See also --monochrome'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-m', '--monochrome',
|
||||||
|
dest='color',
|
||||||
|
action='store_false',
|
||||||
|
help='Force monochrome output. See also --color'
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
args = parser.parse_args(args)
|
||||||
|
except argparse.ArgumentError as ex:
|
||||||
|
print('Error parsing arguments.')
|
||||||
|
parser.error(str(ex.message))
|
||||||
|
sys.exit(1)
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = parse_args(sys.argv[1:])
|
||||||
|
|
||||||
|
if args.revision == '':
|
||||||
|
try:
|
||||||
|
pi_info().pprint(color=args.color)
|
||||||
|
except IOError:
|
||||||
|
print('This device is not a Raspberry Pi')
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
pi_info(args.revision).pprint(color=args.color)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
3
setup.py
3
setup.py
@@ -75,6 +75,9 @@ __entry_points__ = {
|
|||||||
'MockPin = gpiozero.pins.mock:MockPin',
|
'MockPin = gpiozero.pins.mock:MockPin',
|
||||||
'MockPWMPin = gpiozero.pins.mock:MockPWMPin',
|
'MockPWMPin = gpiozero.pins.mock:MockPWMPin',
|
||||||
],
|
],
|
||||||
|
'console_scripts': [
|
||||||
|
'pinout = gpiozero.cli.pinout:main',
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
35
tests/cli/test_pinout.py
Normal file
35
tests/cli/test_pinout.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
from __future__ import (
|
||||||
|
unicode_literals,
|
||||||
|
absolute_import,
|
||||||
|
print_function,
|
||||||
|
division,
|
||||||
|
)
|
||||||
|
str = type('')
|
||||||
|
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from gpiozero.cli import pinout
|
||||||
|
|
||||||
|
|
||||||
|
def test_args_incorrect():
|
||||||
|
with pytest.raises(SystemExit) as ex:
|
||||||
|
pinout.parse_args(['--nonexistentarg'])
|
||||||
|
|
||||||
|
def test_args_color():
|
||||||
|
args = pinout.parse_args([])
|
||||||
|
assert args.color is None
|
||||||
|
args = pinout.parse_args(['--color'])
|
||||||
|
assert args.color is True
|
||||||
|
args = pinout.parse_args(['--monochrome'])
|
||||||
|
assert args.color is False
|
||||||
|
|
||||||
|
def test_args_revision():
|
||||||
|
args = pinout.parse_args(['--revision', '000d'])
|
||||||
|
assert args.revision == '000d'
|
||||||
|
|
||||||
|
def test_help(capsys):
|
||||||
|
with pytest.raises(SystemExit) as ex:
|
||||||
|
pinout.parse_args(['--help'])
|
||||||
|
out, err = capsys.readouterr()
|
||||||
|
assert 'GPIO pinout' in out
|
||||||
Reference in New Issue
Block a user