-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
173 lines (137 loc) · 5.01 KB
/
Copy pathutils.py
File metadata and controls
173 lines (137 loc) · 5.01 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
"""
Utility functions for handling JSON, CSV, and Dataverse API interactions
"""
import json
import csv
import os
from typing import List, Dict, Any
from datetime import datetime
def load_json_file(filepath: str) -> Dict[str, Any]:
"""
Load a JSON file and return its contents
Args:
filepath: Path to the JSON file
Returns:
Dictionary containing the JSON data
"""
try:
with open(csv_file, "r", encoding="utf-8-sig", newline="") as f:
return list(csv.DictReader(f))
except UnicodeDecodeError:
with open(csv_file, "r", encoding="cp1252", newline="") as f:
return list(csv.DictReader(f))
def save_json_file(data: Dict[str, Any], filepath: str, indent: int = 2) -> None:
"""
Save data to a JSON file
Args:
data: Dictionary to save
filepath: Path where to save the JSON file
indent: JSON indentation level
"""
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=indent, ensure_ascii=False)
def load_csv_file(filepath: str) -> List[Dict[str, str]]:
"""
Load a CSV file and return as list of dictionaries
Args:
filepath: Path to the CSV file
Returns:
List of dictionaries where each row is a dictionary
"""
rows = []
with open(filepath, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
rows.append(row)
return rows
def save_csv_file(data: List[Dict[str, str]], filepath: str, fieldnames: List[str]) -> None:
"""
Save list of dictionaries to a CSV file
Args:
data: List of dictionaries to save
filepath: Path where to save the CSV file
fieldnames: List of column names in the CSV
"""
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
def extract_metadata_from_json(json_data: Dict[str, Any]) -> Dict[str, str]:
"""
Extract file metadata from JSON structure
Expected JSON structure:
{
"datasetPersistentId": "doi:...",
"dataset": { "version": "..." },
"data": [ {"label": "...", "directoryLabel": "...", ...}, ... ]
}
Args:
json_data: The JSON data structure
Returns:
Dictionary with extracted metadata
"""
metadata = {}
# Extract DOI
if "datasetPersistentId" in json_data:
metadata["DOI"] = json_data["datasetPersistentId"]
# Extract dataset version
if "dataset" in json_data and "version" in json_data["dataset"]:
metadata["dataset_version"] = json_data["dataset"]["version"]
# Extract file information
if "data" in json_data and isinstance(json_data["data"], list):
metadata["file_count"] = len(json_data["data"])
# Get first file details as example
if json_data["data"]:
first_file = json_data["data"][0]
metadata["first_file_label"] = first_file.get("label", "")
metadata["first_file_directory"] = first_file.get("directoryLabel", "")
return metadata
def create_dataverse_json_structure(doi: str, dataset_name: str, files_metadata: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Create a Dataverse-compatible JSON structure for files
Args:
doi: Dataset DOI
dataset_name: Name of the dataset
files_metadata: List of file metadata dictionaries
Returns:
Dictionary with Dataverse-compatible structure
"""
return {
"datasetPersistentId": doi,
"datasetName": dataset_name,
"dataset": {
"version": 1,
"releaseTime": datetime.now().isoformat()
},
"data": files_metadata,
"lastUpdated": datetime.now().isoformat()
}
def validate_csv_row(row: Dict[str, str], required_fields: List[str]) -> tuple[bool, str]:
"""
Validate a CSV row has all required fields
Args:
row: Dictionary representing a CSV row
required_fields: List of required field names
Returns:
Tuple of (is_valid, error_message)
"""
missing_fields = [field for field in required_fields if field not in row or not row[field].strip()]
if missing_fields:
return False, f"Missing fields: {', '.join(missing_fields)}"
return True, ""
def get_json_files_from_directory(directory: str) -> List[str]:
"""
Get all JSON files from a directory
Args:
directory: Path to directory containing JSON files
Returns:
List of absolute paths to JSON files
"""
json_files = []
if os.path.isdir(directory):
for filename in os.listdir(directory):
if filename.endswith('.json'):
json_files.append(os.path.join(directory, filename))
return sorted(json_files)