Exemplo n.º 1
0
    def upload(self,
               locales=None,
               force=True,
               verbose=False,
               save_stats=True,
               download=False):
        source_lang = self.pod.podspec.default_locale
        locales = locales or self.pod.catalogs.list_locales()
        stats = []
        num_files = len(locales)
        if not locales:
            self.pod.logger.info('No locales to upload.')
            return
        if download and self.pod.file_exists(Translator.TRANSLATOR_STATS_PATH):
            self.download(locales=locales, save_stats=save_stats)
        if not force:
            if (self.has_immutable_translation_resources and
                    self.pod.file_exists(Translator.TRANSLATOR_STATS_PATH)):
                text = 'Found existing translator data in: {}'
                self.pod.logger.info(
                    text.format(Translator.TRANSLATOR_STATS_PATH))
                text = 'This will be updated with new data after the upload is complete.'
                self.pod.logger.info(text)
            text = 'Proceed to upload {} translation catalogs?'
            text = text.format(num_files)
            if not utils.interactive_confirm(text):
                self.pod.logger.info('Aborted.')
                return
        text = 'Uploading translations: %(value)d/{} (in %(elapsed)s)'
        widgets = [progressbar.FormatLabel(text.format(num_files))]
        bar = progressbar.ProgressBar(widgets=widgets, maxval=num_files)
        bar.start()
        threads = []

        def _do_upload(locale):
            catalog = self.pod.catalogs.get(locale)
            stat = self._upload_catalog(catalog, source_lang)
            stats.append(stat)

        for i, locale in enumerate(locales):
            thread = utils.ProgressBarThread(bar,
                                             True,
                                             target=_do_upload,
                                             args=(locale, ))
            threads.append(thread)
            thread.start()
            # Perform the first operation synchronously to avoid oauth2 refresh
            # locking issues.
            if i == 0:
                thread.join()
        for i, thread in enumerate(threads):
            if i > 0:
                thread.join()
        bar.finish()
        stats = sorted(stats, key=lambda stat: stat.lang)
        if verbose:
            self.pretty_print_stats(stats)
        if save_stats:
            self.save_stats(stats)
        return stats
Exemplo n.º 2
0
    def download(self, locales, save_stats=True, inject=False):
        # TODO: Rename to `download_and_import`.
        if not self.pod.file_exists(Translator.TRANSLATOR_STATS_PATH):
            text = 'File {} not found. Nothing to download.'
            self.pod.logger.info(text.format(Translator.TRANSLATOR_STATS_PATH))
            return
        stats_to_download = self._get_stats_to_download(locales)
        if not stats_to_download:
            return
        num_files = len(stats_to_download)
        text = 'Downloading translations: %(value)d/{} (in %(elapsed)s)'
        widgets = [progressbar.FormatLabel(text.format(num_files))]
        if not inject:
            bar = progressbar.ProgressBar(widgets=widgets, maxval=num_files)
            bar.start()
        threads = []
        langs_to_translations = {}
        new_stats = []

        def _do_download(lang, stat):
            try:
                new_stat, content = self._download_content(stat)
            except translator_errors.NotFoundError:
                text = 'No translations to download for: {}'
                self.pod.logger.info(text.format(lang))
                return

            new_stat.uploaded = stat.uploaded  # Preserve uploaded field.
            langs_to_translations[lang] = content
            new_stats.append(new_stat)
        for i, (lang, stat) in enumerate(stats_to_download.iteritems()):
            if inject:
                thread = threading.Thread(
                    target=_do_download, args=(lang, stat))
            else:
                thread = utils.ProgressBarThread(
                    bar, True, target=_do_download, args=(lang, stat))
            threads.append(thread)
            thread.start()
            # Perform the first operation synchronously to avoid oauth2 refresh
            # locking issues.
            if i == 0:
                thread.join()
        for i, thread in enumerate(threads):
            if i > 0:
                thread.join()
        if not inject:
            bar.finish()

        has_changed_content = False
        for lang, translations in langs_to_translations.iteritems():
            if inject:
                if self.pod.catalogs.inject_translations(locale=lang, content=translations):
                    has_changed_content = True
            elif self.pod.catalogs.import_translations(locale=lang, content=translations):
                has_changed_content = True

        if save_stats and has_changed_content:
            self.save_stats(new_stats)
        return new_stats