def test_SERIALISABLE_TYPE_APPLICATION_COMMAND(self):
        def test(obj, dupe_obj):

            self.assertEqual(obj.GetCommandType(), dupe_obj.GetCommandType())

            self.assertEqual(obj.GetData(), dupe_obj.GetData())

        acs = []

        acs.append(
            (CAC.ApplicationCommand(CAC.APPLICATION_COMMAND_TYPE_SIMPLE,
                                    CAC.SIMPLE_ARCHIVE_FILE), 'archive file'))
        acs.append((CAC.ApplicationCommand(
            CAC.APPLICATION_COMMAND_TYPE_CONTENT,
            (HydrusData.GenerateKey(), HC.CONTENT_TYPE_MAPPINGS,
             HC.CONTENT_UPDATE_FLIP, 'test')),
                    'flip on/off mappings "test" for unknown service!'))
        acs.append((CAC.ApplicationCommand(
            CAC.APPLICATION_COMMAND_TYPE_CONTENT,
            (CC.DEFAULT_LOCAL_TAG_SERVICE_KEY, HC.CONTENT_TYPE_MAPPINGS,
             HC.CONTENT_UPDATE_FLIP, 'test')),
                    'flip on/off mappings "test" for my tags'))
        acs.append(
            (CAC.ApplicationCommand(
                CAC.APPLICATION_COMMAND_TYPE_CONTENT,
                (HydrusData.GenerateKey(), HC.CONTENT_TYPE_RATINGS,
                 HC.CONTENT_UPDATE_SET, 0.4)),
             'set ratings uncertain rating, "0.4" for unknown service!'))

        for (ac, s) in acs:

            self._dump_and_load_and_test(ac, test)

            self.assertEqual(ac.ToString(), s)
 def test_services( self ):
     
     def test_service( service, key, service_type, name ):
         
         self.assertEqual( service.GetServiceKey(), key )
         self.assertEqual( service.GetServiceType(), service_type )
         self.assertEqual( service.GetName(), name )
         
     
     repo_key = HydrusData.GenerateKey()
     repo_type = HC.TAG_REPOSITORY
     repo_name = 'test tag repo'
     
     repo = ClientServices.GenerateService( repo_key, repo_type, repo_name )
     
     other_key = HydrusData.GenerateKey()
     
     other = ClientServices.GenerateService( other_key, HC.LOCAL_BOORU, 'booru' )
     
     services = []
     
     services.append( repo )
     services.append( other )
     
     HG.test_controller.SetRead( 'services', services )
     
     services_manager = ClientManagers.ServicesManager( HG.client_controller )
     
     #
     
     service = services_manager.GetService( repo_key )
     
     test_service( service, repo_key, repo_type, repo_name )
     
     service = services_manager.GetService( other_key )
     
     #
     
     services = services_manager.GetServices( ( HC.TAG_REPOSITORY, ) )
     
     self.assertEqual( len( services ), 1 )
     
     self.assertEqual( services[0].GetServiceKey(), repo_key )
     
     #
     
     services = []
     
     services.append( repo )
     
     HG.test_controller.SetRead( 'services', services )
     
     services_manager.RefreshServices()
     
     self.assertRaises( Exception, services_manager.GetService, other_key )
