Example #1
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_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 )
Example #3
0
    def _GetJob(self, for_login=False):

        job = ClientNetworkingJobs.NetworkJobHydrus(MOCK_HYDRUS_SERVICE_KEY,
                                                    'GET', MOCK_HYDRUS_URL)

        job.SetForLogin(for_login)

        mock_controller = TestController.MockController()

        mock_service = ClientServices.GenerateService(MOCK_HYDRUS_SERVICE_KEY,
                                                      HC.TAG_REPOSITORY,
                                                      'test tag repo')

        mock_services_manager = TestController.MockServicesManager(
            (mock_service, ))

        mock_controller.services_manager = mock_services_manager

        bandwidth_manager = ClientNetworkingBandwidth.NetworkBandwidthManager()
        session_manager = ClientNetworkingSessions.NetworkSessionManager()
        domain_manager = ClientNetworkingDomain.NetworkDomainManager()
        login_manager = ClientNetworkingLogin.NetworkLoginManager()

        engine = ClientNetworking.NetworkEngine(mock_controller,
                                                bandwidth_manager,
                                                session_manager,
                                                domain_manager, login_manager)

        job.engine = engine

        return job
    def _InitCaches(self):

        if self._c.execute('SELECT 1 FROM sqlite_master WHERE name = ?;',
                           ('services', )).fetchone() is not None:

            all_data = self._c.execute(
                'SELECT service_id, service_key, service_type, name, dictionary_string FROM services;'
            ).fetchall()

            for (service_id, service_key, service_type, name,
                 dictionary_string) in all_data:

                dictionary = HydrusSerialisable.CreateFromString(
                    dictionary_string)

                service = ClientServices.GenerateService(
                    service_key, service_type, name, dictionary)

                self._service_ids_to_services[service_id] = service

                self._service_keys_to_service_ids[service_key] = service_id

            self.local_file_service_id = self.GetServiceId(
                CC.LOCAL_FILE_SERVICE_KEY)
            self.local_update_service_id = self.GetServiceId(
                CC.LOCAL_UPDATE_SERVICE_KEY)
            self.trash_service_id = self.GetServiceId(CC.TRASH_SERVICE_KEY)
            self.combined_local_file_service_id = self.GetServiceId(
                CC.COMBINED_LOCAL_FILE_SERVICE_KEY)
            self.combined_file_service_id = self.GetServiceId(
                CC.COMBINED_FILE_SERVICE_KEY)
            self.combined_tag_service_id = self.GetServiceId(
                CC.COMBINED_TAG_SERVICE_KEY)
Example #5
0
 def _InitCaches( self ):
     
     if self._Execute( 'SELECT 1 FROM sqlite_master WHERE name = ?;', ( 'services', ) ).fetchone() is not None:
         
         all_data = self._Execute( 'SELECT service_id, service_key, service_type, name, dictionary_string FROM services;' ).fetchall()
         
         for ( service_id, service_key, service_type, name, dictionary_string ) in all_data:
             
             dictionary = HydrusSerialisable.CreateFromString( dictionary_string )
             
             service = ClientServices.GenerateService( service_key, service_type, name, dictionary )
             
             self._service_ids_to_services[ service_id ] = service
             
             self._service_keys_to_service_ids[ service_key ] = service_id
             
         
         self.local_update_service_id = self.GetServiceId( CC.LOCAL_UPDATE_SERVICE_KEY )
         self.trash_service_id = self.GetServiceId( CC.TRASH_SERVICE_KEY )
         self.combined_local_file_service_id = self.GetServiceId( CC.COMBINED_LOCAL_FILE_SERVICE_KEY )
         self.combined_file_service_id = self.GetServiceId( CC.COMBINED_FILE_SERVICE_KEY )
         
         try:
             
             self.combined_deleted_file_service_id = self.GetServiceId( CC.COMBINED_DELETED_FILE_SERVICE_KEY )
             self.combined_local_media_service_id = self.GetServiceId( CC.COMBINED_LOCAL_MEDIA_SERVICE_KEY )
             
         except HydrusExceptions.DataMissing:
             
             # version 465/486 it might not be in yet
             
             pass
             
         
         self.combined_tag_service_id = self.GetServiceId( CC.COMBINED_TAG_SERVICE_KEY )
