Skip to content

Commit

Permalink
Adding document_from_blob() factory to language client.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Aug 23, 2016
1 parent 2bf871a commit 4c0f6df
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/language-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ to content stored in `Google Cloud Storage`_. We can use the

.. code-block:: python
>>> document = client.document_from_blob(bucket='my-text-bucket',
... blob='sentiment-me.txt')
>>> document = client.document_from_blob('my-text-bucket',
... 'sentiment-me.txt')
>>> document.gcs_url
'gs://my-text-bucket/sentiment-me.txt'
>>> document.doc_type == language.Document.PLAIN_TEXT
Expand Down
31 changes: 30 additions & 1 deletion gcloud/language/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ def document_from_url(self, gcs_url,
:class:`Document` constructor.
:rtype: :class:`Document`
:returns: A plain-text document bound to this client.
:returns: A document bound to this client.
"""
return Document(self, gcs_url=gcs_url, doc_type=doc_type, **kwargs)

def document_from_blob(self, bucket_name, blob_name,
doc_type=Document.PLAIN_TEXT, **kwargs):
"""Create a Cloud Storage document bound to this client.
:type bucket_name: str
:param bucket_name: The name of the bucket that contains the
document text.
:type blob_name: str
:param blob_name: The name of the blob (within the bucket) that
contains document text.
:type doc_type: str
:param doc_type: (Optional) The type of text in the document.
Defaults to plain text. Can also be specified
as HTML via :attr:`~.Document.HTML`.
:type kwargs: dict
:param kwargs: Remaining keyword arguments to be passed along to the
:class:`Document` constructor.
:rtype: :class:`Document`
:returns: A document bound to this client.
"""
# NOTE: We assume that the bucket and blob name don't
# need to be URL-encoded.
gcs_url = 'gs://%s/%s' % (bucket_name, blob_name)
return self.document_from_url(gcs_url, doc_type=doc_type, **kwargs)
17 changes: 17 additions & 0 deletions gcloud/language/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ def test_document_from_url_factory_explicit(self):
self.assertEqual(document.doc_type, Document.HTML)
self.assertEqual(document.encoding, encoding)

def test_document_from_blob_factory(self):
from gcloud.language.document import Document

creds = _Credentials()
client = self._makeOne(project='PROJECT',
credentials=creds, http=object())

bucket_name = 'my-text-bucket'
blob_name = 'sentiment-me.txt'
gcs_url = 'gs://%s/%s' % (bucket_name, blob_name)
document = client.document_from_blob(bucket_name, blob_name)
self.assertIsInstance(document, Document)
self.assertIs(document.client, client)
self.assertIsNone(document.content)
self.assertEqual(document.gcs_url, gcs_url)
self.assertEqual(document.doc_type, Document.PLAIN_TEXT)


class _Credentials(object):

Expand Down

0 comments on commit 4c0f6df

Please sign in to comment.