Exemple #3
0
 def setUpClass( cls ):
     
     cls._access_key = HydrusData.GenerateKey()
     
     cls._serverside_file_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo', HC.DEFAULT_SERVICE_PORT + 1 )
     cls._serverside_tag_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo', HC.DEFAULT_SERVICE_PORT )
     cls._serverside_admin_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin', HC.DEFAULT_SERVER_ADMIN_PORT )
     
     cls._clientside_file_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo' )
     cls._clientside_tag_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo' )
     cls._clientside_admin_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin' )
     
     cls._clientside_file_service.SetCredentials( HydrusNetwork.Credentials( '127.0.0.1', HC.DEFAULT_SERVICE_PORT + 1, cls._access_key ) )
     cls._clientside_tag_service.SetCredentials( HydrusNetwork.Credentials( '127.0.0.1', HC.DEFAULT_SERVICE_PORT, cls._access_key ) )
     cls._clientside_admin_service.SetCredentials( HydrusNetwork.Credentials( '127.0.0.1', HC.DEFAULT_SERVER_ADMIN_PORT, cls._access_key ) )
     
     cls._local_booru = ClientServices.GenerateService( CC.LOCAL_BOORU_SERVICE_KEY, HC.LOCAL_BOORU, 'local booru' )
     
     services_manager = HG.test_controller.services_manager
     
     services_manager._keys_to_services[ cls._clientside_file_service.GetServiceKey() ] = cls._clientside_file_service
     services_manager._keys_to_services[ cls._clientside_tag_service.GetServiceKey() ] = cls._clientside_tag_service
     services_manager._keys_to_services[ cls._clientside_admin_service.GetServiceKey() ] = cls._clientside_admin_service
     
     permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ]
     
     account_key = HydrusData.GenerateKey()
     account_type = HydrusNetwork.AccountType.GenerateAdminAccountType( HC.SERVER_ADMIN )
     created = HydrusData.GetNow() - 100000
     expires = None
     
     cls._account = HydrusNetwork.Account( account_key, account_type, created, expires )
     
     cls._file_hash = HydrusData.GenerateKey()
     
     def TWISTEDSetup():
         
         cls._ssl_cert_path = os.path.join( TestController.DB_DIR, 'server.crt' )
         cls._ssl_key_path = os.path.join( TestController.DB_DIR, 'server.key' )
         
         # if db test ran, this is still hanging around and read-only, so don't bother to fail overwriting
         if not os.path.exists( cls._ssl_cert_path ):
             
             HydrusEncryption.GenerateOpenSSLCertAndKeyFile( cls._ssl_cert_path, cls._ssl_key_path )
             
         
         context_factory = twisted.internet.ssl.DefaultOpenSSLContextFactory( cls._ssl_key_path, cls._ssl_cert_path )
         
         reactor.listenSSL( HC.DEFAULT_SERVER_ADMIN_PORT, ServerServer.HydrusServiceAdmin( cls._serverside_admin_service ), context_factory )
         reactor.listenSSL( HC.DEFAULT_SERVICE_PORT + 1, ServerServer.HydrusServiceRepositoryFile( cls._serverside_file_service ), context_factory )
         reactor.listenSSL( HC.DEFAULT_SERVICE_PORT, ServerServer.HydrusServiceRepositoryTag( cls._serverside_tag_service ), context_factory )
         
         reactor.listenTCP( 45866, ClientLocalServer.HydrusServiceBooru( cls._local_booru, allow_non_local_connections = False ) )
         
     
     reactor.callFromThread( TWISTEDSetup )
     
     time.sleep( 3 )
 def test_undo( self ):
     
     hash_1 = HydrusData.GenerateKey()
     hash_2 = HydrusData.GenerateKey()
     hash_3 = HydrusData.GenerateKey()
     
     command_1 = { CC.COMBINED_LOCAL_FILE_SERVICE_KEY : [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_FILES, HC.CONTENT_UPDATE_ARCHIVE, { hash_1 } ) ] }
     command_2 = { CC.COMBINED_LOCAL_FILE_SERVICE_KEY : [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_FILES, HC.CONTENT_UPDATE_INBOX, { hash_2 } ) ] }
     command_3 = { CC.COMBINED_LOCAL_FILE_SERVICE_KEY : [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_FILES, HC.CONTENT_UPDATE_ARCHIVE, { hash_1, hash_3 } ) ] }
     
     command_1_inverted = { CC.COMBINED_LOCAL_FILE_SERVICE_KEY : [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_FILES, HC.CONTENT_UPDATE_INBOX, { hash_1 } ) ] }
     command_2_inverted = { CC.COMBINED_LOCAL_FILE_SERVICE_KEY : [ HydrusData.ContentUpdate( HC.CONTENT_TYPE_FILES, HC.CONTENT_UPDATE_ARCHIVE, { hash_2 } ) ] }
     
     undo_manager = ClientManagers.UndoManager( HG.client_controller )
     
     #
     
     HG.test_controller.ClearWrites( 'content_updates' )
     
     undo_manager.AddCommand( 'content_updates', command_1 )
     
     self.assertEqual( ( 'undo archive 1 files', None ), undo_manager.GetUndoRedoStrings() )
     
     undo_manager.AddCommand( 'content_updates', command_2 )
     
     self.assertEqual( ( 'undo inbox 1 files', None ), undo_manager.GetUndoRedoStrings() )
     
     undo_manager.Undo()
     
     self.assertEqual( ( 'undo archive 1 files', 'redo inbox 1 files' ), undo_manager.GetUndoRedoStrings() )
     
     self.assertEqual( HG.test_controller.GetWrite( 'content_updates' ), [ ( ( command_2_inverted, ), {} ) ] )
     
     undo_manager.Redo()
     
     self.assertEqual( HG.test_controller.GetWrite( 'content_updates' ), [ ( ( command_2, ), {} ) ] )
     
     self.assertEqual( ( 'undo inbox 1 files', None ), undo_manager.GetUndoRedoStrings() )
     
     undo_manager.Undo()
     
     self.assertEqual( HG.test_controller.GetWrite( 'content_updates' ), [ ( ( command_2_inverted, ), {} ) ] )
     
     undo_manager.Undo()
     
     self.assertEqual( HG.test_controller.GetWrite( 'content_updates' ), [ ( ( command_1_inverted, ), {} ) ] )
     
     self.assertEqual( ( None, 'redo archive 1 files' ), undo_manager.GetUndoRedoStrings() )
     
     undo_manager.AddCommand( 'content_updates', command_3 )
     
     self.assertEqual( ( 'undo archive 2 files', None ), undo_manager.GetUndoRedoStrings() )
 def _GetTracker( self, network_context: ClientNetworkingContexts.NetworkContext, making_it_dirty = False ):
     
     if network_context not in self._network_contexts_to_tracker_containers:
         
         bandwidth_tracker = HydrusNetworking.BandwidthTracker()
         
         tracker_container_name = HydrusData.GenerateKey().hex()
         
         tracker_container = NetworkBandwidthManagerTrackerContainer( tracker_container_name, network_context = network_context, bandwidth_tracker = bandwidth_tracker )
         
         self._tracker_container_names_to_tracker_containers[ tracker_container_name ] = tracker_container
         self._network_contexts_to_tracker_containers[ network_context ] = tracker_container
         
         # note this discards ephemeral network contexts, which have temporary identifiers that are generally invisible to the user
         
         if not network_context.IsEphemeral():
             
             self._tracker_container_names.add( tracker_container_name )
             self._dirty_tracker_container_names.add( tracker_container_name )
             
         
         self._SetDirty()
         
     
     tracker_container = self._network_contexts_to_tracker_containers[ network_context ]
     
     if making_it_dirty and not network_context.IsEphemeral():
         
         self._dirty_tracker_container_names.add( tracker_container.GetName() )
         
     
     return tracker_container.bandwidth_tracker
