Example #1
0
 def _topLevelObjectGenerator(self, request, numObjects, getByIndexMethod):
     """
     Returns a generator over the results for the specified request, which
     is over a set of objects of the specified size. The objects are
     returned by call to the specified method, which must take a single
     integer as an argument. The returned generator yields a sequence of
     (object, nextPageToken) pairs, which allows this iteration to be picked
     up at any point.
     """
     currentIndex = 0
     if request.page_token:
         currentIndex, = paging._parsePageToken(request.page_token, 1)
     while currentIndex < numObjects:
         object_ = getByIndexMethod(currentIndex)
         currentIndex += 1
         nextPageToken = None
         if currentIndex < numObjects:
             nextPageToken = str(currentIndex)
         yield object_.toProtocolElement(), nextPageToken
Example #2
0
    def runListReferenceBases(self, requestJson):
        """
        Runs a listReferenceBases request for the specified ID and
        request arguments.
        """
        # In the case when an empty post request is made to the endpoint
        # we instantiate an empty ListReferenceBasesRequest.
        if not requestJson:
            request = protocol.ListReferenceBasesRequest()
        else:
            try:
                request = protocol.fromJson(
                    requestJson,
                    protocol.ListReferenceBasesRequest)
            except protocol.json_format.ParseError:
                raise exceptions.InvalidJsonException(requestJson)
        compoundId = datamodel.ReferenceCompoundId.parse(request.reference_id)
        referenceSet = self.getDataRepository().getReferenceSet(
            compoundId.reference_set_id)
        reference = referenceSet.getReference(request.reference_id)
        start = request.start
        end = request.end
        if end == 0:  # assume meant "get all"
            end = reference.getLength()
        if request.page_token:
            pageTokenStr = request.page_token
            start = paging._parsePageToken(pageTokenStr, 1)[0]

        chunkSize = self._maxResponseLength
        nextPageToken = None
        if start + chunkSize < end:
            end = start + chunkSize
            nextPageToken = str(start + chunkSize)
        sequence = reference.getBases(start, end)

        # build response
        response = protocol.ListReferenceBasesResponse()
        response.offset = start
        response.sequence = sequence
        if nextPageToken:
            response.next_page_token = nextPageToken
        return protocol.toJson(response)
Example #3
0
 def _topLevelObjectGenerator(self, request, numObjects, getByIndexMethod):
     """
     Returns a generator over the results for the specified request, which
     is over a set of objects of the specified size. The objects are
     returned by call to the specified method, which must take a single
     integer as an argument. The returned generator yields a sequence of
     (object, nextPageToken) pairs, which allows this iteration to be picked
     up at any point.
     """
     currentIndex = 0
     if request.page_token:
         currentIndex, = paging._parsePageToken(
             request.page_token, 1)
     while currentIndex < numObjects:
         object_ = getByIndexMethod(currentIndex)
         currentIndex += 1
         nextPageToken = None
         if currentIndex < numObjects:
             nextPageToken = str(currentIndex)
         yield object_.toProtocolElement(), nextPageToken
Example #4
0
    def runListReferenceBases(self, requestJson):
        """
        Runs a listReferenceBases request for the specified ID and
        request arguments.
        """
        # In the case when an empty post request is made to the endpoint
        # we instantiate an empty ListReferenceBasesRequest.
        if not requestJson:
            request = protocol.ListReferenceBasesRequest()
        else:
            try:
                request = protocol.fromJson(requestJson,
                                            protocol.ListReferenceBasesRequest)
            except protocol.json_format.ParseError:
                raise exceptions.InvalidJsonException(requestJson)
        compoundId = datamodel.ReferenceCompoundId.parse(request.reference_id)
        referenceSet = self.getDataRepository().getReferenceSet(
            compoundId.reference_set_id)
        reference = referenceSet.getReference(request.reference_id)
        start = request.start
        end = request.end
        if end == 0:  # assume meant "get all"
            end = reference.getLength()
        if request.page_token:
            pageTokenStr = request.page_token
            start = paging._parsePageToken(pageTokenStr, 1)[0]

        chunkSize = self._maxResponseLength
        nextPageToken = None
        if start + chunkSize < end:
            end = start + chunkSize
            nextPageToken = str(start + chunkSize)
        sequence = reference.getBases(start, end)

        # build response
        response = protocol.ListReferenceBasesResponse()
        response.offset = start
        response.sequence = sequence
        if nextPageToken:
            response.next_page_token = nextPageToken
        return protocol.toJson(response)
Example #5
0
 def testParsePageToken(self):
     goodPageToken = "12:34:567:8:9000"
     parsedToken = paging._parsePageToken(goodPageToken, 5)
     self.assertEqual(parsedToken[2], 567)
Example #6
0
 def testParsePageToken(self):
     goodPageToken = "12:34:567:8:9000"
     parsedToken = paging._parsePageToken(goodPageToken, 5)
     self.assertEqual(parsedToken[2], 567)