Example #1
0
 def CallBlockingToWx( self, func, *args, **kwargs ):
     
     def wx_code( job_key ):
         
         try:
             
             result = func( *args, **kwargs )
             
             job_key.SetVariable( 'result', result )
             
         except HydrusExceptions.PermissionException as e:
             
             job_key.SetVariable( 'error', e )
             
         except Exception as e:
             
             job_key.SetVariable( 'error', e )
             
             HydrusData.Print( 'CallBlockingToWx just caught this error:' )
             HydrusData.DebugPrint( traceback.format_exc() )
             
         finally:
             
             job_key.Finish()
             
         
     
     job_key = ClientThreading.JobKey()
     
     job_key.Begin()
     
     wx.CallAfter( wx_code, job_key )
     
     while not job_key.IsDone():
         
         if self._model_shutdown:
             
             raise HydrusExceptions.ShutdownException( 'Application is shutting down!' )
             
         
         time.sleep( 0.05 )
         
     
     if job_key.HasVariable( 'result' ):
         
         # result can be None, for wx_code that has no return variable
         
         result = job_key.GetIfHasVariable( 'result' )
         
         return result
         
     
     error = job_key.GetIfHasVariable( 'error' )
     
     if error is not None:
         
         raise error
         
     
     raise HydrusExceptions.ShutdownException()
Example #2
0
 def FetchTags( self ):
     
     script = self._script_choice.GetChoice()
     
     if script.UsesUserInput():
         
         message = 'Enter the custom input for the file lookup script.'
         
         with ClientGUIDialogs.DialogTextEntry( self, message ) as dlg:
             
             if dlg.ShowModal() != wx.ID_OK:
                 
                 return
                 
             
             file_identifier = dlg.GetValue()
             
         
     else:
         
         ( m, ) = self._media
         
         file_identifier = script.ConvertMediaToFileIdentifier( m )
         
     
     stop_time = HydrusData.GetNow() + 30
     
     job_key = ClientThreading.JobKey( cancellable = True, stop_time = stop_time )
     
     self._script_management.SetJobKey( job_key )
     
     HG.client_controller.CallToThread( self.THREADFetchTags, script, job_key, file_identifier )
Example #3
0
def ShowExceptionClient(e, do_wait=True):

    (etype, value, tb) = sys.exc_info()

    if etype is None:

        etype = type(e)
        value = HydrusData.ToUnicode(e)

        trace = 'No error trace--here is the stack:' + os.linesep + ''.join(
            traceback.format_stack())

    else:

        trace = ''.join(traceback.format_exception(etype, value, tb))

    trace = HydrusData.ToUnicode(trace)

    pretty_value = HydrusData.ToUnicode(value)

    if os.linesep in pretty_value:

        (first_line,
         anything_else) = HydrusData.ToUnicode(value).split(os.linesep, 1)

        trace = trace + os.linesep + anything_else

    else:

        first_line = pretty_value

    job_key = ClientThreading.JobKey()

    if isinstance(e, HydrusExceptions.ShutdownException):

        return

    else:

        if hasattr(etype, '__name__'):
            title = HydrusData.ToUnicode(etype.__name__)
        else:
            title = HydrusData.ToUnicode(etype)

        job_key.SetVariable('popup_title', title)

        job_key.SetVariable('popup_text_1', first_line)
        job_key.SetVariable('popup_traceback', trace)

    text = job_key.ToString()

    HydrusData.Print('Exception:')

    HydrusData.DebugPrint(text)

    HG.client_controller.pub('message', job_key)

    if do_wait:

        time.sleep(1)
