def makeDictionary(self, po_path) -> dict: from_path = po_path has_path = (po_path != None) and (os.path.isdir(po_path)) if (not has_path): return master_dict = {} print("makeDictionary from: {}".format(from_path)) from_getter = POBasic(from_path, False) from_po_dir_list_sorted = from_getter.getSortedPOFileListRelative() for (index, to_vi_po_file) in enumerate(from_po_dir_list_sorted): if (len(to_vi_po_file) <= 0): continue to_vi_po = os.path.join(po_path, to_vi_po_file) po_doc = Document(to_vi_po) po_doc.loadPOText() translated_dict = po_doc.getDictionary() master_dict.update(translated_dict) print("number of dict items: {}".format(len(master_dict))) return master_dict
def loadDictionary(self): if (self.vipo_path != None): self.vipo_doc = Document(self.vipo_path) self.vipo_doc.loadText() self.vipo_doc.sortDocumentInMSGID() if (self.dictionary_path != None): self.dictionary_doc = Document(self.dictionary_path) self.dictionary_doc.loadText() self.dictionary_doc.sortDocumentInMSGID()
def removeComments(self): getter = POBasic(self.po_dir, False) po_dir_list = getter.getSortedPOFileList() p = re.compile(Common.COMMENTED_LINE) update_count=0 for(index, po_file_path) in enumerate(po_dir_list): if (len(po_file_path) <= 0): continue read_text = self.readFile(po_file_path) if (read_text == None): raise Exception("Unable to read text from file: {}".format(self.path)) m = p.search(read_text) if (m == None): continue print("doc: {}".format(po_file_path)) po_doc = Document(po_file_path) po_doc.loadText() po_doc.cleanupEmpties() print("po_doc: {}".format(po_doc.getTextWithIDFlat())) po_doc.saveText() update_count += 1
def ProcessingSingleDoc(self, po_doc: Document): if ((self.docEventHandler != None) and isinstance(self.docEventHandler, DocumentAction)): self.docEventHandler.setArgs(po_doc) self.docEventHandler.run() if (po_doc.isDirty()): if (self.out_vi_po == None): print("Saving changes to:{}".format(po_doc.path)) po_doc.saveText(out_path=None) else: print("Saving changes to otherfile:{}".format(self.out_vi_po)) po_doc.saveText(out_path=self.out_vi_po)
def loadPOFilesForProcessing(self): getter = POBasic(self.po_dir, False) po_dir_list = getter.getSortedPOFileList() for(index, po_file_path) in enumerate(po_dir_list): if (len(po_file_path) <= 0): continue #print("{}\n{}".format(po_file_path, len(po_file_path) * "=")) po_doc = Document(po_file_path) po_doc.loadText() if ((self.docEventHandler != None) and isinstance(self.docEventHandler, DocumentAction)): self.docEventHandler.setArgs(po_doc) self.docEventHandler.run()
def listMSGID(self): getter_from = POBasic(self.target_dir, False) target_dir_list = getter_from.getSortedPOFileList() for (index, file_name) in enumerate(target_dir_list): po_doc = Document(file_name) po_doc.loadText() po_doc.printTitleOnce() print("{}\n\n".format(po_doc.getTextWithIDFlat()))
def loadPOFilesForProcessing(self): getter: POBasic = POBasic(self.from_dir, False) from_po_dir_list: list = getter.getSortedPOFileList() from_dir_len: int = len(self.from_dir) #to_dir_len : int =len(self.to_dir) for (index, from_po_path) in enumerate(from_po_dir_list): is_ignore_dir: bool = (from_po_path == ".") or (from_po_path == "..") if (is_ignore_dir): continue common_part: str = from_po_path[from_dir_len:] is_leading_with_sep = common_part.startswith(os.sep) if (is_leading_with_sep): common_part = common_part[1:] to_po_path = os.path.join(self.to_dir, common_part) is_to_exists = os.path.exists(to_po_path) if (not is_to_exists): print("Target file doesn't exist: {}".format(to_po_path)) continue from_po_doc = Document(from_po_path) to_po_doc = Document(to_po_path) from_po_doc.loadText() to_po_doc.loadText() from_msgid = from_po_doc.getTextOnly() to_msgid = from_po_doc.getTextOnly() is_diff = (from_msgid != to_msgid) print("FROM:{}\n\nTO:{}\nis_diff:{}".format( from_msgid, to_msgid, is_diff)) if (not is_diff): continue #print("{}\n{}".format(from_po_path, to_po_path)) print("FROM:{}\n\nTO:{}".format(from_po_doc, to_po_doc))
def ProcessingDir(self): po_dir = self.fixPath(self.po_dir) rst_dir = self.fixPath(self.rst_dir) getter = POBasic(po_dir, False) po_dir_list = getter.getSortedPOFileListRelative() for (index, po_file_path) in enumerate(po_dir_list): if (len(po_file_path) <= 0): continue po_full_path = os.path.join(po_dir, po_file_path) po_doc: Document = Document(po_full_path) po_doc.loadText() #print(po_doc) #exit(1) rst_file = self.changeExtension(po_file_path, Common.PO_EXT, Common.RST_EXT) rst_full_path = os.path.join(rst_dir, rst_file) #print("po_full_path:{}, rst_full_path:{}".format(po_full_path, rst_full_path)) is_rst_file_there = os.path.exists(rst_full_path) #print("file: {} is_rst_file_there:{}".format(rst_full_path, is_rst_file_there)) if (not is_rst_file_there): print("NON-EXISTENCE File: {}".format(rst_full_path)) continue #exit(1) rst_doc: Document = Document(rst_full_path) rst_doc.loadRSTText() if (rst_doc.isEmpty()): continue rst_doc.sortDocumentInMSGID() #rst_doc.printTitleOnce() #print(rst_doc) #if (index > 3): #exit(1) self.ProcessingTwoDoc(rst_doc, po_doc)
def ProcessingFile(self): po_doc = Document(self.vi_po_path) po_doc.loadText() if ((self.docEventHandler != None) and isinstance(self.docEventHandler, DocumentAction)): self.docEventHandler.setArgs(po_doc) self.docEventHandler.run() #if (po_doc.isDirty()): print(po_doc) if (po_doc.isDirty()): po_doc.saveText()
def ProcessingWithDictionary(self): po_dir = self.fixPath(self.po_dir) getter = POBasic(po_dir, False) po_dir_list = getter.getSortedPOFileListRelative() for (index, po_file_path) in enumerate(po_dir_list): if (len(po_file_path) <= 0): continue po_full_path = os.path.join(po_dir, po_file_path) po_doc: Document = Document(po_full_path) po_doc.loadText() self.ProcessingSingleDoc(po_doc)
def loadPOFilesForProcessing(self): getter = POBasic(self.po_dir, False) po_dir_list = getter.getSortedPOFileList() for (index, po_file_path) in enumerate(po_dir_list): if (len(po_file_path) <= 0): continue #print("{}\n{}".format(po_file_path, len(po_file_path) * "=")) po_doc = Document(po_file_path) po_doc.loadText() if ((self.docEventHandler != None) and isinstance(self.docEventHandler, DocumentAction)): self.docEventHandler.setArgs(po_doc) self.docEventHandler.run() #if (po_doc.isDirty()): print(po_doc) #if (po_doc.isDirty()): print("Saving: {}".format(po_doc.path)) if (po_doc.isDirty()): po_doc.saveText() po_doc.msg("") key = input("Press any key to continue, Ctrl+C to break")
def LIST(self): repeat_doc:Document = Document(REPEAT_DOC_FILE) repeat_doc.loadText() repeat_doc.setUnique() repeat_doc.setFlat() repeat_doc.sortDocumentInMSGID() print(repeat_doc) self.blockEventHandler.setVIPODoc(repeat_doc) self.loadPOFilesForProcessing() new_dict : dict = self.blockEventHandler.getDict() #print(new_dict) #repeat_doc.mergeDict(new_dict) new_doc = repeat_doc.clone() new_doc.setDict(new_dict) print(new_doc)
def transferText(self, left_doc_path, right_doc_path, is_save=False): left_doc = Document() left_doc.setPath(left_doc_path) right_doc = Document() right_doc.setPath(right_doc_path) left_doc.loadText() right_doc.loadText() #print("left_doc:\n{}\n".format(left_doc)) #print("right_doc:\n{}\n".format(right_doc)) for (index, from_text_block) in enumerate(left_doc.block_list): is_first_block = (index == 0) if (is_first_block): continue from_msgid = from_text_block.msgid from_msgid_text = from_msgid.flatText() from_msgstr = from_text_block.msgstr from_msgstr_text = from_msgstr.flatText() try: to_msgid = right_doc.block_list[index].msgid to_msgid_text = to_msgid.flatText() to_msgstr = right_doc.block_list[index].msgstr to_msgstr_text = to_msgstr.flatText() except: return #print("from_msgid: {}\nto_msgid{}\n".format(from_msgid_text, to_msgid_text)) is_same_msgid = (from_msgid_text == to_msgid_text) if (is_same_msgid): #print("SAME MSGID: [{}] = [{}]".format(from_msgid_text, to_msgid_text)) is_transfer = (len(to_msgstr_text) == 0) or (len(from_msgstr_text) == 0) if (is_transfer): print("******* {} [{}]=> {} [{}]".format( from_msgid_text, from_msgstr_text, to_msgid_text, to_msgstr_text))
def transferVIPOtranslation(self, callback=None): vi_po_doc = Document(self.vi_po_path) vi_po_doc.loadText() vi_po_doc.sortDocumentInMSGID() #print(vi_po_doc) #return getter = POBasic(self.po_dir, False) po_dir_list = getter.getSortedPOFileList() for (index, po_file_path) in enumerate(po_dir_list): if (len(po_file_path) <= 0): continue #print("{}\n{}".format(po_file_path, len(po_file_path) * "=")) po_doc = Document(po_file_path) po_doc.loadText() if ((callback != None) and isinstance(callback, TwoDocumentAction)): callback.setArgs(vi_po_doc, po_doc) callback.run() if (po_doc.isDirty()): print("SAVING DOCUMENT: {}".format(po_doc.path))
def loadVIPO(self): vi_po_doc = Document(self.vi_po_path) vi_po_doc.loadText() vi_po_doc.sortDocumentInMSGID() return vi_po_doc
def processingFilePair(self): from_po_doc = to_po_doc = None has_from = (self.from_vi_po != None) has_to = (self.to_vi_po != None) #print("processingFilePair: has_from={}, has_to={}".format(has_from, has_to)) if (has_from): from_po_doc = Document(self.from_vi_po) from_po_doc.loadPOText() #print(from_po_doc) if (has_to): to_po_doc = Document(self.to_vi_po) to_po_doc.loadPOText() #print(to_po_doc) if ((self.docEventHandler != None) and isinstance(self.docEventHandler, TwoDocumentAction)): self.docEventHandler.setArgs(from_po_doc, to_po_doc) self.docEventHandler.run() #if (po_doc.isDirty()): print(po_doc) if (has_to and to_po_doc.isDirty()): if (self.out_vi_po == None): print("Saving changes to:{}".format(to_po_doc.path)) to_po_doc.saveText(out_path=None) else: print("Saving changes to otherfile:{}".format(self.out_vi_po)) to_po_doc.saveText(out_path=self.out_vi_po)
def compareDir(self): getter = POBasic(self.left_dir, False) left_dir_list = getter.getSortedPOFileListRelative() #right_dir_list = self.getSortedPOFileList(self.right_dir, ".po") #print("self.left_dir: {} left_dir_list: {}".format(self.left_dir, left_dir_list)) for (index, common_name) in enumerate(left_dir_list): #print("common_name: {} index: {}".format(common_name, index)) #is_debug = (common_name.find("outliner") >= 0) #if (is_debug): # print("common_name: {}".format(common_name)) #exit(0) left_file_name = self.left_dir + common_name right_file_name = self.right_dir + common_name po_left = Document(left_file_name) po_left.loadText() is_there = os.path.isfile(right_file_name) # print("left_file_name: {} is_there: {}".format(left_file_name, is_there)) # print("right_file_name: {} is_there: {}".format(right_file_name, is_there)) # exit(1) if (is_there): po_right = Document(right_file_name) po_right.loadText() left_text = po_left.getAllMSGID() right_text = po_right.getAllMSGID() md5left = self.getMD5(left_text) md5right = self.getMD5(right_text) #if (is_debug): #print("\n\n--------------\nleft_text:[{}]\n***********\nright_text:[{}], is_equal:{}\n----------------------------\n\n".format(po_left, po_right, md5left == md5right)) #exit(0) is_diff = (md5left != md5right) if (is_diff): print( "file: {}\nleft: {}\nright: {}\nis_diff: {}\nis_fix: {}" .format(common_name, self.left_dir, self.right_dir, is_diff, self.is_fix)) if (self.is_fix): self.transferText(po_left, po_right) if (po_right.isDirty()): print("Saving: {}".format(po_right.path)) #po_right.saveText() #print(line_sep) else: print("DOESN'T EXIST: {}".format(right_file_name))
class SwapPosition(BaseFileIO): def __init__(self): self.po_dir = None self.rst_dir = None self.vipo_path = None self.dictionary_path = None self.vipo_doc = None self.dictionary_doc = None self.out_vi_po = None def start(self, po_dir, rst_path, vipo_path, dictionary_path, out_file): self.po_dir = po_dir self.rst_dir = rst_path self.vipo_path = vipo_path self.dictionary_path = dictionary_path self.out_vi_po = out_file def setHandlers(self, doc_handler, block_handler): self.docEventHandler = doc_handler self.blockEventHandler = block_handler self.docEventHandler.setCallBack(self.blockEventHandler) def fixPath(self, old_path: str) -> str: new_path = old_path do_not_has_sep = (not old_path.endswith(os.sep)) if (do_not_has_sep): new_path = old_path + os.sep return new_path def changeExtension(self, old_file, from_ext: str, to_ext: str) -> str: new_file = old_file left, mid, right = old_file.rpartition(Common.DOT) #print("left:{}, mid:{}, right:{}".format(left, mid, right)) is_found = (right in from_ext) if (is_found): new_file = left + to_ext return new_file def loadDictionary(self): if (self.vipo_path != None): self.vipo_doc = Document(self.vipo_path) self.vipo_doc.loadText() self.vipo_doc.sortDocumentInMSGID() if (self.dictionary_path != None): self.dictionary_doc = Document(self.dictionary_path) self.dictionary_doc.loadText() self.dictionary_doc.sortDocumentInMSGID() # has_dictionary = (self.vipo_doc != None) or (self.dictionary_doc != None) # if (not has_dictionary): # return # # is_merging = (self.vipo_doc != None) and (self.dictionary_doc != None) # if (not is_merging): # dict_doc = (self.vipo_doc if (self.vipo_doc != None) else self.dictionary_doc) # self.dictionary_doc = dict_doc # return # # #merging both # self.dictionary_doc.mergeDoc(self.vipo_doc) # #print(self.dictionary_doc) # #exit(1) def ProcessingDir(self): po_dir = self.fixPath(self.po_dir) rst_dir = self.fixPath(self.rst_dir) getter = POBasic(po_dir, False) po_dir_list = getter.getSortedPOFileListRelative() for (index, po_file_path) in enumerate(po_dir_list): if (len(po_file_path) <= 0): continue po_full_path = os.path.join(po_dir, po_file_path) po_doc: Document = Document(po_full_path) po_doc.loadText() #print(po_doc) #exit(1) rst_file = self.changeExtension(po_file_path, Common.PO_EXT, Common.RST_EXT) rst_full_path = os.path.join(rst_dir, rst_file) #print("po_full_path:{}, rst_full_path:{}".format(po_full_path, rst_full_path)) is_rst_file_there = os.path.exists(rst_full_path) #print("file: {} is_rst_file_there:{}".format(rst_full_path, is_rst_file_there)) if (not is_rst_file_there): print("NON-EXISTENCE File: {}".format(rst_full_path)) continue #exit(1) rst_doc: Document = Document(rst_full_path) rst_doc.loadRSTText() if (rst_doc.isEmpty()): continue rst_doc.sortDocumentInMSGID() #rst_doc.printTitleOnce() #print(rst_doc) #if (index > 3): #exit(1) self.ProcessingTwoDoc(rst_doc, po_doc) def ProcessingTwoDoc(self, rst_doc: Document, po_doc: Document): self.blockEventHandler.setRSTDoc(rst_doc) self.blockEventHandler.setRSTDoc(rst_doc) if ((self.docEventHandler != None) and isinstance(self.docEventHandler, DocumentAction)): self.docEventHandler.setArgs(po_doc) self.docEventHandler.run() #if (po_doc.isDirty()): print(po_doc) if (po_doc.isDirty()): if (self.out_vi_po == None): print("Saving changes to:{}".format(po_doc.path)) po_doc.saveText(out_path=None) else: print("Saving changes to otherfile:{}".format(self.out_vi_po)) po_doc.saveText(out_path=self.out_vi_po) #print("Saving document:{}".format(po_doc.path)) #po_doc.saveText() #key_input = input("Press anykey to continue") def ProcessingSingleDoc(self, po_doc: Document): if ((self.docEventHandler != None) and isinstance(self.docEventHandler, DocumentAction)): self.docEventHandler.setArgs(po_doc) self.docEventHandler.run() if (po_doc.isDirty()): if (self.out_vi_po == None): print("Saving changes to:{}".format(po_doc.path)) po_doc.saveText(out_path=None) else: print("Saving changes to otherfile:{}".format(self.out_vi_po)) po_doc.saveText(out_path=self.out_vi_po) #print("Saving document:{}".format(po_doc.path)) #po_doc.saveText() def ProcessingWithDictionary(self): po_dir = self.fixPath(self.po_dir) getter = POBasic(po_dir, False) po_dir_list = getter.getSortedPOFileListRelative() for (index, po_file_path) in enumerate(po_dir_list): if (len(po_file_path) <= 0): continue po_full_path = os.path.join(po_dir, po_file_path) po_doc: Document = Document(po_full_path) po_doc.loadText() self.ProcessingSingleDoc(po_doc) def run(self): self.loadDictionary() doc_x = BasicDocumentAction() has_rst = (self.rst_dir != None) if (has_rst): block_event_handler = SwapVietnameseEnglish() block_event_handler.setDictionary(self.dictionary_doc) block_event_handler.setVIPODoc(self.vipo_doc) self.setHandlers(doc_x, block_event_handler) self.ProcessingDir() else: block_event_handler = InsertDictionary() block_event_handler.setDictionary(self.dictionary_doc) block_event_handler.setVIPODoc(self.vipo_doc) self.setHandlers(doc_x, block_event_handler) self.ProcessingWithDictionary() #my_dict = block_event_handler.my_list #my_list = my_dict.keys() #sorted_list = sorted(my_list) #print(os.linesep.join(sorted_list)) #if (self.rst_dir != None): #self.ProcessingFile() #else: #self.ProcessingDir() def loadVIPO(self): vi_po_doc = Document(self.rst_dir) vi_po_doc.loadText() vi_po_doc.sortDocumentInMSGID() return vi_po_doc