def test_set_msg_nested_key():
    rule = http_pb2.HttpRule(custom=http_pb2.CustomHttpPattern(kind="foo", path="bar"))

    protobuf_helpers.set(rule, "custom.kind", "baz")

    assert rule.custom.kind == "baz"
    assert rule.custom.path == "bar"
def test_set_msg_with_dict_field():
    rule = http_pb2.HttpRule()
    pattern = {"kind": "foo", "path": "bar"}

    protobuf_helpers.set(rule, "custom", pattern)

    assert rule.custom.kind == "foo"
    assert rule.custom.path == "bar"
Beispiel #3
0
def test_set_msg_with_msg_field():
    rule = http_pb2.HttpRule()
    pattern = http_pb2.CustomHttpPattern(kind='foo', path='bar')

    protobuf_helpers.set(rule, 'custom', pattern)

    assert rule.custom.kind == 'foo'
    assert rule.custom.path == 'bar'
Beispiel #4
0
def test_set_msg_with_dict_field():
    rule = http_pb2.HttpRule()
    pattern = {'kind': 'foo', 'path': 'bar'}

    protobuf_helpers.set(rule, 'custom', pattern)

    assert rule.custom.kind == 'foo'
    assert rule.custom.path == 'bar'
Beispiel #5
0
def test_set_msg_nested_key():
    rule = http_pb2.HttpRule(
        custom=http_pb2.CustomHttpPattern(kind='foo', path='bar'))

    protobuf_helpers.set(rule, 'custom.kind', 'baz')

    assert rule.custom.kind == 'baz'
    assert rule.custom.path == 'bar'
def test_set_msg_with_msg_field():
    rule = http_pb2.HttpRule()
    pattern = http_pb2.CustomHttpPattern(kind='foo', path='bar')

    protobuf_helpers.set(rule, 'custom', pattern)

    assert rule.custom.kind == 'foo'
    assert rule.custom.path == 'bar'
def test_set_msg_with_msg_field():
    rule = http_pb2.HttpRule()
    pattern = http_pb2.CustomHttpPattern(kind="foo", path="bar")

    protobuf_helpers.set(rule, "custom", pattern)

    assert rule.custom.kind == "foo"
    assert rule.custom.path == "bar"
def test_set_msg_with_dict_field():
    rule = http_pb2.HttpRule()
    pattern = {"kind": "foo", "path": "bar"}

    protobuf_helpers.set(rule, "custom", pattern)

    assert rule.custom.kind == "foo"
    assert rule.custom.path == "bar"
def test_set_msg_with_dict_field():
    rule = http_pb2.HttpRule()
    pattern = {'kind': 'foo', 'path': 'bar'}

    protobuf_helpers.set(rule, 'custom', pattern)

    assert rule.custom.kind == 'foo'
    assert rule.custom.path == 'bar'
def test_set_msg_nested_key():
    rule = http_pb2.HttpRule(
        custom=http_pb2.CustomHttpPattern(kind='foo', path='bar'))

    protobuf_helpers.set(rule, 'custom.kind', 'baz')

    assert rule.custom.kind == 'baz'
    assert rule.custom.path == 'bar'
def test_set_msg_with_msg_field():
    rule = http_pb2.HttpRule()
    pattern = http_pb2.CustomHttpPattern(kind="foo", path="bar")

    protobuf_helpers.set(rule, "custom", pattern)

    assert rule.custom.kind == "foo"
    assert rule.custom.path == "bar"
def test_set_msg_nested_key():
    rule = http_pb2.HttpRule(
        custom=http_pb2.CustomHttpPattern(kind="foo", path="bar"))

    protobuf_helpers.set(rule, "custom.kind", "baz")

    assert rule.custom.kind == "baz"
    assert rule.custom.path == "bar"
def test_set_list_clear_existing():
    list_ops_response = operations_pb2.ListOperationsResponse(
        operations=[{'name': 'baz'}],
    )

    protobuf_helpers.set(list_ops_response, 'operations', [
        {'name': 'foo'},
        operations_pb2.Operation(name='bar'),
    ])

    assert len(list_ops_response.operations) == 2
    for operation in list_ops_response.operations:
        assert isinstance(operation, operations_pb2.Operation)
    assert list_ops_response.operations[0].name == 'foo'
    assert list_ops_response.operations[1].name == 'bar'
def test_set_list_clear_existing():
    list_ops_response = operations_pb2.ListOperationsResponse(
        operations=[{"name": "baz"}]
    )

    protobuf_helpers.set(
        list_ops_response,
        "operations",
        [{"name": "foo"}, operations_pb2.Operation(name="bar")],
    )

    assert len(list_ops_response.operations) == 2
    for operation in list_ops_response.operations:
        assert isinstance(operation, operations_pb2.Operation)
    assert list_ops_response.operations[0].name == "foo"
    assert list_ops_response.operations[1].name == "bar"