Exemple #6
0
 def __init__( self, name = 'new api permissions', access_key = None, basic_permissions = None, search_tag_filter = None ):
     
     if access_key is None:
         
         access_key = HydrusData.GenerateKey()
         
     
     if basic_permissions is None:
         
         basic_permissions = set()
         
     
     if search_tag_filter is None:
         
         search_tag_filter = HydrusTags.TagFilter()
         
     
     HydrusSerialisable.SerialisableBaseNamed.__init__( self, name )
     
     self._access_key = access_key
     
     self._basic_permissions = set( basic_permissions )
     self._search_tag_filter = search_tag_filter
     
     self._last_search_results = None
     self._search_results_timeout = 0
     
     self._lock = threading.Lock()
Exemple #7
0
    def _test_content_creation(self):

        tag = 'character:samus aran'
        hash = HydrusData.GenerateKey()

        mappings_content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPINGS,
                                                 (tag, (hash, )))
        mapping_content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPING,
                                                (tag, hash))

        client_to_server_update = HydrusNetwork.ClientToServerUpdate()

        client_to_server_update.AddContent(HC.CONTENT_UPDATE_PEND,
                                           mappings_content)

        self._write('update', self._tag_service_key,
                    self._tag_service_regular_account, client_to_server_update,
                    HydrusData.GetNow())

        # can extend this to generate and fetch an actual update given a timespan

        #

        result = self._read('account_from_content', self._tag_service_key,
                            mapping_content)

        self.assertEqual(result.GetAccountKey(),
                         self._tag_service_regular_account.GetAccountKey())
Exemple #8
0
    def __init__(self):

        HydrusSerialisable.SerialisableBase.__init__(self)

        file_import_options = HG.client_controller.new_options.GetDefaultFileImportOptions(
            'loud')

        self._pending_jobs = []
        self._gallery_seed_log = ClientImportGallerySeeds.GallerySeedLog()
        self._file_seed_cache = ClientImportFileSeeds.FileSeedCache()
        self._file_import_options = file_import_options
        self._formula_name = 'all files linked by images in page'
        self._queue_paused = False
        self._files_paused = False

        self._downloader_key = HydrusData.GenerateKey()

        self._parser_status = ''
        self._current_action = ''

        self._lock = threading.Lock()

        self._have_started = False

        self._files_network_job = None
        self._page_network_job = None

        self._files_repeating_job = None
        self._queue_repeating_job = None

        self._last_serialisable_change_timestamp = 0

        HG.client_controller.sub(self, 'NotifyFileSeedsUpdated',
                                 'file_seed_cache_file_seeds_updated')
    def __init__(self, url=None, can_generate_more_pages=True):

        if url is None:

            url = 'https://nostrils-central.cx/index.php?post=s&tag=hyper_nostrils&page=3'

        else:

            try:

                url = HG.client_controller.network_engine.domain_manager.NormaliseURL(
                    url)

            except HydrusExceptions.URLClassException:

                pass

        HydrusSerialisable.SerialisableBase.__init__(self)

        self.url = url
        self._can_generate_more_pages = can_generate_more_pages

        self._fixed_service_keys_to_tags = ClientTags.ServiceKeysToTags()

        self.created = HydrusData.GetNow()
        self.modified = self.created
        self.status = CC.STATUS_UNKNOWN
        self.note = ''

        self._referral_url = None

        self._force_next_page_url_generation = False

        self._run_token = HydrusData.GenerateKey()
Exemple #10
0
 def __init__( self ):
     
     HydrusSerialisable.SerialisableBase.__init__( self )
     
     self._gallery_seed_log = ClientImportGallerySeeds.GallerySeedLog()
     self._file_seed_cache = ClientImportFileSeeds.FileSeedCache()
     self._file_import_options = HG.client_controller.new_options.GetDefaultFileImportOptions( 'loud' )
     self._tag_import_options = TagImportOptions.TagImportOptions( is_default = True )
     self._paused = False
     
     self._no_work_until = 0
     self._no_work_until_reason = ''
     
     self._page_key = b'initialising page key'
     self._downloader_key = HydrusData.GenerateKey()
     
     self._lock = threading.Lock()
     
     self._have_started = False
     
     self._files_status = ''
     self._gallery_status = ''
     
     self._files_network_job = None
     self._gallery_network_job = None
     
     self._files_repeating_job = None
     self._gallery_repeating_job = None
     
     self._last_serialisable_change_timestamp = 0
     
     HG.client_controller.sub( self, 'NotifyFileSeedsUpdated', 'file_seed_cache_file_seeds_updated' )
     HG.client_controller.sub( self, 'NotifyGallerySeedsUpdated', 'gallery_seed_log_gallery_seeds_updated' )
    def __init__(self, parent: QW.QWidget, predicate: ClientSearch.Predicate):

        QW.QWidget.__init__(self, parent)

        from hydrus.client.gui.search import ClientGUIACDropdown

        if predicate.GetType() != ClientSearch.PREDICATE_TYPE_OR_CONTAINER:

            raise Exception(
                'Launched an ORPredicateControl without an OR Pred!')

        predicates = predicate.GetValue()

        page_key = HydrusData.GenerateKey()

        file_search_context = ClientSearch.FileSearchContext(
            file_service_key=CC.LOCAL_FILE_SERVICE_KEY, predicates=predicates)

        self._search_control = ClientGUIACDropdown.AutoCompleteDropdownTagsRead(
            self,
            page_key,
            file_search_context,
            hide_favourites_edit_actions=True)

        vbox = QP.VBoxLayout()

        QP.AddToLayout(vbox, self._search_control, CC.FLAGS_EXPAND_BOTH_WAYS)

        self.setLayout(vbox)
