Skip to content

Commit 1e86e20

Browse files
committed
Implements repository file download utility
1 parent 7ea7a99 commit 1e86e20

File tree

3 files changed

+26
-56
lines changed

3 files changed

+26
-56
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ asciichartpy
77
plotly
88
darkdetect
99

10+
pooch
11+
1012
scipy
1113

1214
lmfit

src/easydiffraction/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from easydiffraction.summary import Summary
2020

2121
# Utils
22+
from easydiffraction.utils.utils import download_from_repository
2223
from easydiffraction.utils.formatting import (
2324
chapter,
2425
section,
@@ -37,5 +38,6 @@
3738
"Summary",
3839
"chapter",
3940
"section",
40-
"paragraph"
41+
"paragraph",
42+
'download_from_repository'
4143
]

src/easydiffraction/utils/utils.py

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
General utilities and helpers for easydiffraction.
33
"""
44

5-
import os
65
import pandas as pd
7-
import urllib.request
8-
9-
from pathlib import Path
6+
import pooch
107
from tabulate import tabulate
118

129
try:
@@ -19,42 +16,28 @@
1916
IPython = None
2017

2118

22-
def download_from_repository(url: str,
23-
save_path: str = ".",
24-
overwrite: bool = False) -> str:
19+
def download_from_repository(file_name: str,
20+
branch: str = 'master',
21+
destination: str = 'data') -> None:
2522
"""
26-
Downloads a file from a remote repository and saves it locally.
27-
28-
Args:
29-
url (str): URL to the file to be downloaded.
30-
save_path (str): Local directory or file path where the file will be saved.
31-
overwrite (bool): If True, overwrites the existing file.
32-
33-
Returns:
34-
str: Full path to the downloaded file.
23+
This function downloads a file from the EasyDiffraction repository
24+
on GitHub.
25+
:param file_name: The name of the file to download
26+
:param branch: The branch of the repository to download from
27+
:param destination: The destination folder to save the file
28+
:return: None
3529
"""
36-
save_path = Path(save_path)
37-
38-
# Determine if save_path is a directory or file
39-
if save_path.is_dir():
40-
filename = os.path.basename(url)
41-
file_path = save_path / filename
42-
else:
43-
file_path = save_path
44-
45-
if file_path.exists() and not overwrite:
46-
print(f"[INFO] File already exists: {file_path}. Use overwrite=True to replace it.")
47-
return str(file_path)
48-
49-
try:
50-
print(f"[INFO] Downloading from {url} to {file_path}")
51-
urllib.request.urlretrieve(url, file_path)
52-
print(f"[INFO] Download complete: {file_path}")
53-
except Exception as e:
54-
print(f"[ERROR] Failed to download {url}. Error: {e}")
55-
raise
56-
57-
return str(file_path)
30+
prefix = 'https://raw.githubusercontent.com'
31+
organisation = 'easyscience'
32+
repository = 'diffraction-lib'
33+
source = 'tutorials/data'
34+
url = f'{prefix}/{organisation}/{repository}/refs/heads/{branch}/{source}/{file_name}'
35+
pooch.retrieve(
36+
url=url,
37+
known_hash=None,
38+
fname=file_name,
39+
path=destination
40+
)
5841

5942

6043
def is_notebook() -> bool:
@@ -79,23 +62,6 @@ def is_notebook() -> bool:
7962
return False # Probably standard Python interpreter
8063

8164

82-
def ensure_dir(directory: str) -> str:
83-
"""
84-
Ensures that a directory exists. Creates it if it doesn't exist.
85-
86-
Args:
87-
directory (str): Path to the directory to ensure.
88-
89-
Returns:
90-
str: Absolute path of the ensured directory.
91-
"""
92-
path = Path(directory)
93-
if not path.exists():
94-
print(f"[INFO] Creating directory: {directory}")
95-
path.mkdir(parents=True, exist_ok=True)
96-
return str(path.resolve())
97-
98-
9965
def render_table(columns_headers,
10066
columns_alignment,
10167
columns_data,

0 commit comments

Comments
 (0)