コード例 #1
0
 def _callbackParseGETArgs( self, request ):
     
     hydrus_args = {}
     
     for name in request.args:
         
         values = request.args[ name ]
         
         value = values[0]
         
         if name in ( 'begin', 'expires', 'lifetime', 'num', 'service_type', 'service_port', 'since', 'subindex', 'timespan' ):
             
             try: hydrus_args[ name ] = int( value )
             except: raise HydrusExceptions.ForbiddenException( 'I was expecting to parse \'' + name + '\' as an integer, but it failed.' )
             
         elif name in ( 'access_key', 'title', 'subject_account_key', 'contact_key', 'hash', 'subject_hash', 'subject_tag', 'message_key', 'share_key' ):
             
             try: hydrus_args[ name ] = value.decode( 'hex' )
             except: raise HydrusExceptions.ForbiddenException( 'I was expecting to parse \'' + name + '\' as a hex-encoded string, but it failed.' )
             
         
     
     if 'subject_account_key' in hydrus_args:
         
         hydrus_args[ 'subject_identifier' ] = HydrusData.AccountIdentifier( account_key = hydrus_args[ 'subject_account_key' ] )
         
     elif 'subject_hash' in hydrus_args:
         
         hash = hydrus_args[ 'subject_hash' ]
         
         if 'subject_tag' in hydrus_args:
             
             tag = hydrus_args[ 'subject_tag' ]
             
             content = HydrusData.Content( HC.CONTENT_TYPE_MAPPING, ( tag, hash ) )
             
         else:
             
             content = HydrusData.Content( HC.CONTENT_TYPE_FILES, [ hash ] )
             
         
         hydrus_args[ 'subject_identifier' ] = HydrusData.AccountIdentifier( content = content )
         
     
     request.hydrus_args = hydrus_args
     
     return request
コード例 #2
0
    def _test_repo(self, service, host, port):

        service_key = service.GetServiceKey()

        # news

        news = 'this is the news'

        service.Request(HC.POST, 'news', {'news': news})

        written = HydrusGlobals.test_controller.GetWrite('news')

        [(args, kwargs)] = written

        (written_service_key, written_news) = args

        self.assertEqual(news, written_news)

        # num_petitions

        num_petitions = 23

        HydrusGlobals.test_controller.SetRead('num_petitions', num_petitions)

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

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

        # petition

        action = HC.CONTENT_UPDATE_PETITION
        account_identifier = HydrusData.AccountIdentifier(
            account_key=HydrusData.GenerateKey())
        reason = 'it sucks'
        contents = [
            HydrusData.Content(HC.CONTENT_TYPE_FILES,
                               [HydrusData.GenerateKey() for i in range(10)])
        ]

        petition = HydrusData.ServerToClientPetition(
            action=action,
            petitioner_account_identifier=account_identifier,
            reason=reason,
            contents=contents)

        HydrusGlobals.test_controller.SetRead('petition', petition)

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

        self.assertEqual(type(response), HydrusData.ServerToClientPetition)

        # update

        begin = 100
        subindex_count = 5

        update = HydrusData.ServerToClientServiceUpdatePackage()
        update.SetBeginEnd(begin, begin + HC.UPDATE_DURATION - 1)
        update.SetSubindexCount(subindex_count)

        path = ServerFiles.GetExpectedServiceUpdatePackagePath(
            service_key, begin)

        with open(path, 'wb') as f:
            f.write(update.DumpToNetworkString())

        response = service.Request(HC.GET, 'service_update_package',
                                   {'begin': begin})

        self.assertEqual(response.GetBegin(), update.GetBegin())

        try:
            os.remove(path)
        except:
            pass

        subindex = 2
        num_hashes = 12
        tag = 'series:blah'
        hash_ids_to_hashes = {i: HydrusData.GenerateKey() for i in range(12)}
        rows = [(tag, [i for i in range(num_hashes)])]

        update = HydrusData.ServerToClientContentUpdatePackage()
        update.AddContentData(HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD,
                              rows, hash_ids_to_hashes)

        path = ServerFiles.GetExpectedContentUpdatePackagePath(
            service_key, begin, subindex)

        with open(path, 'wb') as f:
            f.write(update.DumpToNetworkString())

        response = service.Request(HC.GET, 'content_update_package', {
            'begin': begin,
            'subindex': subindex
        })

        self.assertEqual(response.GetNumContentUpdates(),
                         update.GetNumContentUpdates())

        try:
            os.remove(path)
        except:
            pass

        update = HydrusData.ClientToServerContentUpdatePackage(
            {}, hash_ids_to_hashes)

        service.Request(HC.POST, 'content_update_package', {'update': update})

        written = HydrusGlobals.test_controller.GetWrite('update')

        [(args, kwargs)] = written

        (written_service_key, written_account, written_update) = args

        self.assertEqual(update.GetHashes(), written_update.GetHashes())