Example #4
0
    def DoWork(self):

        if HG.view_shutdown:

            return

        if HC.options['pause_import_folders_sync'] or self._paused:

            return

        if not os.path.exists(self._path) or not os.path.isdir(self._path):

            return

        pubbed_job_key = False

        job_key = ClientThreading.JobKey(pausable=False, cancellable=True)

        job_key.SetVariable('popup_title', 'import folder - ' + self._name)

        due_by_check_now = self._check_now
        due_by_period = self._check_regularly and HydrusData.TimeHasPassed(
            self._last_checked + self._period)

        checked_folder = False

        if due_by_check_now or due_by_period:

            if not pubbed_job_key and self._show_working_popup:

                HG.client_controller.pub('message', job_key)

                pubbed_job_key = True

            self._CheckFolder(job_key)

            checked_folder = True

        file_seed = self._file_seed_cache.GetNextFileSeed(CC.STATUS_UNKNOWN)

        did_import_file_work = False

        if file_seed is not None:

            if not pubbed_job_key and self._show_working_popup:

                HG.client_controller.pub('message', job_key)

                pubbed_job_key = True

            did_import_file_work = self._ImportFiles(job_key)

        if checked_folder or did_import_file_work:

            HG.client_controller.WriteSynchronous('serialisable', self)

        job_key.Delete()
Example #5
0
    def __init__(self, parent):

        wx.Frame.__init__(self,
                          parent,
                          style=wx.FRAME_TOOL_WINDOW | wx.FRAME_NO_TASKBAR
                          | wx.FRAME_FLOAT_ON_PARENT | wx.BORDER_NONE)

        self.SetBackgroundColour(
            wx.SystemSettings.GetColour(wx.SYS_COLOUR_FRAMEBK))

        self._max_messages_to_display = 10

        vbox = wx.BoxSizer(wx.VERTICAL)

        self._message_vbox = wx.BoxSizer(wx.VERTICAL)

        self._dismiss_all = PopupDismissAll(self)
        self._dismiss_all.Hide()

        vbox.AddF(self._message_vbox, CC.FLAGS_EXPAND_SIZER_PERPENDICULAR)
        vbox.AddF(self._dismiss_all, CC.FLAGS_EXPAND_PERPENDICULAR)

        self.SetSizer(vbox)

        self._pending_job_keys = []

        parent.Bind(wx.EVT_SIZE, self.EventMove)
        parent.Bind(wx.EVT_MOVE, self.EventMove)

        HG.client_controller.sub(self, 'AddMessage', 'message')

        self._old_excepthook = sys.excepthook
        self._old_show_exception = HydrusData.ShowException

        sys.excepthook = ClientData.CatchExceptionClient
        HydrusData.ShowException = ClientData.ShowExceptionClient
        HydrusData.ShowText = ClientData.ShowTextClient

        self.Bind(wx.EVT_TIMER, self.TIMEREvent)

        self._timer = wx.Timer(self)

        self._timer.Start(500, wx.TIMER_CONTINUOUS)

        job_key = ClientThreading.JobKey()

        job_key.SetVariable('popup_text_1',
                            u'initialising popup message manager\u2026')

        wx.CallAfter(self.AddMessage, job_key)

        wx.CallAfter(self._Update)

        wx.CallAfter(job_key.Delete)

        wx.CallAfter(self._Update)
Example #6
0
def ShowTextClient(text):

    job_key = ClientThreading.JobKey()

    job_key.SetVariable('popup_text_1', HydrusData.ToUnicode(text))

    text = job_key.ToString()

    HydrusData.Print(text)

    HG.client_controller.pub('message', job_key)
Example #7
0
    def __init__(self, parent):

        wx.Frame.__init__(self,
                          parent,
                          style=wx.FRAME_TOOL_WINDOW | wx.FRAME_NO_TASKBAR
                          | wx.FRAME_FLOAT_ON_PARENT | wx.BORDER_NONE)

        self.SetBackgroundColour(
            wx.SystemSettings.GetColour(wx.SYS_COLOUR_FRAMEBK))

        self._last_best_size_i_fit_on = (0, 0)

        self._max_messages_to_display = 10

        vbox = wx.BoxSizer(wx.VERTICAL)

        self._message_vbox = wx.BoxSizer(wx.VERTICAL)

        self._dismiss_all = PopupDismissAll(self)
        self._dismiss_all.Hide()

        vbox.Add(self._message_vbox, CC.FLAGS_EXPAND_SIZER_PERPENDICULAR)
        vbox.Add(self._dismiss_all, CC.FLAGS_EXPAND_PERPENDICULAR)

        self.SetSizer(vbox)

        self._pending_job_keys = []

        parent.Bind(wx.EVT_SIZE, self.EventMove)
        parent.Bind(wx.EVT_MOVE, self.EventMove)

        HG.client_controller.sub(self, 'AddMessage', 'message')

        self._old_excepthook = sys.excepthook
        self._old_show_exception = HydrusData.ShowException

        sys.excepthook = ClientData.CatchExceptionClient
        HydrusData.ShowException = ClientData.ShowExceptionClient
        HydrusData.ShowText = ClientData.ShowTextClient

        job_key = ClientThreading.JobKey()

        job_key.SetVariable('popup_text_1',
                            u'initialising popup message manager\u2026')

        self._update_job = HG.client_controller.CallRepeatingWXSafe(
            self, 0.25, 0.5, self.REPEATINGUpdate)

        HG.client_controller.CallLaterWXSafe(self, 0.5, self.AddMessage,
                                             job_key)

        HG.client_controller.CallLaterWXSafe(self, 1.0, job_key.Delete)
