Notes using Python subprocesses

I want to do various things with pipe. It was unexpectedly difficult. ..

    def _transrate(trans_opt):
        """
translation
        """
        # import locale
        # enc = locale.getpreferredencoding()
        # env['PYTHONIOENCODING'] = enc
        command0 = '/usr/bin/pbpaste'
        command1 = '/usr/local/bin/trans' + trans_opt
        command2 = '/usr/bin/pbcopy'
        env = os.environ.copy()
        env["PATH"] = "/usr/bin:/usr/local/bin/:" + env['PATH']
        # command0
        process0 = subprocess.Popen(shlex.split(
            command0), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
        process0.wait()  # wait()Is a simple implementation
        # command1
        process1 = subprocess.Popen(shlex.split(
            command1), stdin=process0.stdout, stdout=subprocess.PIPE, env=env)
        process1.wait()  # wait()Is a simple implementation
        # Allow ps_process to receive a SIGPIPE if grep_process exits.
        process0.stdout.close()
        # command2
        process2 = subprocess.Popen(shlex.split(
            command2), stdin=process1.stdout, stdout=subprocess.PIPE, env=env)
        process2.wait()  # wait()Is a simple implementation
        # Allow ps_process to receive a SIGPIPE if grep_process exits.
        process1.stdout.close()
        # Allow ps_process to receive a SIGPIPE if grep_process exits.
        process2.stdout.close()

        """
notification
        """
        process0 = subprocess.Popen(shlex.split(
            command0), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
        process0.wait()  # wait()Is a simple implementation
        output = process0.communicate()[0].decode('utf-8')
        process0.stdout.close()
        command3 = '/usr/bin/osascript -e \'display notification "'+output+'" with title "translation"\''
        subprocess.Popen(shlex.split(command3), env=env)

    def transrate_j2e():
        """
translation
        """
        _transrate(' -b ja:en')
    def transrate_e2j():
        """
translation
        """
        _transrate(' -b en:ja')

Recommended Posts

Notes using Python subprocesses
Notes on using MeCab from Python
Notes on installing Python using PyEnv
Notes on using rstrip with python.
Python scraping notes
Python study notes _000
Python beginner notes
Python study notes_006
Start using Python
Python study notes _005
Python grammar notes
Python Library notes
Scraping using Python
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
Notes for using OpenCV on Windows10 Python 3.8.3.
Notes using cChardet and python3-chardet in Python 3.3.1.
Notes for using python (pydev) in eclipse
Notes on using code formatter in Python
Operate Redmine using Python Redmine
missingintegers python personal notes
Fibonacci sequence using Python
Data analysis using Python 0
Python package development notes
Notes on using dict in python [Competition Pro]
python decorator usage notes
Python ipaddress package notes
[Personal notes] Python, Django
Using Python #external packages
Python Pickle format notes
WiringPi-SPI communication using Python
[Python] pytest-mock Usage notes
First Python miscellaneous notes
Age calculation using python
Matlab => Python migration notes
Search Twitter using Python
Notes around Python3 assignments
Name identification using python
Notes on using Alembic
Try using Tweepy [Python2.7]
Python try / except notes
Python framework bottle notes
[Python] Notes on accelerating genetic algorithms using multiprocessing
Minimum notes when using Python on Mac (Homebrew edition)
Flatten using Python yield from
Scraping using Python 3.5 async / await
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
O'Reilly python3 Primer Learning Notes
Web scraping notes in python3
[Python] Try using Tkinter's canvas
Using Quaternion with Python ~ numpy-quaternion ~
Python standard unittest usage notes
Python notes to forget soon
python * args, ** kwargs Usage notes
Try using Kubernetes Client -Python-
Python 處 處 regular expression Notes
[Python] Using OpenCV with Python (Basic)
[Django] Notes on using django-debug-toolbar