Utilities
nplinker.utils
¶
calculate_md5
¶
Calculate the MD5 checksum of a file.
Parameters:
-
fpath(str | PathLike) –Path to the file.
-
chunk_size(int, default:1024 * 1024) –Chunk size for reading the file. Defaults to 1024*1024.
Returns:
-
str–MD5 checksum of the file.
Source code in src/nplinker/utils.py
check_disk_space
¶
A decorator to check available disk space.
If the available disk space is less than 500GB, raise and log a warning.
Warns:
-
UserWarning–If the available disk space is less than 500GB.
Source code in src/nplinker/utils.py
check_md5
¶
Verify the MD5 checksum of a file.
Parameters:
Returns:
-
bool–True if the MD5 checksum matches, False otherwise.
Source code in src/nplinker/utils.py
download_and_extract_archive
¶
download_and_extract_archive(
url: str,
download_root: str | PathLike,
extract_root: str | Path | None = None,
filename: str | None = None,
md5: str | None = None,
remove_finished: bool = False,
) -> None
Download an archive file and then extract it.
This method is a wrapper of download_url and
extract_archive functions.
Parameters:
-
url(str) –URL to download file from
-
download_root(str | PathLike) –Path to the directory to place downloaded file in. If it doesn't exist, it will be created.
-
extract_root(str | Path | None, default:None) –Path to the directory the file will be extracted to. The given directory will be created if not exist. If omitted, the
download_rootis used. -
filename(str | None, default:None) –Name to save the downloaded file under. If None, use the basename of the URL
-
md5(str | None, default:None) –MD5 checksum of the download. If None, do not check
-
remove_finished(bool, default:False) –If
True, remove the downloaded file after the extraction. Defaults to False.
Source code in src/nplinker/utils.py
download_url
¶
download_url(
url: str,
root: str | PathLike,
filename: str | None = None,
md5: str | None = None,
http_method: str = "GET",
allow_http_redirect: bool = True,
) -> None
Download a file from a url and place it in root.
Parameters:
-
url(str) –URL to download file from
-
root(str | PathLike) –Directory to place downloaded file in. If it doesn't exist, it will be created.
-
filename(str | None, default:None) –Name to save the file under. If None, use the basename of the URL.
-
md5(str | None, default:None) –MD5 checksum of the download. If None, do not check.
-
http_method(str, default:'GET') –HTTP request method, e.g. "GET", "POST". Defaults to "GET".
-
allow_http_redirect(bool, default:True) –If true, enable following redirects for all HTTP ("http:") methods.
Source code in src/nplinker/utils.py
extract_archive
¶
extract_archive(
from_path: str | PathLike,
extract_root: str | PathLike | None = None,
members: list | None = None,
remove_finished: bool = False,
) -> str
Extract an archive.
The archive type and a possible compression is automatically detected from the file name.
If the file is compressed but not an archive, the call is dispatched to _decompress function.
Parameters:
-
from_path(str | PathLike) –Path to the file to be extracted.
-
extract_root(str | PathLike | None, default:None) –Path to the directory the file will be extracted to. The given directory will be created if not exist. If omitted, the directory of the archive file is used.
-
members(list | None, default:None) –Optional selection of members to extract. If not specified, all members are extracted. Members must be a subset of the list returned by -
zipfile.ZipFile.namelist()or a list of strings for zip file -tarfile.TarFile.getmembers()for tar file -
remove_finished(bool, default:False) –If
True, remove the file after the extraction.
Returns:
-
str–Path to the directory the file was extracted to.
Source code in src/nplinker/utils.py
is_file_format
¶
Check if the file is in the given format.
Parameters:
-
file(str | PathLike) –Path to the file to check.
-
format(str, default:'tsv') –The format to check for, either "tsv" or "csv".
Returns:
-
bool–True if the file is in the given format, False otherwise.
Source code in src/nplinker/utils.py
list_dirs
¶
List all directories at a given root.
Parameters:
-
root(str | PathLike) –Path to directory whose folders need to be listed
-
keep_parent(bool, default:True) –If true, prepends the path to each result, otherwise only returns the name of the directories found
Source code in src/nplinker/utils.py
list_files
¶
list_files(
root: str | PathLike,
prefix: str | tuple[str, ...] = "",
suffix: str | tuple[str, ...] = "",
keep_parent: bool = True,
) -> list[str]
List all files at a given root.
Parameters:
-
root(str | PathLike) –Path to directory whose files need to be listed
-
prefix(str | tuple[str, ...], default:'') –Prefix of the file names to match, Defaults to empty string '""'.
-
suffix(str | tuple[str, ...], default:'') –Suffix of the files to match, e.g. ".png" or (".jpg", ".png"). Defaults to empty string '""'.
-
keep_parent(bool, default:True) –If true, prepends the parent path to each result, otherwise only returns the name of the files found. Defaults to False.
Source code in src/nplinker/utils.py
transform_to_full_path
¶
Transform a path to a full path.
The path is expanded (i.e. the ~ will be replaced with actual path) and converted to an
absolute path (i.e. . or .. will be replaced with actual path).
Parameters:
Returns:
-
Path–The transformed full path.