From f0fab6e11fef88541f5a094b4174d1898ade51ee Mon Sep 17 00:00:00 2001 From: Alan Bacon Date: Sun, 12 Apr 2020 22:12:06 +0100 Subject: [PATCH 1/4] results function to return multiline output as array --- bash/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bash/__init__.py b/bash/__init__.py index d8da050..aa4d542 100644 --- a/bash/__init__.py +++ b/bash/__init__.py @@ -1,3 +1,4 @@ +import re import sys from subprocess import PIPE, Popen SUBPROCESS_HAS_TIMEOUT = True @@ -9,6 +10,8 @@ # will mean you don't have access to things like timeout SUBPROCESS_HAS_TIMEOUT = False +SPLIT_NEWLINE_REGEX = re.compile(' *\n *') + class bash(object): "This is lower class because it is intended to be used as a method." @@ -58,3 +61,10 @@ def value(self): if self.stdout: return self.stdout.strip().decode(encoding='UTF-8') return '' + + def results(self): + output = self.stdout.decode(encoding='UTF-8').strip() or '' + if output: + return SPLIT_NEWLINE_REGEX.split(output) + else: + return [] From bdbe983455f2e9b498ba00b14d822c3484664601 Mon Sep 17 00:00:00 2001 From: Alan Bacon Date: Sun, 12 Apr 2020 23:58:05 +0100 Subject: [PATCH 2/4] allow iteration over result --- bash/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bash/__init__.py b/bash/__init__.py index aa4d542..2630fba 100644 --- a/bash/__init__.py +++ b/bash/__init__.py @@ -57,6 +57,9 @@ def __nonzero__(self): def __bool__(self): return bool(self.value()) + def __iter__(self): + return self.results().__iter__() + def value(self): if self.stdout: return self.stdout.strip().decode(encoding='UTF-8') From d1eada9795c1aaaf0b9a04ba956026b25d8bd961 Mon Sep 17 00:00:00 2001 From: Alan Bacon Date: Mon, 13 Apr 2020 00:12:46 +0100 Subject: [PATCH 3/4] add unit tests --- tests.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests.py b/tests.py index 41263b4..babc0de 100644 --- a/tests.py +++ b/tests.py @@ -66,3 +66,12 @@ def test_sync_false_does_not_wait(self): self.assertTrue((t2-t1).total_seconds() < 0.5) b.sync() self.assertEqual(b.stdout, b'1\n') + + def test_iterate_over_results(self): + expecting = ['setup.py', 'tests.py'] + b = bash('ls . | grep "\.py"') + results = b.results() + self.assertEqual(results, expecting) + + iteratedResults = [result for result in b] + self.assertEqual(iteratedResults, expecting) From 58ea69a5d0b99d54fe50d4a5a104133da68546b1 Mon Sep 17 00:00:00 2001 From: Alan Bacon Date: Mon, 13 Apr 2020 00:49:41 +0100 Subject: [PATCH 4/4] update readme --- README.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.rst b/README.rst index 339dc65..73afaf8 100644 --- a/README.rst +++ b/README.rst @@ -60,6 +60,15 @@ To get a stripped, unicode string version of bash.stdout call value():: >>> b = bash('ls tests.py').value() u'tests.py' +To get the results (separated by newlines) as a list:: + + >>> b = bash('ls . ').results() + ['bash.pyc', 'tests.pyc'] + +or use the iterator directly:: + + >>> b = [res for res in bash('ls . ')] + ['bash.pyc', 'tests.pyc'] Motivation ----------