caltechdata-api 2.0.0__tar.gz → 2.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/PKG-INFO +31 -14
  2. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/README.md +17 -5
  3. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/__init__.py +4 -0
  4. caltechdata_api-2.2.0/caltechdata_api/download_from_record.py +236 -0
  5. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api.egg-info/PKG-INFO +31 -14
  6. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api.egg-info/SOURCES.txt +1 -3
  7. caltechdata_api-2.2.0/pyproject.toml +69 -0
  8. caltechdata_api-2.2.0/setup.cfg +4 -0
  9. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/tests/test_rdm.py +6 -2
  10. caltechdata_api-2.2.0/tests/test_unit.py +76 -0
  11. caltechdata_api-2.0.0/caltechdata_api/get_files.py +0 -49
  12. caltechdata_api-2.0.0/pyproject.toml +0 -3
  13. caltechdata_api-2.0.0/setup.cfg +0 -55
  14. caltechdata_api-2.0.0/setup.py +0 -3
  15. caltechdata_api-2.0.0/tests/test_unit.py +0 -166
  16. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/LICENSE +0 -0
  17. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/caltechdata_edit.py +0 -0
  18. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/caltechdata_write.py +0 -0
  19. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/cli.py +0 -0
  20. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/customize_schema.py +0 -0
  21. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/download_file.py +0 -0
  22. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/get_metadata.py +0 -0
  23. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/md_to_json.py +0 -0
  24. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/utils.py +0 -0
  25. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies/date_types.yaml +0 -0
  26. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies/description_types.yaml +0 -0
  27. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies/identifier_types.yaml +0 -0
  28. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies/licenses.csv +0 -0
  29. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies/relation_types.yaml +0 -0
  30. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies/resource_types.yaml +0 -0
  31. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies/roles.yaml +0 -0
  32. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies/title_types.yaml +0 -0
  33. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api/vocabularies.yaml +0 -0
  34. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api.egg-info/dependency_links.txt +0 -0
  35. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api.egg-info/entry_points.txt +0 -0
  36. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api.egg-info/requires.txt +0 -0
  37. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/caltechdata_api.egg-info/top_level.txt +0 -0
  38. {caltechdata_api-2.0.0 → caltechdata_api-2.2.0}/tests/test_download.py +0 -0
@@ -1,20 +1,25 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: caltechdata_api
3
- Version: 2.0.0
3
+ Version: 2.2.0
4
4
  Summary: Python wrapper for CaltechDATA API.
5
- Home-page: https://github.com/caltechlibrary/caltechdata_api
6
- Author: Thomas E Morrell, Rohan Bhattarai, Elizabeth Won, Alexander A Abakah, Kshemaahna Nagi
7
- Author-email: tmorrell@caltech.edu, aabakah@caltech.edu, knagi@caltech.edu
8
- License: MIT
9
- Classifier: License :: OSI Approved :: MIT License
5
+ Author: Rohan Bhattarai, Elizabeth Won
6
+ Author-email: Thomas E Morrell <tmorrell@caltech.edu>, Alexander A Abakah <aabakah@caltech.edu>, Kshemaahna Nagi <knagi@caltech.edu>
7
+ Maintainer-email: Thomas E Morrell <tmorrell@caltech.edu>
8
+ License-Expression: BSD-3-Clause
9
+ Project-URL: Homepage, https://github.com/caltechlibrary/caltechdata_api
10
+ Project-URL: Repository, https://github.com/caltechlibrary/caltechdata_api
11
+ Project-URL: Issues, https://github.com/caltechlibrary/caltechdata_api/issues
12
+ Keywords: metadata,software,InvenioRDM
10
13
  Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.7
12
- Classifier: Programming Language :: Python :: 3.8
13
14
  Classifier: Programming Language :: Python :: 3.9
14
15
  Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
15
20
  Classifier: Programming Language :: Python :: Implementation :: CPython
16
21
  Classifier: Operating System :: OS Independent
17
- Requires-Python: >=3.6.0
22
+ Requires-Python: >=3.9
18
23
  Description-Content-Type: text/markdown
19
24
  License-File: LICENSE
20
25
  Requires-Dist: requests
@@ -44,7 +49,7 @@ The `caltechdata_api` Python library provides a convenient interface for interac
44
49
 
45
50
  ## Requirements
46
51
 
47
- - Python 3.6+
52
+ - Python 3.9+
48
53
 
49
54
  ## Installation
50
55
 
@@ -54,6 +59,18 @@ Install the library via pip:
54
59
  pip install caltechdata_api
55
60
  ```
56
61
 
62
+ Or add it to a [uv](https://docs.astral.sh/uv/) project:
63
+
64
+ ```shell
65
+ uv add caltechdata_api
66
+ ```
67
+
68
+ You can also run a script using uv:
69
+
70
+ ```shell
71
+ uv run write.py
72
+ ```
73
+
57
74
  ## Examples
58
75
 
59
76
  There are some example python scripts in the GitHub repository.
@@ -61,7 +78,7 @@ There are some example python scripts in the GitHub repository.
61
78
  ### Create a record:
62
79
 
63
80
  ```shell
