Skip to content

Commit 726e861

Browse files
committed
Add pinout command-line tool
1 parent 322ec13 commit 726e861

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

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 an 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 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)