Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
feat: add context manager support in client (#224)
Browse files Browse the repository at this point in the history
- [ ] Regenerate this pull request now.

chore: fix docstring for first attribute of protos

committer: @busunkim96
PiperOrigin-RevId: 401271153

Source-Link: googleapis/googleapis@787f8c9

Source-Link: googleapis/googleapis-gen@81decff
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
  • Loading branch information
gcf-owl-bot[bot] authored Oct 8, 2021
1 parent 63a8d7c commit 8b65652
Show file tree
Hide file tree
Showing 31 changed files with 348 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ async def review_document(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def process_document(
Expand Down Expand Up @@ -615,6 +612,19 @@ def review_document(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.
.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,5 +333,8 @@ def review_document(
)
return self._stubs["review_document"]

def close(self):
self.grpc_channel.close()


__all__ = ("DocumentProcessorServiceGrpcTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,8 @@ def review_document(
)
return self._stubs["review_document"]

def close(self):
return self.grpc_channel.close()


__all__ = ("DocumentProcessorServiceGrpcAsyncIOTransport",)
15 changes: 15 additions & 0 deletions google/cloud/documentai_v1/types/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Style(proto.Message):

class FontSize(proto.Message):
r"""Font size with unit.
Attributes:
size (float):
Font size for the text.
Expand Down Expand Up @@ -162,6 +163,7 @@ class FontSize(proto.Message):

class Page(proto.Message):
r"""A page in a [Document][google.cloud.documentai.v1.Document].
Attributes:
page_number (int):
1-based index for current
Expand Down Expand Up @@ -218,6 +220,7 @@ class Page(proto.Message):

class Dimension(proto.Message):
r"""Dimension for the page.
Attributes:
width (float):
Page width.
Expand All @@ -233,6 +236,7 @@ class Dimension(proto.Message):

class Image(proto.Message):
r"""Rendered image contents for this page.
Attributes:
content (bytes):
Raw byte content of the image.
Expand Down Expand Up @@ -275,6 +279,7 @@ class Matrix(proto.Message):

class Layout(proto.Message):
r"""Visual element describing a layout unit on a page.
Attributes:
text_anchor (google.cloud.documentai_v1.types.Document.TextAnchor):
Text anchor indexing into the
Expand Down Expand Up @@ -391,6 +396,7 @@ class Line(proto.Message):

class Token(proto.Message):
r"""A detected token.
Attributes:
layout (google.cloud.documentai_v1.types.Document.Page.Layout):
[Layout][google.cloud.documentai.v1.Document.Page.Layout]
Expand Down Expand Up @@ -465,6 +471,7 @@ class VisualElement(proto.Message):

class Table(proto.Message):
r"""A table representation similar to HTML table structure.
Attributes:
layout (google.cloud.documentai_v1.types.Document.Page.Layout):
[Layout][google.cloud.documentai.v1.Document.Page.Layout]
Expand All @@ -480,6 +487,7 @@ class Table(proto.Message):

class TableRow(proto.Message):
r"""A row of table cells.
Attributes:
cells (Sequence[google.cloud.documentai_v1.types.Document.Page.Table.TableCell]):
Cells that make up this row.
Expand All @@ -491,6 +499,7 @@ class TableRow(proto.Message):

class TableCell(proto.Message):
r"""A cell representation inside the table.
Attributes:
layout (google.cloud.documentai_v1.types.Document.Page.Layout):
[Layout][google.cloud.documentai.v1.Document.Page.Layout]
Expand Down Expand Up @@ -529,6 +538,7 @@ class TableCell(proto.Message):

class FormField(proto.Message):
r"""A form field detected on the page.
Attributes:
field_name (google.cloud.documentai_v1.types.Document.Page.Layout):
[Layout][google.cloud.documentai.v1.Document.Page.Layout]
Expand Down Expand Up @@ -577,6 +587,7 @@ class FormField(proto.Message):

class DetectedLanguage(proto.Message):
r"""Detected language for a structural component.
Attributes:
language_code (str):
The BCP-47 language code, such as "en-US" or "sr-Latn". For
Expand Down Expand Up @@ -671,6 +682,7 @@ class Entity(proto.Message):

class NormalizedValue(proto.Message):
r"""Parsed and normalized entity value.
Attributes:
money_value (google.type.money_pb2.Money):
Money value. See also:
Expand Down Expand Up @@ -934,6 +946,7 @@ class Parent(proto.Message):

class Revision(proto.Message):
r"""Contains past or forward revisions of this document.
Attributes:
agent (str):
If the change was made by a person specify
Expand All @@ -956,6 +969,7 @@ class Revision(proto.Message):

class HumanReview(proto.Message):
r"""Human Review information of the document.
Attributes:
state (str):
Human review state. e.g. ``requested``, ``succeeded``,
Expand All @@ -982,6 +996,7 @@ class HumanReview(proto.Message):

class TextChange(proto.Message):
r"""This message is used for text changes aka. OCR corrections.
Attributes:
text_anchor (google.cloud.documentai_v1.types.Document.TextAnchor):
Provenance of the correction. Text anchor indexing into the
Expand Down
4 changes: 4 additions & 0 deletions google/cloud/documentai_v1/types/document_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

class RawDocument(proto.Message):
r"""Payload message of raw document content (bytes).
Attributes:
content (bytes):
Inline document content.
Expand All @@ -45,6 +46,7 @@ class RawDocument(proto.Message):

class GcsDocument(proto.Message):
r"""Specifies a document stored on Cloud Storage.
Attributes:
gcs_uri (str):
The Cloud Storage object uri.
Expand All @@ -58,6 +60,7 @@ class GcsDocument(proto.Message):

class GcsDocuments(proto.Message):
r"""Specifies a set of documents on Cloud Storage.
Attributes:
documents (Sequence[google.cloud.documentai_v1.types.GcsDocument]):
The list of documents.
Expand Down Expand Up @@ -111,6 +114,7 @@ class DocumentOutputConfig(proto.Message):

class GcsOutputConfig(proto.Message):
r"""The configuration used when outputting documents.
Attributes:
gcs_uri (str):
The Cloud Storage uri (a directory) of the
Expand Down
10 changes: 9 additions & 1 deletion google/cloud/documentai_v1/types/document_processor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

class ProcessRequest(proto.Message):
r"""Request message for the process document method.
Attributes:
inline_document (google.cloud.documentai_v1.types.Document):
An inline document proto.
Expand All @@ -64,6 +65,7 @@ class ProcessRequest(proto.Message):

class HumanReviewStatus(proto.Message):
r"""The status of human review on a processed document.
Attributes:
state (google.cloud.documentai_v1.types.HumanReviewStatus.State):
The state of human review on the processing
Expand Down Expand Up @@ -94,6 +96,7 @@ class State(proto.Enum):

class ProcessResponse(proto.Message):
r"""Response message for the process document method.
Attributes:
document (google.cloud.documentai_v1.types.Document):
The document payload, will populate fields
Expand All @@ -111,6 +114,7 @@ class ProcessResponse(proto.Message):

class BatchProcessRequest(proto.Message):
r"""Request message for batch process document method.
Attributes:
name (str):
Required. The processor resource name.
Expand All @@ -134,11 +138,13 @@ class BatchProcessRequest(proto.Message):


class BatchProcessResponse(proto.Message):
r"""Response message for batch process document method. """
r"""Response message for batch process document method.
"""


class BatchProcessMetadata(proto.Message):
r"""The long running operation metadata for batch process method.
Attributes:
state (google.cloud.documentai_v1.types.BatchProcessMetadata.State):
The state of the current batch processing.
Expand Down Expand Up @@ -205,6 +211,7 @@ class IndividualProcessStatus(proto.Message):

class ReviewDocumentRequest(proto.Message):
r"""Request message for review document method.
Attributes:
inline_document (google.cloud.documentai_v1.types.Document):
An inline document proto.
Expand Down Expand Up @@ -234,6 +241,7 @@ class Priority(proto.Enum):

class ReviewDocumentResponse(proto.Message):
r"""Response message for review document method.
Attributes:
gcs_destination (str):
The Cloud Storage uri for the human reviewed
Expand Down
1 change: 1 addition & 0 deletions google/cloud/documentai_v1/types/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class NormalizedVertex(proto.Message):

class BoundingPoly(proto.Message):
r"""A bounding polygon for the detected image annotation.
Attributes:
vertices (Sequence[google.cloud.documentai_v1.types.Vertex]):
The bounding polygon vertices.
Expand Down
1 change: 1 addition & 0 deletions google/cloud/documentai_v1/types/operation_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

class CommonOperationMetadata(proto.Message):
r"""The common metadata for long running operations.
Attributes:
state (google.cloud.documentai_v1.types.CommonOperationMetadata.State):
The state of the operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ async def process_document(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def batch_process_documents(
Expand Down Expand Up @@ -485,6 +482,19 @@ def process_document(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.
.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,8 @@ def process_document(
)
return self._stubs["process_document"]

def close(self):
self.grpc_channel.close()


__all__ = ("DocumentUnderstandingServiceGrpcTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,8 @@ def process_document(
)
return self._stubs["process_document"]

def close(self):
return self.grpc_channel.close()


__all__ = ("DocumentUnderstandingServiceGrpcAsyncIOTransport",)
Loading

0 comments on commit 8b65652

Please sign in to comment.