Example #6
0
 def _SetServices( self, services: typing.Iterable[ ClientServices.Service ] ):
     
     self._keys_to_services = { service.GetServiceKey() : service for service in services }
     
     self._keys_to_services[ CC.TEST_SERVICE_KEY ] = ClientServices.GenerateService( CC.TEST_SERVICE_KEY, HC.TEST_SERVICE, 'test service' )
     
     key = lambda s: s.GetName()
     
     self._services_sorted = list( services )
     self._services_sorted.sort( key = key )
 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 )
Example #8
0
    def AddService(self, service_key, service_type, name,
                   dictionary: HydrusSerialisable.SerialisableBase) -> int:

        dictionary_string = dictionary.DumpToString()

        self._Execute(
            'INSERT INTO services ( service_key, service_type, name, dictionary_string ) VALUES ( ?, ?, ?, ? );',
            (sqlite3.Binary(service_key), service_type, name,
             dictionary_string))

        service_id = self._GetLastRowId()

        service = ClientServices.GenerateService(service_key, service_type,
                                                 name, dictionary)

        self._service_ids_to_services[service_id] = service
        self._service_keys_to_service_ids[service_key] = service_id

        if service_key == CC.LOCAL_UPDATE_SERVICE_KEY:

            self.local_update_service_id = service_id

        elif service_key == CC.TRASH_SERVICE_KEY:

            self.trash_service_id = service_id

        elif service_key == CC.COMBINED_LOCAL_FILE_SERVICE_KEY:

            self.combined_local_file_service_id = service_id

        elif service_key == CC.COMBINED_FILE_SERVICE_KEY:

            self.combined_file_service_id = service_id

        elif service_key == CC.COMBINED_DELETED_FILE_SERVICE_KEY:

            self.combined_deleted_file_service_id = service_id

        elif service_key == CC.COMBINED_TAG_SERVICE_KEY:

            self.combined_tag_service_id = service_id

        return service_id