Example #8
0
    def CallBlockingToWx(self, callable, *args, **kwargs):
        def wx_code(job_key):

            try:

                result = callable(*args, **kwargs)

                job_key.SetVariable('result', result)

            except HydrusExceptions.PermissionException as e:

                job_key.SetVariable('error', e)

            except Exception as e:

                job_key.SetVariable('error', e)

                HydrusData.Print('CallBlockingToWx just caught this error:')
                HydrusData.DebugPrint(traceback.format_exc())

            finally:

                job_key.Finish()

        job_key = ClientThreading.JobKey()

        job_key.Begin()

        wx.CallAfter(wx_code, job_key)

        while not job_key.IsDone():

            if self._model_shutdown:

                return

            time.sleep(0.05)

        if job_key.HasVariable('result'):

            return job_key.GetVariable('result')

        elif job_key.HasVariable('error'):

            raise job_key.GetVariable('error')

        else:

            raise HydrusExceptions.ShutdownException()
Example #9
0
    def Start(self):

        try:

            results = []

            for (network_context, key, value, reason) in self._header_tuples:

                job_key = ClientThreading.JobKey()

                # generate question

                question = 'For the network context ' + network_context.ToUnicode(
                ) + ', can the client set this header?'
                question += os.linesep * 2
                question += key + ': ' + value
                question += os.linesep * 2
                question += reason

                job_key.SetVariable('popup_yes_no_question', question)

                HG.client_controller.pub('message', job_key)

                result = job_key.GetIfHasVariable('popup_yes_no_answer')

                while result is None:

                    if HG.view_shutdown:

                        return

                    time.sleep(0.25)

                    result = job_key.GetIfHasVariable('popup_yes_no_answer')

                if result:

                    approved = VALID_APPROVED

                else:

                    approved = VALID_DENIED

                self._domain_manager.SetHeaderValidation(
                    network_context, key, approved)

        finally:

            self._is_done = True
Example #10
0
def PublishPresentationHashes(name, hashes, publish_to_popup_button,
                              publish_files_to_page):

    if publish_to_popup_button:

        files_job_key = ClientThreading.JobKey()

        files_job_key.SetVariable('popup_files_mergable', True)
        files_job_key.SetVariable('popup_files', (list(hashes), name))

        HG.client_controller.pub('message', files_job_key)

    if publish_files_to_page:

        HG.client_controller.pub('imported_files_to_page', list(hashes), name)
