Skip to content

Commit 9267a85

Browse files
committed
Adding document_from_blob() factory to language client.
1 parent e6f6fae commit 9267a85

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

docs/language-usage.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ to content stored in `Google Cloud Storage`_. We can use the
127127

128128
.. code-block:: python
129129
130-
>>> document = client.document_from_blob(bucket='my-text-bucket',
131-
... blob='sentiment-me.txt')
130+
>>> document = client.document_from_blob('my-text-bucket',
131+
... 'sentiment-me.txt')
132132
>>> document.gcs_url
133133
'gs://my-text-bucket/sentiment-me.txt'
134134
>>> document.doc_type == language.Document.PLAIN_TEXT

gcloud/language/client.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,36 @@ def document_from_url(self, gcs_url,
101101
:class:`Document` constructor.
102102
103103
:rtype: :class:`Document`
104-
:returns: A plain-text document bound to this client.
104+
:returns: A document bound to this client.
105105
"""
106106
return Document(self, gcs_url=gcs_url, doc_type=doc_type, **kwargs)
107+
108+
109+
def document_from_blob(self, bucket_name, blob_name,
110+
doc_type=Document.PLAIN_TEXT, **kwargs):
111+
"""Create a Cloud Storage document bound to this client.
112+
113+
:type bucket_name: str
114+
:param bucket_name: The name of the bucket that contains the
115+
document text.
116+
117+
:type blob_name: str
118+
:param blob_name: The name of the blob (within the bucket) that
119+
contains document text.
120+
121+
:type doc_type: str
122+
:param doc_type: (Optional) The type of text in the document.
123+
Defaults to plain text. Can also be specified
124+
as HTML via :attr:`~.Document.HTML`.
125+
126+
:type kwargs: dict
127+
:param kwargs: Remaining keyword arguments to be passed along to the
128+
:class:`Document` constructor.
129+
130+
:rtype: :class:`Document`
131+
:returns: A document bound to this client.
132+
"""
133+
# NOTE: We assume that the bucket and blob name don't
134+
# need to be URL-encoded.
135+
gcs_url = 'gs://%s/%s' % (bucket_name, blob_name)
136+
return self.document_from_url(gcs_url, doc_type=doc_type, **kwargs)

gcloud/language/test_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ def test_document_from_url_factory_explicit(self):
121121
self.assertEqual(document.doc_type, Document.HTML)
122122
self.assertEqual(document.encoding, encoding)
123123

124+
def test_document_from_blob_factory(self):
125+
from gcloud.language.document import Document
126+
127+
creds = _Credentials()
128+
client = self._makeOne(project='PROJECT',
129+
credentials=creds, http=object())
130+
131+
bucket_name = 'my-text-bucket'
132+
blob_name = 'sentiment-me.txt'
133+
gcs_url = 'gs://%s/%s' % (bucket_name, blob_name)
134+
document = client.document_from_blob(bucket_name, blob_name)
135+
self.assertIsInstance(document, Document)
136+
self.assertIs(document.client, client)
137+
self.assertIsNone(document.content)
138+
self.assertEqual(document.gcs_url, gcs_url)
139+
self.assertEqual(document.doc_type, Document.PLAIN_TEXT)
140+
124141

125142
class _Credentials(object):
126143

0 commit comments

Comments
 (0)