This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added parsed_output in Response method, r.std_out gives entire output…

… in string. This works fine for one line output, but commands like ps, netstat will yield many lines. In those cases users need to split('\n') and store it in a list and process the output. This is cumbersome. So parsed output will split the lines and results are further split and each column of the ouput can be accessed like a list. Eg. r.parsed_output[0][0] => will yield first column in the output.
  • Loading branch information...
kracekumar committed Jan 21, 2012
1 parent 04e88fe commit 11fa2febb2b8d8dce87c2288fcb53d27c59014fa
Showing with 29 additions and 2 deletions.
  1. +2 −1 AUTHORS
  2. +7 −0 README.rst
  3. +1 −1 envoy/__init__.py
  4. +8 −0 envoy/core.py
  5. +11 −0 test_envoy.py
View
@@ -11,4 +11,5 @@ Patches and Suggestions
```````````````````````
- Mark Holland
-- mrshu
+- mrshu
+- kracekumar
View
@@ -25,6 +25,13 @@ Run a command, get the response::
'usage: git config [options]'
>>> r.std_err
''
+ >>> r = envoy.run('netstat')
+
+ >>> r.parsed_output[0]
+ ['Active', 'Internet', 'connections', '(w/o', 'servers)']
+
+
+
Pipe stuff around too::
View
@@ -1,3 +1,3 @@
from core import *
-from core import __version__
+from core import __version__
View
@@ -123,6 +123,7 @@ def __init__(self, process=None):
self.std_out = None
self.status_code = None
self.history = []
+ self.parsed_output = []
def __repr__(self):
@@ -178,6 +179,13 @@ def run(command, data=None, timeout=None, env=None):
r.std_out = out
r.std_err = err
r.status_code = cmd.returncode
+ out = r.std_out.split('\n')
+
+ if len(out) > 2:
+ for item in out:
+ r.parsed_output.append(item.split(' '))
+ else:
+ r.parsed_output = r.std_out
history.append(r)
View
@@ -16,7 +16,18 @@ def test_pipe(self):
def test_timeout(self):
r = envoy.run('yes | head', timeout=1)
self.assertEqual(r.std_out, 'y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n')
+ self.assertEqual(r.status_code, 0)
+
+ def test_netstat(self):
+ r = envoy.run('netstat')
+ self.assertEqual(r.status_code, 0)
+ self.assertIsInstance(r.parsed_output, list)
+
+ def test_whoami(self):
+ r = envoy.run('whoami')
self.assertEqual(r.status_code, 0)
+ self.assertNotIsInstance(r.parsed_output, list)
+
if __name__ == "__main__":
unittest.main()

0 comments on commit 11fa2fe

Please sign in to comment.