From 726e861238ce43c62eaf6c29f7784cb1b39cb53c Mon Sep 17 00:00:00 2001 From: Stewart Date: Mon, 7 Nov 2016 22:09:59 +0000 Subject: [PATCH 1/8] Add pinout command-line tool --- gpiozero/cli/__init__.py | 0 gpiozero/cli/pinout.py | 66 ++++++++++++++++++++++++++++++++++++++++ tests/cli/test_pinout.py | 40 ++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 gpiozero/cli/__init__.py create mode 100644 gpiozero/cli/pinout.py create mode 100644 tests/cli/test_pinout.py diff --git a/gpiozero/cli/__init__.py b/gpiozero/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gpiozero/cli/pinout.py b/gpiozero/cli/pinout.py new file mode 100644 index 0000000..8c60322 --- /dev/null +++ b/gpiozero/cli/pinout.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +""" +pinout.py - 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)) + 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 an Raspberry Pi?') + exit(2) + else: + pi_info(args.revision).pprint(color=args.color) + + +if __name__ == '__main__': + main() diff --git a/tests/cli/test_pinout.py b/tests/cli/test_pinout.py new file mode 100644 index 0000000..8aac0df --- /dev/null +++ b/tests/cli/test_pinout.py @@ -0,0 +1,40 @@ +from __future__ import ( + unicode_literals, + absolute_import, + print_function, + division, + ) +str = type('') + + +import pytest + +import cli.pinout as pinout + + +def test_args_incorrect(): + with pytest.raises(SystemExit) as ex: + pinout.parse_args(['--nonexistentarg']) + assert ex.value.code == 2 + + +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 + assert ex.value.code == 0 From 5f47bcd3796193af0649565e61372b87b4a59821 Mon Sep 17 00:00:00 2001 From: Stewart Date: Mon, 7 Nov 2016 22:34:21 +0000 Subject: [PATCH 2/8] Add terse documentation for pinout tool --- docs/cli_pinout.rst | 26 ++++++++++++++++++++++++++ docs/index.rst | 1 + 2 files changed, 27 insertions(+) create mode 100644 docs/cli_pinout.rst diff --git a/docs/cli_pinout.rst b/docs/cli_pinout.rst new file mode 100644 index 0000000..4f8950b --- /dev/null +++ b/docs/cli_pinout.rst @@ -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 write 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. *e.g.*: + + pinout.py --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. *e.g.*: + + pinout.py --monochrome + +Full usage details are available with: + + pinout.py --help diff --git a/docs/index.rst b/docs/index.rst index 256db1e..c4245c4 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -18,6 +18,7 @@ Table of Contents api_tools api_pins api_exc + cli_pinout changelog license From a812bfedebd1937b22a5c38a21d8c05fa30f5edb Mon Sep 17 00:00:00 2001 From: Stewart Date: Mon, 7 Nov 2016 22:34:41 +0000 Subject: [PATCH 3/8] Fix minor typo --- gpiozero/cli/pinout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpiozero/cli/pinout.py b/gpiozero/cli/pinout.py index 8c60322..8e652dc 100644 --- a/gpiozero/cli/pinout.py +++ b/gpiozero/cli/pinout.py @@ -56,7 +56,7 @@ def main(): try: pi_info().pprint(color=args.color) except IOError: - print('This device is not an Raspberry Pi?') + print('This device is not a Raspberry Pi?') exit(2) else: pi_info(args.revision).pprint(color=args.color) From 48d792439524945110e50f21f8172bd6924c1096 Mon Sep 17 00:00:00 2001 From: Stewart Date: Mon, 7 Nov 2016 22:46:46 +0000 Subject: [PATCH 4/8] Fix import in tests --- tests/cli/test_pinout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cli/test_pinout.py b/tests/cli/test_pinout.py index 8aac0df..3dbc840 100644 --- a/tests/cli/test_pinout.py +++ b/tests/cli/test_pinout.py @@ -9,7 +9,7 @@ str = type('') import pytest -import cli.pinout as pinout +import gpiozero.cli.pinout as pinout def test_args_incorrect(): From 02938b48ecca15f270adbe917d297a71be3a74db Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Mon, 5 Dec 2016 21:03:06 +0000 Subject: [PATCH 5/8] Tidy up pinout tool PR --- docs/{cli_pinout.rst => cli_tools.rst} | 12 ++++++------ docs/index.rst | 2 +- gpiozero/cli/pinout.py | 4 ++-- setup.py | 3 +++ 4 files changed, 12 insertions(+), 9 deletions(-) rename docs/{cli_pinout.rst => cli_tools.rst} (73%) mode change 100644 => 100755 gpiozero/cli/pinout.py diff --git a/docs/cli_pinout.rst b/docs/cli_tools.rst similarity index 73% rename from docs/cli_pinout.rst rename to docs/cli_tools.rst index 4f8950b..a92ec71 100644 --- a/docs/cli_pinout.rst +++ b/docs/cli_tools.rst @@ -11,16 +11,16 @@ tool to write 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. *e.g.*: +option. e.g:: - pinout.py --revision 000d + 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. *e.g.*: +options to force colored or non-colored output, respectively. e.g:: - pinout.py --monochrome + pinout --monochrome -Full usage details are available with: +Full usage details are available with:: - pinout.py --help + pinout --help diff --git a/docs/index.rst b/docs/index.rst index c4245c4..6ea2d2a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -18,7 +18,7 @@ Table of Contents api_tools api_pins api_exc - cli_pinout + cli_tools changelog license diff --git a/gpiozero/cli/pinout.py b/gpiozero/cli/pinout.py old mode 100644 new mode 100755 index 8e652dc..d28cc1c --- a/gpiozero/cli/pinout.py +++ b/gpiozero/cli/pinout.py @@ -1,6 +1,6 @@ #!/usr/bin/env python """ -pinout.py - gpiozero command-line pinout tool. +pinout - gpiozero command-line pinout tool. Output Raspberry Pi GPIO pinout information. """ @@ -56,7 +56,7 @@ def main(): try: pi_info().pprint(color=args.color) except IOError: - print('This device is not a Raspberry Pi?') + print('This device is not a Raspberry Pi') exit(2) else: pi_info(args.revision).pprint(color=args.color) diff --git a/setup.py b/setup.py index a7ea6ae..6d70a29 100644 --- a/setup.py +++ b/setup.py @@ -75,6 +75,9 @@ __entry_points__ = { 'MockPin = gpiozero.pins.mock:MockPin', 'MockPWMPin = gpiozero.pins.mock:MockPWMPin', ], + 'console_scripts': [ + 'pinout = gpiozero.cli.pinout:main', + ] } From a1b3847cab0bdbace72e7b1967fb01421066843d Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Tue, 6 Dec 2016 13:36:59 +0000 Subject: [PATCH 6/8] Grammar --- docs/cli_tools.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cli_tools.rst b/docs/cli_tools.rst index a92ec71..1d6c5e2 100644 --- a/docs/cli_tools.rst +++ b/docs/cli_tools.rst @@ -11,13 +11,13 @@ tool to write 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. e.g:: +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. e.g:: +options to force colored or non-colored output, respectively. For example:: pinout --monochrome From 117e4f59720de9d13ddb4eaa439915addb616f1d Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Tue, 6 Dec 2016 13:39:16 +0000 Subject: [PATCH 7/8] Use from to import rather than rename --- tests/cli/test_pinout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cli/test_pinout.py b/tests/cli/test_pinout.py index 3dbc840..afc52f6 100644 --- a/tests/cli/test_pinout.py +++ b/tests/cli/test_pinout.py @@ -9,7 +9,7 @@ str = type('') import pytest -import gpiozero.cli.pinout as pinout +from gpiozero.cli import pinout def test_args_incorrect(): From fc54667f3476942624c0d22877edf7bf6bacd4d5 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Sun, 18 Dec 2016 03:33:50 +0000 Subject: [PATCH 8/8] More small tidyups - switch to using sys.exit instead of exit - always exit with error-code 1 - don't bother testing error-codes - documentation wording tweak --- docs/cli_tools.rst | 2 +- gpiozero/cli/pinout.py | 4 ++-- tests/cli/test_pinout.py | 5 ----- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/docs/cli_tools.rst b/docs/cli_tools.rst index 1d6c5e2..1b1443d 100644 --- a/docs/cli_tools.rst +++ b/docs/cli_tools.rst @@ -7,7 +7,7 @@ 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 write details of the GPIO pins available. +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 diff --git a/gpiozero/cli/pinout.py b/gpiozero/cli/pinout.py index d28cc1c..70b84c8 100755 --- a/gpiozero/cli/pinout.py +++ b/gpiozero/cli/pinout.py @@ -45,7 +45,7 @@ def parse_args(args): except argparse.ArgumentError as ex: print('Error parsing arguments.') parser.error(str(ex.message)) - exit(-1) + sys.exit(1) return args @@ -57,7 +57,7 @@ def main(): pi_info().pprint(color=args.color) except IOError: print('This device is not a Raspberry Pi') - exit(2) + sys.exit(1) else: pi_info(args.revision).pprint(color=args.color) diff --git a/tests/cli/test_pinout.py b/tests/cli/test_pinout.py index afc52f6..7e47b1e 100644 --- a/tests/cli/test_pinout.py +++ b/tests/cli/test_pinout.py @@ -15,8 +15,6 @@ from gpiozero.cli import pinout def test_args_incorrect(): with pytest.raises(SystemExit) as ex: pinout.parse_args(['--nonexistentarg']) - assert ex.value.code == 2 - def test_args_color(): args = pinout.parse_args([]) @@ -26,15 +24,12 @@ def test_args_color(): 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 - assert ex.value.code == 0