10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-09-27 12:00:56 +02:00
quantum_package/docs/source/auto_generate.py

241 lines
6.5 KiB
Python
Raw Normal View History

2018-11-26 13:45:32 +01:00
#!/usr/bin/env python2
from __future__ import print_function
import os
import ConfigParser
2018-11-25 14:59:02 +01:00
def generate_modules(abs_module, entities):
MODULE = os.path.split(abs_module)[-1]
module = MODULE.lower()
if module == "dummy":
return
with open( os.path.join(abs_module,'README.rst'), 'r' ) as f:
readme = f.read()
rst = [
".. _%s:"%(module), "",
".. program:: %s"%(module), "",
".. default-role:: option", "",
readme, "",
]
EZFIO = os.path.join(abs_module,'EZFIO.cfg')
if os.path.exists(EZFIO):
rst += [ "", "EZFIO parameters", "----------------", "" ]
config_file = ConfigParser.ConfigParser()
with open(EZFIO,'r') as f:
config_file.readfp(f)
for section in config_file.sections():
doc = config_file.get(section,"doc")
2018-11-26 13:45:32 +01:00
doc = " " + doc.replace("\n","\n\n ")+"\n"
try:
default = config_file.get(section,"default")
2018-11-26 13:45:32 +01:00
default = " " + "Default: %s\n"%default
except:
default = ""
rst += [ ".. option:: %s\n"%(section), doc, default ]
2018-11-25 14:59:02 +01:00
providers = []
subroutines = []
2018-11-26 13:45:32 +01:00
for k in sorted(entities.keys()):
2018-11-25 14:59:02 +01:00
e = entities[k]
2018-11-26 13:45:32 +01:00
if e["module"].lower() == module.lower():
2018-11-25 14:59:02 +01:00
if "/" not in e["file"] and e["file"] != "ezfio_interface.irp.f":
if e["type"] == 's':
subroutines.append(e)
elif e["type"] == 'p':
providers.append(e)
if providers:
rst += [ "", "Providers", "---------", "" ]
for p in providers:
rst += [ """
2018-11-26 13:45:32 +01:00
.. c:var:: %s
2018-11-25 14:59:02 +01:00
.. code:: text
%s
2018-11-26 13:45:32 +01:00
File: :file:`%s`
2018-11-25 14:59:02 +01:00
%s
""" % ( p["name"],
"\n ".join(p["decl"]),
p["file"],
2018-11-26 13:45:32 +01:00
" ".join(p["doc"]).replace("\n ","\n "),
) ]
if subroutines:
rst += [ "", "Subroutines / functions", "-----------------------", "" ]
for p in subroutines:
rst += [ """
.. c:function:: %s
.. code:: text
%s
File: :file:`%s`
%s
""" % ( p["name"],
"\n ".join(p["decl"]),
p["file"],
" ".join(p["doc"]).replace("\n ","\n "),
2018-11-25 14:59:02 +01:00
) ]
rst_file = os.path.join('modules',module+".rst")
with open(rst_file,'w') as f:
f.write("\n".join(rst))
2018-11-25 14:59:02 +01:00
def generate_providers(abs_module):
MODULE = os.path.split(abs_module)[-1]
module = MODULE.lower()
if module == "dummy":
return
files = {}
entities = {}
mandir = os.path.join(abs_module,'IRPF90_man')
if not os.path.exists(mandir):
return {}
for f in os.listdir(mandir):
filename = os.path.join(mandir,f)
if f not in files:
files[f] = 0
name = f.split('.')[0]
with open(filename, 'r') as f:
state = 0
entity = { "decl": [], "doc": [] ,
"name": name , "module": module }
2019-01-04 00:03:37 +01:00
text=f.read()
text_old = None
while text_old != text:
text_old = text
2019-01-04 00:30:34 +01:00
text = text.replace("$"," :math:`",1).replace("$","` ",1)
2019-01-04 00:03:37 +01:00
for line in text.splitlines():
2019-01-02 23:04:13 +01:00
line = line.rstrip()
2018-11-25 14:59:02 +01:00
if line.startswith(".SH Declaration"):
state = 1
continue
elif line.startswith(".nf"): continue
elif line.startswith(".ni"): continue
elif line.startswith(".P"): continue
if line.startswith(".SH Description"):
state = 2
continue
elif line.startswith(".SH File"):
state = 3
continue
if line.startswith(".SH Need"):
2018-11-26 13:45:32 +01:00
state = 0
continue
2018-11-25 14:59:02 +01:00
if line.startswith(".SH Instability"):
2018-11-26 13:45:32 +01:00
state = 0
continue
2018-11-25 14:59:02 +01:00
if line.startswith(".SH Call"):
2018-11-26 13:45:32 +01:00
state = 0
continue
2018-11-25 14:59:02 +01:00
if state == 1:
entity["decl"] += [ line ]
if line.startswith("subroutine") \
or line.startswith("function ") \
or " function " in line:
entity["type"] = 's'
else:
entity["type"] = 'p'
elif state == 2:
2018-11-26 13:45:32 +01:00
if line.startswith(".br"):
2019-01-02 23:04:13 +01:00
line = "\n\n"
2018-11-25 14:59:02 +01:00
entity["doc"] += [ line ]
elif state == 3:
if line.startswith(".br"):
continue
2018-11-26 13:45:32 +01:00
entity["file"] = line.split("/")[-1]
try:
entity["module"] = line.split("/")[-2]
except: pass
break
2018-11-25 14:59:02 +01:00
entities [ entity["name"] ] = entity
return entities
2018-11-26 13:45:32 +01:00
def generate_index(entities):
rst_file = os.path.join('programmers_guide','index_providers.rst')
with open(rst_file,'w') as f:
rst = [ "Index of Providers",
"------------------",
"" ]
for e in sorted(entities.keys()):
e = entities[e]
if e["type"] == 'p':
rst.append("* :c:data:`%s`" % (e["name"]))
rst += [ "",
"Index of Subroutines/Functions",
"------------------------------",
"" ]
for e in sorted(entities.keys()):
e = entities[e]
if e["type"] == 's':
rst.append("* :c:func:`%s`" % (e["name"]))
f.write("\n".join(rst))
2018-11-25 14:59:02 +01:00
def main():
if "QP_ROOT" in os.environ:
QP_ROOT=os.environ["QP_ROOT"]
else:
QP_ROOT="../../"
SRC = os.path.join(QP_ROOT, "src")
2018-11-25 14:59:02 +01:00
entities = {}
for abs_module in os.listdir(SRC):
2018-12-28 19:44:10 +01:00
if os.path.islink(os.path.join(SRC,abs_module)):
continue
abs_module = os.path.join(SRC,abs_module)
if os.path.exists( os.path.join(abs_module, "README.rst") ):
2018-11-25 14:59:02 +01:00
read_entities = generate_providers(abs_module)
if read_entities:
for k in read_entities:
entities[k] = read_entities[k]
2018-11-26 13:45:32 +01:00
for abs_module in os.listdir(SRC):
abs_module = os.path.join(SRC,abs_module)
2018-12-28 19:44:10 +01:00
if os.path.islink(os.path.join(SRC,abs_module)):
continue
2018-11-26 13:45:32 +01:00
if os.path.exists( os.path.join(abs_module, "README.rst") ):
generate_modules(abs_module,entities)
generate_index(entities)
if __name__ == '__main__':
main()