fixed some naming convensions

This commit is contained in:
Abubakar Abid 2021-04-30 00:38:27 -04:00
parent dac5ce24c6
commit a098a46f40
2 changed files with 15 additions and 11 deletions

View File

@ -894,18 +894,18 @@ class File(InputComponent):
Input type: Union[file-object, bytes, List[Union[file-object, bytes]]]
"""
def __init__(self, file_count="single", type="file", label=None, keepfilename=True):
def __init__(self, file_count="single", type="file", label=None, keep_filename=True):
'''
Parameters:
file_count (str): if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
type (str): Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name, "binary" returns an bytes object.
keepfilename (bool): whether to keep the original filename in the f.name field upon upload. If true, will place 'originalfilename' + a '_' before the unique temporary safe filename string and extension
keep_filename (bool): whether to keep the original filename in the f.name field upon upload. If true, will place 'originalfilename' + a '_' before the unique temporary safe filename string and extension
label (str): component name in interface.
'''
self.file_count = file_count
self.type = type
self.test_input = None
self.keepfilename = keepfilename
self.keep_filename = keep_filename
super().__init__(label)
def get_template_context(self):
@ -928,9 +928,11 @@ class File(InputComponent):
if is_local_example:
return open(name)
else:
if self.keepfilename: filenameprefix=Path(name).stem+'_'
else: filenameprefix=""
return processing_utils.decode_base64_to_file(data, filenameprefix=filenameprefix)
if self.keep_filename:
filename_prefix=Path(name).stem+'_'
else:
filename_prefix=""
return processing_utils.decode_base64_to_file(data, filename_prefix=filename_prefix)
elif self.type == "bytes":
if is_local_example:
with open(name, "rb") as file_data:
@ -939,8 +941,10 @@ class File(InputComponent):
else:
raise ValueError("Unknown type: " + str(self.type) + ". Please choose from: 'file', 'bytes'.")
if self.file_count == "single":
if isinstance(x, list): return process_single_file(x[0])
else: return process_single_file(x)
if isinstance(x, list):
return process_single_file(x[0])
else:
return process_single_file(x)
else:
return [process_single_file(f) for f in x]

View File

@ -79,12 +79,12 @@ def decode_base64_to_binary(encoding):
data = encoding
return base64.b64decode(data), extension
def decode_base64_to_file(encoding, encryption_key=None, filenameprefix=""):
def decode_base64_to_file(encoding, encryption_key=None, filename_prefix=""):
data, extension = decode_base64_to_binary(encoding)
if extension is None:
file_obj = tempfile.NamedTemporaryFile(delete=False, prefix=filenameprefix)
file_obj = tempfile.NamedTemporaryFile(delete=False, prefix=filename_prefix)
else:
file_obj = tempfile.NamedTemporaryFile(delete=False, prefix=filenameprefix, suffix="."+extension)
file_obj = tempfile.NamedTemporaryFile(delete=False, prefix=filename_prefix, suffix="."+extension)
if encryption_key is not None:
data = encryptor.encrypt(encryption_key, data)
#print("saving to ", file_obj.name)