recordtransfer.utils - Utility Functions
- recordtransfer.utils.zip_directory(directory: str, zipf: ZipFile)
Zip a directory structure into a zip file.
- Parameters:
directory (str) – The folder to zip
zipf (ZipFile) – A zipfile.ZipFile handle
- recordtransfer.utils.snake_to_camel_case(string: str)
- recordtransfer.utils.html_to_text(html: str)
- recordtransfer.utils.get_human_readable_size(size_bytes: int, base=1024, precision=2)
Convert bytes into a human-readable size.
- Parameters:
size_bytes – The number of bytes to convert
base – Either of 1024 or 1000. 1024 for sizes like MiB, 1000 for sizes like MB
precision – The number of decimals on the returned size
- Returns:
The bytes converted to a human readable size
- Return type:
(str)
- recordtransfer.utils.get_human_readable_file_count(file_names: list, accepted_file_groups: dict, logger=None)
Count the number of files falling into the ACCEPTED_FILE_FORMATS groups, and report (in English) the number of files in each group.
- Parameters:
file_names (list) – A list of file paths or names with extension intact
accepted_file_groups (dict) – A dictionary of file group names mapping to a list of lowercase file extensions without periods.
logger (Logger) – A logging instance for any messages.
- Returns:
A string reporting the number of files in each group.
- Return type:
(str)
- recordtransfer.utils.count_file_types(file_names: list, accepted_file_groups: dict, logger=None)
Tabulate how many files fall into the file groups specified in the ACCEPTED_FILE_FORMATS dictionary.
If a file’s extension does not match any of the accepted file extensions, it is ignored. For that reason, it is important to ensure that the files are accepted before trying to count them.
- Parameters:
file_names (list) – A list of file paths or names with extension intact
accepted_file_groups (dict) – A dictionary of file group names mapping to a list of lowercase file extensions without periods.
logger (Logger) – A logging instance.
- Returns:
A dictionary mapping from group name to number of files in that group.
- Return type:
(dict)
- recordtransfer.utils.mib_to_bytes(m: int) int
Convert MiB to bytes.
- Parameters:
m (int) – Size in MiB.
- Returns:
Size in bytes.
- Return type:
int
- recordtransfer.utils.bytes_to_mib(b: int) float
Convert bytes to MiB.
- Parameters:
b (int) – Size in bytes.
- Returns:
Size in MiB.
- Return type:
float
- recordtransfer.utils.accept_file(filename: str, filesize: str | int) dict
Determine if a new file should be accepted. Does not check the file’s contents, only its name and its size.
These checks are applied: - The file name is not empty - The file has an extension - The file’s extension exists in ACCEPTED_FILE_FORMATS - The file’s size is an integer greater than zero - The file’s size is less than or equal to the maximum allowed size for one file
- Parameters:
filename (str) – The name of the file
filesize (Union[str, int]) – A string or integer representing the size of the file (in bytes)
- Returns:
- A dictionary containing an ‘accepted’ key that contains True if
the session is valid, or False if not. The dictionary also contains an ‘error’ and ‘verboseError’ key if ‘accepted’ is False.
- Return type:
(dict)
- recordtransfer.utils.accept_session(filename: str, filesize: str | int, session: UploadSession) dict
Determine if a new file should be accepted as part of the session.
These checks are applied: - The session has room for more files according to the MAX_TOTAL_UPLOAD_COUNT - The session has room for more files according to the MAX_TOTAL_UPLOAD_SIZE - A file with the same name has not already been uploaded
- Parameters:
filename (str) – The name of the file
filesize (Union[str, int]) – A string or integer representing the size of the file (in bytes)
session (UploadSession) – The session files are being uploaded to
- Returns:
- A dictionary containing an ‘accepted’ key that contains True if
the session is valid, or False if not. The dictionary also contains an ‘error’ and ‘verboseError’ key if ‘accepted’ is False.
- Return type:
(dict)