Skip to content

Commit 50f2ccd

Browse files
committed
Fixing url download from project
1 parent 54ed446 commit 50f2ccd

2 files changed

Lines changed: 63 additions & 5 deletions

File tree

earthpy/io.py

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import tarfile
1414
import zipfile
1515

16-
from .config import DEFAULT_DATA_HOME, DATA_URLS, FIGSHARE_API_URL
16+
from .config import DEFAULT_DATA_HOME, DATA_URLS, FIGSHARE_API_URL, DVCIGNORE
1717

1818
ALLOWED_FILE_TYPES = ["file", "tar", "tar.gz", "zip"]
1919

@@ -176,15 +176,17 @@ def get_data(self,
176176
f"Title '{title}' not found in available datasets."
177177
)
178178
urls = self._get_figshare_download_urls(article_id)
179-
179+
print(urls)
180180
this_data = []
181-
for name, url in urls.items():
181+
for fname, data_url in urls.items():
182182
# Determine filetype using file name extension
183183
file_type = "file"
184184
for ext in ALLOWED_FILE_TYPES:
185185
if fname.endswith(ext):
186186
file_type = ext
187-
this_data.append((url, name, file_type))
187+
fname = fname.replace('.'+ext, "")
188+
this_data.append((data_url, fname, file_type))
189+
print(this_data)
188190

189191
if url is not None:
190192
with requests.head(url) as r:
@@ -220,7 +222,9 @@ def get_data(self,
220222
this_data = [this_data]
221223

222224
data_paths = []
225+
print(this_data)
223226
for url, name, kind in this_data:
227+
print(name)
224228

225229
if kind not in ALLOWED_FILE_TYPES:
226230
raise ValueError(
@@ -237,6 +241,7 @@ def get_data(self,
237241
verbose=verbose,
238242
)
239243
data_paths.append(this_path)
244+
print(data_paths)
240245

241246
# Return the data path or list of paths
242247
if len(data_paths) == 1:
@@ -401,3 +406,56 @@ def _download_and_extract(self, path, r, kind, verbose):
401406
archive.extractall(path)
402407
if verbose is True:
403408
print("Extracted output to {}".format(path))
409+
410+
def _zip_dir(self, dir_path, zip_home, zipf):
411+
"""Zip a directory and return the zip file path."""
412+
for subfile in dir_path.rglob("*"):
413+
if subfile.name in DVCIGNORE:
414+
print(f"Skipping {subfile} as it is in DVCIGNORE.")
415+
continue
416+
if subfile.is_file():
417+
dest_path = subfile.relative_to(zip_home)
418+
print(f" Zipping {subfile} to {dest_path}")
419+
zipf.write(subfile, dest_path)
420+
if subfile.is_dir():
421+
# Ensure directories are included in the zip
422+
self._zip_dir(subfile, zip_home, zipf)
423+
424+
def prepare_for_upload(self):
425+
"""
426+
Prepare files for upload to Figshare.
427+
428+
This function collects all files in the project path,
429+
excluding hidden files and those in the DVCIGNORE list, and
430+
zips them into a directory named "figshare-upload".
431+
432+
Returns
433+
-------
434+
output_path : Path
435+
Path to the directory containing files prepared for upload.
436+
"""
437+
files_and_dirs = [
438+
f for f in self.path.glob("*")
439+
if (
440+
not f in DVCIGNORE)
441+
and (not f.name == "figshare-upload")
442+
and (not f.name.startswith(".")
443+
)
444+
]
445+
446+
output_path = self.path / "figshare-upload"
447+
output_path.mkdir(parents=True, exist_ok=True)
448+
for path in files_and_dirs:
449+
if path.is_file():
450+
# If it's a file, just copy it to the output path
451+
dest_file = output_path / path.name
452+
print(f"Copying file: {path} to {dest_file}")
453+
dest_file.write_bytes(path.read_bytes())
454+
if path.is_dir():
455+
zipfile_name = output_path / f"{path.name}.zip"
456+
with zipfile.ZipFile(zipfile_name, "w") as zipf:
457+
print(f"Zipping directory: {path} to {zipfile_name}")
458+
self._zip_dir(path, path, zipf)
459+
460+
print(f"Prepared files for upload in {output_path}")
461+
return output_path

earthpy/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def get_data(
144144
key=self.key, replace=replace, verbose=verbose)
145145
if url:
146146
self.data.get_data(
147-
url=self.url, filename=filename,
147+
url=url, filename=filename,
148148
replace=replace, verbose=verbose)
149149
if self.title:
150150
self.data.get_data(

0 commit comments

Comments
 (0)