Example #1
0
    def scanComponents(self):
        """
        Scan the current application components for the localized text messages.
        """
        for component in self.componentService.getComponents():
            assert isinstance(component, Component)
            files = {file.Path: file for file in self.fileService.getAll(q=QFile(component=component.Id))}
            if component.InEgg:
                lastModified = modificationTimeFor(component.Path)
                file = files.get(component.Path)
                if file and lastModified <= file.LastModified:
                    log.info('No modifications for component zip file "%s" in %s', component.Path, component.Name)
                    continue
                if not file:
                    file = File()
                    file.Component = component.Id
                    file.Path = component.Path
                    file.LastModified = lastModified
                    files[component.Path] = file
                    self.fileService.insert(file)
                else:
                    file.LastModified = lastModified
                    self.fileService.update(file)
                scanner = scanZip(component.Path)
            else:
                lastModified, scanner = None, scanFolder(component.Path)

            files.update(
                {source.Path: source for source in self.sourceService.getAll(q=QSource(component=component.Id))}
            )
            self._persist(files, scanner, component.Path, lastModified, component.Id, None)
Example #2
0
    def scanPlugins(self):
        """
        Scan the current application plugins for the localized text messages.
        """
        for plugin in self.pluginService.getPlugins():
            assert isinstance(plugin, Plugin)
            files = {file.Path: file for file in self.fileService.getAll(q=QFile(plugin=plugin.Id))}
            if plugin.InEgg:
                lastModified = modificationTimeFor(plugin.Path)
                file = files.get(plugin.Path)
                if file and lastModified <= file.LastModified:
                    log.info('No modifications for plugin zip file "%s" in %s', plugin.Path, plugin.Name)
                    continue
                if not file:
                    file = File()
                    file.Plugin = plugin.Id
                    file.Path = plugin.Path
                    file.LastModified = lastModified
                    files[plugin.Path] = file
                    self.fileService.insert(file)
                else:
                    file.LastModified = lastModified
                    self.fileService.update(file)
                scanner = scanZip(plugin.Path)
            else:
                lastModified, scanner = None, scanFolder(plugin.Path)

            files.update({source.Path: source for source in self.sourceService.getAll(q=QSource(plugin=plugin.Id))})
            self._persist(files, scanner, plugin.Path, lastModified, None, plugin.Id)
Example #3
0
    def scanComponents(self):
        '''
        Scan the current application components for the localized text messages.
        '''
        for component in self.componentService.getComponents():
            assert isinstance(component, Component)
            files = {file.Path: file for file in self.fileService.getAll(q=QFile(component=component.Id))}
            if component.InEgg:
                lastModified = modificationTimeFor(component.Path)
                file = files.get(component.Path)
                if file and lastModified <= file.LastModified:
                    log.info('No modifications for component zip file "%s" in %s', component.Path, component.Name)
                    continue
                if not file:
                    file = File()
                    file.Component = component.Id
                    file.Path = component.Path
                    file.LastModified = lastModified
                    files[component.Path] = file
                    self.fileService.insert(file)
                else:
                    file.LastModified = lastModified
                    self.fileService.update(file)
                scanner = scanZip(component.Path)
            else:
                lastModified, scanner = None, scanFolder(component.Path)

            files.update({source.Path: source for source in self.sourceService.getAll(q=QSource(component=component.Id))})
            self._persist(files, scanner, component.Path, lastModified, component.Id, None)
Example #4
0
    def scanPlugins(self):
        '''
        Scan the current application plugins for the localized text messages.
        '''
        for pluginId in self.pluginService.getPlugins():
            plugin = self.pluginService.getById(pluginId)
            assert isinstance(plugin, Plugin), 'Invalid plugin %s' % plugin
            files = {file.Path: file for file in self.fileService.getAll(q=QFile(plugin=plugin.Id))}
            if plugin.InEgg:
                lastModified = modificationTimeFor(plugin.Path)
                file = files.get(plugin.Path)
                if file and lastModified <= file.LastModified:
                    log.info('No modifications for plugin zip file "%s" in %s', plugin.Path, plugin.Name)
                    continue
                if not file:
                    file = File()
                    file.Plugin = plugin.Id
                    file.Path = plugin.Path
                    file.LastModified = lastModified
                    files[plugin.Path] = file
                    self.fileService.insert(file)
                else:
                    file.LastModified = lastModified
                    self.fileService.update(file)
                scanner = scanZip(plugin.Path)
            else:
                lastModified, scanner = None, scanFolder(plugin.Path)


            files.update({source.Path: source for source in self.sourceService.getAll(q=QSource(plugin=plugin.Id))})
            self._persist(files, scanner, plugin.Path, lastModified, None, plugin.Id)