64
- python write.py example.json -fnames logo.gif
81
+ uv run write.py example.json -fnames logo.gif
65
82
  # Output: pbkn6-m9y63 (unique identifier)
66
83
  ```
67
84
  > The response will be the unique identifier for the record. You can put this at
@@ -71,7 +88,7 @@ https://data.caltechlibrary.dev/records/pbkn6-m9y63)
71
88
  ### Edit a record
72
89
  Make changes to the example.json file to see a change)
73
90
  ```
74
- python edit.py example.json -id pbkn6-m9y63
91
+ uv run edit.py example.json -id pbkn6-m9y63
75
92
  10.33569/pbkn6-m9y63
76
93
  ```
77
94
  > The response is the DOI for the record, which includes the unique identifier
@@ -82,13 +99,13 @@ Some groups have worked with the library to create custom DOIs. These can be
82
99
  passed in the metadata like:
83
100
 
84
101
  ```shell
85
- python write.py example_custom.json -fnames logo.gif
102
+ uv run write.py example_custom.json -fnames logo.gif
86
103
  m6zxz-p4j22
87
104
  ```
88
105
 
89
106
  And then you can edit with
90
107
  ```
91
- python edit.py example_custom.json -id m6zxz-p4j22
108
+ uv run edit.py example_custom.json -id m6zxz-p4j22
92
109
  10.5281/inveniordm.1234
93
110
  ```
94
111
 
@@ -15,7 +15,7 @@ The `caltechdata_api` Python library provides a convenient interface for interac
15
15
 
16
16
  ## Requirements
17
17
 
18
- - Python 3.6+
18
+ - Python 3.9+
19
19
 
20
20
  ## Installation
21
21
 
@@ -25,6 +25,18 @@ Install the library via pip:
25
25
  pip install caltechdata_api
26
26
  ```
27
27
 
28
+ Or add it to a [uv](https://docs.astral.sh/uv/) project:
29
+
30
+ ```shell
31
+ uv add caltechdata_api
32
+ ```
33
+
34
+ You can also run a script using uv:
35
+
36
+ ```shell
37
+ uv run write.py
38
+ ```
39
+
28
40
  ## Examples
29
41
 
30
42
  There are some example python scripts in the GitHub repository.
@@ -32,7 +44,7 @@ There are some example python scripts in the GitHub repository.
32
44
  ### Create a record:
33
45
 
34
46
  ```shell
35
- python write.py example.json -fnames logo.gif
47
+ uv run write.py example.json -fnames logo.gif
36
48
  # Output: pbkn6-m9y63 (unique identifier)
37
49
  ```
38
50
  > The response will be the unique identifier for the record. You can put this at
@@ -42,7 +54,7 @@ https://data.caltechlibrary.dev/records/pbkn6-m9y63)
42
54
  ### Edit a record
43
55
  Make changes to the example.json file to see a change)
44
56
  ```
45
- python edit.py example.json -id pbkn6-m9y63
57
+ uv run edit.py example.json -id pbkn6-m9y63
46
58
  10.33569/pbkn6-m9y63
47
59
  ```
48
60
  > The response is the DOI for the record, which includes the unique identifier
@@ -53,13 +65,13 @@ Some groups have worked with the library to create custom DOIs. These can be
53
65
  passed in the metadata like:
54
66
 
55
67
  ```shell
56
- python write.py example_custom.json -fnames logo.gif
68
+ uv run write.py example_custom.json -fnames logo.gif
57
69
  m6zxz-p4j22
58
70
  ```
59
71
 
60
72
  And then you can edit with
61
73
  ```
62
- python edit.py example_custom.json -id m6zxz-p4j22
74
+ uv run edit.py example_custom.json -id m6zxz-p4j22
63
75
  10.5281/inveniordm.1234
64
76
  ```
65
77
 
