Skip to content

Commit 1fd5368

Browse files
author
cpolymeris@gmail.com
committed
Saga Help file generator.
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@239 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
1 parent 377bbf8 commit 1fd5368

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/python
2+
3+
import saga_api as saga
4+
import os
5+
6+
class Library:
7+
def __init__(self, filename):
8+
self.sagalib = saga.CSG_Module_Library(saga.CSG_String(str(filename)))
9+
if not self.sagalib.is_Valid():
10+
raise ImportError(filename)
11+
self.libname = filename.split(os.sep)[-1].split(".")[0]
12+
if self.libname.startswith("lib"):
13+
self.libname = self.libname[3:]
14+
self.name = self.sagalib.Get_Name().c_str()
15+
self._modules = None
16+
def modules(self):
17+
if self._modules is not None:
18+
return self._modules
19+
self._modules = list()
20+
for i in range(self.sagalib.Get_Count()):
21+
try:
22+
self._modules.append(Module(self.sagalib, i))
23+
except ImportError:
24+
pass
25+
return self._modules
26+
def __del__(self):
27+
self.sagalib.Destroy()
28+
29+
class Module:
30+
def __init__(self, lib, i):
31+
self.module = lib.Get_Module(i)
32+
if not self.module:
33+
raise ImportError("Module #%i is invalid" % i)
34+
if self.module.is_Interactive():
35+
raise ImportError("Ignoring interactive module")
36+
self.name = self.module.Get_Name()
37+
self.grid = self.module.is_Grid()
38+
if self.module.is_Grid():
39+
self.module = lib.Get_Module_Grid(i)
40+
self.description = self.module.Get_Description()
41+
self.author = self.module.Get_Author()
42+
self._parameters = None
43+
def parameters(self):
44+
if self._parameters is not None:
45+
return self._parameters
46+
params = list()
47+
params.append(self.module.Get_Parameters())
48+
for i in range(self.module.Get_Parameters_Count()):
49+
params.append(self.module.Get_Parameters(i))
50+
self._parameters = list()
51+
for p in params:
52+
for j in range(p.Get_Count()):
53+
try:
54+
self._parameters.append(Parameter(p, j))
55+
except:
56+
pass
57+
return self._parameters
58+
59+
60+
class Parameter:
61+
def __init__(self, params, i):
62+
self.parameter = params.Get_Parameter(i)
63+
self.name = self.parameter.Get_Name()
64+
self.description = self.parameter.Get_Description()
65+
66+
def getLibraryPaths(userPath = None):
67+
try:
68+
paths = os.environ['MLB_PATH'].split(':')
69+
except KeyError:
70+
paths = ['/usr/lib/saga/', '/usr/local/lib/saga/']
71+
noMLBpath = True
72+
if userPath:
73+
paths = [userPath] + paths
74+
print "Looking for libraries in " + ', '.join(paths)
75+
for p in paths:
76+
if os.path.exists(p):
77+
return [os.path.join(p, fn) for fn in os.listdir(p)]
78+
if noMLBpath:
79+
print "Warning: MLB_PATH not set."
80+
return []
81+
82+
def qgisizeString(s):
83+
try:
84+
s = str(s)
85+
s = str.replace(s, "Gridd", "Raster")
86+
s = str.replace(s, "Grid", "Raster")
87+
s = str.replace(s, "gridd", "raster")
88+
s = str.replace(s, "grid", "raster")
89+
except:
90+
# Some unicode characters seem to produce exceptions.
91+
# Just ignore those cases.
92+
pass
93+
return s
94+
95+
def writeHTML(path, mod):
96+
docs = unicode()
97+
docs += "<h1 class='module'>%s</h1>\n" % mod.name
98+
docs += "<div class='author'>%s</div>\n" % mod.author
99+
docs += "<div class='description'>%s</div>\n" % mod.description.replace('\n', '<br/>\n')
100+
if mod.parameters():
101+
docs += "<h2>Parameters:</h2>\n<dl class='parameters'>\n"
102+
for p in mod.parameters():
103+
docs += "\t<dt>%s</dt>" % p.name
104+
docs += "<dd>%s</dd>\n" % p.description
105+
docs += "</dl>"
106+
out = open(path, 'w')
107+
out.write('<html><body>\n')
108+
out.write(docs.encode('utf-8'))
109+
out.write('\n</body></html>\n')
110+
out.close()
111+
112+
if __name__ == '__main__':
113+
import argparse
114+
115+
parser = argparse.ArgumentParser(description='Generate SAGA documentation in HTML form.')
116+
parser.add_argument('dest', metavar='DESTINATION', type = str, help='HTML output path.')
117+
parser.add_argument('-l', dest='libpath', help='Location of SAGA libraries.')
118+
119+
args = parser.parse_args()
120+
121+
libs = list()
122+
paths = getLibraryPaths(args.libpath)
123+
for p in paths:
124+
try:
125+
libs.append(Library(p))
126+
except ImportError:
127+
pass
128+
129+
if not libs:
130+
print "No saga libraries found"
131+
exit(1)
132+
133+
print "%i libraries loaded." % len(libs)
134+
for lib in libs:
135+
mods = lib.modules()
136+
print "%s (%i modules):" % (lib.name, len(mods))
137+
for mod in mods:
138+
path = args.dest + os.sep + mod.name.replace(" ", '').replace("/", '') + ".html"
139+
print '\t', mod.name,
140+
writeHTML(path, mod)
141+
print '\t-> ', path

0 commit comments

Comments
 (0)