This decorator validates the file extension and file size of an in memory file object.
Project description
File Validation Decorator
A decorator for validating file types and sizes. If the validation fails, an Exception will be raised and the code will stop before reaching the function you are decorating. A successful validation will execute the function being decorated - business as usual.
Example Of A Successful File Validation
Successful file validations allow the function that is being decorated to execute. The first parameter defined in your function needs to be the file object.
from file_validation_decorator.file_validation import file_validation
@file_validation(accepted_file_extensions=['txt'], accepted_mime_types=['text/plain'])
def hello(file):
print('Hello World!')
with open('test.txt', 'r') as file:
hello(file)
Example Of A File That Is Too Large In Size
The decorator is provided a file size of .000001 and will raise a FileSizeExceeded error.
from file_validation_decorator.file_validation import file_validation
@file_validation(accepted_file_extensions=['txt'], max_file_size=.000001)
def hello(file):
print('Hello World!')
with open('test.txt', 'r') as file:
hello(file)
Example Of A File Extension That Is Not Allowed
The example file being uploaded here is a .txt but a text file is not provided in the accepted file extensions list. This will raise a FileTypeNotAllowed error.
from file_validation_decorator.file_validation import file_validation
@file_validation(accepted_file_extensions=['jpeg'])
def hello(file):
print('Hello World!')
with open('test.txt', 'r') as file:
hello(file)
Exceptions
-
FileExtensionNotAllowed - Raised when the file's extension is not found in the list of accepted_file_extensions
-
FileTypeNotAllowed - Raised when the file's type is not found in the list of accepted_file_types
-
FileMimeTypeNotAllowed - Raised when the file's mime type is not found in the list of accepted_mime_types
-
FileSizeExceeded - Raised when the file's size is larger than the max_file_size
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for file_validation_decorator-0.0.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35777332bca551cef3a257a0cdb9eed281a8c402118375d830397e6883111655 |
|
MD5 | 521e4b2e3e842c53c70b4b29c09d8aa3 |
|
BLAKE2b-256 | fe7909f4e599d3abfdee01de1cda88ea44da5c24ab6970994774d6f11b85ff1e |
Hashes for file_validation_decorator-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5530ea98f91ee473b90e25c2895bceddc09429e7bddb64c95ef9ef391633c0c9 |
|
MD5 | 697ba888d08f14fbc6543e58a99d2828 |
|
BLAKE2b-256 | 9301f6d0afbc2f65152aa55468c761f44555187a0d3d0ea630f8bbfc1ee7995d |