Skip to content

Commit 094c071

Browse files
committed
Adding document_from_blob() factory to language client.
1 parent bbd2126 commit 094c071

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

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