Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Document factory constructors on language client #2164

Merged
merged 5 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')

This comment was marked as spam.

This comment was marked as spam.

>>> 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