Exemple #12
0
    def _test_server_admin(self, service):

        # init

        access_key = HydrusData.GenerateKey()

        HG.test_controller.SetRead('access_key', access_key)

        response = service.Request(HC.GET, 'access_key',
                                   {'registration_key': b'init'})

        self.assertEqual(response['access_key'], access_key)

        #

        response = service.Request(HC.GET, 'busy')

        self.assertEqual(response, b'0')

        response = service.Request(HC.POST, 'lock_on')

        response = service.Request(HC.GET, 'busy')

        self.assertEqual(response, b'1')

        response = service.Request(HC.POST, 'lock_off')

        response = service.Request(HC.GET, 'busy')

        self.assertEqual(response, b'0')

        #

        response = service.Request(HC.POST, 'backup')
        response = service.Request(HC.POST, 'vacuum')
    def __init__(self):

        HydrusSerialisable.SerialisableBase.__init__(self)

        self._gallery_seed_log = ClientImportGallerySeeds.GallerySeedLog()
        self._file_seed_cache = ClientImportFileSeeds.FileSeedCache()
        self._file_import_options = HG.client_controller.new_options.GetDefaultFileImportOptions(
            'loud')
        self._tag_import_options = ClientImportOptions.TagImportOptions(
            is_default=True)
        self._paused = False

        self._downloader_key = HydrusData.GenerateKey()

        self._lock = threading.Lock()

        self._files_network_job = None
        self._gallery_network_job = None

        self._files_repeating_job = None
        self._gallery_repeating_job = None

        HG.client_controller.sub(self, 'NotifyFileSeedsUpdated',
                                 'file_seed_cache_file_seeds_updated')
        HG.client_controller.sub(self, 'NotifyGallerySeedsUpdated',
                                 'gallery_seed_log_gallery_seeds_updated')
Exemple #14
0
    def _Add(self, service_type):

        service_key = HydrusData.GenerateKey()

        port = self._GetNextPort()

        name = 'new service'

        dictionary = HydrusNetwork.GenerateDefaultServiceDictionary(
            service_type)

        service = HydrusNetwork.GenerateService(service_key, service_type,
                                                name, port, dictionary)

        with ClientGUITopLevelWindowsPanels.DialogEdit(
                self, 'edit serverside service') as dlg_edit:

            panel = EditServersideService(dlg_edit, service)

            dlg_edit.SetPanel(panel)

            if dlg_edit.exec() == QW.QDialog.Accepted:

                new_service = panel.GetValue()

                self._services_listctrl.SetNonDupeName(new_service)

                self._SetNonDupePort(new_service)

                self._services_listctrl.AddDatas((new_service, ))
Exemple #15
0
def ConvertLegacyToNewBandwidth(
        legacy_bandwidth_manager: NetworkBandwidthManagerLegacy):

    tracker_containers = []

    (network_contexts_to_bandwidth_trackers,
     network_contexts_to_bandwidth_rules) = legacy_bandwidth_manager.GetData()

    for (network_context,
         bandwidth_tracker) in network_contexts_to_bandwidth_trackers.items():

        tracker_container_name = HydrusData.GenerateKey().hex()

        tracker_container = ClientNetworkingBandwidth.NetworkBandwidthManagerTrackerContainer(
            tracker_container_name,
            network_context=network_context,
            bandwidth_tracker=bandwidth_tracker)

        tracker_containers.append(tracker_container)

    bandwidth_manager = ClientNetworkingBandwidth.NetworkBandwidthManager()

    for (network_context,
         bandwidth_rules) in network_contexts_to_bandwidth_rules.items():

        bandwidth_manager.SetRules(network_context, bandwidth_rules)

    bandwidth_manager.SetTrackerContainers(tracker_containers,
                                           set_all_trackers_dirty=True)

    bandwidth_manager.SetDirty()

    return bandwidth_manager
Exemple #16
0
    def _test_service_creation(self):

        self._tag_service_key = HydrusData.GenerateKey()
        self._file_service_key = HydrusData.GenerateKey()

        current_services = self._read('services')

        self._tag_service = HydrusNetwork.GenerateService(
            self._tag_service_key, HC.TAG_REPOSITORY, 'tag repo', 100)
        self._file_service = HydrusNetwork.GenerateService(
            self._file_service_key, HC.FILE_REPOSITORY, 'file repo', 101)

        new_services = list(current_services)
        new_services.append(self._tag_service)
        new_services.append(self._file_service)

        service_keys_to_access_keys = self._write('services',
                                                  self._admin_account,
                                                  new_services)

        self.assertEqual(set(service_keys_to_access_keys.keys()),
                         {self._tag_service_key, self._file_service_key})

        self._tag_service_access_key = service_keys_to_access_keys[
            self._tag_service_key]
        self._file_service_access_key = service_keys_to_access_keys[
            self._file_service_key]

        self._tag_service_account_key = self._read(
            'account_key_from_access_key', self._tag_service_key,
            self._tag_service_access_key)
        self._file_service_account_key = self._read(
            'account_key_from_access_key', self._file_service_key,
            self._file_service_access_key)

        self._tag_service_account = self._read('account',
                                               self._tag_service_key,
                                               self._tag_service_account_key)
        self._file_service_account = self._read('account',
                                                self._file_service_key,
                                                self._file_service_account_key)

        self.assertEqual(self._tag_service_account.GetAccountKey(),
                         self._tag_service_account_key)
        self.assertEqual(self._file_service_account.GetAccountKey(),
                         self._file_service_account_key)
