Je veux faire diverses choses avec la pipe. C'était étonnamment difficile. ..
def _transrate(trans_opt):
"""
Traduction
"""
# 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()Est une mise en œuvre simple
# command1
process1 = subprocess.Popen(shlex.split(
command1), stdin=process0.stdout, stdout=subprocess.PIPE, env=env)
process1.wait() # wait()Est une mise en œuvre simple
# 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()Est une mise en œuvre simple
# 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()Est une mise en œuvre simple
output = process0.communicate()[0].decode('utf-8')
process0.stdout.close()
command3 = '/usr/bin/osascript -e \'display notification "'+output+'" with title "Traduction"\''
subprocess.Popen(shlex.split(command3), env=env)
def transrate_j2e():
"""
Traduction
"""
_transrate(' -b ja:en')
def transrate_e2j():
"""
Traduction
"""
_transrate(' -b en:ja')
Recommended Posts