Merge "Remove File type"

This commit is contained in:
Zuul 2020-07-21 14:45:13 +00:00 committed by Gerrit Code Review
commit f85c23fe4c
5 changed files with 2 additions and 123 deletions

View File

@ -16,7 +16,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import cgi
import datetime
import decimal
import json
@ -187,11 +186,6 @@ def from_param(datatype, value):
if datatype is datetime.datetime:
return dateparser.parse(value) if value else None
if datatype is atypes.File:
if isinstance(value, cgi.FieldStorage):
return atypes.File(fieldstorage=value)
return atypes.File(content=value)
if isinstance(datatype, atypes.UserType):
return datatype.frombasetype(
from_param(datatype.basetype, value))

View File

@ -15,6 +15,7 @@
from http import client as http_client
import inspect
import io
import re
import jsonpatch
@ -425,8 +426,7 @@ def vendor_passthru(ident, method, topic, data=None, driver_passthru=False):
if isinstance(return_value, str):
# If unicode, convert to bytes
return_value = return_value.encode('utf-8')
file_ = atypes.File(content=return_value)
api.response.app_iter = static.FileIter(file_.file)
api.response.app_iter = static.FileIter(io.BytesIO(return_value))
# Since we've attached the return value to the response
# object the response body should now be empty.
return_value = None

View File

@ -23,7 +23,6 @@ import base64
import datetime
import decimal
import inspect
import io
import re
import weakref
@ -700,52 +699,6 @@ class Base(metaclass=BaseMeta):
setattr(self, key, value)
class File(Base):
"""A complex type that represents a file.
In the particular case of protocol accepting form encoded data as
input, File can be loaded from a form file field.
"""
#: The file name
filename = str
#: Mime type of the content
contenttype = str
def _get_content(self):
if self._content is None and self._file:
self._content = self._file.read()
return self._content
def _set_content(self, value):
self._content = value
self._file = None
#: File content
content = wsproperty(binary, _get_content, _set_content)
def __init__(self, filename=None, file=None, content=None,
contenttype=None, fieldstorage=None):
self.filename = filename
self.contenttype = contenttype
self._file = file
self._content = content
if fieldstorage is not None:
if fieldstorage.file:
self._file = fieldstorage.file
self.filename = fieldstorage.filename
self.contenttype = str(fieldstorage.type)
else:
self._content = fieldstorage.value
@property
def file(self):
if self._file is None and self._content:
self._file = io.BytesIO(self._content)
return self._file
class Response(object):
"""Object to hold the "response" from a view function"""
def __init__(self, obj, status_code=None, error=None,

View File

@ -258,12 +258,6 @@ class TestArgs(test_base.TestCase):
)
self.assertIsNone(args.from_param(datetime.datetime, None))
# file param
self.assertEqual(
b'foo',
args.from_param(atypes.File, b'foo').content
)
# usertype param
self.assertEqual(
['0', '1', '2', 'three'],

View File

@ -474,68 +474,6 @@ Value: 'v3'. Value should be one of: v., v.",
assert isinstance(a.datatype, list)
assert a.datatype[0] is int
def test_file_get_content_by_reading(self):
class buffer:
def read(self):
return 'abcdef'
f = types.File(file=buffer())
assert f.content == 'abcdef'
def test_file_content_overrides_file(self):
class buffer:
def read(self):
return 'from-file'
f = types.File(content='from-content', file=buffer())
assert f.content == 'from-content'
def test_file_setting_content_discards_file(self):
class buffer:
def read(self):
return 'from-file'
f = types.File(file=buffer())
f.content = 'from-content'
assert f.content == 'from-content'
def test_file_field_storage(self):
class buffer:
def read(self):
return 'from-file'
class fieldstorage:
filename = 'static.json'
file = buffer()
type = 'application/json'
f = types.File(fieldstorage=fieldstorage)
assert f.content == 'from-file'
def test_file_field_storage_value(self):
class buffer:
def read(self):
return 'from-file'
class fieldstorage:
filename = 'static.json'
file = None
type = 'application/json'
value = 'from-value'
f = types.File(fieldstorage=fieldstorage)
assert f.content == 'from-value'
def test_file_property_file(self):
class buffer:
def read(self):
return 'from-file'
buf = buffer()
f = types.File(file=buf)
assert f.file is buf
def test_file_property_content(self):
class buffer:
def read(self):
return 'from-file'
f = types.File(content=b'from-content')
assert f.file.read() == b'from-content'
def test_unregister(self):
class TempType(object):
pass