Exemple #17
0
 def GenerateSessionKey( self, access_key ):
     
     session_key = HydrusData.GenerateKey()
     
     with self._lock:
         
         self._session_keys_to_access_keys_and_expirys[ session_key ] = ( access_key, HydrusData.GetNow() + SESSION_EXPIRY )
         
     
     return session_key
Exemple #18
0
 def _PopulateTagIdsToTagsCache( self, tag_ids ):
     
     if len( self._tag_ids_to_tags_cache ) > 100000:
         
         if not isinstance( tag_ids, set ):
             
             tag_ids = set( tag_ids )
             
         
         self._tag_ids_to_tags_cache = { tag_id : tag for ( tag_id, tag ) in self._tag_ids_to_tags_cache.items() if tag_id in tag_ids }
         
     
     uncached_tag_ids = { tag_id for tag_id in tag_ids if tag_id not in self._tag_ids_to_tags_cache }
     
     if len( uncached_tag_ids ) > 0:
         
         if len( uncached_tag_ids ) == 1:
             
             ( uncached_tag_id, ) = uncached_tag_ids
             
             rows = self._c.execute( 'SELECT tag_id, namespace, subtag FROM tags NATURAL JOIN namespaces NATURAL JOIN subtags WHERE tag_id = ?;', ( uncached_tag_id, ) ).fetchall()
             
         else:
             
             with HydrusDB.TemporaryIntegerTable( self._c, uncached_tag_ids, 'tag_id' ) as temp_table_name:
                 
                 # temp tag_ids to tags to subtags and namespaces
                 rows = self._c.execute( 'SELECT tag_id, namespace, subtag FROM {} CROSS JOIN tags USING ( tag_id ) CROSS JOIN subtags USING ( subtag_id ) CROSS JOIN namespaces USING ( namespace_id );'.format( temp_table_name ) ).fetchall()
                 
             
         
         uncached_tag_ids_to_tags = { tag_id : HydrusTags.CombineTag( namespace, subtag ) for ( tag_id, namespace, subtag ) in rows }
         
         if len( uncached_tag_ids_to_tags ) < len( uncached_tag_ids ):
             
             for tag_id in uncached_tag_ids:
                 
                 if tag_id not in uncached_tag_ids_to_tags:
                     
                     tag = 'unknown tag:' + HydrusData.GenerateKey().hex()
                     
                     ( namespace, subtag ) = HydrusTags.SplitTag( tag )
                     
                     namespace_id = self.GetNamespaceId( namespace )
                     subtag_id = self.GetSubtagId( subtag )
                     
                     self._c.execute( 'REPLACE INTO tags ( tag_id, namespace_id, subtag_id ) VALUES ( ?, ?, ? );', ( tag_id, namespace_id, subtag_id ) )
                     
                     uncached_tag_ids_to_tags[ tag_id ] = tag
                     
                 
             
         
         self._tag_ids_to_tags_cache.update( uncached_tag_ids_to_tags )
    def _do_fake_imports(self):

        self._md5_to_sha256 = {}
        self._sha256_to_md5 = {}
        self._sha256_to_sha1 = {}

        self._my_files_sha256 = set()

        self._hashes_to_current_tags = {}
        self._hashes_to_pending_tags = {}
        self._hashes_to_deleted_tags = {}

        (size, mime, width, height, duration, num_frames, has_audio,
         num_words) = (65535, HC.IMAGE_JPEG, 640, 480, None, None, False, None)

        file_import_options = HG.client_controller.new_options.GetDefaultFileImportOptions(
            'loud')

        for i in range(100):

            hash = HydrusData.GenerateKey()
            md5 = os.urandom(16)
            sha1 = os.urandom(20)
            sha512 = os.urandom(64)

            self._md5_to_sha256[md5] = hash
            self._sha256_to_md5[hash] = md5
            self._sha256_to_sha1[hash] = sha1

            self._hashes_to_current_tags[hash] = set(
                random.sample(current_tag_pool, 3))
            self._hashes_to_pending_tags[hash] = set(
                random.sample(pending_tag_pool, 3))
            self._hashes_to_deleted_tags[hash] = set(
                random.sample(deleted_tag_pool, 3))

            if i < 50:

                fake_file_import_job = ClientImportFiles.FileImportJob(
                    'fake path', file_import_options)

                fake_file_import_job._pre_import_file_status = ClientImportFiles.FileImportStatus(
                    CC.STATUS_UNKNOWN, hash)
                fake_file_import_job._file_info = (size, mime, width, height,
                                                   duration, num_frames,
                                                   has_audio, num_words)
                fake_file_import_job._extra_hashes = (md5, sha1, sha512)
                fake_file_import_job._perceptual_hashes = [os.urandom(8)]
                fake_file_import_job._file_import_options = FileImportOptions.FileImportOptions(
                )

                self.WriteSynchronous('import_file', fake_file_import_job)

                self._my_files_sha256.add(hash)
