4
#===============================================================================
5
# Author: Walapa Muangjeen
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__]))
35
def open(self,parent,cfg):
36
self.encoding = 'utf8' if 'utf8' in cfg['encoding'].lower().replace('-','') else cfg['encoding']
37
self.inputfile = cfg['inputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['inputfile'] else self.inputfile
38
self.outputfile = cfg['outputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['outputfile'] else self.outputfile
39
if (self.inputfile and not self.outputfile) or (not self.inputfile and self.outputfile):
40
self.errors.append([__name__,'invalid','[%s] inputfile=%s without outputfile= value'%(__name__,cfg['inputfile'])])
41
logger.warn('%s\t%s',*self.errors[-1][1:])
45
skipclose = not self.inputfile
46
if self.inputfile: return
48
self.p.cfoutput[k]['tempbuff'] = [re.sub( r' +' , r'', line ) for line in self.p.cfoutput[k]['tempbuff']]
58
if not os.path.exists(self.inputfile):
59
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.inputfile)])
60
logger.error('%s\t%s',*self.errors[-1][1:])
65
os.makedirs(os.path.dirname(self.outputfile))
68
logger.exception('%s\t%s, %s, %s',*['failed',e.errno,e.strerror,e.filename,])
71
# open input and output files
73
if out == self.inputfile:
75
fd,out = tempfile.mkstemp(suffix='.tmp', prefix='~', dir=self.p.tempdir)
78
o = codecs.open(out,'w',self.encoding)
79
i = codecs.open(self.inputfile,'r',self.encoding)
81
raise RuntimeError('Failed to open [%s] input/output files'%(__name__))
83
sys.stderr.write('[%s] %s\n Please wait'%(__name__,self.outputfile))
86
# loop writes output line-by-line
88
o.write('%s\n'%(re.sub( r' +' , r'', line.rstrip('\r\n'))))
90
if not cnt%5000: sys.stderr.write('.')
91
sys.stderr.write('\n')
92
# close input and output files
96
if not out == self.outputfile:
98
shutil.move(out,self.outputfile)
100
except KeyboardInterrupt:
102
raise KeyboardInterrupt()
104
if not os.path.exists(self.outputfile):
105
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.outputfile)])
106
logger.error('%s\t%s',*self.errors[-1][1:])
109
'''Command prompt help.'''
110
return "\n%s\n\tUsage:\n\tfrom %s import filter\n"%(
111
os.path.basename(sys.argv[0]),
112
os.path.splitext(os.path.basename(sys.argv[0]))[0]
115
licensetxt=u'''CorpusFiltergraph™ v4.0
116
Copyright © 2010-2011 Precision Translation Tools Co., Ltd.
118
This program is free software: you can redistribute it and/or modify
119
it under the terms of the GNU Lesser General Public License as published by
120
the Free Software Foundation, either version 3 of the License, or
121
(at your option) any later version.
123
This program is distributed in the hope that it will be useful,
124
but WITHOUT ANY WARRANTY; without even the implied warranty of
125
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
126
GNU Lesser General Public License for more details.
128
You should have received a copy of the GNU Lesser General Public License
129
along with this program. If not, see http://www.gnu.org/licenses/.
131
For more information, please contact Precision Translation Tools Co., Ltd.
132
at: http://www.precisiontranslationtools.com'''
134
if __name__ == "__main__":
137
sys.stdout.write(usage().encode('utf8')+'\n')