-
Notifications
You must be signed in to change notification settings - Fork 16
/
build_catalog_rst.py
208 lines (172 loc) · 6.83 KB
/
build_catalog_rst.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
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
import os
import pathlib
import intake
import re
import jinja2
from rstcloth.rstcloth import RstCloth
template_html = """ <div class="panel-group" id="accordion-{{ id }}">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion-{{ id }}" href="#collapse1-{{ id }}">
load in python
</a>
</h4>
</div>
<div id="collapse1-{{ id }}" class="panel-collapse collapse">
<div class="panel-body">
<pre>{{ open_code | indent(4) | e }}</pre>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion-{{ id }}" href="#collapse2-{{ id }}">
metadata</a>
</h4>
</div>
<div id="collapse2-{{ id }}" class="panel-collapse collapse">
<div class="panel-body">
<table class="table table-condensed table-hover">
<tbody>
{% for key, value in metadata.items() %}
<tr><td>{{ key | e }}</td><td>{{ value | e }}</td></tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion-{{ id }}" href="#collapse3-{{ id }}">
xarray preview</a>
</h4>
</div>
<div id="collapse3-{{ id }}" class="panel-collapse collapse">
<div class="panel-body">
<pre>{{ repr | indent(4) | e }}</pre>
</div>
</div>
</div>
</div>
"""
template = jinja2.Template(template_html)
# https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename
def to_valid_filename(s):
s = str(s).strip().replace(' ', '_')
return re.sub(r'(?u)[^-\w.]', '', s).lower()
def normalize_document_path(prefix_and_name, extension=''):
if len(prefix_and_name)==0:
return
path = os.path.join(*[to_valid_filename(p) for p in prefix_and_name])
return path + extension
def ensure_dir_exists(path):
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
class CatalogRSTBuilder:
def __init__(self, master_catalog_path, output_dir, remote_url_base,
actually_load=False):
self.master_catalog_path = master_catalog_path
self.output_dir = output_dir
self.remote_url_base = remote_url_base
self.actually_load = actually_load
self.cat = intake.Catalog(master_catalog_path)
def remote_catalog_url(self, path):
# translate local path name to url
local_base_path = os.path.abspath(os.path.dirname(self.master_catalog_path))
abs_path = os.path.abspath(path)
url_path = abs_path.replace(local_base_path, self.remote_url_base)
return url_path
def dataset_open_code(self, intake_ds):
path = self.remote_catalog_url(intake_ds.cat.path)
code = ['import intake',
f'cat = intake.Catalog("{path}")',
f'ds = cat["{intake_ds.name}"].to_dask()']
return '\n'.join(code)
def dataset_repr(self, intake_ds):
if self.actually_load:
return repr(intake_ds.to_dask())
else:
return ""
def walk_and_write_rst(self, cat, prefix=None, depth=2):
prefix = [] if prefix is None else prefix
parent_dir = normalize_document_path([self.output_dir] + prefix)
document_path = normalize_document_path([self.output_dir] + prefix + [cat.name],
extension='.rst')
print(document_path)
d = RstCloth()
assert cat.description, 'Catalog needs a description for title'
#title = cat.description
title = cat.name + ': ' + cat.description
d.title(title)
d.newline()
d.content('Catalog URL:')
d.newline()
d.codeblock(self.remote_catalog_url(cat.path), language='html')
d.newline()
if len(prefix) > 0:
d.h2('Parent Catalogs')
d.newline()
parent_links = []
for n in range(len(prefix)):
levels_up = len(prefix) - n
extra_prefixes = ['..'] * levels_up
full_path = extra_prefixes + [to_valid_filename(prefix[n])]
parent_cat_path = os.path.join(*full_path)
parent_link = ':doc:' + d.inline_link(prefix[n], parent_cat_path)[:-1]
parent_links.append(parent_link)
d.content(' / '.join(parent_links))
d.newline()
sub_catalogs = []
entries = []
for name, item in cat.items():
if item._container == 'catalog':
if depth > 1:
self.walk_and_write_rst(item(), prefix + [cat.name], depth-1)
sub_catalogs.append(name)
else:
entries.append(name)
n = '.'.join(prefix + [name])
path_path = '/'.join(prefix) + '.rst'
if len(sub_catalogs) > 0:
d.h2('Child Catalogs')
d.newline()
sub_catalog_path = os.path.join(to_valid_filename(cat.name), '*')
d.directive('toctree',
fields=[#('caption', 'Child Datasets'),
('glob', ''), ('maxdepth', '1')],
content=sub_catalog_path)
if len(entries) > 0:
d.h2('Datasets')
d.newline()
for name in entries:
d.h3(name)
d.newline()
ds_item = cat[name]
description = ds_item.description
if description:
d.content(description)
d.newline()
ds_repr = self.dataset_repr(ds_item)
open_code = self.dataset_open_code(ds_item)
name_jsfriendly = to_valid_filename(name).replace('.', '_')
html = template.render(id=name_jsfriendly, open_code=open_code,
repr=ds_repr, metadata=ds_item.metadata)
d.directive('raw', arg='html', content=html)
d.newline()
ensure_dir_exists(parent_dir)
d.write(document_path)
def build(self, depth=5):
self.walk_and_write_rst(self.cat, depth=depth)
def main():
master_catalog = './intake-catalogs/master.yaml'
output_rst_dir = 'catalog-docs'
remote_url_base = 'https://raw.githubusercontent.com/pangeo-data/pangeo-datastore/master/intake-catalogs'
builder = CatalogRSTBuilder(master_catalog, output_rst_dir, remote_url_base,
actually_load=True)
builder.build()
if __name__ == "__main__":
# execute only if run as a script
main()