Пример #1
0
    def testLocalFilesystemCDM(self):
        poManager = POFileManager()
        poManager.messageService = TestMessageService()
        poManager.sourceService = TestSourceService()
        poRepDir = TemporaryDirectory()
        poManager.locale_dir_path = poRepDir.name

        # ********************************************
        # test updateGlobalPOFile
        with open(join(self._poDir, 'global_ro.po')) as f:
            globalCat = read_po(f); f.seek(0)
            poManager.updateGlobalPOFile('ro', f)
        with open(join(poManager.locale_dir_path, 'global_ro.po')) as f:
            globalTestCat = read_po(f)
        self.assertEqual(len(globalCat), len(globalTestCat))
        for msg in globalCat:
            if msg and msg.id:
                self.assertEqual(msg.string, globalTestCat.get(msgId(msg), msg.context).string)

        # ********************************************
        # test updateComponentPOFile
        with open(join(self._poDir, 'component 1_ro.po')) as f:
            componentCat = read_po(f); f.seek(0)
            poManager.updateComponentPOFile('1', 'ro', f)
        with open(join(poManager.locale_dir_path, 'component', '1_ro.po')) as f:
            componentTestCat = read_po(f)
        for msg in componentTestCat:
            if msg and msg.id:
                self.assertEqual(msg.string, componentCat.get(msgId(msg), msg.context).string)
                self.assertNotEqual(msg.string, globalCat.get(msgId(msg), msg.context).string)

        # ********************************************
        # test updatePluginPOFile
        with open(join(self._poDir, 'plugin 1_ro.po')) as f:
            pluginCat = read_po(f); f.seek(0)
            poManager.updatePluginPOFile('1', 'ro', f)
        with open(join(poManager.locale_dir_path, 'plugin', '1_ro.po')) as f:
            pluginTestCat = read_po(f)
        for msg in pluginTestCat:
            if msg and msg.id:
                self.assertEqual(msg.string, pluginCat.get(msgId(msg), msg.context).string)
                self.assertNotEqual(msg.string, globalCat.get(msgId(msg), msg.context).string)

        # ********************************************
        # test getGlobalPOFile
        poFile = poManager.getGlobalPOFile('ro')
        globalTestCat = read_po(poFile)
        self.assertEqual(len(globalCat), len(globalTestCat))
        self._checkHeader(globalTestCat, globalCat)
        for msg in globalCat:
            if msg and msg.id:
                self.assertEqual(msg.locations, globalTestCat.get(msgId(msg), msg.context).locations)
                self.assertEqual(msg.string, globalTestCat.get(msgId(msg), msg.context).string)

        # ********************************************
        # test getComponentPOFile
        poFile = poManager.getComponentPOFile('1', 'ro')
        componentTestCat = read_po(poFile)
        self.assertEqual(len(componentCat), len(componentTestCat))
        self._checkHeader(componentTestCat, componentCat)
        for msg in componentCat:
            if msg and msg.id:
                self.assertEqual(msg.locations, componentTestCat.get(msgId(msg), msg.context).locations)
                self.assertEqual(msg.string, componentTestCat.get(msg.id, msg.context).string)

        # ********************************************
        # test getPluginPOFile
        poFile = poManager.getPluginPOFile('1', 'ro')
        pluginTestCat = read_po(poFile)
        self.assertEqual(len(pluginCat), len(pluginTestCat))
        self._checkHeader(pluginTestCat, pluginCat)
        for msg in pluginCat:
            if msg and msg.id:
                self.assertEqual(msg.locations, pluginTestCat.get(msgId(msg), msg.context).locations)
                self.assertEqual(msg.string, pluginTestCat.get(msg.id, msg.context).string)
