def ProcessRepositoryDefinitions(self, service_key: bytes,
                                     definition_hash: bytes,
                                     definition_iterator_dict, content_types,
                                     job_key, work_time):

        # ignore content_types for now

        service_id = self.modules_services.GetServiceId(service_key)

        precise_time_to_stop = HydrusData.GetNowPrecise() + work_time

        (hash_id_map_table_name, tag_id_map_table_name
         ) = GenerateRepositoryDefinitionTableNames(service_id)

        num_rows_processed = 0

        if 'service_hash_ids_to_hashes' in definition_iterator_dict:

            i = definition_iterator_dict['service_hash_ids_to_hashes']

            for chunk in HydrusData.SplitIteratorIntoAutothrottledChunks(
                    i, 50, precise_time_to_stop):

                inserts = []

                for (service_hash_id, hash) in chunk:

                    hash_id = self.modules_hashes_local_cache.GetHashId(hash)

                    inserts.append((service_hash_id, hash_id))

                self._ExecuteMany(
                    'REPLACE INTO {} ( service_hash_id, hash_id ) VALUES ( ?, ? );'
                    .format(hash_id_map_table_name), inserts)

                num_rows_processed += len(inserts)

                if HydrusData.TimeHasPassedPrecise(
                        precise_time_to_stop) or job_key.IsCancelled():

                    return num_rows_processed

            del definition_iterator_dict['service_hash_ids_to_hashes']

        if 'service_tag_ids_to_tags' in definition_iterator_dict:

            i = definition_iterator_dict['service_tag_ids_to_tags']

            for chunk in HydrusData.SplitIteratorIntoAutothrottledChunks(
                    i, 50, precise_time_to_stop):

                inserts = []

                for (service_tag_id, tag) in chunk:

                    try:

                        tag_id = self.modules_tags_local_cache.GetTagId(tag)

                    except HydrusExceptions.TagSizeException:

                        # in future what we'll do here is assign this id to the 'do not show' table, so we know it exists, but it is knowingly filtered out
                        # _or something_. maybe a small 'invalid' table, so it isn't mixed up with potentially re-addable tags
                        tag_id = self.modules_tags_local_cache.GetTagId(
                            'invalid repository tag')

                    inserts.append((service_tag_id, tag_id))

                self._ExecuteMany(
                    'REPLACE INTO {} ( service_tag_id, tag_id ) VALUES ( ?, ? );'
                    .format(tag_id_map_table_name), inserts)

                num_rows_processed += len(inserts)

                if HydrusData.TimeHasPassedPrecise(
                        precise_time_to_stop) or job_key.IsCancelled():

                    return num_rows_processed

            del definition_iterator_dict['service_tag_ids_to_tags']

        self.SetUpdateProcessed(service_id, definition_hash,
                                (HC.CONTENT_TYPE_DEFINITIONS, ))

        return num_rows_processed
    def MainLoop(self):

        while not HydrusThreading.IsThreadShuttingDown():

            time.sleep(0.00001)

            with self._lock:

                do_wait = len(self._waterfall_queue) == 0 and len(
                    self._delayed_regeneration_queue) == 0

            if do_wait:

                self._waterfall_event.wait(1)

                self._waterfall_event.clear()

            start_time = HydrusData.GetNowPrecise()
            stop_time = start_time + 0.005  # a bit of a typical frame

            page_keys_to_rendered_medias = collections.defaultdict(list)

            num_done = 0
            max_at_once = 16

            while not HydrusData.TimeHasPassedPrecise(
                    stop_time) and num_done <= max_at_once:

                with self._lock:

                    if len(self._waterfall_queue) == 0:

                        break

                    result = self._waterfall_queue.pop()

                    if len(self._waterfall_queue) == 0:

                        self._waterfall_queue_empty_event.set()

                    self._waterfall_queue_quick.discard(result)

                (page_key, media) = result

                if media.GetDisplayMedia() is not None:

                    self.GetThumbnail(media)

                    page_keys_to_rendered_medias[page_key].append(media)

                num_done += 1

            if len(page_keys_to_rendered_medias) > 0:

                for (page_key,
                     rendered_medias) in page_keys_to_rendered_medias.items():

                    self._controller.pub('waterfall_thumbnails', page_key,
                                         rendered_medias)

                time.sleep(0.00001)

            # now we will do regen if appropriate

            with self._lock:

                # got more important work or no work to do
                if len(self._waterfall_queue) > 0 or len(
                        self._delayed_regeneration_queue
                ) == 0 or HG.client_controller.CurrentlyPubSubbing():

                    continue

                media_result = self._delayed_regeneration_queue.pop()

                self._delayed_regeneration_queue_quick.discard(media_result)

            if HG.file_report_mode:

                hash = media_result.GetHash()

                HydrusData.ShowText(
                    'Thumbnail {} now regenerating from source.'.format(
                        hash.hex()))

            try:

                self._controller.files_maintenance_manager.RunJobImmediately(
                    [media_result],
                    ClientFiles.REGENERATE_FILE_DATA_JOB_FORCE_THUMBNAIL,
                    pub_job_key=False)

            except HydrusExceptions.FileMissingException:

                pass

            except Exception as e:

                hash = media_result.GetHash()

                summary = 'The thumbnail for file {} was incorrect, but a later attempt to regenerate it or load the new file back failed.'.format(
                    hash.hex())

                self._HandleThumbnailException(e, summary)
Exemple #3
0
    def eventFilter(self, watched, event):

        if event.type() == QC.QEvent.KeyPress:

            i_should_catch_shortcut_event = IShouldCatchShortcutEvent(
                watched, event=event)

            shortcut = ConvertKeyEventToShortcut(event)

            if shortcut is not None:

                if HG.shortcut_report_mode:

                    message = 'Key shortcut "' + shortcut.ToString(
                    ) + '" passing through ' + repr(self._parent) + '.'

                    if i_should_catch_shortcut_event:

                        message += ' I am in a state to catch it.'

                    else:

                        message += ' I am not in a state to catch it.'

                    HydrusData.ShowText(message)

                if i_should_catch_shortcut_event:

                    shortcut_processed = self._ProcessShortcut(shortcut)

                    if shortcut_processed:

                        event.accept()

                        return True

        elif self._catch_mouse:

            if event.type() in (QC.QEvent.MouseButtonPress,
                                QC.QEvent.MouseButtonRelease,
                                QC.QEvent.MouseButtonDblClick,
                                QC.QEvent.Wheel):

                if event.type(
                ) != QC.QEvent.Wheel and self._ignore_activating_mouse_click and not HydrusData.TimeHasPassedPrecise(
                        self._frame_activated_time + 0.1):

                    if event.type() == QC.QEvent.MouseButtonRelease:

                        self._frame_activated_time = 0.0

                    return False

                i_should_catch_shortcut_event = IShouldCatchShortcutEvent(
                    watched, event=event)

                shortcut = ConvertMouseEventToShortcut(event)

                if shortcut is not None:

                    if HG.shortcut_report_mode:

                        message = 'Mouse Press shortcut "' + shortcut.ToString(
                        ) + '" passing through ' + repr(self._parent) + '.'

                        if i_should_catch_shortcut_event:

                            message += ' I am in a state to catch it.'

                        else:

                            message += ' I am not in a state to catch it.'

                        HydrusData.ShowText(message)

                    if i_should_catch_shortcut_event:

                        shortcut_processed = self._ProcessShortcut(shortcut)

                        if shortcut_processed:

                            event.accept()

                            return True

        return False