Example #5
0
    def _persist(self, files, scanner, path, lastModified, componentId,
                 pluginId):
        '''
        Persist the sources and messages. 
        '''
        assert isinstance(files, dict), 'Invalid files %s' % files
        processModified = lastModified is None
        for filePath, method, extractor in scanner:
            assert method in TYPES, 'Invalid method %s' % method

            file = files.get(filePath)
            if processModified:
                lastModified = modificationTimeFor(filePath)
                if file:
                    assert isinstance(file, File)
                    if lastModified <= file.LastModified:
                        log.info('No modifications for file "%s"', filePath)
                        continue
                    file.LastModified = lastModified
                    self.fileService.update(file)

            if isinstance(file, Source): source = file
            else: source = None
            messages = None
            try:
                for text, context, lineno, comments in extractor:
                    if not source:
                        if file: self.fileService.delete(file.Id)
                        source = Source()
                        source.Component = componentId
                        source.Plugin = pluginId
                        source.Path = filePath
                        source.Type = method
                        source.LastModified = lastModified
                        files[filePath] = source
                        self.sourceService.insert(source)

                    if messages is None:
                        messages = {
                            msg.Singular: msg
                            for msg in self.messageService.getMessages(
                                source.Id)
                        }

                    if isinstance(text, str): singular, plurals = text, None
                    elif len(text) == 1: singular, plurals = text[0], None
                    else: singular, plurals = text[0], list(text[1:])

                    msg = messages.get(singular)
                    if not msg:
                        msg = Message()
                        msg.Source = source.Id
                        msg.Singular = singular
                        msg.Plural = plurals
                        msg.Context = context
                        msg.LineNumber = lineno
                        msg.Comments = '\n'.join(comments)

                        self.messageService.insert(msg)
                        messages[singular] = msg
                    else:
                        msg.Plural = plurals
                        msg.Context = context
                        msg.LineNumber = lineno
                        msg.Comments = '\n'.join(comments)
                        self.messageService.update(msg)
            except UnicodeDecodeError as e:
                log.error('%s: %s' % (filePath, str(e)))

            if processModified and filePath not in files:
                file = File()
                file.Component = componentId
                file.Plugin = pluginId
                file.Path = filePath
                file.LastModified = lastModified
                files[filePath] = file
                self.fileService.insert(file)
Example #6
0
    def _persist(self, files, scanner, path, lastModified, componentId, pluginId):
        """
        Persist the sources and messages. 
        """
        assert isinstance(files, dict), "Invalid files %s" % files
        processModified = lastModified is None
        for filePath, method, extractor in scanner:
            assert method in TYPES, "Invalid method %s" % method

            file = files.get(filePath)
            if processModified:
                lastModified = modificationTimeFor(filePath)
                if file:
                    assert isinstance(file, File)
                    if lastModified <= file.LastModified:
                        log.info('No modifications for file "%s"', filePath)
                        continue
                    file.LastModified = lastModified
                    self.fileService.update(file)

            if isinstance(file, Source):
                source = file
            else:
                source = None
            messages = None
            try:
                for text, context, lineno, comments in extractor:
                    if not source:
                        if file:
                            self.fileService.delete(file.Id)
                        source = Source()
                        source.Component = componentId
                        source.Plugin = pluginId
                        source.Path = filePath
                        source.Type = method
                        source.LastModified = lastModified
                        files[filePath] = source
                        self.sourceService.insert(source)

                    if messages is None:
                        messages = {msg.Singular: msg for msg in self.messageService.getMessages(source.Id)}

                    if isinstance(text, str):
                        singular, plurals = text, None
                    elif len(text) == 1:
                        singular, plurals = text[0], None
                    else:
                        singular, plurals = text[0], list(text[1:])

                    msg = messages.get(singular)
                    if not msg:
                        msg = Message()
                        msg.Source = source.Id
                        msg.Singular = singular
                        msg.Plural = plurals
                        msg.Context = context
                        msg.LineNumber = lineno
                        msg.Comments = "\n".join(comments)

                        self.messageService.insert(msg)
                        messages[singular] = msg
                    else:
                        msg.Plural = plurals
                        msg.Context = context
                        msg.LineNumber = lineno
                        msg.Comments = "\n".join(comments)
                        self.messageService.update(msg)
            except UnicodeDecodeError as e:
                log.error("%s: %s" % (filePath, str(e)))

            if processModified and filePath not in files:
                file = File()
                file.Component = componentId
                file.Plugin = pluginId
                file.Path = filePath
                file.LastModified = lastModified
                files[filePath] = file
                self.fileService.insert(file)