Example #11
0
def DAEMONDownloadFiles(controller):

    hashes = controller.Read('downloads')

    num_downloads = len(hashes)

    if num_downloads > 0:

        client_files_manager = controller.client_files_manager

        successful_hashes = set()

        job_key = ClientThreading.JobKey()

        job_key.SetVariable('popup_text_1', 'initialising downloader')

        controller.pub('message', job_key)

        for hash in hashes:

            job_key.SetVariable(
                'popup_text_1', 'downloading ' +
                HydrusData.ConvertIntToPrettyString(num_downloads -
                                                    len(successful_hashes)) +
                ' files from repositories')

            (media_result, ) = controller.Read('media_results', (hash, ))

            service_keys = list(
                media_result.GetLocationsManager().GetCurrent())

            random.shuffle(service_keys)

            for service_key in service_keys:

                if service_key == CC.LOCAL_FILE_SERVICE_KEY: break
                elif service_key == CC.TRASH_SERVICE_KEY: continue

                try:

                    service = controller.services_manager.GetService(
                        service_key)

                except:

                    continue

                if service.GetServiceType() == HC.FILE_REPOSITORY:

                    file_repository = service

                    if file_repository.IsFunctional():

                        try:

                            (os_file_handle,
                             temp_path) = HydrusPaths.GetTempPath()

                            try:

                                file_repository.Request(HC.GET,
                                                        'file', {'hash': hash},
                                                        temp_path=temp_path)

                                controller.WaitUntilModelFree()

                                automatic_archive = False
                                exclude_deleted = False  # this is the important part here
                                min_size = None
                                min_resolution = None

                                file_import_options = ClientImporting.FileImportOptions(
                                    automatic_archive=automatic_archive,
                                    exclude_deleted=exclude_deleted,
                                    min_size=min_size,
                                    min_resolution=min_resolution)

                                file_import_job = ClientImporting.FileImportJob(
                                    temp_path, file_import_options)

                                client_files_manager.ImportFile(
                                    file_import_job)

                                successful_hashes.add(hash)

                                break

                            finally:

                                HydrusPaths.CleanUpTempPath(
                                    os_file_handle, temp_path)

                        except HydrusExceptions.ServerBusyException:

                            job_key.SetVariable(
                                'popup_text_1',
                                file_repository.GetName() +
                                ' was busy. waiting 30s before trying again')

                            time.sleep(30)

                            job_key.Delete()

                            controller.pub('notify_new_downloads')

                            return

                        except Exception as e:

                            HydrusData.ShowText('Error downloading file!')
                            HydrusData.ShowException(e)

                elif service.GetServiceType() == HC.IPFS:

                    multihashes = HG.client_controller.Read(
                        'service_filenames', service_key, {hash})

                    if len(multihashes) > 0:

                        multihash = multihashes[0]

                        # this actually calls to a thread that can launch gui 'select from tree' stuff, so let's just break at this point
                        service.ImportFile(multihash)

                        break

                if HydrusThreading.IsThreadShuttingDown():

                    return

        if len(successful_hashes) > 0:

            job_key.SetVariable(
                'popup_text_1',
                HydrusData.ConvertIntToPrettyString(len(successful_hashes)) +
                ' files downloaded')

        job_key.Delete()
Example #12
0
 def DoWork( self ):
     
     if HG.view_shutdown:
         
         return
         
     
     if HC.options[ 'pause_import_folders_sync' ] or self._paused:
         
         return
         
     
     checked_folder = False
     
     did_import_file_work = False
     
     error_occured = False
     
     job_key = ClientThreading.JobKey( pausable = False, cancellable = True )
     
     try:
         
         if not os.path.exists( self._path ) or not os.path.isdir( self._path ):
             
             raise Exception( 'Path "' + self._path + '" does not seem to exist, or is not a directory.' )
             
         
         pubbed_job_key = False
         
         job_key.SetVariable( 'popup_title', 'import folder - ' + self._name )
         
         due_by_check_now = self._check_now
         due_by_period = self._check_regularly and HydrusData.TimeHasPassed( self._last_checked + self._period )
         
         if due_by_check_now or due_by_period:
             
             if not pubbed_job_key and self._show_working_popup:
                 
                 HG.client_controller.pub( 'message', job_key )
                 
                 pubbed_job_key = True
                 
             
             self._CheckFolder( job_key )
             
             checked_folder = True
             
         
         file_seed = self._file_seed_cache.GetNextFileSeed( CC.STATUS_UNKNOWN )
         
         if file_seed is not None:
             
             if not pubbed_job_key and self._show_working_popup:
                 
                 HG.client_controller.pub( 'message', job_key )
                 
                 pubbed_job_key = True
                 
             
             did_import_file_work = self._ImportFiles( job_key )
             
         
     except Exception as e:
         
         error_occured = True
         self._paused = True
         
         HydrusData.ShowText( 'The import folder "' + self._name + '" encountered an exception! It has been paused!' )
         HydrusData.ShowException( e )
         
     
     if checked_folder or did_import_file_work or error_occured:
         
         HG.client_controller.WriteSynchronous( 'serialisable', self )
         
     
     job_key.Delete()