Example #9
0
    def __init__(self, win, only_run):

        self.app = win
        self.win = win
        self.only_run = only_run

        self.db_dir = tempfile.mkdtemp()

        global DB_DIR

        DB_DIR = self.db_dir

        self._server_files_dir = os.path.join(self.db_dir, 'server_files')
        self._updates_dir = os.path.join(self.db_dir, 'test_updates')

        client_files_default = os.path.join(self.db_dir, 'client_files')

        HydrusPaths.MakeSureDirectoryExists(self._server_files_dir)
        HydrusPaths.MakeSureDirectoryExists(self._updates_dir)
        HydrusPaths.MakeSureDirectoryExists(client_files_default)

        HG.controller = self
        HG.client_controller = self
        HG.server_controller = self
        HG.test_controller = self

        self.db = self
        self.gui = self

        self.frame_splash_status = ClientGUISplash.FrameSplashStatus()

        self._call_to_threads = []

        self._pubsub = HydrusPubSub.HydrusPubSub(self, lambda o: True)

        self.new_options = ClientOptions.ClientOptions()

        HC.options = ClientDefaults.GetClientDefaultOptions()

        self.options = HC.options

        def show_text(text):
            pass

        HydrusData.ShowText = show_text

        self._reads = {}

        self._reads['local_booru_share_keys'] = []
        self._reads['messaging_sessions'] = []
        self._reads['options'] = ClientDefaults.GetClientDefaultOptions()
        self._reads['file_system_predicates'] = []
        self._reads['media_results'] = []

        self._param_reads = {}

        self.example_tag_repo_service_key = HydrusData.GenerateKey()

        services = []

        services.append(
            ClientServices.GenerateService(CC.LOCAL_BOORU_SERVICE_KEY,
                                           HC.LOCAL_BOORU, 'local booru'))
        services.append(
            ClientServices.GenerateService(CC.CLIENT_API_SERVICE_KEY,
                                           HC.CLIENT_API_SERVICE,
                                           'client api'))
        services.append(
            ClientServices.GenerateService(CC.COMBINED_LOCAL_FILE_SERVICE_KEY,
                                           HC.COMBINED_LOCAL_FILE,
                                           'all local files'))
        services.append(
            ClientServices.GenerateService(CC.LOCAL_FILE_SERVICE_KEY,
                                           HC.LOCAL_FILE_DOMAIN, 'my files'))
        services.append(
            ClientServices.GenerateService(CC.TRASH_SERVICE_KEY,
                                           HC.LOCAL_FILE_TRASH_DOMAIN,
                                           'trash'))
        services.append(
            ClientServices.GenerateService(CC.DEFAULT_LOCAL_TAG_SERVICE_KEY,
                                           HC.LOCAL_TAG, 'my tags'))
        services.append(
            ClientServices.GenerateService(self.example_tag_repo_service_key,
                                           HC.TAG_REPOSITORY,
                                           'example tag repo'))
        services.append(
            ClientServices.GenerateService(CC.COMBINED_TAG_SERVICE_KEY,
                                           HC.COMBINED_TAG, 'all known tags'))
        services.append(
            ClientServices.GenerateService(
                LOCAL_RATING_LIKE_SERVICE_KEY, HC.LOCAL_RATING_LIKE,
                'example local rating like service'))
        services.append(
            ClientServices.GenerateService(
                LOCAL_RATING_NUMERICAL_SERVICE_KEY, HC.LOCAL_RATING_NUMERICAL,
                'example local rating numerical service'))

        self._reads['services'] = services

        client_files_locations = {}

        for prefix in HydrusData.IterateHexPrefixes():

            for c in ('f', 't'):

                client_files_locations[c + prefix] = client_files_default

        self._reads['client_files_locations'] = client_files_locations

        self._reads['sessions'] = []
        self._reads['tag_parents'] = {}
        self._reads['tag_siblings_all_ideals'] = {}
        self._reads['in_inbox'] = False

        self._writes = collections.defaultdict(list)

        self._managers = {}

        self.column_list_manager = ClientGUIListManager.ColumnListManager()

        self.services_manager = ClientServices.ServicesManager(self)
        self.client_files_manager = ClientFiles.ClientFilesManager(self)

        self.parsing_cache = ClientCaches.ParsingCache()

        bandwidth_manager = ClientNetworkingBandwidth.NetworkBandwidthManager()
        session_manager = ClientNetworkingSessions.NetworkSessionManager()
        domain_manager = ClientNetworkingDomain.NetworkDomainManager()

        ClientDefaults.SetDefaultDomainManagerData(domain_manager)

        login_manager = ClientNetworkingLogin.NetworkLoginManager()

        self.network_engine = ClientNetworking.NetworkEngine(
            self, bandwidth_manager, session_manager, domain_manager,
            login_manager)

        self.CallToThreadLongRunning(self.network_engine.MainLoop)

        self.tag_display_manager = ClientTagsHandling.TagDisplayManager()

        self._managers['undo'] = ClientManagers.UndoManager(self)
        self.server_session_manager = HydrusSessions.HydrusSessionManagerServer(
        )

        self.bitmap_manager = ClientManagers.BitmapManager(self)

        self.local_booru_manager = ClientCaches.LocalBooruCache(self)
        self.client_api_manager = ClientAPI.APIManager()

        self._cookies = {}

        self._job_scheduler = HydrusThreading.JobScheduler(self)

        self._job_scheduler.start()