Skip to content

Commit

Permalink
downloader gets str
Browse files Browse the repository at this point in the history
  • Loading branch information
oolonek committed Apr 12, 2024
1 parent dce597a commit 7277630
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
docs/source
data

# From https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore

Expand Down
16 changes: 9 additions & 7 deletions mgf_filterer/download_manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from pathlib import Path

import click
import requests


def download_file(url: str, dest_folder: Path) -> None:
@click.command()
@click.argument("url")
@click.argument("dest_folder", type=click.Path())
def download_file(url: str, dest_folder: str) -> None:
"""
Downloads a file from the specified URL and saves it to the destination folder.
Expand All @@ -20,21 +24,19 @@ def download_file(url: str, dest_folder: Path) -> None:

# Extract filename from URL and construct full path
filename = url.split("/")[-1]
dest_path = dest_folder / filename
dest_path = Path(dest_folder) / filename

# Write file to the destination
with open(dest_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)

print(f"File downloaded successfully: {dest_path}")
click.echo(f"File downloaded successfully: {dest_path}")
except requests.RequestException as e:
print(f"Failed to download the file: {e}")
click.echo(f"Failed to download the file: {e}", err=True)
raise


# Example usage
if __name__ == "__main__":
download_url = "https://external.gnps2.org/gnpslibrary/GNPS-LIBRARY.mgf"
destination_folder = Path("/path/to/your/destination/folder")
download_file(download_url, destination_folder)
download_file()
8 changes: 5 additions & 3 deletions mgf_filterer/mgf_processing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from pathlib import Path
from typing import List

import click
from matchms.importing import load_from_mgf

from mgf_filterer.exceptions import MGFFileNotFoundError


@click.command()
@click.argument("filepath", type=click.Path(exists=True))
def load_mgf_files(filepath: str) -> List:
"""
Load spectra from a .mgf file.
Expand All @@ -23,11 +26,10 @@ def load_mgf_files(filepath: str) -> List:
raise MGFFileNotFoundError(filepath)

spectra = list(load_from_mgf(filepath))
click.echo(f"Loaded {len(spectra)} spectra from the file.")
return spectra


# Example usage:
if __name__ == "__main__":
# Replace 'path_to_your_file.mgf' with the actual file path
spectra = load_mgf_files("path_to_your_file.mgf")
print(f"Loaded {len(spectra)} spectra from the file.")
load_mgf_files()

0 comments on commit 7277630

Please sign in to comment.