4
#===============================================================================
6
#===============================================================================
9
#4.0.264 - version update
17
logger = logging.getLogger('.'.join([os.path.splitext(os.path.basename(sys.argv[0]))[0],'manager','filtergraph',__name__]))
37
'universal_newlines': False,
56
def open(self,parent,cfg):
57
self.args = dict(cfg['args'])
58
self.kw = dict(cfg['kw'])
60
self.script = self.p.decodepath('%s,%s'%(__name__.split(',')[1],cfg['script']),True)
62
self.errors.append([__name__,'missing',cfg['script']])
63
logger.error('%s\t%s',*self.errors[-1][1:])
64
self.args[0] = self.script
65
self.kw['cwd'] = os.path.dirname(self.script)
68
# convert args dict to args list
69
self.args = [str(self.args[k]) for k in sorted(self.args.keys())]
70
if 'executable' in cfg['kw'] and cfg['kw']['executable']:
71
self.kw['executable'] = self.p.decodepath('%s,%s'%(__name__.split(',')[1],cfg['kw']['executable']),True)
72
if not self.kw['executable']:
73
self.errors.append([__name__,'missing',cfg['kw']['executable']])
74
logger.error('%s\t%s',*self.errors[-1][1:])
75
self.args.insert(0,cfg['exeoptions'] if cfg['exeoptions'] else '')
77
self.kw['executable'] = None
78
self.kw['stdin'] = subprocess.PIPE if cfg['kw']['stdin'] == 'PIPE' else cfg['kw']['stdin']
79
self.kw['stdout'] = subprocess.PIPE if cfg['kw']['stdout'] == 'PIPE' else cfg['kw']['stdout']
80
self.kw['stderr'] = subprocess.PIPE if cfg['kw']['stderr'] == 'PIPE' else cfg['kw']['stderr']
82
self.encoding = 'utf8' if 'utf8' in cfg['encoding'].lower().replace('-','') else cfg['encoding']
83
self.inputfile = cfg['inputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['inputfile'] else self.inputfile
84
self.outputfile = cfg['outputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['outputfile'] else self.outputfile
85
if (self.inputfile and not self.outputfile) or (not self.inputfile and self.outputfile):
86
self.errors.append([__name__,'invalid','[%s] inputfile=%s without outputfile= value'%(__name__,cfg['inputfile'])])
87
logger.warn('%s\t%s',*self.errors[-1][1:])
91
skipclose = not self.inputfile
92
if self.inputfile: return
95
subproc = subprocess.Popen(self.args,**self.kw)
97
raise RuntimeError('failed \"[%s]\" \"%s %s\" - %s'%(__name__,self.kw['executable'],' '.join(self.args),str(e)))
98
txtout, txterr = subproc.communicate('\x0a'.join(self.p.cfoutput[k]['tempbuff']).encode(self.encoding) + '\n')
99
self.p.cfoutput[k]['tempbuff'] = [line.decode(self.encoding) for line in txtout.splitlines()]
109
if not os.path.exists(self.inputfile):
110
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.inputfile)])
111
logger.error('%s\t%s',*self.errors[-1][1:])
116
os.makedirs(os.path.dirname(self.outputfile))
118
if not e.errno == 17:
119
logger.exception('%s\t%s, %s, %s',*['failed',e.errno,e.strerror,e.filename,])
122
# open input and output files
123
out = self.outputfile
124
if out == self.inputfile:
126
fd,out = tempfile.mkstemp(suffix='.tmp', prefix='~', dir=self.p.tempdir)
129
o = codecs.open(out,'w',self.encoding)
130
i = codecs.open(self.inputfile,'r',self.encoding)
133
raise RuntimeError('Failed to open [%s] input/output files'%(__name__))
135
sys.stderr.write('[%s] %s\n Please wait'%(__name__,self.outputfile))
139
subproc = subprocess.Popen(self.args,**self.kw)
141
raise RuntimeError('failed \"[%s]\" \"%s %s\" - %s'%(__name__,self.kw['executable'],' '.join(self.args),str(e)))
144
# loop writes output line-by-line
146
subproc[tgtlang]['popen'].stdin.write('%s\n'%(line.encode(self.encoding).strip()))
147
o.write('%s\n'%(subproc[tgtlang]['popen'].stdout.readline().rstrip('\r\n').decode(self.encoding)))
149
if not cnt%5000: sys.stderr.write('.')
150
sys.stderr.write('\n')
151
# close input and output files
155
if not out == self.outputfile:
157
shutil.move(out,self.outputfile)
159
except KeyboardInterrupt:
161
raise KeyboardInterrupt()
163
if not os.path.exists(self.outputfile):
164
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.outputfile)])
165
logger.error('%s\t%s',*self.errors[-1][1:])
168
'''Command prompt help.'''
169
return "\n%s\n\tUsage:\n\tfrom %s import filter\n"%(
170
os.path.basename(sys.argv[0]),
171
os.path.splitext(os.path.basename(sys.argv[0]))[0]
174
licensetxt=u'''CorpusFiltergraph™ v4.0
175
Copyright © 2010-2012 Precision Translation Tools Co., Ltd.
177
This program is free software: you can redistribute it and/or modify
178
it under the terms of the GNU Lesser General Public License as published by
179
the Free Software Foundation, either version 3 of the License, or
180
(at your option) any later version.
182
This program is distributed in the hope that it will be useful,
183
but WITHOUT ANY WARRANTY; without even the implied warranty of
184
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
185
GNU Lesser General Public License for more details.
187
You should have received a copy of the GNU Lesser General Public License
188
along with this program. If not, see http://www.gnu.org/licenses/.
190
For more information, please contact Precision Translation Tools Co., Ltd.
191
at: http://www.precisiontranslationtools.com'''
193
if __name__ == "__main__":
196
sys.stdout.write(usage().encode('utf8')+'\n')