forked from gdcc/python-dvuploader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
175 lines (131 loc) · 4.47 KB
/
Copy pathutils.py
File metadata and controls
175 lines (131 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import pathlib
import re
from typing import List
from urllib.parse import urljoin
import httpx
from rich.progress import Progress
from dvuploader.file import File
def build_url(
endpoint: str,
**kwargs,
) -> str:
"""Builds a URL string, given access points and credentials
Args:
endpoint (str): The base endpoint of the URL
**kwargs: Additional key-value pairs to be included as query parameters
Returns:
str: The complete URL string with query parameters
"""
if not isinstance(endpoint, str):
raise TypeError("Endpoint must be a string")
assert all(isinstance(v, str) for v in kwargs.keys()), "All keys must be strings"
queries = "&".join([f"{k}={str(v)}" for k, v in kwargs.items()])
if kwargs == {}:
return endpoint
return f"{endpoint}?{queries}"
def retrieve_dataset_files(
dataverse_url: str,
persistent_id: str,
api_token: str,
):
"""
Retrieve the files of a specific dataset from a Dataverse repository.
Args:
dataverse_url (str): The base URL of the Dataverse repository.
persistent_id (str): The persistent identifier (PID) of the dataset.
api_token (str): API token for authentication.
Returns:
list: A list of files in the dataset.
Raises:
HTTPError: If the request to the Dataverse repository fails.
"""
DATASET_ENDPOINT = f"/api/datasets/:persistentId/?persistentId={persistent_id}"
response = httpx.get(
urljoin(dataverse_url, DATASET_ENDPOINT),
headers={"X-Dataverse-key": api_token},
)
response.raise_for_status()
return response.json()["data"]["latestVersion"]["files"]
def add_directory(
directory: str,
ignore: List[str] = [r"^\."],
rootDirectoryLabel: str = "",
):
r"""
Recursively adds all files in the specified directory to a list of File objects.
Args:
directory (str): The directory path to scan for files.
ignore (List[str], optional): A list of regular expressions to ignore certain files or directories. Defaults to [r"^\."].
rootDirectoryLabel (str, optional): The label to be prepended to the directory path of each file. Defaults to "".
Returns:
List[File]: A list of File objects representing the files in the directory.
"""
files = []
# Part of the path to remove from the directory
for file in pathlib.Path(directory).rglob("*"):
if not file.is_file():
continue
if part_is_ignored(str(file.name), ignore):
continue
if any(part_is_ignored(part, ignore) for part in list(file.parts)):
continue
directory_label = _truncate_path(
file.parent,
pathlib.Path(directory),
)
files.append(
File(
filepath=str(file),
directoryLabel=os.path.join(
rootDirectoryLabel,
directory_label,
),
)
)
return files
def _truncate_path(path: pathlib.Path, to_remove: pathlib.Path):
"""
Truncate a path by removing a prefix path.
Args:
path (pathlib.Path): The full path to truncate.
to_remove (pathlib.Path): The prefix path to remove.
Returns:
str: The truncated path as a string, or empty string if nothing remains after truncation.
"""
parts = path.parts[len(to_remove.parts) :]
if len(parts) == 0:
return ""
return os.path.join(*[str(part) for part in parts])
def part_is_ignored(part, ignore):
"""
Check if a path part should be ignored based on a list of regex patterns.
Args:
part (str): The path part to check.
ignore (List[str]): A list of regex patterns to match against.
Returns:
bool: True if the part matches any ignore pattern, False otherwise.
"""
for pattern in ignore:
if re.match(pattern, part):
return True
return False
def setup_pbar(
file: File,
progress: Progress,
) -> int:
"""
Set up a progress bar for tracking file upload progress.
Args:
file (File): The File object containing file information.
progress (Progress): The rich Progress instance for displaying progress.
Returns:
int: The task ID for the created progress bar.
"""
file_size = file._size
fname = file.file_name
return progress.add_task(
f"[pink]├── {fname}",
start=True,
total=file_size,
)