@@ -13,5 +13,9 @@ from .caltechdata_edit import (
13
13
  from .customize_schema import customize_schema, validate_metadata
14
14
  from .get_metadata import get_metadata
15
15
  from .download_file import download_file, download_url
16
+ from .download_from_record import (
17
+ get_files_from_record,
18
+ download_files_from_record,
19
+ )
16
20
  from .utils import humanbytes
17
21
  from .md_to_json import parse_readme_to_json
@@ -0,0 +1,236 @@
1
+ """Functions to download files directly from a CaltechData record.
2
+
3
+ A CaltechData record ID is the ten characters, separated into two groups
4
+ of five by a dash, found at the end of a CaltechData URL or default DOI.
5
+ For instance, in the URL ``https://data.caltech.edu/records/4rgh7-zss31``
6
+ or DOI ``10.22002/4rgh7-zss31``, the record ID is ``4rgh7-zss31``.
7
+
8
+ The same functions can download from CaltechAUTHORS by passing
9
+ ``authors=True``, and from the test (development) instances of either
10
+ repository by passing ``production=False``.
11
+ """
12
+
13
+ import argparse
14
+ import os
15
+ import requests
16
+ from tqdm.auto import tqdm
17
+
18
+ from typing import Any, Dict, Optional, Sequence
19
+
20
+
21
+ def get_base_url(production: bool = True, authors: bool = False) -> str:
22
+ """Get the base URL of the repository to download from.
23
+
24
+ Parameters
25
+ ----------
26
+ production
27
+ Whether to use the production repository. If ``False``, the
28
+ test (development) instance is used instead.
29
+
30
+ authors
31
+ Whether to use CaltechAUTHORS instead of CaltechDATA.
32
+
33
+ Returns
34
+ -------
35
+ str
36
+ The base URL, without a trailing slash.
37
+ """
38
+ if production:
39
+ if authors:
40
+ return "https://authors.library.caltech.edu"
41
+ return "https://data.caltech.edu"
42
+ else:
43
+ if authors:
44
+ return "https://authors.caltechlibrary.dev"
45
+ return "https://data.caltechlibrary.dev"
46
+
47
+
48
+ def get_files_from_record(
49
+ record_id: str, production: bool = True, authors: bool = False
50
+ ) -> Dict[str, Any]:
51
+ """Get a dictionary of files associated with a record.
52
+
53
+ Parameters
54
+ ----------
55
+ record_id
56
+ The record ID for which to obtain the list of files. See
57
+ the module documentation for how to find the record ID.
58
+
59
+ production
60
+ Whether to query the production repository. If ``False``, the
61
+ test (development) instance is used instead.
62
+
63
+ authors
64
+ Whether to query CaltechAUTHORS instead of CaltechDATA.
65
+
66
+ Returns
67
+ -------
68
+ dict
69
+ A dictionary with the file names as keys and the metadata
70
+ associated with those files as values.
71
+ """
72
+ base_url = get_base_url(production=production, authors=authors)
73
+ with requests.get(f"{base_url}/api/records/{record_id}/files") as r:
74
+ r.raise_for_status()
75
+ files = dict()
76
+ for entry in r.json().get("entries", []):
77
+ key = entry.pop("key")
78
+ files[key] = entry
79
+
80
+ return files
81
+
82
+
83
+ def download_files_from_record(
84
+ record_id: str,
85
+ output_path: os.PathLike,
86
+ filenames: Optional[Sequence[str]] = None,
87
+ max_redirects: int = 5,
88
+ production: bool = True,
89
+ authors: bool = False,
90
+ ):
91
+ """Download one or more files from a record.
92
+
93
+ Parameters
94
+ ----------
95
+ record_id
96
+ The record ID from which to download the file. See
97
+ the module documentation for how to find the record ID.
98
+
99
+ output_path
100
+ Where to download the files. Must be an existing directory.
101
+ If you want control over exactly how each file is named,
102
+ use :func:`get_files_from_record` to get the URLs for each
103
+ file, then manually call :func:`download_content` for each
104
+ file you wish to download.
105
+
106
+ filenames
107
+ The name of the files in the record. Must match exactly.
108
+ If you are not sure of the filenames in the record,
109
+ they are the keys of the dictionary returned by
110
+ :func:`get_files_from_record`. If omitted, all files
111
+ are downloaded.
112
+
113
+ max_redirects
114
+ If the record's link to the file content returns a
115
+ redirection to another location, this function will follow
116
+ that redirection. This parameter sets the maximum number of
117
+ hops that the function can take. Usually, only a single
118
+ redirection should be necessary (from the record to the file
119
+ provider), but the default allows for a few extra hops.
120
+
121
+ production
122
+ Whether to download from the production repository. If ``False``,
123
+ the test (development) instance is used instead.
124
+
125
+ authors
126
+ Whether to download from CaltechAUTHORS instead of CaltechDATA.
127
+ """
128
+ if not os.path.isdir(output_path):
129
+ raise IOError(f"{output_path} is not an extant directory")
130
+
131
+ files = get_files_from_record(record_id, production=production, authors=authors)
132
+ if filenames is None:
133
+ filenames = sorted(files.keys())
134
+
135
+ for filename in filenames:
136
+ output_file = os.path.join(output_path, filename)
137
+ if filename not in files:
138
+ raise KeyError(f'File "{filename}" not found in record {record_id}')
139
+
140
+ entry = files[filename]
141
+ if "links" not in entry or "content" not in entry["links"]:
142
+ raise NotImplementedError(
143
+ f'Metadata for file "{filename}" in record {record_id} is '
144
+ "missing the content link"
145
+ )
146
+
147
+ content_url = entry["links"]["content"]
148
+ download_content(content_url, output_file, max_redirects=max_redirects)
149
+
150
+
151
+ def download_content(content_url: str, fname: os.PathLike, max_redirects=5):
152
+ """Download the contents of a file.
153
+
154
+ Parameters
155
+ ----------
156
+ content_url
157
+ The URL pointing to the contents of the file. In the dictionary
158
+ returned by :func:`get_files_from_record` (call it ``D``), this
159
+ is usually the value at ``D[FILENAME]["links"]["content"]``.
160
+
161
+ fname
162
+ The path to which to save the file contents. Will be overwritten!
163
+
164
+
165
+ max_redirects
166
+ If the ``content_url`` returns a redirection to another location,
167
+ this function will follow that redirection. This parameter sets
168
+ the maximum number of hops that the function can take. Usually,
169
+ only a single redirection should be necessary (from the record to
170
+ the file provider), but the default allows for a few extra hops.
171
+ """
172
+ url = content_url
173
+ # If max_redirects == 0, then this loop will never run. Since the
174
+ # likely expected behavior for ``max_redirects = 0`` is to only
175
+ # look at the link directly given back by the record, we add 1
176
+ # for the loop to ensure it runs once in that case.
177
+ for _ in range(max_redirects + 1):
178
+ with requests.get(url, stream=True) as r:
179
+ r.raise_for_status()
180
+ if "Location" in r.headers:
181
+ # Redirection - follow to the next URL
182
+ url = r.headers["Location"]
183
+ else:
184
+ with open(fname, "wb") as f:
185
+ content_length = r.headers.get("content-length", None)
186
+ if content_length is not None:
187
+ content_length = int(content_length)
188
+ pbar = tqdm(total=content_length // 1024, unit="kB")
189
+ for chunk in r.iter_content(chunk_size=1024):
190
+ if chunk:
191
+ pbar.update()
192
+ f.write(chunk)
193
+ print(f"Download to {fname} complete.")
194
+ return
195
+ raise RuntimeError(f"Exceeded maximum number of redirects ({max_redirects})")
196
+
197
+
198
+ if __name__ == "__main__":
199
+ parser = argparse.ArgumentParser(
200
+ description="download_from_record downloads the files attached to a "
201
+ "CaltechDATA or CaltechAUTHORS record"
202
+ )
203
+ parser.add_argument(
204
+ "record_id", help="The record ID for the record to download files from"
205
+ )
206
+ parser.add_argument(
207
+ "filenames",
208
+ nargs="*",
209
+ default=None,
210
+ help="The names of the files to download. If omitted, all files in "
211
+ "the record are downloaded.",
212
+ )
213
+ parser.add_argument(
214
+ "-output_path",
215
+ default=".",
216
+ help="Existing directory to download the files into",
217
+ )
218
+ parser.add_argument(
219
+ "-max_redirects",
220
+ type=int,
221
+ default=5,
222
+ help="Maximum number of redirects to follow per file",
223
+ )
224
+ parser.add_argument("-test", dest="production", action="store_false")
225
+ parser.add_argument("-authors", dest="authors", action="store_true")
226
+
227
+ args = parser.parse_args()
228
+
229
+ download_files_from_record(
230
+ args.record_id,
231
+ args.output_path,
232
+ filenames=args.filenames or None,
233
+ max_redirects=args.max_redirects,
234
+ production=args.production,
235
+ authors=args.authors,
236
+ )
@@ -1,20 +1,25 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: caltechdata_api
3
- Version: 2.0.0
3
+ Version: 2.2.0
4
4
  Summary: Python wrapper for CaltechDATA API.
5
- Home-page: https://github.com/caltechlibrary/caltechdata_api
6
- Author: Thomas E Morrell, Rohan Bhattarai, Elizabeth Won, Alexander A Abakah, Kshemaahna Nagi
7
- Author-email: tmorrell@caltech.edu, aabakah@caltech.edu, knagi@caltech.edu
8
- License: MIT
9
- Classifier: License :: OSI Approved :: MIT License
5
+ Author: Rohan Bhattarai, Elizabeth Won
6
+ Author-email: Thomas E Morrell <tmorrell@caltech.edu>, Alexander A Abakah <aabakah@caltech.edu>, Kshemaahna Nagi <knagi@caltech.edu>
7
+ Maintainer-email: Thomas E Morrell <tmorrell@caltech.edu>
8
+ License-Expression: BSD-3-Clause
9
+ Project-URL: Homepage, https://github.com/caltechlibrary/caltechdata_api
10
+ Project-URL: Repository, https://github.com/caltechlibrary/caltechdata_api
11
+ Project-URL: Issues, https://github.com/caltechlibrary/caltechdata_api/issues
12
+ Keywords: metadata,software,InvenioRDM
10
13
  Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.7
12
- Classifier: Programming Language :: Python :: 3.8
13
14
  Classifier: Programming Language :: Python :: 3.9
14
15
  Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
15
20
  Classifier: Programming Language :: Python :: Implementation :: CPython
16
21
  Classifier: Operating System :: OS Independent
17
- Requires-Python: >=3.6.0
22
+ Requires-Python: >=3.9
18
23
  Description-Content-Type: text/markdown
19
24
  License-File: LICENSE
20
25
  Requires-Dist: requests
@@ -44,7 +49,7 @@ The `caltechdata_api` Python library provides a convenient interface for interac
44
49
 
45
50
  ## Requirements
46
51
 
47
- - Python 3.6+
52
+ - Python 3.9+
48
53
 
49
54
  ## Installation
50
55
 
@@ -54,6 +59,18 @@ Install the library via pip:
54
59
  pip install caltechdata_api
55
60
  ```
56
61
 
62
+ Or add it to a [uv](https://docs.astral.sh/uv/) project:
63
+
64
+ ```shell
65
+ uv add caltechdata_api
66
+ ```
67
+
68
+ You can also run a script using uv:
69
+
70
+ ```shell
71
+ uv run write.py
72
+ ```
73
+
57
74
  ## Examples
58
75
 
59
76
  There are some example python scripts in the GitHub repository.
@@ -61,7 +78,7 @@ There are some example python scripts in the GitHub repository.
61
78
  ### Create a record:
62
79
 
63
80
  ```shell
64
- python write.py example.json -fnames logo.gif
81
+ uv run write.py example.json -fnames logo.gif
65
82
  # Output: pbkn6-m9y63 (unique identifier)
66
83
  ```
67
84
  > The response will be the unique identifier for the record. You can put this at
@@ -71,7 +88,7 @@ https://data.caltechlibrary.dev/records/pbkn6-m9y63)
71
88
  ### Edit a record
72
89
  Make changes to the example.json file to see a change)
73
90
  ```
74
- python edit.py example.json -id pbkn6-m9y63
91
+ uv run edit.py example.json -id pbkn6-m9y63
75
92
  10.33569/pbkn6-m9y63
76
93
  ```
77
94
  > The response is the DOI for the record, which includes the unique identifier
@@ -82,13 +99,13 @@ Some groups have worked with the library to create custom DOIs. These can be
82
99
  passed in the metadata like:
83
100
 
84
101
  ```shell
85
- python write.py example_custom.json -fnames logo.gif
102
+ uv run write.py example_custom.json -fnames logo.gif
86
103
  m6zxz-p4j22
87
104
  ```
88
105
 
89
106
  And then you can edit with
90
107
  ```
91
- python edit.py example_custom.json -id m6zxz-p4j22
108
+ uv run edit.py example_custom.json -id m6zxz-p4j22
92
109
  10.5281/inveniordm.1234
93
110
  ```
94
111
 
@@ -1,15 +1,13 @@
1
1
  LICENSE
2
2
  README.md
3
3
  pyproject.toml
4
- setup.cfg
5
- setup.py
6
4
  caltechdata_api/__init__.py
7
5
  caltechdata_api/caltechdata_edit.py
8
6
  caltechdata_api/caltechdata_write.py
9
7
  caltechdata_api/cli.py
10
8
  caltechdata_api/customize_schema.py
11
9
  caltechdata_api/download_file.py
12
- caltechdata_api/get_files.py
10
+ caltechdata_api/download_from_record.py
13
11
  caltechdata_api/get_metadata.py
14
12
  caltechdata_api/md_to_json.py
15
13
  caltechdata_api/utils.py
@@ -0,0 +1,69 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "caltechdata_api"
7
+ version = "2.2.0"
8
+ description = "Python wrapper for CaltechDATA API."
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = "BSD-3-Clause"
12
+ license-files = ["LICENSE"]
13
+ authors = [
14
+ {name = "Thomas E Morrell", email = "tmorrell@caltech.edu"},
15
+ {name = "Rohan Bhattarai"},
16
+ {name = "Elizabeth Won"},
17
+ {name = "Alexander A Abakah", email = "aabakah@caltech.edu"},
18
+ {name = "Kshemaahna Nagi", email = "knagi@caltech.edu"},
19
+ ]
20
+ maintainers = [
21
+ {name = "Thomas E Morrell", email = "tmorrell@caltech.edu"},
22
+ ]
23
+ keywords = ["metadata", "software", "InvenioRDM"]
24
+ classifiers = [
25
+ "Programming Language :: Python :: 3",
26
+ "Programming Language :: Python :: 3.9",
27
+ "Programming Language :: Python :: 3.10",
28
+ "Programming Language :: Python :: 3.11",
29
+ "Programming Language :: Python :: 3.12",
30
+ "Programming Language :: Python :: 3.13",
31
+ "Programming Language :: Python :: 3.14",
32
+ "Programming Language :: Python :: Implementation :: CPython",
33
+ "Operating System :: OS Independent",
34
+ ]
35
+ dependencies = [
36
+ "requests",
37
+ "datacite>1.1.0",
38
+ "tqdm>=4.62.3",
39
+ "pyyaml",
40
+ "s3fs",
41
+ "cryptography",
42
+ "s3cmd",
43
+ "jsonschema-specifications",
44
+ ]
45
+
46
+ [project.urls]
47
+ Homepage = "https://github.com/caltechlibrary/caltechdata_api"
48
+ Repository = "https://github.com/caltechlibrary/caltechdata_api"
49
+ Issues = "https://github.com/caltechlibrary/caltechdata_api/issues"
50
+
51
+ [project.scripts]
52
+ caltechdata_api = "caltechdata_api.cli:main"
53
+
54
+ [dependency-groups]
55
+ dev = [
56
+ "pytest>=7.0",
57
+ "black",
58
+ ]
59
+
60
+ [tool.setuptools.packages.find]
61
+ include = ["caltechdata_api*"]
62
+
63
+ [tool.setuptools.package-data]
64
+ caltechdata_api = ["vocabularies.yaml", "vocabularies/*"]
65
+
66
+ [tool.pytest.ini_options]
67
+ addopts = "--verbose"
68
+ testpaths = ["tests"]
69
+ pythonpath = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -7,6 +7,10 @@ from caltechdata_api import (
7
7
  import json
8
8
  import os
9
9
 
10
+ # Any file will do here, we just need something to attach to the test records
11
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
12
+ UPLOAD_FILE = os.path.join(BASE_DIR, "helpers.py")
13
+
10
14
 
11
15
  def test_datacite_rdm_conversion(full_datacite43_record, full_rdm_record):
12
16
 
@@ -48,7 +52,7 @@ def test_datacite_rdm_create_edit(full_datacite43_record):
48
52
  full_datacite43_record,
49
53
  schema="43",
50
54
  production=False,
51
- files=["helpers.py"],
55
+ files=[UPLOAD_FILE],
52
56
  publish=True,
53
57
  token=env_token,
54
58
  )
@@ -77,7 +81,7 @@ def test_datacite_rdm_create_edit(full_datacite43_record):
77
81
  new_doi = caltechdata_edit(
78
82
  recid,
79
83
  full_datacite43_record,
80
- files=["helpers.py"],
84
+ files=[UPLOAD_FILE],
81
85
  schema="43",
82
86
  production=False,
83
87
  publish=True,
@@ -0,0 +1,76 @@
1
+ import os
2
+ import pytest
3
+ import logging
4
+ from caltechdata_api import validate_metadata as validator43
5
+ from helpers import load_json_path
6
+
7
+ # Configure logging
8
+ logging.basicConfig(level=logging.DEBUG)
9
+ logger = logging.getLogger(__name__)
10
+
11
+ # Dynamically determine the base path
12
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
13
+ INVALID_DATACITE43_DIR = os.path.join(BASE_DIR, "data", "invalid_datacite43")
14
+
15
+
16
+ # Function to get all JSON files in the directory
17
+ def get_all_json_files(directory):
18
+ return [
19
+ os.path.join(directory, f) for f in os.listdir(directory) if f.endswith(".json")
20
+ ]
21
+
22
+
23
+ # Get list of all valid and invalid JSON files
24
+ VALID_DATACITE43_FILES = get_all_json_files(
25
+ os.path.join(BASE_DIR, "data", "datacite43")
26
+ )
27
+ INVALID_DATACITE43_FILES = get_all_json_files(INVALID_DATACITE43_DIR)
28
+
29
+
30
+ @pytest.mark.parametrize("valid_file", VALID_DATACITE43_FILES)
31
+ def test_valid_json(valid_file):
32
+ """Test that valid example files validate successfully."""
33
+ print(f"\nValidating file: {valid_file}")
34
+ json_data = load_json_path(valid_file)
35
+ validation_errors = None
36
+ try:
37
+ validation_errors = validator43(json_data)
38
+ except ValueError as e:
39
+ pytest.fail(f"Validation failed for: {valid_file}\nErrors: {str(e)}")
40
+
41
+ assert (
42
+ not validation_errors
43
+ ), f"Validation failed for: {valid_file}\nErrors: {validation_errors}"
44
+ print(f"Validation passed for: {valid_file}")
45
+
46
+
47
+ @pytest.mark.parametrize("invalid_file", INVALID_DATACITE43_FILES)
48
+ def test_invalid_json(invalid_file):
49
+ """Test that invalid example files do not validate successfully."""
50
+ logger.debug(f"Attempting to validate invalid file: {invalid_file}")
51
+
52
+ json_data = load_json_path(invalid_file)
53
+
54
+ def validate_wrapper():
55
+ try:
56
+ validation_errors = validator43(json_data)
57
+
58
+ logger.debug(f"Validation result for {invalid_file}: {validation_errors}")
59
+
60
+ if validation_errors:
61
+ logger.debug(f"Found validation errors in {invalid_file}")
62
+ return
63
+
64
+ logger.error(
65
+ f"No validation errors found for supposedly invalid file: {invalid_file}"
66
+ )
67
+ raise ValueError(
68
+ f"Validation did not fail for invalid file: {invalid_file}"
69
+ )
70
+
71
+ except Exception as e:
72
+ logger.error(f"Validation exception for {invalid_file}: {str(e)}")
73
+ raise
74
+
75
+ with pytest.raises((ValueError, KeyError, AssertionError, TypeError)):
76
+ validate_wrapper()
@@ -1,49 +0,0 @@
1
- import argparse
2
- import requests
3
-
4
-
5
- def get_files(idv, production=True):
6
- # Returns file block
7
-
8
- if production == True:
9
- api_url = "https://data.caltech.edu/api/records/"
10
- else:
11
- api_url = "https://data.caltechlibrary.dev/api/records/"
12
-
13
- r = requests.get(api_url + str(idv) + "/files")
14
- r_data = r.json()
15
- if "message" in r_data:
16
- raise AssertionError(
17
- "id "
18
- + str(idv)
19
- + " expected http status 200, got "
20
- + str(r.status_code)
21
- + " "
22
- + r_data["message"]
23
- )
24
- if not "entries" in r_data:
25
- raise AssertionError("expected as entries property in response, got " + r_data)
26
- return r_data["entries"]
27
-
28
-
29
- if __name__ == "__main__":
30
- parser = argparse.ArgumentParser(
31
- description="get_files queries the caltechDATA (Invenio 3) API\
32
- and returns file information"
33
- )
34
- parser.add_argument(
35
- "ids",
36
- metavar="ID",
37
- type=str,
38
- nargs="+",
39
- help="The CaltechDATA ID for each record of interest",
40
- )
41
- parser.add_argument("-test", dest="production", action="store_false")
42
-
43
- args = parser.parse_args()
44
-
45
- production = args.production
46
-
47
- for idv in args.ids:
48
- metadata = get_files(idv, production)
49
- print(metadata)
@@ -1,3 +0,0 @@
1
- [build-system]
2
- requires = ["setuptools>=61.0", "wheel"]
3
- build-backend = "setuptools.build_meta"
@@ -1,55 +0,0 @@
1
- [build-system]
2
- requires = ["setuptools>=64.0","wheel"]
3
- build-backend = "setuptools.build_meta"
4
-
5
- [metadata]
6
- name = caltechdata_api
7
- version = 2.0.0
8
- author = Thomas E Morrell, Rohan Bhattarai, Elizabeth Won, Alexander A Abakah, Kshemaahna Nagi
9
- author_email = tmorrell@caltech.edu, aabakah@caltech.edu, knagi@caltech.edu
10
- description = Python wrapper for CaltechDATA API.
11
- long_description = file: README.md
12
- long_description_content_type = text/markdown
13
- url = https://github.com/caltechlibrary/caltechdata_api
14
- license = MIT
15
- classifiers =
16
- License :: OSI Approved :: MIT License
17
- Programming Language :: Python :: 3
18
- Programming Language :: Python :: 3.7
19
- Programming Language :: Python :: 3.8
20
- Programming Language :: Python :: 3.9
21
- Programming Language :: Python :: 3.10
22
- Programming Language :: Python :: Implementation :: CPython
23
- Operating System :: OS Independent
24
-
25
- [options]
26
- packages = find:
27
- python_requires = >=3.6.0
28
- install_requires =
29
- requests
30
- datacite>1.1.0
31
- tqdm>=4.62.3
32
- pyyaml
33
- s3fs
34
- cryptography
35
- s3cmd
36
- jsonschema-specifications
37
- include_package_data = True
38
-
39
- [options.packages.find]
40
- exclude = tests
41
-
42
- [options.package_data]
43
- caltechdata_api = vocabularies.yaml, vocabularies/*
44
-
45
- [options.entry_points]
46
- console_scripts =
47
- caltechdata_api=caltechdata_api.cli:main
48
-
49
- [tool:pytest]
50
- addopts = --verbose
51
-
52
- [egg_info]
53
- tag_build =
54
- tag_date = 0
55
-
@@ -1,3 +0,0 @@
1
- from setuptools import setup
2
-
3
- setup()
@@ -1,166 +0,0 @@
1
- import os
2
- import pytest
3
- import logging
4
- from caltechdata_api import validate_metadata as validator43
5
- from helpers import load_json_path
6
-
7
- # Configure logging
8
- logging.basicConfig(level=logging.DEBUG)
9
- logger = logging.getLogger(__name__)
10
-
11
- # Dynamically determine the base path
12
- BASE_DIR = os.path.dirname(os.path.abspath(__file__))
13
- INVALID_DATACITE43_DIR = os.path.join(BASE_DIR, "data", "invalid_datacite43")
14
- DATACITE43_DIR = os.path.join(BASE_DIR, "data")
15
-
16
-
17
- # Function to get all JSON files in the directory
18
- def get_all_json_files(directory):
19
- return [
20
- os.path.join(directory, f) for f in os.listdir(directory) if f.endswith(".json")
21
- ]
22
-
23
-
24
- # Get list of all valid and invalid JSON files
25
- VALID_DATACITE43_FILES = get_all_json_files(
26
- os.path.join(BASE_DIR, "data", "datacite43")
27
- )
28
- INVALID_DATACITE43_FILES = get_all_json_files(INVALID_DATACITE43_DIR)
29
-
30
-
31
- @pytest.mark.parametrize("valid_file", VALID_DATACITE43_FILES)
32
- def test_valid_json(valid_file):
33
- """Test that valid example files validate successfully."""
34
- print(f"\nValidating file: {valid_file}")
35
- json_data = load_json_path(valid_file)
36
- validation_errors = None
37
- try:
38
- validation_errors = validator43(json_data)
39
- except ValueError as e:
40
- pytest.fail(f"Validation failed for: {valid_file}\nErrors: {str(e)}")
41
-
42
- assert (
43
- not validation_errors
44
- ), f"Validation failed for: {valid_file}\nErrors: {validation_errors}"
45
- print(f"Validation passed for: {valid_file}")
46
-
47
-
48
- @pytest.mark.parametrize("invalid_file", INVALID_DATACITE43_FILES)
49
- def test_invalid_json(invalid_file):
50
- """Test that invalid example files do not validate successfully."""
51
- logger.debug(f"Attempting to validate invalid file: {invalid_file}")
52
-
53
- json_data = load_json_path(invalid_file)
54
-
55
- def validate_wrapper():
56
- try:
57
- validation_errors = validator43(json_data)
58
-
59
- logger.debug(f"Validation result for {invalid_file}: {validation_errors}")
60
-
61
- if validation_errors:
62
- logger.debug(f"Found validation errors in {invalid_file}")
63
- return
64
-
65
- logger.error(
66
- f"No validation errors found for supposedly invalid file: {invalid_file}"
67
- )
68
- raise ValueError(
69
- f"Validation did not fail for invalid file: {invalid_file}"
70
- )
71
-
72
- except Exception as e:
73
- logger.error(f"Validation exception for {invalid_file}: {str(e)}")
74
- raise
75
-
76
- with pytest.raises((ValueError, KeyError, AssertionError, TypeError)):
77
- validate_wrapper()
78
-
79
-
80
- @pytest.mark.parametrize(
81
- "missing_field_file",
82
- [
83
- {
84
- "file": os.path.join(DATACITE43_DIR, "missing_creators.json"),
85
- "missing_field": "creators",
86
- },
87
- {
88
- "file": os.path.join(DATACITE43_DIR, "missing_titles.json"),
89
- "missing_field": "titles",
90
- },
91
- ],
92
- )
93
- def test_missing_required_fields(missing_field_file):
94
- """Test that JSON files missing required fields fail validation."""
95
- print(
96
- f"\nTesting missing field: {missing_field_file['missing_field']} in file: {missing_field_file['file']}"
97
- )
98
-
99
- # Skip the test if the file doesn't exist
100
- if not os.path.exists(missing_field_file["file"]):
101
- pytest.skip(f"Test file not found: {missing_field_file['file']}")
102
-
103
- json_data = load_json_path(missing_field_file["file"])
104
- with pytest.raises(
105
- ValueError,
106
- match=f"Missing required metadata field: {missing_field_file['missing_field']}",
107
- ):
108
- validator43(json_data)
109
-
110
-
111
- @pytest.mark.parametrize(
112
- "type_error_file",
113
- [
114
- {
115
- "file": os.path.join(DATACITE43_DIR, "type_error_creators.json"),
116
- "field": "creators",
117
- },
118
- {
119
- "file": os.path.join(DATACITE43_DIR, "type_error_dates.json"),
120
- "field": "dates",
121
- },
122
- ],
123
- )
124
- def test_incorrect_field_types(type_error_file):
125
- """Test that JSON files with incorrect field types fail validation."""
126
- print(
127
- f"\nTesting incorrect type in field: {type_error_file['field']} for file: {type_error_file['file']}"
128
- )
129
-
130
- # Skip the test if the file doesn't exist
131
- if not os.path.exists(type_error_file["file"]):
132
- pytest.skip(f"Test file not found: {type_error_file['file']}")
133
-
134
- json_data = load_json_path(type_error_file["file"])
135
- with pytest.raises(
136
- ValueError, match=f"Incorrect type for field: {type_error_file['field']}"
137
- ):
138
- validator43(json_data)
139
-
140
-
141
- def test_multiple_errors():
142
- """Test JSON file with multiple issues to check all errors are raised."""
143
- multiple_errors_file = os.path.join(DATACITE43_DIR, "multiple_errors.json")
144
-
145
- # Skip the test if the file doesn't exist
146
- if not os.path.exists(multiple_errors_file):
147
- pytest.skip(f"Test file not found: {multiple_errors_file}")
148
-
149
- json_data = load_json_path(multiple_errors_file)
150
- with pytest.raises(ValueError, match="Multiple validation errors"):
151
- validator43(json_data)
152
-
153
-
154
- def test_error_logging(caplog):
155
- """Test that errors are logged correctly during validation."""
156
- some_invalid_file = os.path.join(INVALID_DATACITE43_DIR, "some_invalid_file.json")
157
-
158
- # Skip the test if the file doesn't exist
159
- if not os.path.exists(some_invalid_file):
160
- pytest.skip(f"Test file not found: {some_invalid_file}")
161
-
162
- json_data = load_json_path(some_invalid_file)
163
- with caplog.at_level(logging.ERROR):
164
- with pytest.raises(ValueError):
165
- validator43(json_data)
166
- assert "Validation failed" in caplog.text
File without changes