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