Пример #2
0
    def _update(self, locale, messages, poFile, path, pathMO, isGlobal=True):
        assert isinstance(locale, Locale), 'Invalid locale %s' % locale
        assert isinstance(messages, Iterable), 'Invalid messages %s' % messages
        assert isinstance(poFile, IInputStream), 'Invalid file object %s' % poFile
        assert isinstance(path, str), 'Invalid path %s' % path
        assert isinstance(pathMO, str), 'Invalid path MO %s' % pathMO
        assert isinstance(isGlobal, bool), 'Invalid is global flag %s' % isGlobal

        catalog = read_po(poFile, locale=locale)
        assert isinstance(catalog, Catalog), 'Invalid catalog %s' % catalog
        if not catalog:
            # The catalog has no messages, no need for updating.
            return

        if not isGlobal:
            pathGlobal = self._filePath(locale)
            pathGlobalMO = self._filePath(locale, format=FORMAT_MO)
            if isfile(pathGlobal):
                with open(pathGlobal) as fObj: catalogGlobal = read_po(fObj, locale)
                self._processCatalog(catalogGlobal, self.messageService.getMessages())
            else:
                isGlobal, path, pathMO = True, pathGlobal, pathGlobalMO
                messages = self.messageService.getMessages()
        self._processCatalog(catalog, messages)

        if isfile(path):
            with open(path) as fObj: catalogOld = read_po(fObj, locale)
            for msg in catalog:
                msgO = catalogOld.get(msgId(msg), msg.context)
                if not isMsgTranslated(msg) and msgO and isMsgTranslated(msgO):
                    msg.string = msgO.string
            catalog.creation_date = catalogOld.creation_date
        else:
            pathDir = dirname(path)
            if not isdir(pathDir): os.makedirs(pathDir)
            catalog.creation_date = datetime.now()

        if not isGlobal:
            # We remove all the messages that are not translated or have the same translation as in the global locale
            # or are the only plugin that makes use of the message in the global.
            updatedGlobal = False
            for msg in list(catalog):
                id = msgId(msg)
                if not id: continue
                if not isMsgTranslated(msg):
                    catalog.delete(id, msg.context)
                else:
                    msgG = catalogGlobal.get(id, msg.context)
                    if not msgG or msgG.string == msg.string:
                        catalog.delete(id, msg.context)
                    elif not isMsgTranslated(msgG) or msgG.locations == msg.locations:
                        copyTranslation(msg, msgG)
                        catalog.delete(id, msg.context)
                        updatedGlobal = True

            if updatedGlobal:
                # We remove all the messages that are not translated.
                for msg in list(catalogGlobal):
                    if not isMsgTranslated(msg):
                        catalogGlobal.delete(msgId(msg), msg.context)

                catalogGlobal.revision_date = datetime.now()
                os.makedirs(dirname(pathGlobal), exist_ok=True)
                with open(pathGlobal, 'wb') as fObj: write_po(fObj, catalogGlobal, **self.write_po_config)
                os.makedirs(dirname(pathGlobalMO), exist_ok=True)
                with open(pathGlobalMO, 'wb') as fObj: write_mo(fObj, catalogGlobal)
        else:
            # We remove all the messages that are not translated.
            for msg in list(catalog):
                if not isMsgTranslated(msg):
                    catalog.delete(msgId(msg), msg.context)

        catalog.revision_date = datetime.now()
        os.makedirs(dirname(path), exist_ok=True)
        with open(path, 'wb') as fObj: write_po(fObj, catalog, **self.write_po_config)
        os.makedirs(dirname(pathMO), exist_ok=True)
        with open(pathMO, 'wb') as fObj: write_mo(fObj, catalog)
Пример #3
0
    def testLocalFilesystemCDM(self):
        poManager = POFileManager()
        poManager.messageService = TestMessageService()
        poManager.sourceService = TestSourceService()
        poRepDir = TemporaryDirectory()
        poManager.locale_dir_path = poRepDir.name

        # ********************************************
        # test updateGlobalPOFile
        with open(join(self._poDir, 'global_ro.po')) as f:
            globalCat = read_po(f)
            f.seek(0)
            poManager.updateGlobalPOFile('ro', f)
        with open(join(poManager.locale_dir_path, 'global_ro.po')) as f:
            globalTestCat = read_po(f)
        self.assertEqual(len(globalCat), len(globalTestCat))
        for msg in globalCat:
            if msg and msg.id:
                self.assertEqual(
                    msg.string,
                    globalTestCat.get(msgId(msg), msg.context).string)

        # ********************************************
        # test updateComponentPOFile
        with open(join(self._poDir, 'component 1_ro.po')) as f:
            componentCat = read_po(f)
            f.seek(0)
            poManager.updateComponentPOFile('1', 'ro', f)
        with open(join(poManager.locale_dir_path, 'component',
                       '1_ro.po')) as f:
            componentTestCat = read_po(f)
        for msg in componentTestCat:
            if msg and msg.id:
                self.assertEqual(
                    msg.string,
                    componentCat.get(msgId(msg), msg.context).string)
                self.assertNotEqual(
                    msg.string,
                    globalCat.get(msgId(msg), msg.context).string)

        # ********************************************
        # test updatePluginPOFile
        with open(join(self._poDir, 'plugin 1_ro.po')) as f:
            pluginCat = read_po(f)
            f.seek(0)
            poManager.updatePluginPOFile('1', 'ro', f)
        with open(join(poManager.locale_dir_path, 'plugin', '1_ro.po')) as f:
            pluginTestCat = read_po(f)
        for msg in pluginTestCat:
            if msg and msg.id:
                self.assertEqual(msg.string,
                                 pluginCat.get(msgId(msg), msg.context).string)
                self.assertNotEqual(
                    msg.string,
                    globalCat.get(msgId(msg), msg.context).string)

        # ********************************************
        # test getGlobalPOFile
        poFile = poManager.getGlobalPOFile('ro')
        globalTestCat = read_po(poFile)
        self.assertEqual(len(globalCat), len(globalTestCat))
        self._checkHeader(globalTestCat, globalCat)
        for msg in globalCat:
            if msg and msg.id:
                self.assertEqual(
                    msg.locations,
                    globalTestCat.get(msgId(msg), msg.context).locations)
                self.assertEqual(
                    msg.string,
                    globalTestCat.get(msgId(msg), msg.context).string)

        # ********************************************
        # test getComponentPOFile
        poFile = poManager.getComponentPOFile('1', 'ro')
        componentTestCat = read_po(poFile)
        self.assertEqual(len(componentCat), len(componentTestCat))
        self._checkHeader(componentTestCat, componentCat)
        for msg in componentCat:
            if msg and msg.id:
                self.assertEqual(
                    msg.locations,
                    componentTestCat.get(msgId(msg), msg.context).locations)
                self.assertEqual(
                    msg.string,
                    componentTestCat.get(msg.id, msg.context).string)

        # ********************************************
        # test getPluginPOFile
        poFile = poManager.getPluginPOFile('1', 'ro')
        pluginTestCat = read_po(poFile)
        self.assertEqual(len(pluginCat), len(pluginTestCat))
        self._checkHeader(pluginTestCat, pluginCat)
        for msg in pluginCat:
            if msg and msg.id:
                self.assertEqual(
                    msg.locations,
                    pluginTestCat.get(msgId(msg), msg.context).locations)
                self.assertEqual(msg.string,
                                 pluginTestCat.get(msg.id, msg.context).string)