Exemple #20
0
    def __init__(self):

        HydrusSerialisable.SerialisableBase.__init__(self)

        self._page_key = 'initialising page key'
        self._publish_to_page = False

        self._url = ''

        self._gallery_seed_log = ClientImportGallerySeeds.GallerySeedLog()
        self._file_seed_cache = ClientImportFileSeeds.FileSeedCache()

        self._fixed_service_keys_to_tags = ClientTags.ServiceKeysToTags()

        self._checker_options = HG.client_controller.new_options.GetDefaultWatcherCheckerOptions(
        )
        self._file_import_options = HG.client_controller.new_options.GetDefaultFileImportOptions(
            'loud')
        self._tag_import_options = ClientImportOptions.TagImportOptions(
            is_default=True)
        self._last_check_time = 0
        self._checking_status = ClientImporting.CHECKER_STATUS_OK
        self._subject = 'unknown subject'

        self._next_check_time = None

        self._file_network_job = None
        self._checker_network_job = None

        self._check_now = False
        self._files_paused = False
        self._checking_paused = False

        self._no_work_until = 0
        self._no_work_until_reason = ''

        self._creation_time = HydrusData.GetNow()

        self._file_velocity_status = ''
        self._file_status = ''
        self._watcher_status = ''

        self._watcher_key = HydrusData.GenerateKey()

        self._lock = threading.Lock()

        self._last_pubbed_page_name = ''

        self._files_repeating_job = None
        self._checker_repeating_job = None

        HG.client_controller.sub(self, 'NotifyFileSeedsUpdated',
                                 'file_seed_cache_file_seeds_updated')
 def test_SERIALISABLE_TYPE_SUBSCRIPTION( self ):
     
     def test( obj, dupe_obj ):
         
         self.assertEqual( obj.GetName(), dupe_obj.GetName() )
         
         self.assertEqual( obj._gug_key_and_name, dupe_obj._gug_key_and_name )
         self.assertEqual( len( obj._queries ), len( dupe_obj._queries ) )
         self.assertEqual( obj._initial_file_limit, dupe_obj._initial_file_limit )
         self.assertEqual( obj._periodic_file_limit, dupe_obj._periodic_file_limit )
         self.assertEqual( obj._paused, dupe_obj._paused )
         
         self.assertEqual( obj._file_import_options.GetSerialisableTuple(), dupe_obj._file_import_options.GetSerialisableTuple() )
         self.assertEqual( obj._tag_import_options.GetSerialisableTuple(), dupe_obj._tag_import_options.GetSerialisableTuple() )
         
         self.assertEqual( obj._no_work_until, dupe_obj._no_work_until )
         
     
     sub = ClientImportSubscriptions.Subscription( 'test sub' )
     
     self._dump_and_load_and_test( sub, test )
     
     gug_key_and_name = ( HydrusData.GenerateKey(), 'muh test gug' )
     queries = [ ClientImportSubscriptionQuery.SubscriptionQuery( 'test query' ), ClientImportSubscriptionQuery.SubscriptionQuery( 'test query 2' ) ]
     checker_options = ClientImportOptions.CheckerOptions()
     initial_file_limit = 100
     periodic_file_limit = 50
     paused = False
     
     file_import_options = ClientImportOptions.FileImportOptions()
     
     service_tag_import_options = ClientImportOptions.ServiceTagImportOptions( get_tags = False, additional_tags = { 'test additional tag', 'and another' } )
     
     tag_import_options = ClientImportOptions.TagImportOptions( service_keys_to_service_tag_import_options = { HydrusData.GenerateKey() : service_tag_import_options } )
     
     no_work_until = HydrusData.GetNow() - 86400 * 20
     
     sub.SetTuple( gug_key_and_name, checker_options, initial_file_limit, periodic_file_limit, paused, file_import_options, tag_import_options, no_work_until )
     
     sub.SetQueries( queries )
     
     self.assertEqual( sub.GetGUGKeyAndName(), gug_key_and_name )
     self.assertEqual( sub.GetTagImportOptions(), tag_import_options )
     self.assertEqual( sub.GetQueries(), queries )
     
     self.assertEqual( sub._paused, False )
     sub.PauseResume()
     self.assertEqual( sub._paused, True )
     sub.PauseResume()
     self.assertEqual( sub._paused, False )
     
     self._dump_and_load_and_test( sub, test )
    def test_basics(self):
        def test(obj, dupe_obj):

            self.assertEqual(len(list(obj.items())),
                             len(list(dupe_obj.items())))

            for (key, value) in list(obj.items()):

                self.assertEqual(value, dupe_obj[key])

        #

        d = HydrusSerialisable.SerialisableDictionary()

        d[1] = 2
        d[3] = 'test1'

        d['test2'] = 4
        d['test3'] = 5

        d[6] = HydrusSerialisable.SerialisableDictionary(
            {i: 'test' + str(i)
             for i in range(20)})
        d[ClientSearch.Predicate(ClientSearch.PREDICATE_TYPE_TAG,
                                 'test pred 1')] = 56

        d[ClientSearch.Predicate(
            ClientSearch.PREDICATE_TYPE_TAG,
            'test pred 2')] = HydrusSerialisable.SerialisableList([
                ClientSearch.Predicate(ClientSearch.PREDICATE_TYPE_TAG,
                                       'test' + str(i)) for i in range(10)
            ])

        self.assertEqual(len(list(d.keys())), 7)

        for (key, value) in list(d.items()):

            self.assertEqual(d[key], value)

        self._dump_and_load_and_test(d, test)

        #

        db = HydrusSerialisable.SerialisableBytesDictionary()

        db[HydrusData.GenerateKey()] = HydrusData.GenerateKey()
        db[HydrusData.GenerateKey()] = [
            HydrusData.GenerateKey() for i in range(10)
        ]
        db[1] = HydrusData.GenerateKey()
        db[2] = [HydrusData.GenerateKey() for i in range(10)]

        self.assertEqual(len(list(db.keys())), 4)

        for (key, value) in list(db.items()):

            self.assertEqual(db[key], value)

        self._dump_and_load_and_test(db, test)
    def test_SERIALISABLE_TYPE_APPLICATION_COMMAND(self):
        def test(obj, dupe_obj):

            self.assertEqual(obj.GetCommandType(), dupe_obj.GetCommandType())

            self.assertSequenceEqual(tuple(obj._data), tuple(dupe_obj._data))

        acs = []

        acs.append((CAC.ApplicationCommand.STATICCreateSimpleCommand(
            CAC.SIMPLE_ARCHIVE_FILE), 'archive file'))
        acs.append((CAC.ApplicationCommand.STATICCreateSimpleCommand(
            CAC.SIMPLE_MEDIA_SEEK_DELTA,
            simple_data=(-1, 2500)), 'seek media (back 2.5 seconds)'))
        acs.append((CAC.ApplicationCommand.STATICCreateSimpleCommand(
            CAC.SIMPLE_MEDIA_SEEK_DELTA,
            simple_data=(1, 800)), 'seek media (forwards 800 milliseconds)'))
        acs.append((CAC.ApplicationCommand(
            CAC.APPLICATION_COMMAND_TYPE_CONTENT,
            (HydrusData.GenerateKey(), HC.CONTENT_TYPE_MAPPINGS,
             HC.CONTENT_UPDATE_FLIP, 'test')),
                    'flip on/off mappings "test" for unknown service!'))
        acs.append((CAC.ApplicationCommand(
            CAC.APPLICATION_COMMAND_TYPE_CONTENT,
            (CC.DEFAULT_LOCAL_TAG_SERVICE_KEY, HC.CONTENT_TYPE_MAPPINGS,
             HC.CONTENT_UPDATE_FLIP, 'test')),
                    'flip on/off mappings "test" for my tags'))
        acs.append(
            (CAC.ApplicationCommand(
                CAC.APPLICATION_COMMAND_TYPE_CONTENT,
                (HydrusData.GenerateKey(), HC.CONTENT_TYPE_RATINGS,
                 HC.CONTENT_UPDATE_SET, 0.4)),
             'set ratings uncertain rating, "0.4" for unknown service!'))

        for (ac, s) in acs:

            self._dump_and_load_and_test(ac, test)

            self.assertEqual(ac.ToString(), s)
 def __init__( self ):
     
     HydrusSerialisable.SerialisableBase.__init__( self )
     
     self._gallery_seeds = HydrusSerialisable.SerialisableList()
     
     self._gallery_seeds_to_indices = {}
     
     self._gallery_seed_log_key = HydrusData.GenerateKey()
     
     self._status_cache = None
     
     self._status_dirty = True
     
     self._lock = threading.Lock()