Example #13
0
def DAEMONDownloadFiles(controller):

    hashes = controller.Read('downloads')

    num_downloads = len(hashes)

    if num_downloads > 0:

        successful_hashes = set()

        job_key = ClientThreading.JobKey()

        job_key.SetVariable('popup_text_1', 'initialising downloader')

        controller.pub('message', job_key)

        for hash in hashes:

            job_key.SetVariable(
                'popup_text_1', 'downloading ' +
                HydrusData.ConvertIntToPrettyString(num_downloads -
                                                    len(successful_hashes)) +
                ' files from repositories')

            (media_result, ) = controller.Read('media_results', (hash, ))

            service_keys = list(
                media_result.GetLocationsManager().GetCurrent())

            random.shuffle(service_keys)

            for service_key in service_keys:

                if service_key == CC.LOCAL_FILE_SERVICE_KEY: break
                elif service_key == CC.TRASH_SERVICE_KEY: continue

                try:

                    file_repository = controller.GetServicesManager(
                    ).GetService(service_key)

                except:

                    continue

                if file_repository.CanDownload():

                    try:

                        request_args = {'hash': hash.encode('hex')}

                        (os_file_handle, temp_path) = HydrusPaths.GetTempPath()

                        try:

                            file_repository.Request(HC.GET,
                                                    'file',
                                                    request_args=request_args,
                                                    temp_path=temp_path)

                            controller.WaitUntilPubSubsEmpty()

                            controller.WriteSynchronous('import_file',
                                                        temp_path,
                                                        override_deleted=True)

                            successful_hashes.add(hash)

                            break

                        finally:

                            HydrusPaths.CleanUpTempPath(
                                os_file_handle, temp_path)

                    except HydrusExceptions.ServerBusyException:

                        job_key.SetVariable(
                            'popup_text_1',
                            file_repository.GetName() +
                            ' was busy. waiting 30s before trying again')

                        time.sleep(30)

                        job_key.Delete()

                        controller.pub('notify_new_downloads')

                        return

                    except Exception as e:

                        HydrusData.ShowText('Error downloading file!')
                        HydrusData.ShowException(e)

                if HydrusThreading.IsThreadShuttingDown():

                    return

        if len(successful_hashes) > 0:

            job_key.SetVariable(
                'popup_text_1',
                HydrusData.ConvertIntToPrettyString(len(successful_hashes)) +
                ' files downloaded')

        else:

            job_key.SetVariable('popup_text_1', 'all files failed to download')

        job_key.Delete()
Example #14
0
            def do_it(dest_dir):

                try:

                    update_hashes = self._service.GetUpdateHashes()

                    num_to_do = len(update_hashes)

                    if num_to_do == 0:

                        wx.CallAfter(wx.MessageBox, 'No updates to export!')

                    else:

                        job_key = ClientThreading.JobKey(cancellable=True)

                        try:

                            job_key.SetVariable(
                                'popup_title', 'exporting updates for ' +
                                self._service.GetName())
                            HydrusGlobals.client_controller.pub(
                                'message', job_key)

                            client_files_manager = HydrusGlobals.client_controller.GetClientFilesManager(
                            )

                            for (i, update_hash) in enumerate(update_hashes):

                                (i_paused,
                                 should_quit) = job_key.WaitIfNeeded()

                                if should_quit:

                                    job_key.SetVariable(
                                        'popup_text_1', 'Cancelled!')

                                    return

                                try:

                                    update_path = client_files_manager.GetFilePath(
                                        update_hash,
                                        HC.APPLICATION_HYDRUS_UPDATE_CONTENT)

                                    dest_path = os.path.join(
                                        dest_dir, update_hash.encode('hex'))

                                    HydrusPaths.MirrorFile(
                                        update_path, dest_path)

                                except HydrusExceptions.FileMissingException:

                                    continue

                                finally:

                                    job_key.SetVariable(
                                        'popup_text_1',
                                        HydrusData.
                                        ConvertValueRangeToPrettyString(
                                            i + 1, num_to_do))
                                    job_key.SetVariable(
                                        'popup_gauge_1', (i, num_to_do))

                            job_key.SetVariable('popup_text_1', 'Done!')

                        finally:

                            job_key.DeleteVariable('popup_gauge_1')

                            job_key.Finish()

                finally:

                    wx.CallAfter(self._export_updates_button.SetLabelText,
                                 'export updates')
                    wx.CallAfter(self._export_updates_button.Enable)