Пример #4
0
    def _update(self, locale, messages, poFile, path, pathMO, isGlobal=True):
        assert isinstance(locale, Locale), 'Invalid locale %s' % locale
        assert isinstance(messages, Iterable), 'Invalid messages %s' % messages
        assert isinstance(poFile,
                          IInputStream), 'Invalid file object %s' % poFile
        assert isinstance(path, str), 'Invalid path %s' % path
        assert isinstance(pathMO, str), 'Invalid path MO %s' % pathMO
        assert isinstance(isGlobal,
                          bool), 'Invalid is global flag %s' % isGlobal

        catalog = read_po(poFile, locale=locale)
        assert isinstance(catalog, Catalog), 'Invalid catalog %s' % catalog
        if not catalog:
            # The catalog has no messages, no need for updating.
            return

        if not isGlobal:
            pathGlobal = self._filePath(locale)
            pathGlobalMO = self._filePath(locale, format=FORMAT_MO)
            if isfile(pathGlobal):
                with open(pathGlobal) as fObj:
                    catalogGlobal = read_po(fObj, locale)
                self._processCatalog(catalogGlobal,
                                     self.messageService.getMessages())
            else:
                isGlobal, path, pathMO = True, pathGlobal, pathGlobalMO
                messages = self.messageService.getMessages()
        self._processCatalog(catalog, messages)

        if isfile(path):
            with open(path) as fObj:
                catalogOld = read_po(fObj, locale)
            for msg in catalog:
                msgO = catalogOld.get(msgId(msg), msg.context)
                if not isMsgTranslated(msg) and msgO and isMsgTranslated(msgO):
                    msg.string = msgO.string
            catalog.creation_date = catalogOld.creation_date
        else:
            pathDir = dirname(path)
            if not isdir(pathDir): os.makedirs(pathDir)
            catalog.creation_date = datetime.now()

        if not isGlobal:
            # We remove all the messages that are not translated or have the same translation as in the global locale
            # or are the only plugin that makes use of the message in the global.
            updatedGlobal = False
            for msg in list(catalog):
                id = msgId(msg)
                if not id: continue
                if not isMsgTranslated(msg):
                    catalog.delete(id, msg.context)
                else:
                    msgG = catalogGlobal.get(id, msg.context)
                    if not msgG or msgG.string == msg.string:
                        catalog.delete(id, msg.context)
                    elif not isMsgTranslated(
                            msgG) or msgG.locations == msg.locations:
                        copyTranslation(msg, msgG)
                        catalog.delete(id, msg.context)
                        updatedGlobal = True

            if updatedGlobal:
                # We remove all the messages that are not translated.
                for msg in list(catalogGlobal):
                    if not isMsgTranslated(msg):
                        catalogGlobal.delete(msgId(msg), msg.context)

                catalogGlobal.revision_date = datetime.now()
                os.makedirs(dirname(pathGlobal), exist_ok=True)
                with open(pathGlobal, 'wb') as fObj:
                    write_po(fObj, catalogGlobal, **self.write_po_config)
                os.makedirs(dirname(pathGlobalMO), exist_ok=True)
                with open(pathGlobalMO, 'wb') as fObj:
                    write_mo(fObj, catalogGlobal)
        else:
            # We remove all the messages that are not translated.
            for msg in list(catalog):
                if not isMsgTranslated(msg):
                    catalog.delete(msgId(msg), msg.context)

        catalog.revision_date = datetime.now()
        os.makedirs(dirname(path), exist_ok=True)
        with open(path, 'wb') as fObj:
            write_po(fObj, catalog, **self.write_po_config)
        os.makedirs(dirname(pathMO), exist_ok=True)
        with open(pathMO, 'wb') as fObj:
            write_mo(fObj, catalog)