-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathdocs_macros.py
More file actions
232 lines (191 loc) · 7.43 KB
/
Copy pathdocs_macros.py
File metadata and controls
232 lines (191 loc) · 7.43 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import json
import os
import re
import subprocess
import sys
from pathlib import Path
ROOT_DIR = Path(__file__).resolve().parent
EXAMPLES_DIR = ROOT_DIR / 'docs' / 'examples'
sys.path.insert(0, str(ROOT_DIR / 'lib'))
sys.path.insert(0, str(ROOT_DIR / 'tests'))
MANIFEST_BASES = {
'frame-manifest': 'https://w3c.github.io/json-ld-framing/tests',
'manifest-urgna2012': 'https://w3c.github.io/rdf-canon/tests',
'manifest-urdna2015': 'https://w3c.github.io/rdf-canon/tests',
}
DEFAULT_TEST_BASE = 'https://w3c.github.io/json-ld-api/tests'
_SKIP_ID_PATTERN = re.compile(r'^\.\*(?P<manifest>[^#]+)#(?P<test_id>[^$]+)\$$')
_MANIFEST_PATHS = (
ROOT_DIR / 'specifications' / 'json-ld-api' / 'tests',
ROOT_DIR / 'specifications' / 'json-ld-framing' / 'tests',
)
def _parse_skip_id_regex(pattern):
match = _SKIP_ID_PATTERN.fullmatch(pattern)
if not match:
return None
return match.group('manifest'), match.group('test_id')
def _test_url(manifest, test_id):
base = MANIFEST_BASES.get(manifest, DEFAULT_TEST_BASE)
return f'{base}/{manifest}.html#{test_id}'
def _jsonld_values(data, key):
if key not in data:
return []
value = data[key]
return value if isinstance(value, list) else [value]
def _entry_test_types(entry):
values = []
values.extend(_jsonld_values(entry, '@type'))
values.extend(_jsonld_values(entry, 'type'))
return values
def _manifest_entries():
for manifest_dir in _MANIFEST_PATHS:
if not manifest_dir.exists():
continue
for path in sorted(manifest_dir.glob('*-manifest.jsonld')):
data = json.loads(path.read_text())
manifest = path.stem
for entry in _jsonld_values(data, 'sequence'):
if not isinstance(entry, dict):
continue
test_id = entry.get('@id', entry.get('id', ''))
if test_id.startswith('#'):
test_id = test_id[1:]
yield {
'entry': entry,
'id': f'{manifest}#{test_id}',
'link': f'[{test_id}]({_test_url(manifest, test_id)})',
'types': _entry_test_types(entry),
}
def _skip_reason(test_type, skip, test):
test_id = test['id']
entry = test['entry']
for pattern in skip.get('idRegex', []):
if re.match(pattern, test_id):
return f'Explicit skip (`{test_type}`)'
for pattern in skip.get('descriptionRegex', []):
if re.match(pattern, entry.get('description', '')):
return f'Description skip (`{test_type}`)'
processing_mode = entry.get('option', {}).get('processingMode')
if processing_mode in skip.get('processingMode', []):
return f'Processing mode `{processing_mode}` (`{test_type}`)'
spec_version = entry.get('option', {}).get('specVersion')
if spec_version in skip.get('specVersion', []):
return f'Spec version `{spec_version}` (`{test_type}`)'
return None
def _pending_reason(test_type, pending, test):
test_id = test['id']
for pattern in pending.get('idRegex', []):
if re.match(pattern, test_id):
return f'Pending expected failure (`{test_type}`)'
return None
def _example_path(name):
path = (EXAMPLES_DIR / name).resolve()
if not path.is_relative_to(EXAMPLES_DIR.resolve()):
raise ValueError(f'Invalid example path: {name}')
return path
def _github_branch():
branch = os.environ.get('GITHUB_REF_NAME')
if branch:
return branch
result = subprocess.run(
['git', 'symbolic-ref', '--short', 'HEAD'],
capture_output=True,
text=True,
cwd=ROOT_DIR,
)
if result.returncode == 0 and result.stdout.strip():
return result.stdout.strip()
return 'master'
def _example_github_url(name, repo_url):
rel_path = Path('docs/examples') / name
branch = 'master'
return f'{repo_url.rstrip("/")}/blob/{branch}/{rel_path.as_posix()}'
def define_env(env):
@env.macro
def bundled_contexts_table():
from pyld import BUNDLED_CONTEXTS
rows = [
'| Context URL | Bundled file |',
'| --- | --- |',
]
for url, path in sorted(BUNDLED_CONTEXTS.items()):
rows.append(f'| `{url}` | `{Path(path).name}` |')
return '\n'.join(rows)
@env.macro
def skipped_tests_table():
from runtests import TEST_TYPES
skipped_or_pending = {}
seen_links = set()
tests = list(_manifest_entries())
for test_type, config in sorted(TEST_TYPES.items()):
skip = config.get('skip', {})
pending = config.get('pending', {})
for test in tests:
if test_type not in test['types']:
continue
reason = _skip_reason(test_type, skip, test) or _pending_reason(
test_type, pending, test
)
if not reason or test['link'] in seen_links:
continue
skipped_or_pending.setdefault(reason, []).append(test['link'])
seen_links.add(test['link'])
for pattern in skip.get('idRegex', []):
parsed = _parse_skip_id_regex(pattern)
if not parsed:
continue
manifest, test_id = parsed
link = f'[{test_id}]({_test_url(manifest, test_id)})'
if link in seen_links:
continue
skipped_or_pending.setdefault(
f'Explicit skip (`{test_type}`)', []
).append(link)
seen_links.add(link)
for pattern in pending.get('idRegex', []):
parsed = _parse_skip_id_regex(pattern)
if not parsed:
continue
manifest, test_id = parsed
link = f'[{test_id}]({_test_url(manifest, test_id)})'
if link in seen_links:
continue
skipped_or_pending.setdefault(
f'Pending expected failure (`{test_type}`)', []
).append(link)
seen_links.add(link)
rows = [
'| Reason | Tests |',
'| --- | --- |',
]
for reason, links in sorted(skipped_or_pending.items()):
rows.append(f'| {reason} | {", ".join(sorted(links))} |')
return '\n'.join(rows)
@env.macro
def example(name, output_syntax=None, indent=0):
path = _example_path(name)
source = path.read_text()
result = subprocess.run(
[sys.executable, str(path)],
capture_output=True,
text=True,
check=True,
cwd=ROOT_DIR,
env={**os.environ, 'PYTHONPATH': str(ROOT_DIR / 'lib')},
)
github_url = _example_github_url(name, env.conf['repo_url'])
display_name = path.name
title = (
f'Example<span class="example-source-link" markdown>'
f':fontawesome-brands-github: [`{display_name}`]({github_url})'
f'</span>'
)
output_lang = output_syntax or 'console'
body = (
f'```python\n{source}```\n\n'
f'```{output_lang} title="Output"\n{result.stdout}```'
)
content_indent = indent + 4
pad = ' ' * content_indent
indented = '\n'.join(f'{pad}{line}' for line in body.splitlines())
return f'!!! example "{title}"\n\n{indented}\n'