Beispiel #15
0
    def annotate_image(self,
                       request,
                       *,
                       retry=None,
                       timeout=None,
                       metadata=()):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.AnnotateImageRequest`)
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            :class:`~.vision_v1.AnnotateImageResponse` The API response.
        """
        if not isinstance(request, proto.Message):
            # If the image is a file handler, set the content.
            image = protobuf.get(request, "image")
            if not isinstance(image, proto.Message):
                if hasattr(image, "read"):
                    img_bytes = image.read()
                    protobuf.set(request, "image", {})
                    protobuf.set(request, "image.content", img_bytes)
                    image = protobuf.get(request, "image")

                # If a filename is provided, read the file.
                filename = protobuf.get(image, "source.filename", default=None)
                if filename:
                    with open(filename, "rb") as img_file:
                        protobuf.set(request, "image.content", img_file.read())
                        protobuf.set(request, "image.source", None)

        # This method allows features not to be specified, and you get all
        # of them.
        if not isinstance(request, proto.Message):
            protobuf.setdefault(request, "features", self._get_all_features())
        elif len(request.features) == 0:
            request.features = self._get_all_features()
        r = self.batch_annotate_images(requests=[request],
                                       retry=retry,
                                       timeout=timeout,
                                       metadata=metadata)
        return r.responses[0]
Beispiel #16
0
def test_set_list():
    list_ops_response = operations_pb2.ListOperationsResponse()

    protobuf_helpers.set(list_ops_response, 'operations', [
        {
            'name': 'foo'
        },
        operations_pb2.Operation(name='bar'),
    ])

    assert len(list_ops_response.operations) == 2

    for operation in list_ops_response.operations:
        assert isinstance(operation, operations_pb2.Operation)

    assert list_ops_response.operations[0].name == 'foo'
    assert list_ops_response.operations[1].name == 'bar'
def test_set_list():
    list_ops_response = operations_pb2.ListOperationsResponse()

    protobuf_helpers.set(
        list_ops_response,
        "operations",
        [{
            "name": "foo"
        }, operations_pb2.Operation(name="bar")],
    )

    assert len(list_ops_response.operations) == 2

    for operation in list_ops_response.operations:
        assert isinstance(operation, operations_pb2.Operation)

    assert list_ops_response.operations[0].name == "foo"
    assert list_ops_response.operations[1].name == "bar"
Beispiel #18
0
    def annotate_image(self, request, retry=None, timeout=None):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.types.AnnotateImageRequest`)
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.

        Returns:
            :class:`~.vision_v1.types.AnnotateImageResponse` The API response.
        """
        # If the image is a file handler, set the content.
        image = protobuf.get(request, 'image')
        if hasattr(image, 'read'):
            img_bytes = image.read()
            protobuf.set(request, 'image', {})
            protobuf.set(request, 'image.content', img_bytes)
            image = protobuf.get(request, 'image')

        # If a filename is provided, read the file.
        filename = protobuf.get(image, 'source.filename', default=None)
        if filename:
            with io.open(filename, 'rb') as img_file:
                protobuf.set(request, 'image.content', img_file.read())
                protobuf.set(request, 'image.source', None)

        # This method allows features not to be specified, and you get all
        # of them.
        protobuf.setdefault(request, 'features', self._get_all_features())
        r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
        return r.responses[0]
    def annotate_image(self, request, retry=None, timeout=None):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.types.AnnotateImageRequest`)
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.

        Returns:
            :class:`~.vision_v1.types.AnnotateImageResponse` The API response.
        """
        # If the image is a file handler, set the content.
        image = protobuf.get(request, "image")
        if hasattr(image, "read"):
            img_bytes = image.read()
            protobuf.set(request, "image", {})
            protobuf.set(request, "image.content", img_bytes)
            image = protobuf.get(request, "image")

        # If a filename is provided, read the file.
        filename = protobuf.get(image, "source.filename", default=None)
        if filename:
            with io.open(filename, "rb") as img_file:
                protobuf.set(request, "image.content", img_file.read())
                protobuf.set(request, "image.source", None)

        # This method allows features not to be specified, and you get all
        # of them.
        protobuf.setdefault(request, "features", self._get_all_features())
        r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
        return r.responses[0]
    def annotate_image(self, request, retry=None, timeout=None):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.types.AnnotateImageRequest`)
            options (:class:`google.gax.CallOptions`): Overrides the default
                settings for this call, e.g, timeout, retries, etc.

        Returns:
            :class:`~.vision_v1.types.AnnotateImageResponse` The API response.
        """
        # If the image is a file handler, set the content.
        image = protobuf.get(request, 'image')
        if hasattr(image, 'read'):
            img_bytes = image.read()
            protobuf.set(request, 'image', {})
            protobuf.set(request, 'image.content', img_bytes)
            image = protobuf.get(request, 'image')

        # If a filename is provided, read the file.
        filename = protobuf.get(image, 'source.filename', default=None)
        if filename:
            with io.open(filename, 'rb') as img_file:
                protobuf.set(request, 'image.content', img_file.read())
                protobuf.set(request, 'image.source', None)

        # This method allows features not to be specified, and you get all
        # of them.
        protobuf.setdefault(request, 'features', self._get_all_features())
        r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
        return r.responses[0]
Beispiel #21
0
    def annotate_image(self, request, retry=None, timeout=None):
        """Run image detection and annotation for an image.

        Example:
            >>> from google.cloud.vision_v1 import ImageAnnotatorClient
            >>> client = ImageAnnotatorClient()
            >>> request = {
            ...     'image': {
            ...         'source': {'image_uri': 'https://foo.com/image.jpg'},
            ...     },
            ... }
            >>> response = client.annotate_image(request)

        Args:
            request (:class:`~.vision_v1.types.AnnotateImageRequest`)
            options (:class:`google.gax.CallOptions`): Overrides the default
                settings for this call, e.g, timeout, retries, etc.

        Returns:
            :class:`~.vision_v1.types.AnnotateImageResponse` The API response.
        """
        # If the image is a file handler, set the content.
        image = protobuf.get(request, 'image')
        if hasattr(image, 'read'):
            img_bytes = image.read()
            protobuf.set(request, 'image', {})
            protobuf.set(request, 'image.content', img_bytes)
            image = protobuf.get(request, 'image')

        # If a filename is provided, read the file.
        filename = protobuf.get(image, 'source.filename', default=None)
        if filename:
            with io.open(filename, 'rb') as img_file:
                protobuf.set(request, 'image.content', img_file.read())
                protobuf.set(request, 'image.source', None)

        # This method allows features not to be specified, and you get all
        # of them.
        protobuf.setdefault(request, 'features', self._get_all_features())
        r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
        return r.responses[0]
def test_set_invalid_object():
    with pytest.raises(TypeError):
        protobuf_helpers.set(object(), "foo", "bar")
def test_set_invalid_object():
    with pytest.raises(TypeError):
        protobuf_helpers.set(object(), 'foo', 'bar')
def test_set_dict_nested():
    mapping = {}
    protobuf_helpers.set(mapping, 'foo.bar', 'baz')
    assert mapping == {'foo': {'bar': 'baz'}}
def test_set_msg():
    msg = timestamp_pb2.Timestamp()
    protobuf_helpers.set(msg, 'seconds', 42)
    assert msg.seconds == 42
Beispiel #26
0
def test_set_msg():
    msg = timestamp_pb2.Timestamp()
    protobuf_helpers.set(msg, 'seconds', 42)
    assert msg.seconds == 42
def test_set_invalid_object():
    with pytest.raises(TypeError):
        protobuf_helpers.set(object(), "foo", "bar")
def test_set_dict():
    mapping = {}
    protobuf_helpers.set(mapping, "foo", "bar")
    assert mapping == {"foo": "bar"}
def test_set_dict_nested():
    mapping = {}
    protobuf_helpers.set(mapping, "foo.bar", "baz")
    assert mapping == {"foo": {"bar": "baz"}}
def test_set_dict():
    mapping = {}
    protobuf_helpers.set(mapping, "foo", "bar")
    assert mapping == {"foo": "bar"}
Beispiel #31
0
def test_set_invalid_object():
    with pytest.raises(TypeError):
        protobuf_helpers.set(object(), 'foo', 'bar')
Beispiel #32
0
def test_set_dict_nested():
    mapping = {}
    protobuf_helpers.set(mapping, 'foo.bar', 'baz')
    assert mapping == {'foo': {'bar': 'baz'}}
def test_set_dict_nested():
    mapping = {}
    protobuf_helpers.set(mapping, "foo.bar", "baz")
    assert mapping == {"foo": {"bar": "baz"}}
def test_set_dict():
    mapping = {}
    protobuf_helpers.set(mapping, 'foo', 'bar')
    assert mapping == {'foo': 'bar'}
Beispiel #35
0
def test_set_dict():
    mapping = {}
    protobuf_helpers.set(mapping, 'foo', 'bar')
    assert mapping == {'foo': 'bar'}