Skip to content

Commit 6e6c80a

Browse files
committed
Merge branch 'master' of https://github.com/stewartadcock/python-gpiozero into stewartadcock-master
2 parents cbdd9b6 + 48d7924 commit 6e6c80a

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

docs/cli_pinout.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
==================
2+
Command-line Tools
3+
==================
4+
5+
Pinout
6+
======
7+
8+
The gpiozero package contains a database of information about the various
9+
revisions of Raspberry Pi. This is queried by the ``pinout`` command-line
10+
tool to write details of the GPIO pins available.
11+
12+
Unless specified, the revision of the current device will be detected. A
13+
particular revision may be selected with the --revision command-line
14+
option. *e.g.*:
15+
16+
pinout.py --revision 000d
17+
18+
By default, the output will include ANSI color codes if run in a color-capable
19+
terminal. This behaviour may be overridden by the --color or --monochrome
20+
options to force colored or non-colored output, respectively. *e.g.*:
21+
22+
pinout.py --monochrome
23+
24+
Full usage details are available with:
25+
26+
pinout.py --help

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Table of Contents
1818
api_tools
1919
api_pins
2020
api_exc
21+
cli_pinout
2122
changelog
2223
license
2324

gpiozero/cli/__init__.py

Whitespace-only changes.

gpiozero/cli/pinout.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
"""
3+
pinout.py - gpiozero command-line pinout tool.
4+
5+
Output Raspberry Pi GPIO pinout information.
6+
"""
7+
8+
from __future__ import unicode_literals, absolute_import, print_function, division
9+
10+
import argparse
11+
import sys
12+
13+
from gpiozero import *
14+
15+
16+
def parse_args(args):
17+
parser = argparse.ArgumentParser(
18+
description=__doc__
19+
)
20+
21+
parser.add_argument(
22+
'-r', '--revision',
23+
dest='revision',
24+
default='',
25+
help='RPi revision. Default is to autodetect revision of current device'
26+
)
27+
28+
parser.add_argument(
29+
'-c', '--color',
30+
action="store_true",
31+
default=None,
32+
help='Force colored output (by default, the output will include ANSI'
33+
'color codes if run in a color-capable terminal). See also --monochrome'
34+
)
35+
36+
parser.add_argument(
37+
'-m', '--monochrome',
38+
dest='color',
39+
action='store_false',
40+
help='Force monochrome output. See also --color'
41+
)
42+
43+
try:
44+
args = parser.parse_args(args)
45+
except argparse.ArgumentError as ex:
46+
print('Error parsing arguments.')
47+
parser.error(str(ex.message))
48+
exit(-1)
49+
return args
50+
51+
52+
def main():
53+
args = parse_args(sys.argv[1:])
54+
55+
if args.revision == '':
56+
try:
57+
pi_info().pprint(color=args.color)
58+
except IOError:
59+
print('This device is not a Raspberry Pi?')
60+
exit(2)
61+
else:
62+
pi_info(args.revision).pprint(color=args.color)
63+
64+
65+
if __name__ == '__main__':
66+
main()

tests/cli/test_pinout.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import (
2+
unicode_literals,
3+
absolute_import,
4+
print_function,
5+
division,
6+
)
7+
str = type('')
8+
9+
10+
import pytest
11+
12+
import gpiozero.cli.pinout as pinout
13+
14+
15+
def test_args_incorrect():
16+
with pytest.raises(SystemExit) as ex:
17+
pinout.parse_args(['--nonexistentarg'])
18+
assert ex.value.code == 2
19+
20+
21+
def test_args_color():
22+
args = pinout.parse_args([])
23+
assert args.color is None
24+
args = pinout.parse_args(['--color'])
25+
assert args.color is True
26+
args = pinout.parse_args(['--monochrome'])
27+
assert args.color is False
28+
29+
30+
def test_args_revision():
31+
args = pinout.parse_args(['--revision', '000d'])
32+
assert args.revision == '000d'
33+
34+
35+
def test_help(capsys):
36+
with pytest.raises(SystemExit) as ex:
37+
pinout.parse_args(['--help'])
38+
out, err = capsys.readouterr()
39+
assert 'GPIO pinout' in out
40+
assert ex.value.code == 0

0 commit comments

Comments
 (0)