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))
Пример #2
0
 def loadVIPO(self):
     vi_po_doc = Document(self.vi_po_path)
     vi_po_doc.loadText()
     vi_po_doc.sortDocumentInMSGID()
     return vi_po_doc
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