-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When using Attachments, we need to properly encode them into json to share them outside. OutputToJSON handled this correctly, but both MfgInspector and StationServer did not. Move TestRecordEncoder into openhtf.util.json, so that it can be shared by all three. For station_server, patch the tornado json encoder, so that all values being sent to the client through tornado are properly encoded.
- Loading branch information
Jonathan Van Eenwyk
committed
Aug 19, 2020
1 parent
95f15c4
commit 1ead92c
Showing
4 changed files
with
37 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import base64 | ||
import json | ||
|
||
from openhtf.core import test_record | ||
|
||
|
||
class TestRecordEncoder(json.JSONEncoder): | ||
"""JSON encoder that supports Attachments.""" | ||
def default(self, obj): | ||
if isinstance(obj, test_record.Attachment): | ||
dct = obj._asdict() | ||
dct['data'] = base64.standard_b64encode(obj.data).decode('utf-8') | ||
return dct | ||
return super(TestRecordEncoder, self).default(obj) |