Skip to content

Commit

Permalink
[backport on 2.14] enable custom help in python expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and m-kuhn committed Sep 6, 2017
1 parent 9117137 commit d8fa85e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ matrix:
- BUILD=qt4
- QT_VERSION=4
# - LLVM_VERSION=3.8
dist: precise
sudo: false
cache:
apt: true
Expand Down
3 changes: 2 additions & 1 deletion python/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def func(self, values, feature, parent):

helptemplate = string.Template("""<h3>$name function</h3><br>$doc""")
name = kwargs.get('name', function.__name__)
helptext = function.__doc__ or ''
helptext = kwargs.get('help_text') or function.__doc__ or ''
helptext = helptext.strip()
expandargs = False

Expand Down Expand Up @@ -138,6 +138,7 @@ def wrapper(func):
return register_function(func, args, group, **kwargs)
return wrapper


try:
# Add a __nonzero__ method onto QPyNullVariant so we can check for null values easier.
# >>> value = QPyNullVariant("int")
Expand Down
24 changes: 24 additions & 0 deletions tests/src/python/test_qgsexpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ def special(values, feature, parent):
def sqrt(values, feature, parent):
pass

@qgsfunction(1, 'testing', register=False)
def help_with_docstring(values, feature, parent):
"""The help comes from the python docstring."""
pass

help_text = 'The help comes from a variable.'

@qgsfunction(1, 'testing', register=False, help_text=help_text)
def help_with_variable(values, feature, parent):
"""This docstring is not used for the help."""
pass

@qgsfunction(1, 'testing', register=False, usesgeometry=True)
def geomtest(values, feature, parent):
pass
Expand All @@ -66,6 +78,17 @@ def testAutoCountsCorrectArgs(self):
args = function.params()
self.assertEqual(args, 3)

def testHelp(self):
QgsExpression.registerFunction(self.help_with_variable)
html = ('<h3>help_with_variable function</h3><br>'
'The help comes from a variable.')
self.assertEqual(self.help_with_variable.helptext(), html)

QgsExpression.registerFunction(self.help_with_docstring)
html = ('<h3>help_with_docstring function</h3><br>'
'The help comes from the python docstring.')
self.assertEqual(self.help_with_docstring.helptext(), html)

def testAutoArgsAreExpanded(self):
function = self.expandargs
args = function.params()
Expand Down Expand Up @@ -186,5 +209,6 @@ def testComment(self):
result = exp.evaluate()
self.assertEqual(exp_res, result)


if __name__ == "__main__":
unittest.main()

0 comments on commit d8fa85e

Please sign in to comment.