File: create_sphinx.py

package info (click to toggle)
cpl-plugin-vimos 4.1.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 29,888 kB
  • sloc: ansic: 189,758; cpp: 16,237; sh: 4,309; python: 3,678; makefile: 1,241; perl: 10
file content (255 lines) | stat: -rw-r--r-- 6,779 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import cpl
import os
import sys
import re

rst_template = '''The {recipe} recipe
===============================================================

.. data:: {recipe}

Synopsis
--------

{synopsis}

Description
-----------

{description}

Constructor
-----------

.. method:: cpl.Recipe("{recipe}")
   :noindex:

   Create an object for the recipe {recipe}.

::

   import cpl
   {recipe} = cpl.Recipe("{recipe}")

{parameters}

.. seealso:: `cpl.Recipe <https://packages.python.org/python-cpl/recipe.html>`_
   for more information about the recipe object.

Bug reports
-----------

Please report any problems to `{author} <{email}>`_. Alternatively, you may 
send a report to the `ESO User Support Department <usd-help@eso.org>`_.

Copyright
---------

{license}

.. codeauthor:: {author} <{email}>
'''

par_template = '''.. py:attribute:: {recipe}.param.{par}

    {description} [default={default}].
'''

fname_template ="{recipe}.rst"

index_template = '''.. title:: Overview

The {PIPELINE} {version} pipeline
###################################

These pages describe the python interface for the {PIPELINE} pipeline recipes.

{recipes}

.. toctree::
   :hidden:

{toctree}

.. seealso::

     * The `{PIPELINE} Pipeline User Manual 
       <ftp://ftp.eso.org/pub/dfs/pipelines/{pipeline}/{pipeline}-pipeline-manual-6.9.pdf>`_ in PDF format,

     * an `overview <https://www.eso.org/sci/software/pipelines/>`_
       over the existing ESO pipelines,

     * the `python-cpl <https://packages.python.org/python-cpl>`_ package.
    
Bug reports
===========

If you experience an unexpected behavior of any component of the {PIPELINE}
pipeline recipes package, please, first refer to the list of known problems
and limitations in the pipeline manual of the current {PIPELINE} pipeline
release.

For any other issues or requests, please, send a report to the `ESO User
Support Department <usd-help@eso.org>`_, describing:

 * the {PIPELINE} pipeline version,
 * the version of your OS and C compiler,
 * the exact sequence of actions that were performed before the problem occurred,
 * what were precisely the symptoms and the possible error message(s),
 * whether the problem is repeatable.
'''

rst_partemplate = '''Parameters
----------

{pars}

The following code snippet shows the default settings for the available 
parameters.

::

   import cpl
   {recipe} = cpl.Recipe("{recipe}")

{pars_example1}

You may also set or overwrite some or all parameters by the recipe 
parameter `param`, as shown in the following example:

::

   import cpl
   {recipe} = cpl.Recipe("{recipe}")
   [...]
   res = {recipe}( ..., param = {{{pars_example2}}})
'''

conf_template = '''project = u'{PIPELINE} pipeline'
version = '{version}'
release = '{version}'
master_doc = 'index'
show_authors = True
html_theme = 'sphinxdoc'
'''

pipeline = sys.argv[1]

cpl.Recipe.path = "recipes"
recipes = [ name for name, version in cpl.Recipe.list() ]
oca = open(os.path.join("calib", "gasgano", "config", pipeline + ".oca")).read()
oca = oca[oca.find("action"):]
recipes_oca = [name for name in recipes if name in oca]
recipes_oca.sort()

recipes_x = [name for name in recipes if not name in oca]
recipes_x.sort()

def par(recipe, template, delimiter = "", count = None):
    return delimiter.join(template.format(
        recipe = recipe.__name__,
        par = p.name.replace("-","_"),
        type = p.type.__name__,
        description = p.__doc__.replace("\n", "  "),
        default = '"{0}"'.format(p.default) if p.type is str else p.default
    ) for i, p in enumerate(recipe.param) if count is None or i < count)

def get_description(s):
    o = []
    l = s.splitlines()
    enabled = True
    for i, s in enumerate(l):
        if "BASIC PARAMETERS" in s:
            enabled = False
        elif "Examples:" in s:
            enabled = False
        elif "Input files" in s:
            enabled = True
            o += ["Input files", "^^^^^^^^^^^^", "::"]
            if len(l[i+1]) != 0:
                 o += [""]
        elif "Output files" in s:
            enabled = True
            o += ["Output files", "^^^^^^^^^^^^", "::"]
            if len(l[i+1]) != 0:
                 o += [""]
        elif not enabled:
            continue
        elif "-"*40 in s:
            pass
        elif len(s) == 0:
            o += [ s ]
        elif s[-1] == ".":
            o += [ s, "" ]
        else:
            o += [ s ]
    return "\n".join(o)

def rstpage(recipe, template, partemplate):
    return template.format(
        recipe = recipe.__name__,
        version = recipe.__version__,
        synopsis = recipe.description[0],
        description = get_description(recipe.description[1]),
        email = recipe.__email__,
        author = recipe.__author__,
        license = recipe.__copyright__,
        pipeline = pipeline,
        parameters = partemplate.format(
            recipe = recipe.__name__,
            pars = par(recipe, par_template),
            pars_example1 = par(recipe,
                                '   {recipe}.param.{par} = {default}\n'),
            pars_example2 = par(recipe, '"{par}":{default}', ', ', 2)
        ) if len(recipe.param) > 0 else ''
    )

version = ""
for name in recipes_oca + recipes_x:
    recipe = cpl.Recipe(name)
    f = open(os.path.join("sphinx",
                          fname_template.format(recipe = recipe.__name__)), "w")
    f.write(rstpage(recipe, rst_template, rst_partemplate))
    f.close()
    version = recipe.__version__
    del recipe._recipe

if len(recipes_oca) > 0 and len(recipes_x) > 0:
    toc_recipes = "Standard recipes\n----------------\n"

for name in recipes_oca:
    recipe = cpl.Recipe(name)
    toc_recipes += "\n:data:`{recipe}`\n   {synopsis}\n".format(
        recipe = recipe.__name__,
        synopsis = recipe.description[0])
    del recipe._recipe

if len(recipes_oca) > 0 and len(recipes_x) > 0:
    toc_recipes += "\nAdditional recipes\n--------------------\n"

for name in recipes_x:
    recipe = cpl.Recipe(name)
    toc_recipes += "\n:data:`{recipe}`\n   {synopsis}\n".format(
        recipe = recipe.__name__,
        synopsis = recipe.description[0])
    del recipe._recipe

toc = "\n".join("   {recipe}".format(
    recipe = name
) for name in recipes_oca + recipes_x)

f = open(os.path.join("sphinx", "index.rst"), "w")
f.write(index_template.format(toctree = toc,
                              recipes = toc_recipes,
                              pipeline = pipeline,
                              PIPELINE = pipeline.upper(),
                              version = version))
f.close()

f = open(os.path.join("sphinx", "conf.py"), "w")
f.write(conf_template.format(PIPELINE = pipeline.upper(),
                             version = version))
f.close()