Example #15
0
        def do_it():

            job_key = ClientThreading.JobKey(pausable=True, cancellable=True)

            job_key.SetVariable('popup_title',
                                self._service.GetName() + ': immediate sync')
            job_key.SetVariable('popup_text_1', 'downloading')

            self._controller.pub('message', job_key)

            content_update_package = self._service.Request(
                HC.GET, 'immediate_content_update_package')

            c_u_p_num_rows = content_update_package.GetNumRows()
            c_u_p_total_weight_processed = 0

            update_speed_string = ''

            content_update_index_string = 'content row ' + HydrusData.ConvertValueRangeToPrettyString(
                c_u_p_total_weight_processed, c_u_p_num_rows) + ': '

            job_key.SetVariable(
                'popup_text_1', content_update_index_string + 'committing' +
                update_speed_string)

            job_key.SetVariable('popup_gauge_1',
                                (c_u_p_total_weight_processed, c_u_p_num_rows))

            for (content_updates, weight
                 ) in content_update_package.IterateContentUpdateChunks():

                (i_paused, should_quit) = job_key.WaitIfNeeded()

                if should_quit:

                    job_key.Delete()

                    return

                content_update_index_string = 'content row ' + HydrusData.ConvertValueRangeToPrettyString(
                    c_u_p_total_weight_processed, c_u_p_num_rows) + ': '

                job_key.SetVariable(
                    'popup_text_1', content_update_index_string +
                    'committing' + update_speed_string)

                job_key.SetVariable(
                    'popup_gauge_1',
                    (c_u_p_total_weight_processed, c_u_p_num_rows))

                precise_timestamp = HydrusData.GetNowPrecise()

                self._controller.WriteSynchronous(
                    'content_updates', {self._service_key: content_updates})

                it_took = HydrusData.GetNowPrecise() - precise_timestamp

                rows_s = weight / it_took

                update_speed_string = ' at ' + HydrusData.ConvertIntToPrettyString(
                    rows_s) + ' rows/s'

                c_u_p_total_weight_processed += weight

            job_key.DeleteVariable('popup_gauge_1')

            self._service.SyncThumbnails(job_key)

            job_key.SetVariable(
                'popup_text_1', 'done! ' +
                HydrusData.ConvertIntToPrettyString(c_u_p_num_rows) +
                ' rows added.')

            job_key.Finish()
Example #16
0
def CatchExceptionClient(etype, value, tb):

    try:

        trace_list = traceback.format_tb(tb)

        trace = ''.join(trace_list)

        pretty_value = HydrusData.ToUnicode(value)

        if os.linesep in pretty_value:

            (first_line, anything_else) = pretty_value.split(os.linesep, 1)

            trace = trace + os.linesep + anything_else

        else:

            first_line = pretty_value

        trace = HydrusData.ToUnicode(trace)

        job_key = ClientThreading.JobKey()

        if etype == HydrusExceptions.ShutdownException:

            return

        else:

            try:
                job_key.SetVariable('popup_title',
                                    HydrusData.ToUnicode(etype.__name__))
            except:
                job_key.SetVariable('popup_title', HydrusData.ToUnicode(etype))

            job_key.SetVariable('popup_text_1', first_line)
            job_key.SetVariable('popup_traceback', trace)

        text = job_key.ToString()

        HydrusData.Print('Uncaught exception:')

        HydrusData.DebugPrint(text)

        HG.client_controller.pub('message', job_key)

    except:

        text = 'Encountered an error I could not parse:'

        text += os.linesep

        text += HydrusData.ToUnicode((etype, value, tb))

        try:
            text += traceback.format_exc()
        except:
            pass

        HydrusData.ShowText(text)

    time.sleep(1)