python subprocess wrapper class

Deprecated in version 2.6: The commands module has been removed in Python 3.0. Use the subprocess module instead.

Try using Subprocess to say goodbye to commands.

shell.py


#!/usr/bin/python

import sys
import shlex
from   subprocess import Popen, PIPE, STDOUT

__version__ = '0.0.1'

#----------------------------------------------------------
# SHELL CLASS
#----------------------------------------------------------
class Shell(object):

    def __init__(self):

        self.p  = None

    def cmd(self, cmd):

        p_args = {'stdin'     : None,
                  'stdout'    : PIPE,
                  'stderr'    : STDOUT,
                  'shell'     : False,}

        return self._execute(cmd, p_args)

    def pipe(self, cmd):

        p_args = {'stdin'     : self.p.stdout,
                  'stdout'    : PIPE,
                  'stderr'    : STDOUT,
                  'shell'     : False,}

        return self._execute(cmd, p_args)

    def _execute(self, cmd, p_args):

        try :
            self.p = Popen(shlex.split(cmd), **p_args)
            return self

        except :
            print 'Command Not Found: %s' % (cmd)
            sys.exit()

    def commit(self):

        result = self.p.communicate()[0]
        status = self.p.returncode

        return (status, result)

#----------------------------------------------------------
# TEST
#----------------------------------------------------------
def test():
    '''
    tac ./test.txt | sort | uniq -c

    '''
    shell= Shell()

    #--- CASE 1
    (return_code, stdout) = shell.cmd('tac ./test.txt').pipe('sort').pipe('uniq').commit()
    print 'RETURN_CODE: %s  \nSTDOUT: \n%s' % (return_code, stdout)

    #--- CASE 2
    s = shell.cmd('tac ./test.txt')
    s.pipe('sort')
    s.pipe('uniq')
    (return_code, stdout) = s.commit()
    print 'RETURN_CODE: %s, \nSTDOUT: \n%s' % (return_code, stdout)

if __name__ == '__main__':test()

test.txt


1
2
3
4
5
6
7
8
9
0

result

RETURN_CODE: 0
STDOUT:
0
1
2
3
4
5
6
7
8
9

RETURN_CODE: 0,
STDOUT:
0
1
2
3
4
5
6
7
8
9

Conclusion

I don't think I'm in the wrong direction, but the error handling is strange. Let's review it soon.

Recommended Posts

python subprocess wrapper class
python syslog wrapper class
YOLO Python wrapper class
[Python] class, instance
"Kanrika" python class
About python, class
Python class, instance
#Python basics (class)
Python class (Python learning memo ⑦)
case class in python
[Python] Class inheritance (super)
Python self-made class sort
[python] class basic methods
Method_missing-like wrapper in Python
[Python] Class inheritance, override
Class notation in Python
Python exception class list
Python: Class and instance variables
Python class member scope summary
Python class variables and instance variables
Wrapper running Hadoop in Python
How to write a Python class
perl objects and python class part 2.
Python
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Landmines hidden in Python class variables
Python class definitions and instance handling
"The easiest Python introductory class" modified
class
[Python] Road to snake charmer (3) Python class
Examine the object's class in python
perl objects and python class part 1.
Easy parallel execution with python subprocess
class
[Python] Inherit a class with class variables
Generate a first class collection in Python
Create a Python function decorator with Class
[Introduction to Python] How to use class in Python?
Implement __eq__ etc. generically in Python class
[Learning memo] Basics of class by python
Build a blockchain with Python ① Create a class
[Python] How to make a class iterable
Python basic operation 3rd: Object-oriented and class
[Python] Difference between class method and static method
How to use __slots__ in Python class
Generate a class from a string in Python
[Python2.7] Summary of how to use subprocess
Implemented Python wrapper for Qiita API v2
Talking about Python class attributes and metaclasses