-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathinternet_drafts.py
180 lines (150 loc) · 5.69 KB
/
internet_drafts.py
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
176
177
178
179
180
"""Retrieving Internet Drafts from Datatracker.
Registers :func:`.get_internet_draft()`
as an :term:`external source` of bibliographic data
for “Internet-Draft” document identifier type.
"""
import logging
from typing import Dict, Any, List, Tuple
import ast
import datetime
import re
import json
import requests
from bib_models import construct_bibitem
from main.types import ExternalBibliographicItem, ExternalSourceMeta
from main.exceptions import RefNotFoundError
from main import external_sources
from .request import get, BASE_DOMAIN
__all__ = (
'get_internet_draft',
'remove_version',
'version_re',
)
log = logging.getLogger(__name__)
version_re = re.compile(r'^(?P<versionless>[-\w]+?)(\-(?P<version>\d{2}))?$')
"""A regular expression that allows extracting
a version or a versionless name from an Internet Draft name
that possibly has version."""
def remove_version(id: str) -> Tuple[str, str]:
"""
Extracts a version from a possibly versioned Internet Draft ID,
and returns a 2-tuple of strings: versionless ID
and optionally version (or None).
"""
match = version_re.match(id)
if not match or not match.group('versionless'):
raise ValueError("Invalid Datatracker ID: %s" % id)
versionless = match.group('versionless')
return versionless, match.group('version')
@external_sources.register_for_types('datatracker', {'Internet-Draft': True})
def get_internet_draft(
docid: str,
strict: bool = True,
) -> ExternalBibliographicItem:
"""Retrieves an Internet Draft from Datatracker in Relaton format.
Makes necessary requests to Datatracker API
and converts .
:param str docid:
:param bool strict: see :ref:`strict-validation`
:rtype: main.types.ExternalBibliographicItem
"""
# We cannot request a particular I-D version from Datatracker,
# so we ignore the second tuple element (version)
versionless, _ = remove_version(docid)
resp = get(f'/api/v1/doc/document/{versionless}/')
if resp.status_code == 404:
raise RefNotFoundError()
data = resp.json()
bibitem_data: Dict[str, Any] = dict(
type='draft',
abstract=[{
'content': data['abstract'], # .replace('\n', ' '),
}] if 'abstract' in data else None,
link=[{
'content': f'{BASE_DOMAIN}/doc/html/{versionless}-%s' % data['rev'],
}],
version=[{
'draft': data['rev'],
}],
docid=[{
'type': 'IETF',
'id': "I-D %s" % data['name'],
'primary': True,
}, {
'type': 'Internet-Draft',
'id': "%s-%s" % (data['name'], data['rev']),
}],
title=[{
'type': 'main',
'content': data['title'],
}],
fetched=datetime.datetime.now(),
contributor=[],
)
# Some data (e.g., authors and dates) is only available
# via separate submission endpoint
try:
latest_submission_url = data['submissions'][-1]
except IndexError:
log.warning(
"Unable to retrieve complete Internet Draft metadata: "
"no submissions available")
else:
try:
latest_submission_data = get(latest_submission_url).json()
except requests.exceptions.ConnectionError:
pass
else:
if 'document_date' in latest_submission_data:
bibitem_data['date'] = [{
'type': 'created',
'value': latest_submission_data['document_date'],
}, {
'type': 'submitted',
'value': latest_submission_data['submission_date'],
}]
if 'authors' in latest_submission_data:
authors: List[Any]
if isinstance(latest_submission_data['authors'], list):
authors = latest_submission_data['authors']
elif isinstance(latest_submission_data['authors'], str):
try:
authors = json.loads(latest_submission_data['authors'])
except json.JSONDecodeError:
try:
# IMPORTANT: Using literal_eval means we assume
# Datatracker’s responses are always trustworthy.
# The reason we’re using it is because Datatracker
# appears to return something like Python’s repr()
# of a list sometimes here
# (i.e., not JSON-decodable)
authors = ast.literal_eval(
latest_submission_data['authors'])
except (ValueError, SyntaxError):
authors = []
else:
authors = []
if authors:
bibitem_data['contributor'] += [
{
'role': ['author'],
'person': {
'name': {
'completename': {
'content': a['name']
},
},
},
}
for a in authors
]
bibitem, errors = construct_bibitem(bibitem_data, strict)
return ExternalBibliographicItem(
source=ExternalSourceMeta(
id='datatracker',
home_url="http://datatracker.ietf.org/api",
),
bibitem=bibitem,
validation_errors=errors,
requests=[], # TODO: Provide requests incurred later
)