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__]))
39
def open(self,parent,cfg):
40
self.encoding = 'utf8' if 'utf8' in cfg['encoding'].lower().replace('-','') else cfg['encoding']
41
self.inputfile = cfg['inputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['inputfile'] else self.inputfile
42
self.outputfile = cfg['outputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['outputfile'] else self.outputfile
43
if (self.inputfile and not self.outputfile) or (not self.inputfile and self.outputfile):
44
self.errors.append([__name__,'invalid','[%s] inputfile=%s without outputfile= value'%(__name__,cfg['inputfile'])])
45
logger.warn('%s\t%s',*self.errors[-1][1:])
47
self.searchfor = cfg['searchfor'][1:-1] if cfg['searchfor'].startswith('\"') and cfg['searchfor'].endswith('\"') else cfg['searchfor']
48
self.replacewith = cfg['replacewith'][1:-1] if cfg['replacewith'].startswith('\"') and cfg['replacewith'].endswith('\"') else cfg['replacewith']
49
self.regex = re.compile((self.searchfor), re.I + re.U)
53
skipclose = not self.inputfile
54
if self.inputfile: return
56
self.p.cfoutput[k]['tempbuff'] = [self.regex.sub(self.replacewith,line) for line in self.p.cfoutput[k]['tempbuff']]
66
if not os.path.exists(self.inputfile):
67
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.inputfile)])
68
logger.error('%s\t%s',*self.errors[-1][1:])
73
os.makedirs(os.path.dirname(self.outputfile))
76
logger.exception('%s\t%s, %s, %s',*['failed',e.errno,e.strerror,e.filename,])
79
# open input and output files
81
if out == self.inputfile:
83
fd,out = tempfile.mkstemp(suffix='.tmp', prefix='~', dir=self.p.tempdir)
86
o = codecs.open(out,'w',self.encoding)
87
i = codecs.open(self.inputfile,'r',self.encoding)
89
raise RuntimeError('Failed to open [%s] input/output files'%(__name__))
91
sys.stderr.write('[%s] %s\n Please wait'%(__name__,self.outputfile))
94
# loop writes output line-by-line
96
o.write('%s\n'%(self.regex.sub(self.replacewith,line.rstrip('\r\n'))))
98
if not cnt%5000: sys.stderr.write('.')
99
sys.stderr.write('\n')
100
# close input and output files
104
if not out == self.outputfile:
106
shutil.move(out,self.outputfile)
108
except KeyboardInterrupt:
110
raise KeyboardInterrupt()
112
if not os.path.exists(self.outputfile):
113
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.outputfile)])
114
logger.error('%s\t%s',*self.errors[-1][1:])
117
'''Command prompt help.'''
118
return "\n%s\n\tUsage:\n\tfrom %s import filter\n"%(
119
os.path.basename(sys.argv[0]),
120
os.path.splitext(os.path.basename(sys.argv[0]))[0]
123
licensetxt=u'''CorpusFiltergraph™ v4.0
124
Copyright © 2010-2012 Precision Translation Tools Co., Ltd.
126
This program is free software: you can redistribute it and/or modify
127
it under the terms of the GNU Lesser General Public License as published by
128
the Free Software Foundation, either version 3 of the License, or
129
(at your option) any later version.
131
This program is distributed in the hope that it will be useful,
132
but WITHOUT ANY WARRANTY; without even the implied warranty of
133
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134
GNU Lesser General Public License for more details.
136
You should have received a copy of the GNU Lesser General Public License
137
along with this program. If not, see http://www.gnu.org/licenses/.
139
For more information, please contact Precision Translation Tools Co., Ltd.
140
at: http://www.precisiontranslationtools.com'''
142
if __name__ == "__main__":
145
sys.stdout.write(usage().encode('utf8')+'\n')