File: genrst.py

package info (click to toggle)
python-escript 5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 87,772 kB
  • ctags: 49,550
  • sloc: python: 585,488; cpp: 133,173; ansic: 18,675; xml: 3,283; sh: 690; makefile: 215
file content (158 lines) | stat: -rwxr-xr-x 4,935 bytes parent folder | download
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
#!/usr/bin/env python
##############################################################################
#
# Copyright (c) 2003-2016 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
#
##############################################################################

from __future__ import division, print_function

import os
import inspect
import sys

if len(sys.argv)!=4:
  sys.stderr.write('Usage: startdir startpackage outputdirectory\n')
  sys.exit(1)

#startdir='./esys'
#startpackage='esys'
#outdir='doctest'

pathdir=sys.argv[1]
startpackage=sys.argv[2]
startdir=os.path.join(pathdir, startpackage)
outdir=sys.argv[3]

def dumpPackage(mname, ignorelist, modset, banset):
  try:
    exec('import '+mname+' as PP')
  except ImportError:
    return
  modset.add(mname)
  print("Starting dump on "+mname+' with ignore at '+str(ignorelist))
  pack=open(os.path.join(outdir,mname+'.rst'),'w')
  pack.write(mname+' Package\n')
  pack.write('='*len(mname)+'========\n\n')
#  pack.write('.. py:module:: '+mname+'\n\n')
  moddoc = inspect.getdoc(PP)
  if moddoc:
    pack.write(".. automodule:: %s\n   :synopsis: %s\n"%(mname, "".join(moddoc.replace("\n\n", "\n").replace("\n", " "))))
  #Automodule does not seem to do what we want so we need to drill down
  clist=[]
  flist=[]
  vlist=[]
  norecurse=[]
  try:
    norecurse=PP.__nodocorecursion
    print("Supressing the following packages")
    for n in norecurse:
      print(mname+'.'+n)
      banset.add(mname+'.'+n)
    print('---------')
  except AttributeError:
    pass
  # esys.escript.models does not get picked up by this loop
  for (name, mem) in inspect.getmembers(PP):
      print("    "+name)
      if inspect.ismodule(mem):
        if not name in ignorelist:
           try:
             ppfile=inspect.getfile(PP)
             memfile=inspect.getfile(mem)
           except:
             continue    #It will be a builtin module
           ppdir=ppfile[:ppfile.rfind(os.path.sep)]
           memdir=memfile[:memfile.rfind(os.path.sep)]
           if ppdir==memdir:
              if not mem.__name__ in modset:
                if not mem.__name__ in banset:
                  print("About to dump "+name+"("+mem.__name__+")")
                  dumpPackage(mem.__name__, [], modset, banset)
                  print("Dump of "+mname+" complete")
        #pack.write('Module '+name+'\n')
      elif inspect.isclass(mem):
        clist+=[(name, mem)]
      elif inspect.isfunction(mem):
        flist+=[(name, mem)]
      else:
        if type(mem).__module__+'.'+type(mem).__name__=='Boost.Python.function':
          flist+=[(name, mem)]
        else:
          vlist+=[(name, mem)]
  pack.write('\nClasses\n')
  pack.write('-------\n')
  for (name, mem) in clist:
      pack.write('* `'+name+'`\n')
  pack.write('\n')
  for (name, mem) in clist:
    pack.write('.. autoclass:: '+name+'\n')
    pack.write('   :members:\n   :undoc-members:\n\n   .. automethod:: __init__\n\n')
  pack.write('\n')
    
  pack.write('Functions\n')
  pack.write('---------\n')
  for (name, mem) in flist:
    pack.write('.. autofunction:: '+name+'\n')
  pack.write('\n')
    
  pack.write('Others\n')
  pack.write('------\n')
  for (name, mem) in vlist:
    pack.write('* '+name+'\n')
  pack.write('\n')
  pack.close()

def listmods():
  W=os.walk(startdir,topdown=True)
  sys.path.append(pathdir)
  main=open(os.path.join(outdir,'index.rst'),'w')
  main.write('.. Generated by Joel\'s script\n\n')
  main.write('Documentation for esys.escript\n')
  main.write('==============================\n')
  main.write('\n')
  main.write('Contents:\n\n')
  main.write('.. toctree::\n')
  main.write('   :maxdepth: 4\n')
  main.write('\n')
  modset=set()
  banset=set()
  for z in W:
    if z[0].endswith('__pycache__'): continue
    #if z[0].find('escript')==-1: continue
    print("Beginning ",z[0])
    # Now make the package name
    n=startpackage+'.'.join(z[0][len(startdir):].split(os.path.sep))
    dumpPackage(n, z[1], modset, banset)
    print("-------------"+n)
    
    for m in z[2]:        #This will list the files
      if m.split('.')[1]=='pyc' and m!='__init__.pyc':
        print(".."+n+"."+m)
        print(".."+(n+'.'+m)[:-4])
        if not (n+'.'+m)[:-4] in banset:
          dumpPackage((n+'.'+m)[:-4],[],modset, banset)
  l=list(modset)
  l.sort()
  for n in l:
      main.write("   "+n+"\n")
  main.write('\n')
  main.write('Indices and Tables\n')
  main.write('==================\n')
  main.write('\n')
  main.write('* :ref:`genindex`\n')
  main.write('* :ref:`modindex`\n')
  main.write('\n')
  main.close()
    
    
listmods()