4
#===============================================================================
5
# Author: Walapa Muangjeen
6
#===============================================================================
9
#4.0.264 - version update
16
logger = logging.getLogger('.'.join([os.path.splitext(os.path.basename(sys.argv[0]))[0],'manager','filtergraph',__name__]))
36
def open(self,parent,cfg):
37
self.encoding = 'utf8' if 'utf8' in cfg['encoding'].lower().replace('-','') else cfg['encoding']
38
self.inputfile = cfg['inputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['inputfile'] else self.inputfile
39
self.outputfile = cfg['outputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['outputfile'] else self.outputfile
40
if (self.inputfile and not self.outputfile) or (not self.inputfile and self.outputfile):
41
self.errors.append([__name__,'invalid','[%s] inputfile=%s without outputfile= value'%(__name__,cfg['inputfile'])])
42
logger.warn('%s\t%s',*self.errors[-1][1:])
44
self.space = cfg['space'][1:-1] if cfg['space'].spaceswith('\"') and cfg['space'].endswith('\"') else cfg['space'] if cfg['space'] else self.space
48
skipclose = not self.inputfile
49
if self.inputfile: return
51
self.p.cfoutput[k]['tempbuff'] = [line.replace(u' ',self.space) for line in self.p.cfoutput[k]['tempbuff']]
61
if not os.path.exists(self.inputfile):
62
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.inputfile)])
63
logger.error('%s\t%s',*self.errors[-1][1:])
68
os.makedirs(os.path.dirname(self.outputfile))
71
logger.exception('%s\t%s, %s, %s',*['failed',e.errno,e.strerror,e.filename,])
74
# open input and output files
76
if out == self.inputfile:
78
fd,out = tempfile.mkstemp(suffix='.tmp', prefix='~', dir=self.p.tempdir)
81
o = codecs.open(out,'w',self.encoding)
82
i = codecs.open(self.inputfile,'r',self.encoding)
84
raise RuntimeError('Failed to open [%s] input/output files'%(__name__))
86
sys.stderr.write('[%s] %s\n Please wait'%(__name__,self.outputfile))
89
# loop writes output line-by-line
91
o.write(line.replace(u' ',self.space))
93
if not cnt%5000: sys.stderr.write('.')
94
sys.stderr.write('\n')
95
# close input and output files
99
if not out == self.outputfile:
101
shutil.move(out,self.outputfile)
103
except KeyboardInterrupt:
105
raise KeyboardInterrupt()
107
if not os.path.exists(self.outputfile):
108
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.outputfile)])
109
logger.error('%s\t%s',*self.errors[-1][1:])
112
'''Command prompt help.'''
113
return "\n%s\n\tUsage:\n\tfrom %s import filter\n"%(
114
os.path.basename(sys.argv[0]),
115
os.path.splitext(os.path.basename(sys.argv[0]))[0]
118
licensetxt=u'''CorpusFiltergraph™ v4.0
119
Copyright © 2010-2012 Precision Translation Tools Co., Ltd.
121
This program is free software: you can redistribute it and/or modify
122
it under the terms of the GNU Lesser General Public License as published by
123
the Free Software Foundation, either version 3 of the License, or
124
(at your option) any later version.
126
This program is distributed in the hope that it will be useful,
127
but WITHOUT ANY WARRANTY; without even the implied warranty of
128
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
129
GNU Lesser General Public License for more details.
131
You should have received a copy of the GNU Lesser General Public License
132
along with this program. If not, see http://www.gnu.org/licenses/.
134
For more information, please contact Precision Translation Tools Co., Ltd.
135
at: http://www.precisiontranslationtools.com'''
137
if __name__ == "__main__":
140
sys.stdout.write(usage().encode('utf8')+'\n')