-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for displaying spectral signature interactively (#12)
* Add SpectralWidget * Add a working prototype * Add marker cluster * Add spectral_to_df * Clean up * Change from matplotlib to bqplot * Add button to save spectral data * Add reset button * Fix output widget bug * Add download file function
- Loading branch information
Showing
3 changed files
with
421 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,118 @@ | ||
"""The common module contains common functions and classes used by the other modules. | ||
""" | ||
|
||
import os | ||
|
||
def hello_world(): | ||
"""Prints "Hello World!" to the console.""" | ||
print("Hello World!") | ||
|
||
def github_raw_url(url): | ||
"""Get the raw URL for a GitHub file. | ||
Args: | ||
url (str): The GitHub URL. | ||
Returns: | ||
str: The raw URL. | ||
""" | ||
if isinstance(url, str) and url.startswith("https://github.com/") and "blob" in url: | ||
url = url.replace("github.com", "raw.githubusercontent.com").replace( | ||
"blob/", "" | ||
) | ||
return url | ||
|
||
|
||
def download_file( | ||
url=None, | ||
output=None, | ||
quiet=False, | ||
proxy=None, | ||
speed=None, | ||
use_cookies=True, | ||
verify=True, | ||
id=None, | ||
fuzzy=False, | ||
resume=False, | ||
unzip=True, | ||
overwrite=False, | ||
subfolder=False, | ||
): | ||
"""Download a file from URL, including Google Drive shared URL. | ||
Args: | ||
url (str, optional): Google Drive URL is also supported. Defaults to None. | ||
output (str, optional): Output filename. Default is basename of URL. | ||
quiet (bool, optional): Suppress terminal output. Default is False. | ||
proxy (str, optional): Proxy. Defaults to None. | ||
speed (float, optional): Download byte size per second (e.g., 256KB/s = 256 * 1024). Defaults to None. | ||
use_cookies (bool, optional): Flag to use cookies. Defaults to True. | ||
verify (bool | str, optional): Either a bool, in which case it controls whether the server's TLS certificate is verified, or a string, | ||
in which case it must be a path to a CA bundle to use. Default is True.. Defaults to True. | ||
id (str, optional): Google Drive's file ID. Defaults to None. | ||
fuzzy (bool, optional): Fuzzy extraction of Google Drive's file Id. Defaults to False. | ||
resume (bool, optional): Resume the download from existing tmp file if possible. Defaults to False. | ||
unzip (bool, optional): Unzip the file. Defaults to True. | ||
overwrite (bool, optional): Overwrite the file if it already exists. Defaults to False. | ||
subfolder (bool, optional): Create a subfolder with the same name as the file. Defaults to False. | ||
Returns: | ||
str: The output file path. | ||
""" | ||
import zipfile | ||
import tarfile | ||
import gdown | ||
|
||
if output is None: | ||
if isinstance(url, str) and url.startswith("http"): | ||
output = os.path.basename(url) | ||
|
||
out_dir = os.path.abspath(os.path.dirname(output)) | ||
if not os.path.exists(out_dir): | ||
os.makedirs(out_dir) | ||
|
||
if isinstance(url, str): | ||
if os.path.exists(os.path.abspath(output)) and (not overwrite): | ||
print( | ||
f"{output} already exists. Skip downloading. Set overwrite=True to overwrite." | ||
) | ||
return os.path.abspath(output) | ||
else: | ||
url = github_raw_url(url) | ||
|
||
if "https://drive.google.com/file/d/" in url: | ||
fuzzy = True | ||
|
||
output = gdown.download( | ||
url, output, quiet, proxy, speed, use_cookies, verify, id, fuzzy, resume | ||
) | ||
|
||
if unzip: | ||
if output.endswith(".zip"): | ||
with zipfile.ZipFile(output, "r") as zip_ref: | ||
if not quiet: | ||
print("Extracting files...") | ||
if subfolder: | ||
basename = os.path.splitext(os.path.basename(output))[0] | ||
|
||
output = os.path.join(out_dir, basename) | ||
if not os.path.exists(output): | ||
os.makedirs(output) | ||
zip_ref.extractall(output) | ||
else: | ||
zip_ref.extractall(os.path.dirname(output)) | ||
elif output.endswith(".tar.gz") or output.endswith(".tar"): | ||
if output.endswith(".tar.gz"): | ||
mode = "r:gz" | ||
else: | ||
mode = "r" | ||
|
||
with tarfile.open(output, mode) as tar_ref: | ||
if not quiet: | ||
print("Extracting files...") | ||
if subfolder: | ||
basename = os.path.splitext(os.path.basename(output))[0] | ||
output = os.path.join(out_dir, basename) | ||
if not os.path.exists(output): | ||
os.makedirs(output) | ||
tar_ref.extractall(output) | ||
else: | ||
tar_ref.extractall(os.path.dirname(output)) | ||
|
||
return os.path.abspath(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.