-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.py
425 lines (334 loc) · 12.6 KB
/
gallery.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python
"Create the CIAO gallery pages"
# pylint: disable=consider-using-with
# pylint: disable=import-outside-toplevel
import os
CIAO_VERSION = "4.16"
CWD = os.getcwd()
DATA_DIR = os.path.join(CWD, "data")
PNG_DIR = os.path.join(CWD, "pngs")
TEST_DIR = os.path.join(CWD, "tests/ciao_gallery")
def ch_data_dir(somefun):
"decorator to change dir and back again"
def wrapper(*args, **kwargs):
"wrapper func"
os.chdir(DATA_DIR)
stt = somefun(*args, **kwargs)
os.chdir(CWD)
return stt
return wrapper
class GalleryDoc():
"Class to generate the gallery page"
def writer(self, val):
"Write output"
self.outfile.write(val.strip()+"\n")
def __init__(self, title, outfile):
"Setup page"
self.title = title
self.outfile_name = outfile
self.outfile = open(outfile, "w", encoding="ascii")
self.examples = None
self.__head()
def __head(self):
"Doc header"
self.writer(f"""<?xml version='1.0' encoding='us-ascii' ?>
<!DOCTYPE page>
<!-- This page is automatically generated, do not edit -->
<page>
<info>
<title><short>Gallery: {self.title}</short></title>
<version>{CIAO_VERSION}</version>
<css>img.chips {{
display: block;
margin-left: auto;
margin-right: auto;
}}
div.examplecode {{
padding-left: 0.5em;
background: #99CC66;
}}
div.hardcopies {{
text-align: center;
}}
</css>
<breadcrumbs/>
</info>
<text>
<div style='text-align: center'><h1>Gallery: {self.title}</h1></div>
<p><cxclink href="thumbnail.html">Return to thumbnail page.</cxclink></p>
""")
def close(self):
"Close page"
self.__tail()
self.outfile.close()
def __tail(self):
"Close xml"
self.writer(" </text>\n</page>")
def make_toc(self, examples):
"Create the table of contents"
self.examples = examples
self.writer("""<h2>Examples</h2>
<list type='1'>
""")
for i, e in enumerate(examples):
e.num = i+1
e.set_head()
self.writer(f"""<li><cxclink id="{e.anchor}">{e.title}</cxclink></li>""")
self.writer(""" </list>
<hr/>""")
def make_examples(self):
"Create example"
for exam in self.examples:
for elm in ["hdr", "pre", "img", "cmd", "plt", "pst"]:
val = getattr(exam, elm)
if val:
self.writer(val)
self.writer("<hr/>")
class Thumbnail():
"Class to create the thumbnail view"
outfile = "thumbnail.xml"
def writer(self, val):
"Write page"
self.fp.write(val.strip()+"\n")
def __init__(self):
"Setup"
self.fp = open(self.outfile, "w", encoding="ascii")
self.writer(f"""<?xml version='1.0' encoding='us-ascii' ?>
<!DOCTYPE page>
<!-- This page is automatically generated, do not edit -->
<page>
<info>
<title><short>Thumbnails</short></title>
<version>{CIAO_VERSION}</version>
<css>div.section {{ clear: both; }}
div.sectiontitle {{
padding-top: 1em;
text-align: center;
}}
div.example {{
float: left;
margin: 5px;
padding: 10px;
border-radius: 15px;
background: #99cc66;
}}
</css>
<breadcrumbs/>
</info>
<text>
<div style='text-align: center;'><h1>Thumbnails of CIAO examples</h1></div>
<p><cxclink href="index.html">Go to list of gallery examples.</cxclink></p>
<p>Select an image to see how it was created.</p>
""")
def add_section(self, gallery):
"Create different sections"
for gg in gallery:
outf = gg.outfile_name.replace(".xml", ".html")
self.writer(f"""<div class='section'>
<div class='sectiontitle'>
<h2><cxclink href='{outf}'>{gg.title}</cxclink></h2>
</div>""")
for ee in gg.examples:
clean = ee.title.replace("<new/>", "")
self.writer(f"""
<div class='example'>
<div>{ee.title}</div>
<cxclink href='{outf}' id='{ee.anchor}'>
<img src='pngs/thmb.{ee.anchor}.png' alt='[{clean}]'/>
</cxclink>
</div>
""")
self.writer("</div>")
def close(self):
"Close xml"
self.writer(""" </text>
</page>""")
self.fp.close()
class IndexPage():
"Class to create the index page"
outfile = "index.xml"
def writer(self, val):
"Write outfile"
self.fp.write(val.strip()+"\n")
def __init__(self):
"Setup"
self.fp = open(self.outfile, "w", encoding="ascii")
self.writer(f"""<?xml version='1.0' encoding='us-ascii' ?>
<!DOCTYPE page>
<!-- This page is automatically generated, do not edit -->
<page>
<info>
<title><short>Gallery List</short></title>
<version>{CIAO_VERSION}</version>
<breadcrumbs/>
</info>
<text>
<div style='text-align: center;'><h1>List of CIAO examples</h1></div>
<p><cxclink href="thumbnail.html">Go to thumbnail view.</cxclink></p>
""")
def add_section(self, gallery):
"Add section"
for gg in gallery:
outf = gg.outfile_name.replace(".xml", ".html")
self.writer(f"""<h2><cxclink href='{outf}'>{gg.title}</cxclink></h2>""")
self.writer("<list>")
for ee in gg.examples:
self.writer(f"""<li><cxclink href='{outf}' id='{ee.anchor}'>{ee.title}</cxclink></li>""")
self.writer("</list>")
def close(self):
"Close outfile"
self.writer(""" </text>
</page>""")
self.fp.close()
def write_regression_tests(gallery):
"Class to write commands to a regression test .MAIN file"
for gg in gallery:
for ee in gg.examples:
with open(f"{TEST_DIR}/{ee.anchor}.MAIN", "w", encoding="ascii") as fp:
for c in ee.raw_cmds:
for rr in ee.requires:
c = c.replace(rr, "${CT_INDIR}/"+rr)
fp.write(c+"\n")
class CIAOExample():
"Class to hold parts of each example"
def __init__(self, anchor, title):
self.anchor = anchor
self.title = title
self.num = None # Example number 1-N
self.hdr = None # Example title header
self.pre = None # Text before example commands
self.img = None # Image showing output
self.cmd = None # The commands to run
self.pst = None # The text after the example
self.plt = None # The commands to plot the data
self.raw_cmds = None # The commands to run for reg tests
self.requires = None # List of input files
print(f"\033[33m\033[1mWorking on {self.anchor}\033[0m")
def set_head(self):
"Print header line"
self.hdr = f"""<h2><span id='{self.anchor}'>{self.num}) {self.title}</span></h2>"""
def set_pre(self, words_words_words):
"""This is the "pre" text; the text that appears describing
the example"""
self.pre = f"""<div class="before">{words_words_words}</div>"""
def set_post(self, words_words_words):
"""This is the "post" test; the text that appears after the
commands describing the commands"""
self.pst = f"""<div class="after">{words_words_words}</div>"""
@ch_data_dir
def set_img(self, fits, extras, run=True):
"""This is the text to display the image"""
from textwrap import wrap
self.img = f"""
<cxclink href="pngs/{self.anchor}.png">
<img class='chips' src='pngs/thmb.{self.anchor}.png' alt='{self.anchor}'/>
</cxclink>"""
import re
nogeom = re.sub("-geometry *[0-9]*x[0-9]* ", "", extras)
plt = f"ds9 {fits} {nogeom}"
plt = " \\\n".join(wrap(plt, width=150, break_long_words=False,
break_on_hyphens=False,
subsequent_indent=' '))
self.plt = f"""
<div><p>The following commands can be used to visualize the output</p>
<div class='examplecode'><screen>{plt}</screen></div>
</div>"""
if not run:
return
outf = os.path.join(PNG_DIR, self.anchor+".png")
cmd = f"""ds9 {fits} -view info no -view colorbar yes
-view multi yes -view magnifier no
-view panner no -view buttons no -title foo -tile yes
{extras} -saveimage {outf} -exit"""
cmd = cmd.replace("\n", " ")
print(cmd)
if 0 != os.system(cmd):
raise RuntimeError(f"problem making image {self.anchor}")
if 0 != os.system(f"convert {PNG_DIR}/{self.anchor}.png -resize x320 {PNG_DIR}/thmb.{self.anchor}.png"):
raise RuntimeError(f"problem making thumbnail for {self.anchor}")
@ch_data_dir
def set_cmds(self, cmds_to_run, run=False):
"""This is the text to run the commands"""
self.raw_cmds = cmds_to_run
for cc in cmds_to_run:
print(cc)
if run and 0 != os.system(cc):
raise RuntimeError(f"Problem running \n{cc}")
ss = list(map(lambda x: x.strip(), cmds_to_run))
for ii, cc in enumerate(ss):
skip = ["pset", "punlearn", "pget", "ahelp"]
for c0 in cc.split(" "):
if c0 in skip:
continue
if 0 == len(c0):
continue
ciao = os.environ["ASCDS_INSTALL"]
if os.path.exists(ciao+"/bin/"+c0) or os.path.exists(ciao+"/contrib/bin/"+c0):
cc = cc.replace(c0, f"""<ahelp name="{c0}"/>""")
skip.append(c0)
ss[ii] = cc
cmds = "\n".join(ss)
self.cmd = f"""<div class='examplecode'><screen>{cmds}</screen></div>"""
def parse_cli():
"Parse the command line options"
import argparse
cli_pars = argparse.ArgumentParser()
cli_pars.add_argument("--infile", dest="infile", default="gallery.cfg",
action="store",
help="Configuration file name containing text and commands")
cli_pars.add_argument("--skip-ds9", dest="run_ds9", default=True,
action="store_false", help="Skip running ds9 commands")
cli_pars.add_argument("--skip-run", dest="run_cmd", default=True,
action="store_false", help="Skip running CIAO commands")
args = cli_pars.parse_args()
return args
def parse_config(options):
"Parse the config file"
import configparser as cfg
tasks = cfg.ConfigParser()
tasks.read_file(open(options.infile, "r", encoding="ascii"))
pages = []
examples = {}
for task in tasks.sections():
example = CIAOExample(task, tasks.get(task, "title"))
example.set_cmds(tasks.get(task, "commands").strip().replace("\\\n", "").split("\n"),
run=options.run_cmd)
example.set_img(tasks.get(task, "outfile"), tasks.get(task, "ds9_extras"),
run=options.run_ds9)
if tasks.has_option(task, "pretext"):
example.set_pre(tasks.get(task, "pretext"))
if tasks.has_option(task, "posttext"):
example.set_post(tasks.get(task, "posttext"))
if tasks.has_option(task, "requires"):
example.requires = tasks.get(task, "requires").strip().split()
tt = task.split(".")[0]
if tt not in pages:
pages.append(tt)
examples[tt] = [example]
else:
examples[tt].append(example)
return pages, examples
def build_pages(pages, examples):
"Create the pages"
toc = []
for page in pages:
doc = GalleryDoc(page.capitalize(), page+".xml")
doc.make_toc(examples[page])
doc.make_examples()
doc.close()
toc.append(doc)
tt = Thumbnail()
tt.add_section(toc)
tt.close()
tt = IndexPage()
tt.add_section(toc)
tt.close()
write_regression_tests(toc)
def main():
"Main routine"
options = parse_cli()
pages, examples = parse_config(options)
build_pages(pages, examples)
print("")
if __name__ == "__main__":
main()