subprocess: xterm -c cat, need to send data to cat and have it displayed in the xterm window

Veek. M vek.m1234 at gmail.com
Mon Jul 11 23:20:20 EDT 2016


Script grabs some image data and runs imagemagick on it to extract some 
chinese. Then tesseract OCR to get the actual unicode.

I then need to get it translated which also works and then display in 
XTerm using cat.

I could cat << named_pipe but I was wondering if this was the only way?
Could I juggle fd's instead?


#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, time

import shlex
import codecs, subprocess

import goslate
from textblob.blob import TextBlob


def cmd_exec(cmd, wait=True, shell=True):
    print cmd
    
    cmd_line = shlex.split(cmd)
    retobj = subprocess.Popen(cmd_line, shell, cwd='/tmp', 
stdin=subprocess.PIPE)
    
    if wait:
        retobj.wait()
    
    return retobj

def get_text_and_process(skip=False):
    if skip: return
    cmd_exec('import /tmp/x.png')
    cmd_exec('convert /tmp/x.png -resize 200% /tmp/x.resize.png')
    cmd_exec('tesseract /tmp/x.resize.png /tmp/tao -l chi_sim')


get_text_and_process(skip=True)

fh = codecs.open('/tmp/tao.txt', encoding='utf-8')
fd = cmd_exec('xterm -en utf-8 -geometry 50x20+0+0 -e /bin/bash -c "cat 
-"', shell=False, wait=False)
fd.communicate('test\n') #how do i communicate with cat and not xterm
sys.exit() #I EXIT HERE


#This stuff doesn't run (but works) - sys.exit above
for line in fh.readlines():
    try:
        print >>fd.stdout, line.encode('utf-8')
        
        blob = TextBlob(line)
        print >>fd.stdout, blob.detect_language()
        print >>fd.stdin, blob.translate(to='en') 
    except UnicodeDecodeError, TranslatorError:
        pass

fh.close()



More information about the Python-list mailing list