Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Commit 92bd8ac

Browse files
committed
google-assistant-sdk: use temporary file for html output
- write html content to file instead of using base64 encoded data-uri since content can be too long - add tests for html output Change-Id: Ia67114fa29ec54c54de8c27ddd3b085a8ed4d49f
1 parent 5b8acea commit 92bd8ac

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

google-assistant-sdk/googlesamples/assistant/grpc/browser_helpers.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import base64
15+
import os.path
16+
import tempfile
17+
import webbrowser
1618

19+
ASSISTANT_HTML_FILE = 'google-assistant-sdk-screen-out.html'
1720

18-
def to_data_uri(html):
19-
return ('data:text/html;charset=utf-8;base64,%s'
20-
% base64.b64encode(html).decode('utf-8'))
21+
22+
class SystemBrowser(object):
23+
def __init__(self):
24+
self.tempdir = tempfile.mkdtemp()
25+
self.filename = os.path.join(self.tempdir, ASSISTANT_HTML_FILE)
26+
27+
def display(self, html):
28+
with open(self.filename, 'wb') as f:
29+
f.write(html)
30+
webbrowser.open(self.filename, new=0)
31+
32+
33+
system_browser = SystemBrowser()

google-assistant-sdk/googlesamples/assistant/grpc/textinput.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import os
1818
import logging
1919
import json
20-
import webbrowser
2120

2221
import click
2322
import google.auth.transport.grpc
@@ -146,7 +145,8 @@ def iter_assist_requests():
146145
default='en-US',
147146
help='Language code of the Assistant')
148147
@click.option('--display', is_flag=True, default=False,
149-
help='Enable visual display of Assistant Response.')
148+
help='Enable visual display of Assistant '
149+
'rich media responses (for certain queries).')
150150
@click.option('--verbose', '-v', is_flag=True, default=False,
151151
help='Verbose logging.')
152152
@click.option('--grpc-deadline', default=DEFAULT_GRPC_DEADLINE,
@@ -183,7 +183,8 @@ def main(api_endpoint, credentials,
183183
click.echo('<you> %s' % query)
184184
response_text, response_html = assistant.assist(text_query=query)
185185
if display and response_html:
186-
webbrowser.open(browser_helpers.to_data_uri(response_html))
186+
system_browser = browser_helpers.system_browser
187+
system_browser.display(response_html)
187188
if response_text:
188189
click.echo('<@assistant> %s' % response_text)
189190

google-assistant-sdk/tests/test_endtoend.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,32 @@ def test_endtoend_textinput(device_model, device_instance):
136136
assert 'pamplemousse' in out
137137

138138

139+
def test_endtoend_htmloutput(device_model, device_instance):
140+
temp_dir = tempfile.mkdtemp()
141+
env = os.environ.copy()
142+
env['TMPDIR'] = temp_dir
143+
p = subprocess.Popen(['python', '-m',
144+
'googlesamples.assistant.grpc.textinput',
145+
'--verbose',
146+
'--device-model-id', device_model,
147+
'--device-id', device_instance,
148+
'--display'],
149+
stdin=subprocess.PIPE,
150+
stdout=subprocess.PIPE,
151+
env=env)
152+
out, err = p.communicate(b'how do you say grapefruit in French?')
153+
print(out)
154+
out = builtins.str(out).lower()
155+
assert err is None
156+
assert 'grapefruit' in out
157+
files = [os.path.join(path, f)
158+
for path, _, fs in os.walk(temp_dir) for f in fs]
159+
assert len(files) == 1
160+
assert os.path.basename(files[0]) == 'google-assistant-sdk-screen-out.html'
161+
with open(files[0], 'r') as f:
162+
assert 'pamplemousse' in f.read()
163+
164+
139165
def test_endtoend_audiofileinput(device_model, device_instance):
140166
temp_dir = tempfile.mkdtemp()
141167
audio_out_file = os.path.join(temp_dir, 'out.raw')

0 commit comments

Comments
 (0)