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__]))
52
def open(self,parent,cfg):
53
self.encoding = 'utf8' if 'utf8' in cfg['encoding'].lower().replace('-','') else cfg['encoding']
54
self.inputfile = cfg['inputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['inputfile'] else self.inputfile
55
self.outputfile = cfg['outputfile'].replace('%(rootfolder)s',self.p.rootfolder) if cfg['outputfile'] else self.outputfile
56
if (self.inputfile and not self.outputfile) or (not self.inputfile and self.outputfile):
57
self.errors.append([__name__,'invalid','[%s] inputfile=%s without outputfile= value'%(__name__,cfg['inputfile'])])
58
logger.warn('%s\t%s',*self.errors[-1][1:])
60
self.regex = re.compile(r"<(sub)>([\d\(\)\+\=\-n− ]*)</\1>", re.IGNORECASE | re.DOTALL | re.UNICODE)
63
skipclose = not self.inputfile
64
if self.inputfile: return
66
self.p.cfoutput[k]['tempbuff'] = [self.regex.sub(self.__filter,line) for line in self.p.cfoutput[k]['tempbuff']]
76
if not os.path.exists(self.inputfile):
77
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.inputfile)])
78
logger.error('%s\t%s',*self.errors[-1][1:])
83
os.makedirs(os.path.dirname(self.outputfile))
86
logger.exception('%s\t%s, %s, %s',*['failed',e.errno,e.strerror,e.filename,])
89
# open input and output files
91
if out == self.inputfile:
93
fd,out = tempfile.mkstemp(suffix='.tmp', prefix='~', dir=self.p.tempdir)
96
o = codecs.open(out,'w',self.encoding)
97
i = codecs.open(self.inputfile,'r',self.encoding)
99
raise RuntimeError('Failed to open [%s] input/output files'%(__name__))
101
sys.stderr.write('[%s] %s\n Please wait'%(__name__,self.outputfile))
104
# loop writes output line-by-line
106
o.write('%s\n'%(self.regex.sub(self.__filter,line.rstrip('\r\n'))))
108
if not cnt%5000: sys.stderr.write('.')
109
sys.stderr.write('\n')
110
# close input and output files
114
if not out == self.outputfile:
116
shutil.move(out,self.outputfile)
118
except KeyboardInterrupt:
120
raise KeyboardInterrupt()
122
if not os.path.exists(self.outputfile):
123
self.errors.append([__name__,'missing','[%s] %s'%(__name__,self.outputfile)])
124
logger.error('%s\t%s',*self.errors[-1][1:])
126
def __filter(self,match):
127
text = match.group(2)
128
for i, j in self.replace.items():
129
text = text.replace(i, j)
133
'''Command prompt help.'''
134
return "\n%s\n\tUsage:\n\tfrom %s import filter\n"%(
135
os.path.basename(sys.argv[0]),
136
os.path.splitext(os.path.basename(sys.argv[0]))[0]
139
licensetxt=u'''CorpusFiltergraph™ v4.0
140
Copyright © 2010-2012 Precision Translation Tools Co., Ltd.
142
This program is free software: you can redistribute it and/or modify
143
it under the terms of the GNU Lesser General Public License as published by
144
the Free Software Foundation, either version 3 of the License, or
145
(at your option) any later version.
147
This program is distributed in the hope that it will be useful,
148
but WITHOUT ANY WARRANTY; without even the implied warranty of
149
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
150
GNU Lesser General Public License for more details.
152
You should have received a copy of the GNU Lesser General Public License
153
along with this program. If not, see http://www.gnu.org/licenses/.
155
For more information, please contact Precision Translation Tools Co., Ltd.
156
at: http://www.precisiontranslationtools.com'''
158
if __name__ == "__main__":
161
sys.stdout.write(usage().encode('utf8')+'\n')