def ConvertGalleryIdentifierToGUGKeyAndName(gallery_identifier):

    gug_name = ConvertGalleryIdentifierToGUGName(gallery_identifier)

    from hydrus.client import ClientDefaults

    gugs = ClientDefaults.GetDefaultGUGs()

    for gug in gugs:

        if gug.GetName() == gug_name:

            return gug.GetGUGKeyAndName()

    return (HydrusData.GenerateKey(), gug_name)
    def __init__(self,
                 pausable=False,
                 cancellable=False,
                 maintenance_mode=HC.MAINTENANCE_FORCED,
                 only_start_if_unbusy=False,
                 stop_time=None,
                 cancel_on_shutdown=True):

        self._key = HydrusData.GenerateKey()

        self._creation_time = HydrusData.GetNowFloat()

        self._pausable = pausable
        self._cancellable = cancellable
        self._maintenance_mode = maintenance_mode
        self._only_start_if_unbusy = only_start_if_unbusy
        self._stop_time = stop_time
        self._cancel_on_shutdown = cancel_on_shutdown

        self._start_time = HydrusData.GetNow()

        self._deleted = threading.Event()
        self._deletion_time = None
        self._begun = threading.Event()
        self._done = threading.Event()
        self._cancelled = threading.Event()
        self._paused = threading.Event()

        self._ui_update_pause_period = 0.1
        self._next_ui_update_pause = HydrusData.GetNowFloat(
        ) + self._ui_update_pause_period

        self._yield_pause_period = 10
        self._next_yield_pause = HydrusData.GetNow() + self._yield_pause_period

        self._bigger_pause_period = 100
        self._next_bigger_pause = HydrusData.GetNow(
        ) + self._bigger_pause_period

        self._longer_pause_period = 1000
        self._next_longer_pause = HydrusData.GetNow(
        ) + self._longer_pause_period

        self._exception = None

        self._urls = []
        self._variable_lock = threading.Lock()
        self._variables = dict()
 def test_SERIALISABLE_TYPE_SHORTCUT_SET( self ):
     
     def test( obj, dupe_obj ):
         
         for ( shortcut, command ) in obj:
             
             self.assertEqual( dupe_obj.GetCommand( shortcut ).GetData(), command.GetData() )
             
         
     
     default_shortcuts = ClientDefaults.GetDefaultShortcuts()
     
     for shortcuts in default_shortcuts:
         
         self._dump_and_load_and_test( shortcuts, test )
         
     
     command_1 = CAC.ApplicationCommand( CAC.APPLICATION_COMMAND_TYPE_SIMPLE, CAC.SIMPLE_ARCHIVE_FILE )
     command_2 = CAC.ApplicationCommand( CAC.APPLICATION_COMMAND_TYPE_CONTENT, ( HydrusData.GenerateKey(), HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_FLIP, 'test' ) )
     command_3 = CAC.ApplicationCommand( CAC.APPLICATION_COMMAND_TYPE_CONTENT, ( CC.DEFAULT_LOCAL_TAG_SERVICE_KEY, HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_FLIP, 'test' ) )
     
     k_shortcut_1 = ClientGUIShortcuts.Shortcut( ClientGUIShortcuts.SHORTCUT_TYPE_KEYBOARD_SPECIAL, ClientGUIShortcuts.SHORTCUT_KEY_SPECIAL_SPACE, ClientGUIShortcuts.SHORTCUT_PRESS_TYPE_PRESS, [] )
     k_shortcut_2 = ClientGUIShortcuts.Shortcut( ClientGUIShortcuts.SHORTCUT_TYPE_KEYBOARD_CHARACTER, ord( 'a' ), ClientGUIShortcuts.SHORTCUT_PRESS_TYPE_PRESS, [ ClientGUIShortcuts.SHORTCUT_MODIFIER_CTRL ] )
     k_shortcut_3 = ClientGUIShortcuts.Shortcut( ClientGUIShortcuts.SHORTCUT_TYPE_KEYBOARD_CHARACTER, ord( 'A' ), ClientGUIShortcuts.SHORTCUT_PRESS_TYPE_PRESS, [ ClientGUIShortcuts.SHORTCUT_MODIFIER_CTRL ] )
     k_shortcut_4 = ClientGUIShortcuts.Shortcut( ClientGUIShortcuts.SHORTCUT_TYPE_KEYBOARD_SPECIAL, ClientGUIShortcuts.SHORTCUT_KEY_SPECIAL_HOME, ClientGUIShortcuts.SHORTCUT_PRESS_TYPE_PRESS, [ ClientGUIShortcuts.SHORTCUT_MODIFIER_ALT, ClientGUIShortcuts.SHORTCUT_MODIFIER_CTRL ] )
     
     m_shortcut_1 = ClientGUIShortcuts.Shortcut( ClientGUIShortcuts.SHORTCUT_TYPE_MOUSE, ClientGUIShortcuts.SHORTCUT_MOUSE_LEFT, ClientGUIShortcuts.SHORTCUT_PRESS_TYPE_PRESS, [] )
     m_shortcut_2 = ClientGUIShortcuts.Shortcut( ClientGUIShortcuts.SHORTCUT_TYPE_MOUSE, ClientGUIShortcuts.SHORTCUT_MOUSE_MIDDLE, ClientGUIShortcuts.SHORTCUT_PRESS_TYPE_PRESS, [ ClientGUIShortcuts.SHORTCUT_MODIFIER_CTRL ] )
     m_shortcut_3 = ClientGUIShortcuts.Shortcut( ClientGUIShortcuts.SHORTCUT_TYPE_MOUSE, ClientGUIShortcuts.SHORTCUT_MOUSE_SCROLL_DOWN, ClientGUIShortcuts.SHORTCUT_PRESS_TYPE_PRESS, [ ClientGUIShortcuts.SHORTCUT_MODIFIER_ALT, ClientGUIShortcuts.SHORTCUT_MODIFIER_SHIFT ] )
     
     shortcut_set = ClientGUIShortcuts.ShortcutSet( 'test' )
     
     shortcut_set.SetCommand( k_shortcut_1, command_1 )
     shortcut_set.SetCommand( k_shortcut_2, command_2 )
     shortcut_set.SetCommand( k_shortcut_3, command_2 )
     shortcut_set.SetCommand( k_shortcut_4, command_3 )
     
     shortcut_set.SetCommand( m_shortcut_1, command_1 )
     shortcut_set.SetCommand( m_shortcut_2, command_2 )
     shortcut_set.SetCommand( m_shortcut_3, command_3 )
     
     self._dump_and_load_and_test( shortcut_set, test )
     
     self.assertEqual( shortcut_set.GetCommand( k_shortcut_1 ).GetData(), command_1.GetData() )
     
     shortcut_set.SetCommand( k_shortcut_1, command_3 )
     
     self.assertEqual( shortcut_set.GetCommand( k_shortcut_1 ).GetData(), command_3.GetData() )
 def _set_up_services( self ):
     
     self._test_tag_repo_service_keys = {}
     
     services = self.Read( 'services' )
     
     for i in range( 20 ):
         
         service_key = HydrusData.GenerateKey()
         
         services.append( ClientServices.GenerateService( service_key, HC.TAG_REPOSITORY, 'test repo {}'.format( i ) ) )
         
         self._test_tag_repo_service_keys[ i ] = service_key
         
     
     self.WriteSynchronous( 'update_services', services )
     
     self.services_manager = ClientManagers.ServicesManager( self )
 def AddSession( self, service_key, access_key ):
     
     with self._lock:
         
         account_key = self._GetAccountKeyFromAccessKey( service_key, access_key )
         
         account = self._GetAccountFromAccountKey( service_key, account_key )
         
         session_key = HydrusData.GenerateKey()
         
         now = HydrusData.GetNow()
         
         expires = now + HYDRUS_SESSION_LIFETIME
         
         HG.controller.Write( 'session', session_key, service_key, account_key, expires )
         
         self._service_keys_to_session_keys_to_sessions[ service_key ][ session_key ] = ( account_key, expires )
         
         return ( session_key, expires )
Exemple #30
0
 def _InitialiseSessionContainer( self, network_context ):
     
     session = requests.Session()
     
     if network_context.context_type == CC.NETWORK_CONTEXT_HYDRUS:
         
         session.verify = False
         
     
     session_container_name = HydrusData.GenerateKey().hex()
     
     session_container = NetworkSessionManagerSessionContainer( session_container_name, network_context = network_context, session = session )
     
     self._session_container_names_to_session_containers[ session_container_name ] = session_container
     self._network_contexts_to_session_containers[ network_context ] = session_container
     
     self._session_container_names.add( session_container_name )
     self._dirty_session_container_names.add( session_container_name )
     
     self._SetDirty()