pyrefman-arxiv 0.1.0a1__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.
- pyrefman_arxiv-0.1.0a1/.gitignore +20 -0
- pyrefman_arxiv-0.1.0a1/LICENSE +21 -0
- pyrefman_arxiv-0.1.0a1/PKG-INFO +39 -0
- pyrefman_arxiv-0.1.0a1/README.md +25 -0
- pyrefman_arxiv-0.1.0a1/pyproject.toml +30 -0
- pyrefman_arxiv-0.1.0a1/src/pyrefman_arxiv/__init__.py +103 -0
- pyrefman_arxiv-0.1.0a1/tests/test_arxiv.py +139 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.pyc
|
|
4
|
+
.venv/
|
|
5
|
+
|
|
6
|
+
# Throwaway demo library created by the prototype
|
|
7
|
+
proto/.demo-library/
|
|
8
|
+
proto/.demo-library-custom/
|
|
9
|
+
|
|
10
|
+
# Local tooling
|
|
11
|
+
.site/
|
|
12
|
+
dist/
|
|
13
|
+
*.egg-info/
|
|
14
|
+
|
|
15
|
+
# Scratch space
|
|
16
|
+
.tmp/
|
|
17
|
+
|
|
18
|
+
# Library cache (see INDEX_NAME)
|
|
19
|
+
.refman-index.json
|
|
20
|
+
.refman-index.log
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Icntaann
|
|
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.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pyrefman-arxiv
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: arXiv resolver plugin for pyrefman
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Intended Audience :: Science/Research
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Requires-Python: >=3.11
|
|
11
|
+
Requires-Dist: arxiv>=2.1
|
|
12
|
+
Requires-Dist: pyrefman>=0.1.0a4
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# pyrefman-arxiv
|
|
16
|
+
|
|
17
|
+
An arXiv resolver for [pyrefman](https://github.com/Icntaann/pyrefman).
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
pip install pyrefman pyrefman-arxiv
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from pyrefman import ReferenceManager
|
|
25
|
+
|
|
26
|
+
manager = ReferenceManager("library")
|
|
27
|
+
reference = manager.add(identifier="arXiv:2101.00001")
|
|
28
|
+
print(reference.title, reference.year)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The plugin registers itself through the `pyrefman.resolvers` entry-point group. The arXiv
|
|
32
|
+
identifier has no CSL field, so it is stored in the record's own
|
|
33
|
+
`custom.refman.identifiers` — which is what lets two preprints, one from arXiv and one from
|
|
34
|
+
a DOI, be recognised as the same work.
|
|
35
|
+
|
|
36
|
+
**Status: alpha.** Mapped: title, authors, publication date, journal reference, DOI, URL,
|
|
37
|
+
and the identifier itself. Authors arrive as literal names rather than split into family
|
|
38
|
+
and given — arXiv writes them as one string, and guessing the split would break names like
|
|
39
|
+
*van der Waals*. Abstracts are not mapped: they belong to an enricher, not a resolver.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# pyrefman-arxiv
|
|
2
|
+
|
|
3
|
+
An arXiv resolver for [pyrefman](https://github.com/Icntaann/pyrefman).
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pip install pyrefman pyrefman-arxiv
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from pyrefman import ReferenceManager
|
|
11
|
+
|
|
12
|
+
manager = ReferenceManager("library")
|
|
13
|
+
reference = manager.add(identifier="arXiv:2101.00001")
|
|
14
|
+
print(reference.title, reference.year)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The plugin registers itself through the `pyrefman.resolvers` entry-point group. The arXiv
|
|
18
|
+
identifier has no CSL field, so it is stored in the record's own
|
|
19
|
+
`custom.refman.identifiers` — which is what lets two preprints, one from arXiv and one from
|
|
20
|
+
a DOI, be recognised as the same work.
|
|
21
|
+
|
|
22
|
+
**Status: alpha.** Mapped: title, authors, publication date, journal reference, DOI, URL,
|
|
23
|
+
and the identifier itself. Authors arrive as literal names rather than split into family
|
|
24
|
+
and given — arXiv writes them as one string, and guessing the split would break names like
|
|
25
|
+
*van der Waals*. Abstracts are not mapped: they belong to an enricher, not a resolver.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pyrefman-arxiv"
|
|
7
|
+
version = "0.1.0a1"
|
|
8
|
+
description = "arXiv resolver plugin for pyrefman"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
dependencies = ["pyrefman>=0.1.0a4", "arxiv>=2.1"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[dependency-groups]
|
|
21
|
+
dev = ["pytest>=8"]
|
|
22
|
+
|
|
23
|
+
[project.entry-points."pyrefman.resolvers"]
|
|
24
|
+
arxiv = "pyrefman_arxiv:ArxivResolver"
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.wheel]
|
|
27
|
+
packages = ["src/pyrefman_arxiv"]
|
|
28
|
+
|
|
29
|
+
[tool.pytest.ini_options]
|
|
30
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""An arXiv resolver for pyrefman.
|
|
2
|
+
|
|
3
|
+
Same contract as the Crossref plugin: a name, the schemes it answers for, and a
|
|
4
|
+
``resolve()`` that returns ``(fields, error)``. The arXiv identifier has no CSL field, so
|
|
5
|
+
it travels in the record's own ``custom.refman.identifiers`` slot.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
from collections.abc import Callable
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
import arxiv
|
|
15
|
+
|
|
16
|
+
#: arXiv titles arrive with hard-wrapped lines and stray runs of spaces.
|
|
17
|
+
_SPACES = re.compile(r"\s+")
|
|
18
|
+
|
|
19
|
+
#: arXiv versions a paper: 2101.00001v2 is the same work as 2101.00001.
|
|
20
|
+
_VERSION = re.compile(r"v\d+$", re.IGNORECASE)
|
|
21
|
+
|
|
22
|
+
#: A preprint without a journal reference is not yet an article; one with it usually is.
|
|
23
|
+
JOURNAL_REF_MEANS = "article-journal"
|
|
24
|
+
DEFAULT_TYPE = "posted-content"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ArxivResolver:
|
|
28
|
+
"""Looks an identifier up in the arXiv API."""
|
|
29
|
+
|
|
30
|
+
name = "arxiv"
|
|
31
|
+
schemes: tuple[str, ...] = ("arxiv",)
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
fetch: Callable[[str], arxiv.Result] | None = None,
|
|
36
|
+
page_size: int = 1,
|
|
37
|
+
) -> None:
|
|
38
|
+
self._fetch = fetch or (lambda identifier: _fetch(identifier, page_size))
|
|
39
|
+
|
|
40
|
+
def resolve(self, identifier: str) -> tuple[dict[str, Any] | None, str | None]:
|
|
41
|
+
"""Return CSL fields for an arXiv id, or a reason why there are none."""
|
|
42
|
+
try:
|
|
43
|
+
result = self._fetch(identifier)
|
|
44
|
+
except StopIteration:
|
|
45
|
+
return None, f"arXiv has no entry for {identifier!r}"
|
|
46
|
+
except arxiv.HTTPError as error:
|
|
47
|
+
return None, f"arXiv answered with status {error.status}"
|
|
48
|
+
except arxiv.ArxivError as error:
|
|
49
|
+
return None, f"arXiv could not be read: {error}"
|
|
50
|
+
except Exception as error: # noqa: BLE001 - a plugin must not raise through the caller
|
|
51
|
+
return None, f"arXiv lookup failed: {error}"
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
return to_csl(result), None
|
|
55
|
+
except (AttributeError, TypeError, ValueError) as error:
|
|
56
|
+
return None, f"arXiv sent an unexpected record: {error}"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def to_csl(result: arxiv.Result) -> dict[str, Any]:
|
|
60
|
+
"""One arXiv result as CSL fields plus the identifier CSL cannot hold."""
|
|
61
|
+
title = _SPACES.sub(" ", getattr(result, "title", "") or "").strip()
|
|
62
|
+
fields: dict[str, Any] = {"title": title} if title else {}
|
|
63
|
+
|
|
64
|
+
authors = [
|
|
65
|
+
{"literal": _SPACES.sub(" ", author.name).strip()}
|
|
66
|
+
for author in (getattr(result, "authors", None) or [])
|
|
67
|
+
if getattr(author, "name", "").strip()
|
|
68
|
+
]
|
|
69
|
+
if authors:
|
|
70
|
+
fields["author"] = authors
|
|
71
|
+
|
|
72
|
+
published = getattr(result, "published", None)
|
|
73
|
+
if published is not None:
|
|
74
|
+
fields["issued"] = {"date-parts": [[published.year, published.month, published.day]]}
|
|
75
|
+
|
|
76
|
+
journal = (getattr(result, "journal_ref", None) or "").strip()
|
|
77
|
+
if journal:
|
|
78
|
+
fields["container-title"] = _SPACES.sub(" ", journal)
|
|
79
|
+
doi = (getattr(result, "doi", None) or "").strip()
|
|
80
|
+
if doi:
|
|
81
|
+
fields["DOI"] = doi
|
|
82
|
+
entry_id = (getattr(result, "entry_id", None) or "").strip()
|
|
83
|
+
if entry_id:
|
|
84
|
+
fields["URL"] = entry_id
|
|
85
|
+
|
|
86
|
+
fields["type"] = JOURNAL_REF_MEANS if journal else DEFAULT_TYPE
|
|
87
|
+
fields["custom"] = {"identifiers": {"arXiv": _short_id(result)}}
|
|
88
|
+
return fields
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _short_id(result: arxiv.Result) -> str:
|
|
92
|
+
"""The bare identifier, without the version and without the URL around it."""
|
|
93
|
+
try:
|
|
94
|
+
short = str(result.get_short_id())
|
|
95
|
+
except (AttributeError, TypeError):
|
|
96
|
+
entry = str(getattr(result, "entry_id", "")).rstrip("/")
|
|
97
|
+
short = entry.rsplit("/abs/", 1)[-1]
|
|
98
|
+
return _VERSION.sub("", short)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _fetch(identifier: str, page_size: int) -> arxiv.Result:
|
|
102
|
+
search = arxiv.Search(id_list=[identifier], max_results=page_size)
|
|
103
|
+
return next(arxiv.Client().results(search))
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""An arXiv result becomes CSL, and failures come back as reasons."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import datetime as dt
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import arxiv
|
|
10
|
+
from pyrefman_arxiv import ArxivResolver, to_csl
|
|
11
|
+
|
|
12
|
+
from pyrefman import ReferenceManager
|
|
13
|
+
from pyrefman.plugins import Registry
|
|
14
|
+
|
|
15
|
+
PUBLISHED = dt.datetime(2021, 1, 1, 18, 0, tzinfo=dt.UTC)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def make_result(**overrides: Any) -> arxiv.Result:
|
|
19
|
+
values = {
|
|
20
|
+
"entry_id": "http://arxiv.org/abs/2101.00001v2",
|
|
21
|
+
"updated": PUBLISHED,
|
|
22
|
+
"published": PUBLISHED,
|
|
23
|
+
"title": "A Study of Widgets",
|
|
24
|
+
"authors": [arxiv.Result.Author("Jane Smith"), arxiv.Result.Author("John Doe")],
|
|
25
|
+
"summary": "Abstract text.",
|
|
26
|
+
"journal_ref": None,
|
|
27
|
+
"doi": None,
|
|
28
|
+
"primary_category": "cs.DL",
|
|
29
|
+
"categories": ["cs.DL"],
|
|
30
|
+
}
|
|
31
|
+
values.update(overrides)
|
|
32
|
+
return arxiv.Result(**values)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def fake_fetch(result: arxiv.Result | None = None, error: Exception | None = None):
|
|
36
|
+
def fetch(identifier: str) -> arxiv.Result:
|
|
37
|
+
if error is not None:
|
|
38
|
+
raise error
|
|
39
|
+
return result if result is not None else make_result()
|
|
40
|
+
|
|
41
|
+
return fetch
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_a_preprint_becomes_csl_fields() -> None:
|
|
45
|
+
fields = to_csl(make_result())
|
|
46
|
+
|
|
47
|
+
assert fields["title"] == "A Study of Widgets"
|
|
48
|
+
assert fields["issued"] == {"date-parts": [[2021, 1, 1]]}
|
|
49
|
+
assert fields["URL"] == "http://arxiv.org/abs/2101.00001v2"
|
|
50
|
+
assert fields["type"] == "posted-content"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_the_identifier_travels_in_the_records_own_slot() -> None:
|
|
54
|
+
fields = to_csl(make_result())
|
|
55
|
+
|
|
56
|
+
assert fields["custom"] == {"identifiers": {"arXiv": "2101.00001"}}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_a_journal_reference_makes_it_an_article() -> None:
|
|
60
|
+
fields = to_csl(make_result(journal_ref="Phys. Rev. Lett. 126, 061102"))
|
|
61
|
+
|
|
62
|
+
assert fields["container-title"] == "Phys. Rev. Lett. 126, 061102"
|
|
63
|
+
assert fields["type"] == "article-journal"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_a_published_doi_is_kept() -> None:
|
|
67
|
+
assert to_csl(make_result(doi="10.1103/PhysRevLett.126.061102"))["DOI"] == (
|
|
68
|
+
"10.1103/PhysRevLett.126.061102"
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_hard_wrapped_titles_are_flattened() -> None:
|
|
73
|
+
fields = to_csl(make_result(title="A very long title\n wrapped over two lines"))
|
|
74
|
+
|
|
75
|
+
assert fields["title"] == "A very long title wrapped over two lines"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_authors_stay_literal_names() -> None:
|
|
79
|
+
fields = to_csl(make_result(authors=[arxiv.Result.Author("Jane van der Waals")]))
|
|
80
|
+
|
|
81
|
+
assert fields["author"] == [{"literal": "Jane van der Waals"}]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_an_unknown_identifier_is_a_reason_not_an_exception() -> None:
|
|
85
|
+
fields, error = ArxivResolver(fetch=fake_fetch(error=StopIteration())).resolve("2101.99999")
|
|
86
|
+
|
|
87
|
+
assert fields is None
|
|
88
|
+
assert "no entry" in (error or "")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_an_http_error_reports_its_status() -> None:
|
|
92
|
+
failure = arxiv.HTTPError("http://export.arxiv.org", 0, 503)
|
|
93
|
+
|
|
94
|
+
fields, error = ArxivResolver(fetch=fake_fetch(error=failure)).resolve("2101.00001")
|
|
95
|
+
|
|
96
|
+
assert fields is None
|
|
97
|
+
assert "503" in (error or "")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_an_unexpected_error_is_a_reason_not_an_exception() -> None:
|
|
101
|
+
fields, error = ArxivResolver(fetch=fake_fetch(error=RuntimeError("boom"))).resolve(
|
|
102
|
+
"2101.00001"
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
assert fields is None
|
|
106
|
+
assert "boom" in (error or "")
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_the_plugin_creates_a_reference_through_the_api(tmp_path: Path) -> None:
|
|
110
|
+
registry = Registry()
|
|
111
|
+
registry.register("resolvers", ArxivResolver(fetch=fake_fetch()))
|
|
112
|
+
manager = ReferenceManager(tmp_path / "library", registry=registry)
|
|
113
|
+
|
|
114
|
+
reference = manager.add(identifier="arXiv:2101.00001")
|
|
115
|
+
|
|
116
|
+
assert reference is not None
|
|
117
|
+
assert reference.title == "A Study of Widgets"
|
|
118
|
+
assert reference.identifiers() == [("arXiv", "2101.00001")]
|
|
119
|
+
assert reference.provenance["title"]["source"] == "arxiv"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def test_two_preprints_from_arxiv_fold_into_one(tmp_path: Path) -> None:
|
|
123
|
+
registry = Registry()
|
|
124
|
+
registry.register("resolvers", ArxivResolver(fetch=fake_fetch()))
|
|
125
|
+
manager = ReferenceManager(tmp_path / "library", registry=registry)
|
|
126
|
+
|
|
127
|
+
first = manager.add(identifier="arXiv:2101.00001")
|
|
128
|
+
second = manager.add(identifier="arXiv:2101.00001")
|
|
129
|
+
|
|
130
|
+
assert first is not None and second is not None
|
|
131
|
+
assert second.id == first.id
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_a_failed_lookup_creates_nothing(tmp_path: Path) -> None:
|
|
135
|
+
registry = Registry()
|
|
136
|
+
registry.register("resolvers", ArxivResolver(fetch=fake_fetch(error=StopIteration())))
|
|
137
|
+
manager = ReferenceManager(tmp_path / "library", registry=registry)
|
|
138
|
+
|
|
139
|
+
assert manager.add(identifier="arXiv:2101.99999") is None
|