Skip to content

Commit

Permalink
Saga Help file generator.
Browse files Browse the repository at this point in the history
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@239 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
cpolymeris@gmail.com committed Jun 11, 2012
1 parent 377bbf8 commit 1fd5368
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions src/sextante/saga/SagaHelpGenerator.py
@@ -0,0 +1,141 @@
#!/usr/bin/python

import saga_api as saga
import os

class Library:
def __init__(self, filename):
self.sagalib = saga.CSG_Module_Library(saga.CSG_String(str(filename)))
if not self.sagalib.is_Valid():
raise ImportError(filename)
self.libname = filename.split(os.sep)[-1].split(".")[0]
if self.libname.startswith("lib"):
self.libname = self.libname[3:]
self.name = self.sagalib.Get_Name().c_str()
self._modules = None
def modules(self):
if self._modules is not None:
return self._modules
self._modules = list()
for i in range(self.sagalib.Get_Count()):
try:
self._modules.append(Module(self.sagalib, i))
except ImportError:
pass
return self._modules
def __del__(self):
self.sagalib.Destroy()

class Module:
def __init__(self, lib, i):
self.module = lib.Get_Module(i)
if not self.module:
raise ImportError("Module #%i is invalid" % i)
if self.module.is_Interactive():
raise ImportError("Ignoring interactive module")
self.name = self.module.Get_Name()
self.grid = self.module.is_Grid()
if self.module.is_Grid():
self.module = lib.Get_Module_Grid(i)
self.description = self.module.Get_Description()
self.author = self.module.Get_Author()
self._parameters = None
def parameters(self):
if self._parameters is not None:
return self._parameters
params = list()
params.append(self.module.Get_Parameters())
for i in range(self.module.Get_Parameters_Count()):
params.append(self.module.Get_Parameters(i))
self._parameters = list()
for p in params:
for j in range(p.Get_Count()):
try:
self._parameters.append(Parameter(p, j))
except:
pass
return self._parameters


class Parameter:
def __init__(self, params, i):
self.parameter = params.Get_Parameter(i)
self.name = self.parameter.Get_Name()
self.description = self.parameter.Get_Description()

def getLibraryPaths(userPath = None):
try:
paths = os.environ['MLB_PATH'].split(':')
except KeyError:
paths = ['/usr/lib/saga/', '/usr/local/lib/saga/']
noMLBpath = True
if userPath:
paths = [userPath] + paths
print "Looking for libraries in " + ', '.join(paths)
for p in paths:
if os.path.exists(p):
return [os.path.join(p, fn) for fn in os.listdir(p)]
if noMLBpath:
print "Warning: MLB_PATH not set."
return []

def qgisizeString(s):
try:
s = str(s)
s = str.replace(s, "Gridd", "Raster")
s = str.replace(s, "Grid", "Raster")
s = str.replace(s, "gridd", "raster")
s = str.replace(s, "grid", "raster")
except:
# Some unicode characters seem to produce exceptions.
# Just ignore those cases.
pass
return s

def writeHTML(path, mod):
docs = unicode()
docs += "<h1 class='module'>%s</h1>\n" % mod.name
docs += "<div class='author'>%s</div>\n" % mod.author
docs += "<div class='description'>%s</div>\n" % mod.description.replace('\n', '<br/>\n')
if mod.parameters():
docs += "<h2>Parameters:</h2>\n<dl class='parameters'>\n"
for p in mod.parameters():
docs += "\t<dt>%s</dt>" % p.name
docs += "<dd>%s</dd>\n" % p.description
docs += "</dl>"
out = open(path, 'w')
out.write('<html><body>\n')
out.write(docs.encode('utf-8'))
out.write('\n</body></html>\n')
out.close()

if __name__ == '__main__':
import argparse

parser = argparse.ArgumentParser(description='Generate SAGA documentation in HTML form.')
parser.add_argument('dest', metavar='DESTINATION', type = str, help='HTML output path.')
parser.add_argument('-l', dest='libpath', help='Location of SAGA libraries.')

args = parser.parse_args()

libs = list()
paths = getLibraryPaths(args.libpath)
for p in paths:
try:
libs.append(Library(p))
except ImportError:
pass

if not libs:
print "No saga libraries found"
exit(1)

print "%i libraries loaded." % len(libs)
for lib in libs:
mods = lib.modules()
print "%s (%i modules):" % (lib.name, len(mods))
for mod in mods:
path = args.dest + os.sep + mod.name.replace(" ", '').replace("/", '') + ".html"
print '\t', mod.name,
writeHTML(path, mod)
print '\t-> ', path

0 comments on commit 1fd5368

Please sign in to comment.