Skip to content

Commit

Permalink
Isolate stream property.
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Jan 10, 2017
1 parent bd71305 commit de718b9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion speech/google/cloud/speech/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def streaming_recognize(self, sample, language_code=None,
.cloud_speech_pb2.StreamingRecognizeResponse`
:returns: ``StreamingRecognizeResponse`` instances.
"""
if sample.stream.closed:
if sample.stream is None or sample.stream.closed:
raise ValueError('Stream is closed.')

requests = _stream_requests(sample, language_code=language_code,
Expand Down
12 changes: 7 additions & 5 deletions speech/google/cloud/speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ def __init__(self, credentials=None, http=None, use_gax=None):
_connection_class = Connection
_speech_api = None

def sample(self, content=None, source_uri=None, encoding=None,
def sample(self, content=None, source_uri=None, stream=None, encoding=None,
sample_rate=None):
"""Factory: construct Sample to use when making recognize requests.
:type content: bytes or file
:param content: (Optional) Bytes object containing audio or file-like
object which yields bytes.
:type content: bytes
:param content: (Optional) Bytes object containing audio data.
:type source_uri: str
:param source_uri: (Optional) URI that points to a file that contains
Expand All @@ -78,6 +77,9 @@ def sample(self, content=None, source_uri=None, encoding=None,
supported, which must be specified in the following
format: ``gs://bucket_name/object_name``.
:type stream: file
:param stream: (Optional) File like object to stream.
:type encoding: str
:param encoding: encoding of audio data sent in all RecognitionAudio
messages, can be one of: :attr:`~.Encoding.LINEAR16`,
Expand All @@ -95,7 +97,7 @@ def sample(self, content=None, source_uri=None, encoding=None,
:rtype: :class:`~google.cloud.speech.sample.Sample`
:returns: Instance of ``Sample``.
"""
return Sample(content=content, source_uri=source_uri,
return Sample(content=content, source_uri=source_uri, stream=stream,
encoding=encoding, sample_rate=sample_rate, client=self)

@property
Expand Down
34 changes: 18 additions & 16 deletions speech/google/cloud/speech/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
class Sample(object):
"""Representation of an audio sample to be used with Google Speech API.
:type content: bytes or file
:param content: (Optional) Bytes object containing audio or file-like
object which yields bytes.
:type content: bytes
:param content: (Optional) Bytes object containing audio data.
:type source_uri: str
:param source_uri: (Optional) URI that points to a file that contains
Expand All @@ -33,6 +32,9 @@ class Sample(object):
supported, which must be specified in the following
format: ``gs://bucket_name/object_name``.
:type stream: file
:param stream: (Optional) File like object to stream.
:type encoding: str
:param encoding: encoding of audio data sent in all RecognitionAudio
messages, can be one of: :attr:`~.Encoding.LINEAR16`,
Expand All @@ -53,17 +55,21 @@ class Sample(object):
default_encoding = Encoding.FLAC
default_sample_rate = 16000

def __init__(self, content=None, source_uri=None,
def __init__(self, content=None, source_uri=None, stream=None,
encoding=None, sample_rate=None, client=None):
self._client = client

no_source = content is None and source_uri is None
both_source = content is not None and source_uri is not None
no_source = content is None and source_uri is None and stream is None
both_source = (content is not None
and source_uri is not None
and stream is not None)
if no_source or both_source:
raise ValueError('Supply one of \'content\' or \'source_uri\'')
raise ValueError('Supply one of '
'\'content\', \'source_uri\', \'stream\'')

self._content = content
self._source_uri = source_uri
self._stream = stream

if sample_rate is not None and not 8000 <= sample_rate <= 48000:
raise ValueError('The value of sample_rate must be between 8000'
Expand Down Expand Up @@ -97,13 +103,10 @@ def source_uri(self):
def content(self):
"""Bytes of audio content.
:rtype: bytes or file
:returns: Byte stream of audio content or file like object.
:rtype: bytes
:returns: Byte stream of audio content.
"""
try:
return self._content.read()
except AttributeError:
return self._content
return self._content

@property
def sample_rate(self):
Expand All @@ -121,10 +124,9 @@ def stream(self):
:rtype: file
:returns: File like object to stream.
"""
stream = self._content
if getattr(stream, 'read', None) is None:
if getattr(self._stream, 'read', None) is None:
return None
return stream
return self._stream

@property
def encoding(self):
Expand Down
2 changes: 1 addition & 1 deletion speech/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_stream_requests(self):
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
StreamingRecognizeRequest)

sample = Sample(content=BytesIO(self.AUDIO_CONTENT),
sample = Sample(stream=BytesIO(self.AUDIO_CONTENT),
encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
language_code = 'US-en'
Expand Down
6 changes: 3 additions & 3 deletions speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def speech_api(channel=None):
make_secure_channel=make_channel):
client._speech_api = _gax.GAPICSpeechAPI(client)

sample = client.sample(content=stream,
sample = client.sample(stream=stream,
encoding=Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)

Expand Down Expand Up @@ -593,7 +593,7 @@ def speech_api(channel=None):
make_secure_channel=make_channel):
client._speech_api = _gax.GAPICSpeechAPI(client)

sample = client.sample(content=stream,
sample = client.sample(stream=stream,
encoding=Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)

Expand Down Expand Up @@ -637,7 +637,7 @@ def speech_api(channel=None):
make_secure_channel=make_channel):
client._speech_api = _gax.GAPICSpeechAPI(client)

sample = client.sample(content=stream,
sample = client.sample(stream=stream,
encoding=Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)

Expand Down
27 changes: 15 additions & 12 deletions speech/unit_tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,31 @@ def test_initialize_sample(self):
self.assertEqual(sample.sample_rate, self.SAMPLE_RATE)

def test_content_and_source_uri(self):
from io import BytesIO

with self.assertRaises(ValueError):
self._make_one(content='awefawagaeragere',
source_uri=self.AUDIO_SOURCE_URI)

def test_content_is_file_like(self):
from io import BytesIO
from google.cloud.speech.encoding import Encoding
with self.assertRaises(ValueError):
self._make_one(stream=BytesIO(b'awefawagaeragere'),
source_uri=self.AUDIO_SOURCE_URI)

test_bytes = b'testing 1 2 3 4'
content = BytesIO(test_bytes)
sample = self._make_one(content=content,
encoding=Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
self.assertEqual(sample.content, test_bytes)
with self.assertRaises(ValueError):
self._make_one(content='awefawagaeragere',
stream=BytesIO(b'awefawagaeragere'),
source_uri=self.AUDIO_SOURCE_URI)

def test_stream_property(self):
from io import BytesIO
from google.cloud.speech.encoding import Encoding

content = b'abc 1 2 3'
sample = self._make_one(content=content, encoding=Encoding.FLAC,
data = b'abc 1 2 3 4'
stream = BytesIO(data)
sample = self._make_one(stream=stream, encoding=Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
self.assertIsNone(sample.stream)
self.assertEqual(sample.stream, stream)
self.assertEqual(sample.stream.read(), data)

def test_bytes_converts_to_file_like_object(self):
from google.cloud import speech
Expand Down
2 changes: 1 addition & 1 deletion system_tests/speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _make_async_request(self, content=None, source_uri=None,
def _make_streaming_request(self, file_obj, single_utterance=True,
interim_results=False):
client = Config.CLIENT
sample = client.sample(content=file_obj,
sample = client.sample(stream=file_obj,
encoding=speech.Encoding.LINEAR16,
sample_rate=16000)
return sample.streaming_recognize(single_utterance=single_utterance,
Expand Down

0 comments on commit de718b9

Please sign in to comment.