2
2
General utilities and helpers for easydiffraction.
3
3
"""
4
4
5
- import os
6
5
import pandas as pd
7
- import urllib .request
8
-
9
- from pathlib import Path
6
+ import pooch
10
7
from tabulate import tabulate
11
8
12
9
try :
19
16
IPython = None
20
17
21
18
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 :
25
22
"""
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
35
29
"""
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
+ )
58
41
59
42
60
43
def is_notebook () -> bool :
@@ -79,23 +62,6 @@ def is_notebook() -> bool:
79
62
return False # Probably standard Python interpreter
80
63
81
64
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
-
99
65
def render_table (columns_headers ,
100
66
columns_alignment ,
101
67
columns_data ,
0 commit comments