# -*- coding: utf-8 *- import os import dircache import markup import re import string from EntryDTO import * from EntryDAO import * from EntryManager import * from Languages import Language, Translation import Communication #package='net.ihe.gazelle.simulator' #modull = 'PDQPDSSimulator' #lastId = 84 imfolder = "/img/" class TextTransformer: def __init__(self): pass def replaceSubString(self,texte, oldSubString, newSubString): newTexte = texte tt = newTexte.find(oldSubString) if (tt>=0): newTexte = texte[0:tt] + newSubString + texte[tt + len(oldSubString):] return newTexte def replaceAllSubString(self,texte, oldSubString, newSubString): newTexte = texte if texte != None: tt = 1 while (tt >= 0): tt = newTexte.find(oldSubString) if (tt>=0): newTexte = newTexte[0:tt] + newSubString + newTexte[tt + len(oldSubString):] return newTexte def createMessageSubstitution(self,oldString, package): res = '#{messages[\'' trans = generateWordFromPhrase(oldString) res = res + package + '.' + trans + '\']}' return res def createMessageSubstitutionForPhrase(self,oldPhrase, package): trans = self.generateWordFromPhrase(oldPhrase) res = '#{messages[\'' res = res + package + '.'+ trans + '\']}' return res def createKeywordFromFrase(self,oldPhrase, package): trans = self.generateWordFromPhrase(oldPhrase) res = package + '.'+ trans return res def generateWordFromPhrase(self,phrase): phrase = phrase.strip() listWord = phrase.split(' ') toReturn = string.capwords(phrase) listRet = toReturn.split(' ') toReturn = ''.join(listRet) listAborded = ['!', '?', '.', ',', '(', ')', '[', ']', ':', ';', '/', '"', "'", "-", "_"] for ch in listAborded: toReturn = toReturn.replace(ch, '') return toReturn def purifyText(self, texte): simpleWord = texte if texte: simpleWord.strip() simpleWord = self.replaceAllSubString(simpleWord, '\n', '') simpleWord = self.replaceAllSubString(simpleWord, '\r', '') simpleWord = self.replaceAllSubString(simpleWord, '\t', '') simpleWord = self.replaceAllSubString(simpleWord, '\f', '') simpleWord = self.replaceAllSubString(simpleWord, '\v', '') simpleWord = self.replaceAllSubString(simpleWord, '\a', '') simpleWord = self.replaceAllSubString(simpleWord, '\b', '') simpleWord.rstrip() simpleWord.lstrip() simpleWord.strip() p = re.compile('[ ]+') m = p.findall(simpleWord) if m: for ww in m: if ww==simpleWord: return '' return simpleWord def getNomPackageFromRep(self, rep, origin): res = None tocheck = rep[len(origin):] listWord = tocheck.split('/') if listWord: if len(listWord)>1: return listWord[1] return res class Translator: def __init__(self): self.id = 0 self.listOldEntry = [] def translateFromEnglishToFrensh(self,englishWord): #print 'englishWord = ' + englishWord original_language = Language('ENGLISH') destiny_language = Language('FRENCH') translation = Translation(original_language, destiny_language) translated_word = Communication.translate(englishWord, translation) return translated_word def translateFromEnglishToOtherLanguage(self, englishWord, lang): #original_language = Language('ENGLISH') #destiny_language = Language(lang) #translation = Translation(original_language, destiny_language) #translated_word = Communication.translate(englishWord, translation) #return translated_word return englishWord def transformDocTTranslatedDoc(self,texte, basename, package, module, listLang): ttr = TextTransformer() entrMan = EntryManager() pairR = PairResult() listWord = self.getWordToSubstituteFromText(texte) newTexte = texte if listWord: for word in listWord: simpleWord = self.getSimpleWordFromWordToSubstitute(word) if simpleWord: if simpleWord != '': if not self.wordCanBeTranslated(simpleWord): continue keyIfExist = entrMan.getWellMessageFromListEntryByEnglishWord(simpleWord,self.listOldEntry) if keyIfExist: wordToSibstitute = ttr.replaceSubString(word, simpleWord, '#{messages[\'' + keyIfExist.keyword + '\']}') newTexte = ttr.replaceSubString(newTexte, word, wordToSibstitute) keyIfExist.listFileName.append(basename) continue entr = Entry() entr.listFileName.append(basename) entr.messageFile = 'mess.xml' entr.id = self.id + 1 self.id = self.id + 1 entr.module = module entr.keyword = ttr.createKeywordFromFrase(simpleWord, package) textang = Text() textang.value = simpleWord textang.lang = 'ENGLISH' entr.listText.append(textang) for lang in listLang: textfr = Text() textfr.lang = lang textfr.value = self.translateFromEnglishToOtherLanguage(simpleWord, lang) entr.listText.append(textfr) wordToSibstitute = ttr.replaceSubString(word, simpleWord, entr.createMessgaeFromKeyword()) newTexte = ttr.replaceSubString(newTexte, word, wordToSibstitute) pairR.listEntry.append(entr) self.listOldEntry.append(entr) pairR.newTexte = newTexte return pairR def checkIfTheValueIsOfAParam(self, text, valueWord): toRet = False p = re.compile('') m = p.findall(text) if m: for word in m: p1 = re.compile(valueWord) m1 = p1.findall(word) if m1: if len(m1)>0: toRet = True p = re.compile('') m = p.findall(text) if m: for word in m: p1 = re.compile(valueWord) m1 = p1.findall(word) if m1: if len(m1)>0: toRet = True return toRet def getWordToSubstituteFromText(self, text): listWord = [] pairR = PairResult() p = re.compile('>.*?<') m = p.findall(text) if m: for word in m: if self.wordCanBeTranslated(word): listWord.append(word) p = re.compile('value=".*?"') m = p.findall(text) if m: for word in m: p1 = re.compile(".*" + imfolder + ".*") m1 = p1.match(word) if not m1: if self.wordCanBeTranslated(word): if not self.checkIfTheValueIsOfAParam(text,word): listWord.append(word) p = re.compile('label=".*?"') m = p.findall(text) if m: for word in m: if self.wordCanBeTranslated(word): listWord.append(word) p = re.compile('title=".*?"') m = p.findall(text) if m: for word in m: if self.wordCanBeTranslated(word): listWord.append(word) return listWord def getSimpleWordFromWordToSubstitute(self, word): ttr = TextTransformer() res = word p = re.compile('>.*?<') m = p.match(word) if m: res = word[1:len(word)-1] res = ttr.purifyText(res) return res p = re.compile('value=".*?"') m = p.match(word) if m: res = word[7:len(word)-1] return res p = re.compile('label=".*?"') m = p.match(word) if m: res = word[7:len(word)-1] return res p = re.compile('title=".*?"') m = p.match(word) if m: res = word[7:len(word)-1] return res return res def wordCanBeTranslated(self, simpleWord): p = re.compile('#{.*?}') m = p.findall(simpleWord) if m: if len(m)>0: return False p = re.compile('\${.*?}') m = p.findall(simpleWord) if m: if len(m)>0: return False return True def transformFileToTranslatedFile(self,fileName, package, module, listLang): print 'current file = ' + fileName fm = FileManager() tex = fm.getTextContent(fileName) return self.transformDocTTranslatedDoc(tex, os.path.basename(fileName), package, module, listLang) def transformDirToTranslatedFiles(self, pathOrigin, pathDir, pathTarget, packageOrigin, package, module, listLang, listPairResult): ttr = TextTransformer() fm = FileManager() if not os.path.isdir(pathDir): return None list_output = dircache.listdir(pathDir) for outp in list_output: if os.path.isfile(pathDir + '/' + outp): if fm.verifyIfTheFileIsXHTML(pathDir + '/' + outp): id1 = self.id pr = self.transformFileToTranslatedFile(pathDir + '/' + outp, package, module, listLang) print 'id1 - self.id = ' + str(id1) + ',' + str(self.id) pr.target = pathTarget + '/' + outp listPairResult.append(pr) elif os.path.isdir(pathDir + '/' + outp): #namePack = ttr.getNomPackageFromRep(pathDir,pathOrigin) #if namePack: # if len(namePack)>0: # package = packageOrigin + '.' + namePack self.transformDirToTranslatedFiles(pathOrigin, pathDir + '/' + outp, pathTarget + '/' + outp, packageOrigin, package, module, listLang, listPairResult) def transformParentDirToTranslatedFilesSaved(self, pathDir, pathTarget, package, module, listLang): fm = FileManager() entryMan = EntryManager() listPairResult = [] self.transformDirToTranslatedFiles(pathDir, pathDir, pathTarget, package, package, module, listLang, listPairResult) listNewEntry = [] for pr in listPairResult: if pr.target != None: path = os.path.dirname(pr.target) if not os.path.exists(path): os.makedirs(path) fm.saveTextOnFile(pr.newTexte, pr.target) for entr in pr.listEntry: listNewEntry.append(entr) texMess = entryMan.transformListEntryToText(listNewEntry) fm.saveTextOnFile(texMess, pathTarget + '/' + 'mess.xml') def getLatestIdFromMessagesXML(self, messagesFile): entrDAO = EntryDAO() entrDAO.load(messagesFile) id = 0 if entrDAO.listEntry: for entr in entrDAO.listEntry: if int(entr.id)>int(id): id = int(entr.id) return int(id) def getListOldEntryFromListMessagesFile(self, listMessage): res = [] if listMessage: for messFile in listMessage: entrDAO = EntryDAO() entrDAO.load(messFile) if entrDAO.listEntry: for entr in entrDAO.listEntry: res.append(entr) return res def transParentDirToTranslatedFilesSavedByCurrentMessageByListMessageFile(self, pathDir, pathTarget, package, module, listLang, listMessageFile, currentMessage): self.listOldEntry = self.getListOldEntryFromListMessagesFile(listMessageFile) #idd = self.getLatestIdFromMessagesXML(currentMessage) idd = 2210 self.id = idd self.transformParentDirToTranslatedFilesSaved(pathDir, pathTarget, package, module, listLang) self.createReportFromEntries() def createReportFromEntries(self): fm = FileManager() page = markup.page( ) page.init( title="Report") page.script(src='sorttable.js') page.script.close() page.p("Here are the list of messages added : ") page.table(_class='sortable', border='1', background='#aaaaaa') page.thead() page.tr() page.th('File Name') page.th('Id') page.th('Keyword') page.th('Files') page.th('number') page.tr.close() page.thead.close() page.tbody() i = 0 for entr in self.listOldEntry: if len(entr.listFileName)>0: page.tr() page.td(entr.messageFile, rowspan=self.getNumberOfDistinctName(entr.listFileName)) page.td(entr.id, rowspan=self.getNumberOfDistinctName(entr.listFileName)) page.td(entr.keyword, rowspan=self.getNumberOfDistinctName(entr.listFileName)) for nam in self.getDistinctName(entr.listFileName): page.td(nam) page.td(self.getNumberOccurOfName(nam, entr.listFileName)) page.tr.close() page.tr() i = i + self.getNumberOccurOfName(nam, entr.listFileName) page.tr.close() page.tbody.close() page.table.close() page.p("The number of modification is : " + str(i)) if not os.path.exists('target'): os.mkdir('target') fm.saveTextOnFile(str(page), 'target/result.html') def getNumberOfDistinctName(self, listName): res = 0 listUsed = [] for nam in listName: if not (nam in listUsed): listUsed.append(nam) return len(listUsed) def getDistinctName(self, listName): res = 0 listUsed = [] for nam in listName: if not (nam in listUsed): listUsed.append(nam) return listUsed def getNumberOccurOfName(self, nam, listName): res = 0 for ll in listName: if ll == nam: res = res + 1 return res class FileManager: def __init__(self): pass def verifyIfTheFileIsXHTML(self,fileName): if (os.path.isfile(fileName)): listPath = fileName.split('.'); if (len(listPath)==2): if (listPath[1]=='xhtml'): return True; return False; def getTextContent(self,fileName): fill = None if os.path.isfile(fileName): fich = open(fileName,'r') fill = fich.read() fich.close() return fill def getNameOfFile(self,fileName): namm = None if os.path.isfile(fileName): namm = os.path.basename(fileName) return namm def saveTextOnFile(self,texte, fileName): fich2 = open(fileName, 'w') fich2.write(texte.encode('utf-8')) fich2.close() class PairResult: def __init__(self): self.newTexte = '' self.listEntry = [] self.target = '' def __str__(self): txt = '' for entr in self.listEntry: txt = txt + str(entr) return "PairResult(newText='%s',listEntry='%s', target= '%s')"%(self.newTexte, txt, self.target) class Configuration: def __init__(self): self.listEntry = [] self.listLanguage = [] self.directory = '' self.target = '' self.id = 0 self.package = '' self.module = '' def __str__(self): return "Configuration(directory='%s', target='%s', id='%s', package='%s', module='%s')"%( self.directory, self.target, str(self.id), self.package, self.module ) def __init__(listEntry, listLanguage, directory, target, id, package, module): sself.listEntry = listEntry self.listLanguage = listLanguage self.directory = directory self.target = target self.id = id self.package = package self.module = module def configurationIsCompete(self): if len(self.listLanguage)>0 and self.directory != '' and self.target != '': return True return False if __name__=='__main__': fm = FileManager() tr = Translator() entryDAO = EntryDAO() package='gazelle.tm.invoice' modull = 'TestManagement-ui' file = '/Users/aboufahj/Documents/workspace/TM/TestManagement-ui/WebContent/financial/' fileToSave = '/Users/aboufahj/Documents/workspace/scripts/translator/target/' fileMess = '/Users/aboufahj/Documents/workspace/TM/TestManagement-ui/src/messages-TestManagement-ui.xml' fileCommon = '/Users/aboufahj/Documents/workspace/TM/Common-ui/src/messages-Common-ui.xml' fileSys = '/Users/aboufahj/Documents/workspace/TM/Systems-ui/src/messages-Systems-ui.xml' fileTF = '/Users/aboufahj/Documents/workspace/TM/TF-ui/src/messages-TF-ui.xml' fileUsr = '/Users/aboufahj/Documents/workspace/TM/Users-ui/src/messages-Users-ui.xml' listMess = [fileMess, fileCommon, fileSys, fileTF, fileUsr] tr.transParentDirToTranslatedFilesSavedByCurrentMessageByListMessageFile(file, file, package, modull, ['FRENCH'], listMess, fileMess) # add itemLabel on the parsing