era-data 0.1.0__py3-none-any.whl

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.
era_data/__init__.py ADDED
@@ -0,0 +1,123 @@
1
+ """The ERA journal and conference lists and the ANZSRC classifications, as an RDF graph.
2
+
3
+ The data is the triple store published with the era_data release RELEASE: era_data.nt.gz, gzipped N-Triples.
4
+ This package has none of it. On first use, `path()` downloads the file from GitHub, checks it against SHA256 and
5
+ keeps it in `cache_dir()`. Set ERA_DATA_NT to the path of a local copy to use that instead, without a download.
6
+ The download gives up when the server sends nothing for TIMEOUT seconds.
7
+
8
+ import era_data
9
+
10
+ graph = era_data.graph() # an rdflib.Graph of every triple
11
+ """
12
+
13
+ import gzip
14
+ import hashlib
15
+ import os
16
+ import shutil
17
+ import tempfile
18
+ import urllib.request
19
+ import zlib
20
+ from os import PathLike
21
+ from pathlib import Path
22
+
23
+ import platformdirs
24
+ import rdflib
25
+
26
+ __version__ = "0.1.0"
27
+
28
+ # The release the data comes from, and the checksum of the N-Triples text in its file; `mise run pin` writes both.
29
+ RELEASE = "data-v4"
30
+ SHA256 = "bc68654497e876d01b82efea1fbf30cc8f75320d98ecc742cc9b87d764bcf5d6"
31
+ FILE = "era_data.nt.gz"
32
+ URL = f"https://github.com/jayvdb/era_data/releases/download/{RELEASE}/{FILE}"
33
+
34
+ # The environment variable naming a local copy of the file, which is used as it is.
35
+ LOCAL = "ERA_DATA_NT"
36
+ # The environment variable naming the directory to keep downloads in.
37
+ CACHE = "ERA_DATA_CACHE"
38
+ # How many seconds a download waits for the server to connect or send more, before it gives up.
39
+ TIMEOUT = 60
40
+
41
+ type StrPath = str | PathLike[str]
42
+
43
+
44
+ class ChecksumError(ValueError):
45
+ """A downloaded file is not the one the release published."""
46
+
47
+
48
+ def cache_dir() -> Path:
49
+ """Return the directory the file is kept in once downloaded.
50
+
51
+ It is ERA_DATA_CACHE when that is set, and otherwise era_data in the user's cache directory, as platformdirs
52
+ finds it: %LOCALAPPDATA% on Windows, ~/Library/Caches on macOS and $XDG_CACHE_HOME, or ~/.cache, elsewhere.
53
+ Each release has its own directory in it.
54
+ """
55
+ if base := os.environ.get(CACHE):
56
+ return Path(base) / RELEASE
57
+ # No author or "Cache" subdirectory, which platformdirs adds on Windows by default, so the directory is the same
58
+ # as the Rust crate's.
59
+ return platformdirs.user_cache_path("era_data", appauthor=False, opinion=False) / RELEASE
60
+
61
+
62
+ def sha256(file: StrPath) -> str:
63
+ """Return the SHA-256, in hex, of the N-Triples text in the gzipped file at a path.
64
+
65
+ The text is what is checked, not the gzip bytes around it, which differ with the zlib that compressed them.
66
+ """
67
+ with gzip.open(file, "rb") as f:
68
+ return hashlib.file_digest(f, "sha256").hexdigest()
69
+
70
+
71
+ def cached(file: StrPath) -> str | None:
72
+ """Return the checksum of a kept copy of the file, or None when it is not gzip, so that it is downloaded again.
73
+
74
+ Any other error, such as being unable to read it, is raised.
75
+ """
76
+ try:
77
+ return sha256(file)
78
+ except gzip.BadGzipFile, EOFError, zlib.error:
79
+ return None
80
+
81
+
82
+ def fetch(url: str = URL, checksum: str = SHA256, dest: StrPath | None = None) -> Path:
83
+ """Return the path of the file at url, downloading it to dest unless a copy with that checksum is there already.
84
+
85
+ dest defaults to FILE in `cache_dir()`. The download goes to a temporary directory beside it, and the file replaces
86
+ dest, in one rename, only once its checksum matches, so dest is never a partial or wrong file. ChecksumError is
87
+ raised when it does not, and the temporary directory is removed whatever happens. A copy at dest that is damaged,
88
+ and is not gzip, is downloaded again.
89
+ """
90
+ dest = Path(dest) if dest else cache_dir() / FILE
91
+ if dest.is_file() and cached(dest) == checksum:
92
+ return dest
93
+ dest.parent.mkdir(parents=True, exist_ok=True)
94
+ # A directory rather than a file, as Windows will not rename a file that is still open.
95
+ with tempfile.TemporaryDirectory(dir=dest.parent) as tmp:
96
+ part = Path(tmp) / FILE
97
+ with urllib.request.urlopen(url, timeout=TIMEOUT) as response, open(part, "wb") as f:
98
+ shutil.copyfileobj(response, f)
99
+ if (actual := sha256(part)) != checksum:
100
+ raise ChecksumError(f"{url} has SHA-256 {actual}, not {checksum}")
101
+ part.replace(dest)
102
+ return dest
103
+
104
+
105
+ def path() -> Path:
106
+ """Return the path of the file: ERA_DATA_NT when that is set, or else the release's file, downloaded if need be."""
107
+ if local := os.environ.get(LOCAL):
108
+ return Path(local)
109
+ return fetch()
110
+
111
+
112
+ def ntriples(source: StrPath | None = None) -> str:
113
+ """Return the whole triple store as N-Triples text, from source or, by default, `path()`."""
114
+ with gzip.open(source or path(), "rt", encoding="ascii") as f:
115
+ return f.read()
116
+
117
+
118
+ def graph(source: StrPath | None = None) -> rdflib.Graph:
119
+ """Return the whole triple store as an rdflib.Graph, from source or, by default, `path()`."""
120
+ result = rdflib.Graph()
121
+ with gzip.open(source or path(), "rb") as f:
122
+ result.parse(f, format="nt")
123
+ return result
era_data/py.typed ADDED
File without changes
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.5
2
+ Name: era-data
3
+ Version: 0.1.0
4
+ Summary: The ERA journal and conference lists and the ANZSRC classifications, as an RDF graph
5
+ Project-URL: Homepage, https://github.com/jayvdb/era_data
6
+ Project-URL: Issues, https://github.com/jayvdb/era_data/issues
7
+ Project-URL: Vocabulary, https://github.com/jayvdb/era_data/blob/master/VOCAB.md
8
+ Author: John Vandenberg
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: anzsrc,australia,era,rdf,research
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Topic :: Scientific/Engineering
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.14
21
+ Requires-Dist: platformdirs>=4
22
+ Requires-Dist: rdflib>=7
23
+ Provides-Extra: test
24
+ Requires-Dist: pytest; extra == 'test'
25
+ Description-Content-Type: text/markdown
26
+
27
+ # era-data
28
+
29
+ The Excellence in Research for Australia (ERA) journal lists of 2010, 2012, 2015, 2018 and 2023, its conference lists
30
+ of 2010 and 2015, and the ANZSRC 2008 and 2020 Fields of Research and Socio-Economic Objectives, as one RDF graph.
31
+
32
+ The journals and conferences can also be searched in a browser, at
33
+ [era-open-data.vercel.app](https://era-open-data.vercel.app/).
34
+
35
+ ```python
36
+ import era_data
37
+
38
+ graph = era_data.graph() # an rdflib.Graph of every triple
39
+ ```
40
+
41
+ The package holds no data. On first use it downloads `era_data.nt.gz`, gzipped N-Triples, from the
42
+ [era_data](https://github.com/jayvdb/era_data) release `data-v4`, checks its SHA-256 and keeps it in the user's cache
43
+ directory. Set `ERA_DATA_NT` to the path of a local copy to use that instead, and `ERA_DATA_CACHE` to keep the
44
+ download somewhere else.
45
+
46
+ - `era_data.graph()` returns the triples as an `rdflib.Graph`.
47
+ - `era_data.ntriples()` returns them as N-Triples text.
48
+ - `era_data.path()` returns the path of the file.
49
+
50
+ `era_data.graph()` is slow: rdflib parses the 1.6 million triples into memory, which takes about two minutes and
51
+ 2 GB of memory on a 2019 laptop. Where that matters, load the graph once and keep it, or give `era_data.path()` or
52
+ `era_data.ntriples()` to a faster RDF library or a triple store.
53
+
54
+ `examples/journal_history.py` prints how one journal changed from list to list: its titles, fields of research,
55
+ ISSNs and, in ERA 2010, its rank. It defaults to ERA ID 4465, IEEE Transactions on Cybernetics:
56
+
57
+ ```bash
58
+ python examples/journal_history.py 4465
59
+ ```
60
+
61
+ The repository's README describes the data, and its
62
+ [VOCAB.md](https://github.com/jayvdb/era_data/blob/master/VOCAB.md) the vocabulary.
63
+
64
+ ## Copyright
65
+
66
+ The data comes from the Australian Research Council and the Australian Bureau of Statistics. The
67
+ [Copyright section](https://github.com/jayvdb/era_data#copyright) of the repository's README describes the terms
68
+ each was published under.
@@ -0,0 +1,6 @@
1
+ era_data/__init__.py,sha256=AZdqNMnGGorRV_HiMLhtr4G8MLta1OHL_JhEp3CH4Wk,4969
2
+ era_data/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ era_data-0.1.0.dist-info/METADATA,sha256=9zkT2IzZI7sgQ7GJtzoH1prQtQFi_EtVab4UYdyxqYY,2946
4
+ era_data-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
5
+ era_data-0.1.0.dist-info/licenses/LICENSE,sha256=xggEnJVUrQiyk5NDGs6HVKvNvgnpFNapXGCeGPxXvgw,1072
6
+ era_data-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 John Vandenberg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.